登录
首页 >  Golang >  Go问答

自定义选项在使用 protojson 库生成的 JSON 中不可见

来源:stackoverflow

时间:2024-03-25 09:30:47 396浏览 收藏

使用 protojson 库生成的 JSON 中缺少自定义选项。本文探讨了如何使用 protoreflect 或 protojson 从 filedescriptorset 中提取自定义选项。虽然本文没有直接提供在生成的 JSON 中获取自定义选项的具体方法,但它提供了利用运行时填充的原型注册表来编组和解组选项的解决方案。这种方法涉及注册所有扩展类型并使用 protoreflect 反射来遍历扩展字段,从而获取自定义选项的值。

问题内容

我正在尝试从 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 代码的情况下访问自定义选项。这要感谢 dsnet 对我在 golang issues board 上的问题的回答。不用说,这个棘手的解决方案的所有功劳都归功于他。重点是使用运行时填充的原型注册表来编组和解组选项。实际上了解自定义选项的类型。

我对这种方法的工作原理进行了完整的演示 in this repo,关键部分(所有内容都来自 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
}

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《自定义选项在使用 protojson 库生成的 JSON 中不可见》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>