Skip to content

Commit

Permalink
Merge pull request #6 from origadmin/dev
Browse files Browse the repository at this point in the history
refactor(transport): enhance ResultJSON to support protobuf messages
  • Loading branch information
godcong authored Nov 28, 2024
2 parents c8fdbc5 + 7a105b3 commit 2fbb4d4
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions transport/gins/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/origadmin/toolkits/errors"
"github.com/origadmin/toolkits/errors/httperr"
"github.com/origadmin/toolkits/net/pagination"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)

type Result struct {
Expand All @@ -36,8 +38,14 @@ func (r Result) GetSuccess() bool {
}

// ResultJSON result json data with status code
func ResultJSON(c *gin.Context, status int, resp pagination.Responder) {
buf, err := json.Marshal(resp.GetData())
func ResultJSON(c *gin.Context, status int, data any) {
var buf []byte
var err error
if msg, ok := data.(proto.Message); ok {
buf, err = protojson.Marshal(msg)
} else {
buf, err = json.Marshal(data)
}
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 2fbb4d4

Please sign in to comment.