starwing / lua-protobuf

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

Support parsing services #182

Closed spacewander closed 2 years ago

spacewander commented 2 years ago

We can use this feature to parse binary format .pb and fetch services definition directly.

Signed-off-by: spacewander spacewanderlzx@gmail.com

coveralls commented 2 years ago

Coverage Status

Coverage decreased (-0.04%) to 99.617% when pulling f6664e35c77dc1b99231cf0a994880b2ebdfafb0 on spacewander:serv into 1fe68c8f9684a13635499367d62944bedc4f2abc on starwing:master.

starwing commented 2 years ago

You could just use pb.decode('google.protobuf.FileDescriptorProto', pb_data) to retrieve all informations from pb file. the memory database only store data used to encode/decode message. If the service informations is needed to encode/decode, pb.h will read it, vice visa.

spacewander commented 2 years ago

Sure. But there is no way to get service/method information from a binary pb file. The information is only available in protoc.lua which requires the input as text format proto file.

starwing commented 2 years ago

image

starwing commented 2 years ago
$ cat .\tt.proto
syntax = "proto3";
message fv {}
service foo {
  rpc foo(fv) returns(fv);
}

$ protoc -o tt.pb .\tt.proto

$ cat .\tt.lua
local pb = require "pb"
local pbio = require "pb.io"
require "protoc"

local t = pb.decode("google.protobuf.FileDescriptorSet", pbio.read "tt.pb")
print(require "serpent".block(t))

$ lua tt.lua
{
  file = {
    {
      message_type = {
        {
          name = "fv"
        } --[[table: 0000023F01DFD0F0]]
      } --[[table: 0000023F01DFCA70]],
      name = "tt.proto",
      service = {
        {
          method = {
            {
              input_type = ".fv",
              name = "foo",
              output_type = ".fv"
            } --[[table: 0000023F01DFD3B0]]
          } --[[table: 0000023F01DFC830]],
          name = "foo"
        } --[[table: 0000023F01DFD370]]
      } --[[table: 0000023F01DFD2F0]],
      syntax = "proto3"
    } --[[table: 0000023F01DFD130]]
  } --[[table: 0000023F01DFC8F0]]
} --[[table: 0000023F01DFD1F0]]

$
spacewander commented 2 years ago

I see. Thanks for your suggestion!