代码拉取完成,页面将自动刷新
package proto_parser
import (
"encoding/json"
"fmt"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/jhump/protoreflect/desc"
"testing"
)
func Test_convertMessageToMap(t *testing.T) {
f := getProtoFileDescriptor("parser.proto")
fd := f.FindMessage("proto_parser.ModelOperLog")
if fd == nil {
panic("fd nil")
}
var convertMessageToMap = func(message *desc.MessageDescriptor) map[string]interface{} {
m := make(map[string]interface{})
for _, fieldDescriptor := range message.GetFields() {
fieldName := fieldDescriptor.GetName()
switch fieldDescriptor.GetType() {
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
fmt.Println("type message", fieldDescriptor.GetMessageType())
if fieldDescriptor.IsRepeated() {
// 如果是一个数组的话
m[fieldName] = []interface{}{convertMessageToMap(fieldDescriptor.GetMessageType())}
continue
}
m[fieldName] = convertMessageToMap(fieldDescriptor.GetMessageType())
continue
default:
if fieldDescriptor.IsRepeated() {
switch fieldDescriptor.GetType() {
case descriptor.FieldDescriptorProto_TYPE_BOOL:
m[fieldName] = []interface{}{false, true}
case descriptor.FieldDescriptorProto_TYPE_STRING:
m[fieldName] = []interface{}{""}
default:
m[fieldName] = []interface{}{0}
}
continue
}
m[fieldName] = fieldDescriptor.GetDefaultValue()
}
}
return m
}
data := convertMessageToMap(fd)
bs, err := json.MarshalIndent(data, "", "\t")
if err != nil {
panic(err)
}
fmt.Println(string(bs))
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。