Golang是否提供了一种简单的方法来输出可读性强的protobuf格式?

huangapple go评论131阅读模式
英文:

Does golang provide an easy way to output human readable protobuf

问题

在Golang中,有没有一种好的方法可以获取Protobuf对象的可读字符串表示?类似于https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.message#Message.DebugString这样的功能?

我正在使用https://github.com/golang/protobuf。

英文:

Is there a good way to get a human readable string representation of protobuf objects in golang? Something equivalent to https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.message#Message.DebugString?

I am using https://github.com/golang/protobuf.

答案1

得分: 17

我相信你正在寻找proto.MarshalTextString

  1. p := &example.Test{
  2. Label: proto.String("this"),
  3. Reps: []int64{4, 3, 2, 1},
  4. InnerTest: &example.Test_InnerTest{
  5. InnerLabel: proto.String("is the end"),
  6. },
  7. }
  8. fmt.Println(proto.MarshalTextString(p))

你可以在Go包test中看到一个示例。

英文:

I believe you're looking for proto.MarshalTextString.

  1. p := &example.Test{
  2. Label: proto.String("this"),
  3. Reps: []int64{4, 3, 2, 1},
  4. InnerTest: &example.Test_InnerTest{
  5. InnerLabel: proto.String("is the end"),
  6. },
  7. }
  8. fmt.Println(proto.MarshalTextString(p))

You can see an example in the Go package test.

答案2

得分: 4

你可以使用TextMarshaler。使用稍作修改的示例 proto:

  1. p := &example.Test{
  2. Label: proto.String("this"),
  3. Reps: []int64{4, 3, 2, 1},
  4. InnerTest: &example.Test_InnerTest{
  5. InnerLabel: proto.String("is the end"),
  6. },
  7. }
  8. t := proto.TextMarshaler{}
  9. t.Marshal(os.Stdout, p)

输出结果为:

  1. label: "this"
  2. reps: 4
  3. reps: 3
  4. reps: 2
  5. reps: 1
  6. inner_test: <
  7. inner_label: "is the end"
  8. >
英文:

You can use the TextMarshaler. With a slightly modified example proto:

  1. p := &amp;example.Test{
  2. Label: proto.String(&quot;this&quot;),
  3. Reps: []int64{4, 3, 2, 1},
  4. InnerTest: &amp;example.Test_InnerTest{
  5. InnerLabel: proto.String(&quot;is the end&quot;),
  6. },
  7. }
  8. t := proto.TextMarshaler{}
  9. t.Marshal(os.Stdout, p)

Outputs:

  1. label: &quot;this&quot;
  2. reps: 4
  3. reps: 3
  4. reps: 2
  5. reps: 1
  6. inner_test: &lt;
  7. inner_label: &quot;is the end&quot;
  8. &gt;

答案3

得分: 4

示例一:

  1. package main
  2. import "google.golang.org/protobuf/types/known/structpb"
  3. func example1(x *structpb.Struct) string {
  4. return x.String()
  5. }

示例二:

  1. package main
  2. import (
  3. "google.golang.org/protobuf/proto"
  4. "google.golang.org/protobuf/runtime/protoimpl"
  5. )
  6. func example2(m proto.Message) string {
  7. return protoimpl.X.MessageStringOf(m)
  8. }

示例三:

  1. package main
  2. import (
  3. "google.golang.org/protobuf/encoding/protojson"
  4. "google.golang.org/protobuf/proto"
  5. )
  6. func example3(m proto.Message) string {
  7. return protojson.Format(m)
  8. }

测试:

  1. package main
  2. import "google.golang.org/protobuf/types/known/structpb"
  3. func main() {
  4. m, err := structpb.NewStruct(map[string]interface{}{
  5. "month": 12, "day": 31,
  6. })
  7. if err != nil {
  8. panic(err)
  9. }
  10. println(example1(m))
  11. println(example2(m))
  12. println(example3(m))
  13. }

结果:

  1. fields:{key:"day" value:{number_value:31}} fields:{key:"month" value:{number_value:12}}
  2. fields:{key:"day" value:{number_value:31}} fields:{key:"month" value:{number_value:12}}
  3. {
  4. "day": 31,
  5. "month": 12
  6. }
英文:

Example one:

  1. package main
  2. import &quot;google.golang.org/protobuf/types/known/structpb&quot;
  3. func example1(x *structpb.Struct) string {
  4. return x.String()
  5. }

Example two:

  1. package main
  2. import (
  3. &quot;google.golang.org/protobuf/proto&quot;
  4. &quot;google.golang.org/protobuf/runtime/protoimpl&quot;
  5. )
  6. func example2(m proto.Message) string {
  7. return protoimpl.X.MessageStringOf(m)
  8. }

Example three:

  1. package main
  2. import (
  3. &quot;google.golang.org/protobuf/encoding/protojson&quot;
  4. &quot;google.golang.org/protobuf/proto&quot;
  5. )
  6. func example3(m proto.Message) string {
  7. return protojson.Format(m)
  8. }

Test:

  1. package main
  2. import &quot;google.golang.org/protobuf/types/known/structpb&quot;
  3. func main() {
  4. m, err := structpb.NewStruct(map[string]interface{}{
  5. &quot;month&quot;: 12, &quot;day&quot;: 31,
  6. })
  7. if err != nil {
  8. panic(err)
  9. }
  10. println(example1(m))
  11. println(example2(m))
  12. println(example3(m))
  13. }

Result:

  1. fields:{key:&quot;day&quot; value:{number_value:31}} fields:{key:&quot;month&quot; value:{number_value:12}}
  2. fields:{key:&quot;day&quot; value:{number_value:31}} fields:{key:&quot;month&quot; value:{number_value:12}}
  3. {
  4. &quot;day&quot;: 31,
  5. &quot;month&quot;: 12
  6. }

答案4

得分: 1

你可以使用MarshalToString,如果你需要将你的PB编组为一个结构化消息(JSON格式)以供将来使用。

一个简单的例子:

  1. marshaler := &jsonpb.Marshaler{}
  2. reqString, err := marshaler.MarshalToString(req)
  3. log.Println("@@REQ@@", reqString)

或者参考官方单元测试也可以。

英文:

You can use MarshalToString, if you need to marshal your PB into a structured message(in JSON) for future use.

A simple example:

  1. marshaler := &amp;jsonpb.Marshaler{}
  2. reqString, err := marshaler.MarshalToString(req)
  3. log.Println(&quot;@@REQ@@&quot;, reqString)

Or refer to official unit test is ok.

huangapple
  • 本文由 发表于 2016年2月20日 05:33:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/35515449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定