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

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

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

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

fmt.Println(proto.MarshalTextString(p))

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

英文:

I believe you're looking for proto.MarshalTextString.

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

fmt.Println(proto.MarshalTextString(p))

You can see an example in the Go package test.

答案2

得分: 4

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

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

t := proto.TextMarshaler{}
t.Marshal(os.Stdout, p)

输出结果为:

label: "this"
reps: 4
reps: 3
reps: 2
reps: 1
inner_test: <
  inner_label: "is the end"
>
英文:

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

p := &amp;example.Test{
	Label: proto.String(&quot;this&quot;),
	Reps:  []int64{4, 3, 2, 1},
	InnerTest: &amp;example.Test_InnerTest{
		InnerLabel: proto.String(&quot;is the end&quot;),
	},
}

t := proto.TextMarshaler{}
t.Marshal(os.Stdout, p)

Outputs:

label: &quot;this&quot;
reps: 4
reps: 3
reps: 2
reps: 1
inner_test: &lt;
  inner_label: &quot;is the end&quot;
&gt;

答案3

得分: 4

示例一:

package main
import "google.golang.org/protobuf/types/known/structpb"

func example1(x *structpb.Struct) string {
   return x.String()
}

示例二:

package main

import (
   "google.golang.org/protobuf/proto"
   "google.golang.org/protobuf/runtime/protoimpl"
)

func example2(m proto.Message) string {
   return protoimpl.X.MessageStringOf(m)
}

示例三:

package main

import (
   "google.golang.org/protobuf/encoding/protojson"
   "google.golang.org/protobuf/proto"
)

func example3(m proto.Message) string {
   return protojson.Format(m)
}

测试:

package main
import "google.golang.org/protobuf/types/known/structpb"

func main() {
   m, err := structpb.NewStruct(map[string]interface{}{
      "month": 12, "day": 31,
   })
   if err != nil {
      panic(err)
   }
   println(example1(m))
   println(example2(m))
   println(example3(m))
}

结果:

fields:{key:"day" value:{number_value:31}} fields:{key:"month" value:{number_value:12}}
fields:{key:"day" value:{number_value:31}} fields:{key:"month" value:{number_value:12}}
{
  "day": 31,
  "month": 12
}
英文:

Example one:

package main
import &quot;google.golang.org/protobuf/types/known/structpb&quot;

func example1(x *structpb.Struct) string {
   return x.String()
}

Example two:

package main

import (
   &quot;google.golang.org/protobuf/proto&quot;
   &quot;google.golang.org/protobuf/runtime/protoimpl&quot;
)

func example2(m proto.Message) string {
   return protoimpl.X.MessageStringOf(m)
}

Example three:

package main

import (
   &quot;google.golang.org/protobuf/encoding/protojson&quot;
   &quot;google.golang.org/protobuf/proto&quot;
)

func example3(m proto.Message) string {
   return protojson.Format(m)
}

Test:

package main
import &quot;google.golang.org/protobuf/types/known/structpb&quot;

func main() {
   m, err := structpb.NewStruct(map[string]interface{}{
      &quot;month&quot;: 12, &quot;day&quot;: 31,
   })
   if err != nil {
      panic(err)
   }
   println(example1(m))
   println(example2(m))
   println(example3(m))
}

Result:

fields:{key:&quot;day&quot; value:{number_value:31}} fields:{key:&quot;month&quot; value:{number_value:12}}
fields:{key:&quot;day&quot; value:{number_value:31}} fields:{key:&quot;month&quot; value:{number_value:12}}
{
  &quot;day&quot;: 31,
  &quot;month&quot;: 12
}

答案4

得分: 1

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

一个简单的例子:

marshaler := &jsonpb.Marshaler{}
reqString, err := marshaler.MarshalToString(req)
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:

marshaler := &amp;jsonpb.Marshaler{}
reqString, err := marshaler.MarshalToString(req)
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:

确定