lua-protobuf icon indicating copy to clipboard operation
lua-protobuf copied to clipboard

calling parsefile causes wrong encoding/decoding of messages with imported fields

Open Matus-p opened this issue 4 years ago • 2 comments

I have a proto file that imports google/protobuf/timestamp.proto. The timestamp field is not encoded/decoded at all, when p:parsefile is called before p:loadfile

The following code produces wrong output:

local protoc = require "protoc"
local pb = require "pb"
local p = protoc.new()
p.include_imports = true

local name="hello.proto"
p:parsefile(name) -- if this line is commented out, the code works as expected
p:loadfile(name)

local data = {
  name = "John",
  timestamp = {
    seconds = 10,
    nanos = 50
  }
}

local bytes = pb.encode(".hello.HelloRequest", data)
print(pb.tohex(bytes))

local data2 = pb.decode(".hello.HelloRequest", bytes)
require "pl.pretty".dump(data2)

output:

12 00 0A 04 4A 6F 68 6E
{
  name = "John",
  timestamp = {
  }
}

output when p:parsefile(name) is commented out

12 04 08 0A 10 32 0A 04 4A 6F 68 6E
{
  name = "John",
  timestamp = {
    nanos = 50,
    seconds = 10
  }
}

hello.proto looks like this

syntax = "proto3";
import "google/protobuf/timestamp.proto";
package hello;

message HelloRequest {
  string name = 1;
  google.protobuf.Timestamp timestamp = 2;
}

Matus-p avatar Mar 26 '21 21:03 Matus-p

Thanks for report it!

But It's difficult to correct this issue, for now these is just a work-around that just use two compiler instance to get parsed table and load. I will look into it to find out how to handle on_import() callback when the file is already loaded.

pull request are always welcome :-)

starwing avatar Mar 30 '21 03:03 starwing

I found out that swapping order of of p:parsefile and p:loadfile (i.e. p:loadfile is called before p:parsefile) works for me, so it might be a workaround too.

Matus-p avatar Mar 30 '21 06:03 Matus-p