![](/img/trans.png)
[英]How to convert protobuf wireformat to JSON with just a message descriptor?
[英]protobuf: how to parse descriptor message from FieldOptions
我下面有一個原始文件
syntax = "proto2";
package cmd;
import "google/protobuf/descriptor.proto";
message FlagDetail {
required string name = 1;
required string value = 2;
required string shorthand = 3;
required string usage = 4;
}
extend google.protobuf.FieldOptions {
optional FlagDetail info = 1234;
}
message VersionFlags {
optional bool client = 2 [ (info) = { name: "client" value: "false" shorthand: "c" usage: "Client version only (no server required)."}];
optional bool short = 3 [ (info) = { name: "short" value: "false" shorthand: "baz" usage: "Print just the version number."}];
optional string output = 4 [ (info) = { name: "output" value: "" shorthand: "o" usage: "One of 'yaml' or 'json'."}];
}
如何獲取空消息的默認標志FlagDetail
像這樣的東西
var msg VersionFlags
md := ForMessage(&msg)
o := md.Field[0].GetOptions()
o.GetFlagDetail.GetName() //unfortunately, there's no method like this
BTW ForMessage()來自這里: https : //github.com/golang/protobuf/blob/master/descriptor/descriptor_test.go
_, md := descriptor.ForMessage(msg.(descriptor.Message))
info, err := proto.GetExtension(md.GetOptions(), cmdproto.E_Cmd)
if err != nil {
panic(err)
}
return info.(*cmdproto.CommandInfo)
有點像這樣,但是我實際遇到的問題是這個https://github.com/golang/protobuf/issues/372
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.