无需预编译 Go 代码,将 protobuf 序列化消息转换为 JSON

Convert protobuf serialized messages to JSON without precompiling Go code

我想将 protobuf 序列化消息转换为人类可读的 JSON 格式。我面临的主要问题是我需要在不事先将原型描述符编译成 Go 代码的情况下执行此操作。我可以在运行时访问 .proto 文件,但不能在编译时访问。

我的印象是新的 Protobuf API v2 (https://github.com/protocolbuffers/protobuf-go) 支持动态反序列化 (参见包 types/dynamicpb),但我不知道如何使用它显然:

func readDynamically(in []byte) {
    // How do I load the required descriptor (for NewMessage()) from my `addressbook.proto` file?)
    descriptor := ??

    msg := dynamicpb.NewMessage(descriptor)
    err := protojson.Unmarshal(in, msg)
    if err != nil {
        panic(err)
    }
}

以上代码注释了我的问题:如何从 .proto 文件中获取 dynamicpb.NewMessage() 所需的描述符?

这个问题有点意思。我在 protobuf 插件上做了一些工作。据我所知,需要额外的 cli,因为我们不想“重新发明轮子”。

第一步,我们需要 protoc 将“.proto”文件翻译成某种格式,这样我们就可以轻松获得“protoreflect.MessageDescriptor”。

这个插件是为了获取协议发送给其他插件的原始字节作为输入。

package main

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

func main() {
    if len(os.Args) == 2 && os.Args[1] == "--version" {
        // fmt.Fprintf(os.Stderr, "%v %v\n", filepath.Base(os.Args[0]), version.String())
        os.Exit(0)
    }

    in, err := ioutil.ReadAll(os.Stdin)
    if err != nil {
        fmt.Printf("error: %v", err)
        return
    }
    ioutil.WriteFile("./out.pb", in, 0755)
}

构建并重命名为protoc-gen-raw,然后生成protoc --raw_out=./pb ./server.proto,你会得到out.pb。从现在开始忘记你的“.proto”文件,把这个“out.pb”放在你打算放“.proto”的地方。我们得到的是这个 .pb 文件的官方支持。

第2步:将一个protobuf序列化消息反序列化为JSON.

package main

import (
    "fmt"
    "io/ioutil"

    "google.golang.org/protobuf/proto"
    "google.golang.org/protobuf/compiler/protogen"
    "google.golang.org/protobuf/encoding/protojson"
    "google.golang.org/protobuf/types/dynamicpb"
    "google.golang.org/protobuf/types/pluginpb"
)

func main() {
    in, err := ioutil.ReadFile("./out.pb")
    if err != nil {
        fmt.Printf("failed to read proto file: %v", err)
        return
    }
    req := &pluginpb.CodeGeneratorRequest{}
    if err := proto.Unmarshal(in, req); err != nil {
        fmt.Printf("failed to unmarshal proto: %v", err)
        return
    }
    gen, err := protogen.Options{}.New(req)
    if err != nil {
        fmt.Printf("failed to create new plugin: %v", err)
        return
    }

    // serialize protobuf message "ServerConfig"
    data := &ServerConfig{
        GameType: 1,
        ServerId: 105,
        Host:     "host.host.host",
        Port:     10024,
    }
    raw, err := data.Marshal()
    if err != nil {
        fmt.Printf("failed to marshal protobuf: %v", err)
        return
    }

    for _, f := range gen.Files {
        for _, m := range f.Messages {

            // "ServerConfig" is the message name of the serialized message
            if m.GoIdent.GoName == "ServerConfig" {
                // m.Desc is MessageDescriptor
                msg := dynamicpb.NewMessage(m.Desc)
                // unmarshal []byte into proto message
                err := proto.Unmarshal(raw, msg)
                if err != nil {
                    fmt.Printf("failed to Unmarshal protobuf data: %v", err)
                    return
                }
                // marshal message into json
                jsondata, err := protojson.Marshal(msg)
                if err != nil {
                    fmt.Printf("failed to Marshal to json: %v", err)
                    return
                }
                fmt.Printf("out: %v", string(jsondata))
            }
        }
    }
}
// the output is:
// out: {"gameType":1, "serverId":105, "host":"host.host.host", "port":10024}

dynamicpb 包中应该这样工作。

func readDynamically(in []byte) {
    registry, err := createProtoRegistry(".", "addressbook.proto")
    if err != nil {
        panic(err)
    }

    desc, err := registry.FindFileByPath("addressbook.proto")
    if err != nil {
        panic(err)
    }
    fd := desc.Messages()
    addressBook := fd.ByName("AddressBook")

    msg := dynamicpb.NewMessage(addressBook)
    err = proto.Unmarshal(in, msg)
    jsonBytes, err := protojson.Marshal(msg)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(jsonBytes))
    if err != nil {
        panic(err)
    }
}

func createProtoRegistry(srcDir string, filename string) (*protoregistry.Files, error) {
    // Create descriptors using the protoc binary.
    // Imported dependencies are included so that the descriptors are self-contained.
    tmpFile := filename + "-tmp.pb"
    cmd := exec.Command("./protoc/protoc",
        "--include_imports",
        "--descriptor_set_out=" + tmpFile,
        "-I"+srcDir,
        path.Join(srcDir, filename))

    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    err := cmd.Run()
    if err != nil {
        return nil, err
    }
    defer os.Remove(tmpFile)

    marshalledDescriptorSet, err := ioutil.ReadFile(tmpFile)
    if err != nil {
        return nil, err
    }
    descriptorSet := descriptorpb.FileDescriptorSet{}
    err = proto.Unmarshal(marshalledDescriptorSet, &descriptorSet)
    if err != nil {
        return nil, err
    }

    files, err := protodesc.NewFiles(&descriptorSet)
    if err != nil {
        return nil, err
    }

    return files, nil
}