starwing / lua-protobuf

A Lua module to work with Google protobuf
MIT License
1.73k stars 387 forks source link

custom options support #196

Closed stillcold closed 2 years ago

stillcold commented 2 years ago

Hi guys,

Is there a way I can use custom options with this repo?

custom options:

import "google/protobuf/descriptor.proto";

extend google.protobuf.MessageOptions { optional string my_option = 51234; }

message MyMessage { option (my_option) = "Hello world!"; }

Thanks in advance.

stillcold commented 2 years ago

Below is my code:

local function test()
    local P = protoc.new()

    P.unknown_import = true
    P.unknown_type = true

    assert(P:load([[
        syntax = "proto2";

        import "descriptor.proto";
        extend google.protobuf.MessageOptions {
            optional string my_option = 51234;
        }
    ]], "extend1.proto"))

    P.unknown_import = nil
    P.unknown_type = nil

    local chunk = assert(P:compile [[
        syntax = "proto2";
        import "extend1.proto"

        message MyMessage {
            option (my_option) = "Hello world!";
        }
    ]])

    assert(pb.load(chunk))
    local data = pb.decode("google.protobuf.FileDescriptorSet", chunk)

    table.print(data)
end

test()

And this is what I got:


['file'] = {
    [1] = {
            ['public_dependency'] = {
                    [1] = 0
            },
            ['syntax'] = 'proto2',
            ['name'] = '<input>',
            ['dependency'] = {
                    [1] = 'extend1.proto'
            },
            ['message_type'] = {
                    [1] = {
                            ['options'] = {

                            },
                            ['name'] = 'MyMessage'
                    }
            }
    }
}

As we can see, options is a empty table. So how can I get the right option of my message?

starwing commented 2 years ago

Okay, let me see...

starwing commented 2 years ago

There are some bugs of custom option support in protoc.lua, fixed on HEAD, please try again.

stillcold commented 2 years ago

Works for me. Thanks~