golang protobuf remove omitempty tag from generated json tags

后端 未结 7 1290
余生分开走
余生分开走 2021-02-04 00:00

I am using google grpc with a json proxy. for some reason i need to remove the omitempty tags from the struct generated in the *.pb.go files.

if i have a pr

相关标签:
7条回答
  • 2021-02-04 00:19

    You could use "sed" command to remove this text from files like following

    sed -i "" -e "s/,omitempty//g" ./api/proto/*.go
    

    where args:

    1. -i "" is meaning that keep same name of files
    2. -e "s/,omitempty//g" = format to replace like "s/SEARCH/INSERT/g"
    0 讨论(0)
  • 2021-02-04 00:20

    The Marshaler under the jsonpb package has a EmitDefaults field. Setting this to true, will just ignore the omitempty tag in struct.

    https://godoc.org/github.com/golang/protobuf/jsonpb#JSONPBMarshaler

    0 讨论(0)
  • 2021-02-04 00:24

    I found that the omitempty json tag is hard-coded into the protoc-gen-go source around line 1778:

    tag := fmt.Sprintf("protobuf:%s json:%q",
        g.goTag(message, field, wiretype), jsonName+",omitempty")
    

    it will be easy changing the source and make a new protoc-gen-go binary yourself.

    It's worth noting that this is likely inadvisable and not recommended for several reasons, particularly because you will then be responsible for ensuring that the hacked up binary always gets used if the protobufs need to be regenerated.

    0 讨论(0)
  • 2021-02-04 00:25

    If you are using grpc-gateway and you need the default values to be present during json marshaling, you may consider to add the following option when creating your servemux

        gwmux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{OrigName: true, EmitDefaults: true}))
    

    Outside of grpc-gateway, if you want to marshal your protocul buffer message, use github.com/golang/protobuf/jsonpb package instead of encoding/json

    func sendProtoMessage(resp proto.Message, w http.ResponseWriter) {
        w.Header().Set("Content-Type", "application/json; charset=utf-8")
        m := jsonpb.Marshaler{EmitDefaults: true}
        m.Marshal(w, resp) // You should check for errors here
    }
    
    0 讨论(0)
  • 2021-02-04 00:32

    You can try using gogo proto (https://github.com/gogo/protobuf) With the jsontag extension, your proto message would look like

    message Status {
      int32 code = 1 [(gogoproto.jsontag) = "code"];
      string message = 2 [(gogoproto.jsontag) = "message"];
    }
    

    You can also add more tags, if you like.

    0 讨论(0)
  • 2021-02-04 00:44

    A [more] portable solution:

    Use sed to strip the tags after generating via protoc.

    Example of what I actually use in my go:generate script after having generated the *.pb.go files:

    ls *.pb.go | xargs -n1 -IX bash -c 'sed s/,omitempty// X > X.tmp && mv X{.tmp,}'
    

    Note: sed -i (inline-replacement) is not used here because that flag isn't portable between standard OS-X and Linux.

    0 讨论(0)
提交回复
热议问题