【问题标题】:Protobuf custom options not showing in JSON made by protojson libraryprotobuf 自定义选项未显示在由 protojson 库生成的 JSON 中
【发布时间】:2020-12-08 08:46:54
【问题描述】:

我正在尝试从 protoc 编译器生成的 FileDescriptorSet 中提取 Protobuf 自定义选项。我无法使用 protoreflect 这样做。所以,我尝试使用 protojson 库来做到这一点。

PS:导入 Go 生成的代码不是我的用例的选项。

这是我正在测试的 Protobuf 消息:

syntax = "proto3";

option go_package = "./protoze";

import "google/protobuf/descriptor.proto";

extend google.protobuf.FieldOptions {
    string Meta = 50000;
}
extend google.protobuf.FileOptions {
    string Food = 50001;
}
option (Food) = "cheese";
message X {
 int64 num = 1;
}
message P {
    string Fname          = 1 [json_name = "FNAME"];
    string Lname          = 2 [json_name = "0123", (Meta) = "Yo"]; 
    string Designation    = 3;
    repeated string Email = 4;
    string UserID         = 5;
    string EmpID          = 6;
    repeated X z          = 7;
}
// protoc --go_out=. filename.proto

这是我得到了多远:

package main

import (
    "fmt"
    "io/ioutil"
    "os/exec"

    "google.golang.org/protobuf/encoding/protojson"
    "google.golang.org/protobuf/proto"
    "google.golang.org/protobuf/types/descriptorpb"
)

func main() {
    exec.Command("protoc", "-oBinaryFile", "1.proto").Run()
    Fset := descriptorpb.FileDescriptorSet{}
    byts, _ := ioutil.ReadFile("File")
    proto.Unmarshal(byts, &Fset)
    byts, _ = protojson.Marshal(Fset.File[0])
    fmt.Println(string(byts))
}

这是输出 JSON

{
  "name": "1.proto",
  "dependency": [
    "google/protobuf/descriptor.proto"
  ],
  "messageType": [
    {
      "name": "X",
      "field": [
        {
          "name": "num",
          "number": 1,
          "label": "LABEL_OPTIONAL",
          "type": "TYPE_INT64",
          "jsonName": "num"
        }
      ]
    },
    {
      "name": "P",
      "field": [
        {
          "name": "Fname",
          "number": 1,
          "label": "LABEL_OPTIONAL",
          "type": "TYPE_STRING",
          "jsonName": "FNAME"
        },
        {
          "name": "Lname",
          "number": 2,
          "label": "LABEL_OPTIONAL",
          "type": "TYPE_STRING",
          "jsonName": "0123",
          "options": {}
        },
        {
          "name": "Designation",
          "number": 3,
          "label": "LABEL_OPTIONAL",
          "type": "TYPE_STRING",
          "jsonName": "Designation"
        },
        {
          "name": "Email",
          "number": 4,
          "label": "LABEL_REPEATED",
          "type": "TYPE_STRING",
          "jsonName": "Email"
        },
        {
          "name": "UserID",
          "number": 5,
          "label": "LABEL_OPTIONAL",
          "type": "TYPE_STRING",
          "jsonName": "UserID"
        },
        {
          "name": "EmpID",
          "number": 6,
          "label": "LABEL_OPTIONAL",
          "type": "TYPE_STRING",
          "jsonName": "EmpID"
        },
        {
          "name": "z",
          "number": 7,
          "label": "LABEL_REPEATED",
          "type": "TYPE_MESSAGE",
          "typeName": ".X",
          "jsonName": "z"
        }
      ]
    }
  ],
  "extension": [
    {
      "name": "Meta",
      "number": 50000,
      "label": "LABEL_OPTIONAL",
      "type": "TYPE_STRING",
      "extendee": ".google.protobuf.FieldOptions",
      "jsonName": "Meta"
    },
    {
      "name": "Food",
      "number": 50001,
      "label": "LABEL_OPTIONAL",
      "type": "TYPE_STRING",
      "extendee": ".google.protobuf.FileOptions",
      "jsonName": "Food"
    }
  ],
  "options": {
    "goPackage": "./protoze"
  },
  "syntax": "proto3"
}

