gpt4 book ai didi

go - protobuf : how to parse descriptor message from FieldOptions

转载 作者:行者123 更新时间:2023-12-01 21:15:07 25 4
gpt4 key购买 nike

我下面有一个原始文件

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

这是我的原始文件 https://gist.githubusercontent.com/shiywang/3d9f53fe253bb4195d65b3626442cb66/raw/89c286599a4103f67b80a62c87c69847497fa289/protofile

最佳答案

_, 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

关于go - protobuf : how to parse descriptor message from FieldOptions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46925704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com