英文:
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 := &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)
Outputs:
label: "this"
reps: 4
reps: 3
reps: 2
reps: 1
inner_test: <
inner_label: "is the end"
>
答案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
}
- https://godocs.io/google.golang.org/protobuf/encoding/protojson
- https://godocs.io/google.golang.org/protobuf/runtime/protoimpl
- https://godocs.io/google.golang.org/protobuf/types/known/structpb
英文:
Example one:
package main
import "google.golang.org/protobuf/types/known/structpb"
func example1(x *structpb.Struct) string {
return x.String()
}
Example two:
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)
}
Example three:
package main
import (
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
func example3(m proto.Message) string {
return protojson.Format(m)
}
Test:
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))
}
Result:
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
}
答案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 := &jsonpb.Marshaler{}
reqString, err := marshaler.MarshalToString(req)
log.Println("@@REQ@@", reqString)
Or refer to official unit test is ok.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论