starwing / lua-protobuf

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

Can we get all the messages as the order it defined in the file? #164

Closed Gowa2017 closed 2 years ago

Gowa2017 commented 2 years ago

As now the pb.types() 's return is no order.

I found the command :

protoc --decode-raw  < proto.pb 

get the output below:

1 {
  1: "server/login.proto"
  2: "s2c.login"
  4 {
    1: "G2CChallenge"
    2 {
      1: "time"
      3: 1
      4: 1
      5: 13
      10: "time"
    }
    2 {
      1: "challenge"
      3: 2
      4: 1
      5: 9
      10: "challenge"
    }
  }
  4 {
    1: "G2CKey"
    2 {
      1: "key"
      3: 1
      4: 1
      5: 9
      10: "key"
    }
  }
  4 {
    1: "G2CHandshakeOK"
    2 {
      1: "code"
      3: 1
      4: 1
      5: 13
      10: "code"
    }
    2 {
      1: "errmsg"
      3: 2
      4: 1
      5: 9
      10: "errmsg"
    }
    2 {
      1: "token"
      3: 3
      4: 1
      5: 9
      10: "token"
    }
  }
  4 {
    1: "G2CLoginOK"
    2 {
      1: "token"
      3: 1
      4: 1
      5: 9
      10: "token"
    }
  }
  12: "proto3"
}
1 {
  1: "client/login.proto"
  2: "c2s.login"
  4 {
    1: "C2SHandshake"
    2 {
      1: "time"
      3: 1
      4: 1
      5: 13
      10: "time"
    }
    2 {
      1: "key"
      3: 2
      4: 1
      5: 9
      10: "key"
    }
  }
  4 {
    1: "C2SAuth"
    2 {
      1: "hash"
      3: 1
      4: 1
      5: 9
      10: "hash"
    }
  }
  4 {
    1: "C2SLogin"
    2 {
      1: "username"
      3: 1
      4: 1
      5: 9
      10: "username"
    }
    2 {
      1: "password"
      3: 2
      4: 1
      5: 9
      10: "password"
    }
    2 {
      1: "channel"
      3: 3
      4: 1
      5: 13
      10: "channel"
    }
  }
  12: "proto3"
}

Its order is same with the .proto file.

starwing commented 2 years ago

you could just use pb.decode('google.FileDescriptorSet', <pb file binary data>) to get a Lua table about pb file with structure of FileDescriptorSet (in google's descriptor.proto schema file). in this table, the messages are in order of schema file.

Gowa2017 commented 2 years ago

thanks ..I will try it

Gowa2017 commented 2 years ago
lua: gen.lua:126: bad argument #1 to 'decode' (type 'google.FileDescriptorSet' does not exists)

I try to decode it, but failed.

starwing commented 2 years ago

You can load descriptor.pb file content (from Google) or simply require "protoc"

Gowa2017 @.***>于2021年10月12日 周二11:29写道:

lua: gen.lua:126: bad argument #1 to 'decode' (type 'google.FileDescriptorSet' does not exists)

I try to decode it, but failed.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/starwing/lua-protobuf/issues/164#issuecomment-940623904, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA36MZZ6XI7RCNAAKE35S3UGOTTBANCNFSM5FVO6RPA .

-- regards, Xavier Wang.

Gowa2017 commented 2 years ago

Not so sure how to do it. ,my code is :

package.cpath = package.cpath .. ";../../luaclib/?.so"
local pb        = require("pb")
local pbio      = require("pb.io")
local protoFile = "proto.pb"
local data      = assert(pbio.read(protoFile))
local ok, n     = pb.load(data)
assert(ok, n)

pb.decode("google.FileDescriptorSet", protoFile)

Error the same.

starwing commented 2 years ago

you should call this function: https://github.com/starwing/lua-protobuf/blob/e49582f704821b1b6049b4e5c3870ec89025cf2c/protoc.lua#L1147

because this function will be called after you require "protoc", so just require the protoc.lua.

Gowa2017 commented 2 years ago

I realy do this:

package.cpath = package.cpath .. ";../../luaclib/?.so"
local protoc    = require("protoc")
local pb        = require("pb")
local pbio      = require("pb.io")
local protoFile = "proto.pb"

local data      = assert(pbio.read(protoFile))
local ok, n     = pb.load(data)
assert(ok, n)

pb.decode("google.FileDescriptorSet", protoFile)

But :

lua: test.lua:12: bad argument #1 to 'decode' (type 'google.FileDescriptorSet' does not exists)
stack traceback:
        [C]: in function 'pb.decode'
        test.lua:12: in main chunk
        [C]: in ?
Gowa2017 commented 2 years ago

Oh, I read the protoc code, the type may be is 'google.protobuf.FileDescriptorSet', this workd:

package.cpath = package.cpath .. ";../../luaclib/?.so"
package.path = package.path .. ";3rd/Penlight/lua/?.lua"
local protoc    = require("protoc")
local pb        = require("pb")
local pbio      = require("pb.io")
local protoFile = "proto.pb"

local data      = assert(pbio.read(protoFile))
local pretty    = require("pl.pretty")
local t         = pb.decode("google.protobuf.FileDescriptorSet", data)
pretty.dump(t)
starwing commented 2 years ago

sorry for my typo 😢

Gowa2017 commented 2 years ago

thanks. I closed this issue