starwing / lua-protobuf

A Lua module to work with Google protobuf
MIT License
1.71k stars 388 forks source link

how to use #220

Open ghost opened 1 year ago

ghost commented 1 year ago

I have a proto file in directory proto/: proto/order.proto

I want to use this proto file.

how can I do this?

Could someone give me a full, working and self-contained example how to do this?

local pb = require "pb"
local pbio   = require "pb.io"
local protoc = require "protoc"

pbio.dump("abc.proto", [[
  syntax = "proto3";

package abc;

message Msg {
  int32 id = 1;
}]])

assert(protoc.new():loadfile("abc.proto"))

local data = {
  id = 0
}

local chunk, other_result = pb.encode("Msg", data)

result:

 % lua test.lua
lua: test.lua:20: bad argument #1 to 'encode' (type 'Msg' does not exists)
stack traceback:
        [C]: in function 'pb.encode'
        test.lua:20: in main chunk
        [C]: in ?

what am I doing wrong?

ausk commented 1 year ago

try to add package prefix : pb.encode("abc.Msg", data)