因此,有关我的自定义选项的数据显示在扩展程序中。但我真正想要的是“选项”中那些自定义选项的价值。 (在我的情况下是(食物)=“奶酪”,我想要奶酪)

谁能告诉我如何使用 Protoreflect 或使用 Protojson 从 FileDescriptorSet 中提取自定义选项。

我尝试了很多尝试使用 Protoreflect 提取它但失败了!

【问题讨论】:

    标签: json go protocol-buffers proto google-app-engine-golang


    【解决方案1】:

    虽然没有具体回答如何在生成的 JSON 中获取自定义选项,但我相信我可以回答您的基本问题:如何在不加载生成的 Go 代码的情况下访问自定义选项。这要感谢 dsnet 在golang issues board 上对我的问题的回答。不用说,这个棘手的解决方案的所有功劳都归功于他。关键是 Marshal 然后 Unmarshal 使用运行时填充的 protoregistry.Types 实际了解自定义选项的选项。

    我对@9​​87654322@ 的这种方法进行了完整的演示,关键部分(所有内容都来自 dsnet 的示例)在这里:

    func main() {
        protogen.Options{
        }.Run(func(gen *protogen.Plugin) error {
    
            gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
    
            // The type information for all extensions is in the source files,
            // so we need to extract them into a dynamically created protoregistry.Types.
            extTypes := new(protoregistry.Types)
            for _, file := range gen.Files {
                if err := registerAllExtensions(extTypes, file.Desc); err != nil {
                    panic(err)
                }
            }
    
            // run through the files again, extracting and printing the Message options
            for _, sourceFile := range gen.Files {
                if !sourceFile.Generate {
                    continue
                }
    
                // setup output file
                outputfile := gen.NewGeneratedFile("./out.txt", sourceFile.GoImportPath)
    
                for _, message := range sourceFile.Messages {
                    outputfile.P(fmt.Sprintf("\nMessage %s:", message.Desc.Name()))
    
                    // The MessageOptions as provided by protoc does not know about
                    // dynamically created extensions, so they are left as unknown fields.
                    // We round-trip marshal and unmarshal the options with
                    // a dynamically created resolver that does know about extensions at runtime.
                    options := message.Desc.Options().(*descriptorpb.MessageOptions)
                    b, err := proto.Marshal(options)
                    if err != nil {
                        panic(err)
                    }
                    options.Reset()
                    err = proto.UnmarshalOptions{Resolver: extTypes}.Unmarshal(b, options)
                    if err != nil {
                        panic(err)
                    }
    
                    // Use protobuf reflection to iterate over all the extension fields,
                    // looking for the ones that we are interested in.
                    options.ProtoReflect().Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
                        if !fd.IsExtension() {
                            return true
                        }
    
                        outputfile.P(fmt.Sprintf("Value of option %s is %s",fd.Name(), v.String()))
    
                        // Make use of fd and v based on their reflective properties.
    
                        return true
                    })
                }
            }
            
            return nil
        })
    }
    
    // Recursively register all extensions into the provided protoregistry.Types,
    // starting with the protoreflect.FileDescriptor and recursing into its MessageDescriptors,
    // their nested MessageDescriptors, and so on.
    //
    // This leverages the fact that both protoreflect.FileDescriptor and protoreflect.MessageDescriptor 
    // have identical Messages() and Extensions() functions in order to recurse through a single function
    func registerAllExtensions(extTypes *protoregistry.Types, descs interface {
        Messages() protoreflect.MessageDescriptors
        Extensions() protoreflect.ExtensionDescriptors
    }) error {
        mds := descs.Messages()
        for i := 0; i < mds.Len(); i++ {
            registerAllExtensions(extTypes, mds.Get(i))
        }
        xds := descs.Extensions()
        for i := 0; i < xds.Len(); i++ {
            if err := extTypes.RegisterExtension(dynamicpb.NewExtensionType(xds.Get(i))); err != nil {
                return err
            }
        }
        return nil
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多