Golang是否提供了一种简单的方法来输出人类可读的protobuf

mmvthczy  于 2023-11-14  发布在  Go
关注(0)|答案(5)|浏览(154)

在golang中,有没有一个好的方法来获得protobuf对象的人类可读的字符串表示?https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.message#Message.DebugString?
我用https://github.com/golang/protobuf

9gm1akwq

9gm1akwq1#

我相信你要找的是. 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包测试中看到一个例子。

zour9fqk

zour9fqk2#

您可以使用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. >

展开查看全部
cgyqldqp

cgyqldqp3#

示例一:

  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. }

展开查看全部
utugiqy6

utugiqy64#

如果您需要将PB封送到结构化消息(JSON格式)中以备将来使用,则可以使用MarshalToString。
举个简单的例子:

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

字符串
或者参考official unit test就可以了。

1sbrub3j

1sbrub3j5#

由于github.com/golang/protobuf现在已被弃用,我为google.golang.org/protobuf软件包写了这个答案,因为它可能会帮助新读者。
prototext包旨在将消息转换为文本格式。输出是人类可读的。使用其他答案中的相同示例:

  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(prototext.Format(p))

字符串
然而,我建议使用protojson包,因为它的输出也是人类可读的,并且它有更多的选项可用。

  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(
  9. protojson.MarshalOptions{
  10. Multiline: true,
  11. EmitUnpopulated: true,
  12. }.Format(p)
  13. )

展开查看全部

相关问题