sean-lin / protoc-gen-lua Goto Github PK
View Code? Open in Web Editor NEWGoogle's Protocol Buffers project, ported to Lua
License: BSD 3-Clause "New" or "Revised" License
Google's Protocol Buffers project, ported to Lua
License: BSD 3-Clause "New" or "Revised" License
Lua has a built in limit of 200 local variables in any one instance on the VM.
Currently, protoc-gen-lua will exceed this limit for very large protocol buffer specifications. This causes an error when one tries to use the generated code.
I propose to use global variables when creating *_pb.lua files. Although they are slower, they are only used the first time this file is parsed. This would make the effect negligible in situations where the file is require'd once and the resultant API objects are used repeatedly within the same VM context, for example in NGINX across multiple requests.
in module decoder.lua, there is no implementions of function _SkipGroup and _EndGroup
protobuf.lua
line 759
name = io:format("(%s)", field.full_name)
line 765
prefix = io:format("%s[%d].", name, i)
please use string.format instead
Protocol Buffers v3.0.0 have been released for somedays, do you have a plan to support it ?
Is this usage correct?
pb_instance.ClearField("field_name")
Seems to fail with a nil
reference to fields_by_name
if message_descriptor.fields_by_name[field_name] == nil then
error('Protocol message has no "' .. field_name .. '" field.')
end
It looks like the member fields_by_name
is never set anywhere, further in the ClearField
method there also looks like another potential nil
reference as field
isn't set either.
my lua version is 5.1.
When I changed code io:format to string.format because of io nil erro .
Then I got error: protobuf.lua:752: attempt to call field 'append' (a nil value).
How can I fix it.
有64位字段的message,里面数值很大时 在计算 bytesize时出错, 应该是之前代码没兼容64位
function _VarintSize(value)
if value <= 0x7f then return 1 end
if value <= 0x3fff then return 2 end
if value <= 0x1fffff then return 3 end
if value <= 0xfffffff then return 4 end
return 5
end
may need to change:
function _VarintSize(value)
if value <= 0x7f then return 1 end
if value <= 0x3fff then return 2 end
if value <= 0x1fffff then return 3 end
if value <= 0xfffffff then return 4 end
if value <= 0x7ffffffff then return 5 end
if value <= 0x3ffffffffff then return 6 end
if value <= 0x1ffffffffffff then return 7 end
if value <= 0xffffffffffffff then return 8 end
if value <= 0x7fffffffffffffff then return 9 end
return 10
end
不知道这样改可行不可行?
双嵌套的不能用呀
下面是报错信息 和 3个proto文件内容 求解决呀 ^~^
LUA ERROR: [string "protobuf.lua"]:288: attempt to index upvalue 'message_type' (a nil value)
文件p_result.proto:
import "p_user.proto";
import "p_major.proto";
message Result {
required int32 resultCode = 1;
optional string failCode = 2;
optional User user = 3;
optional Major major = 4;
}
文件p_user.proto:
message User{
optional string userId = 1;
required string username = 2;
optional string password = 3;
optional string channel = 4;
optional string regdate = 5;
optional string lastlogindate = 6;
optional string tokenId = 7;
optional bool isFast = 8;
optional bool isBinded = 9;
}
文件p_major.proto:
message Major{
optional string userId = 1;
optional string majorId = 2;
optional string majorCode = 3;
}
I saw in the "descriptor.lua" that there were TYPE_UINT64 and CPPTYPE_UINT64 in the file.
Now i define a uint64 field named id in a .proto file and gen a _pb.lua file.
How can i use the filed?
I write a testcase:
print(type(xxx.id))
the result is still "number".
Lua's number is a double value. It can't hold a uint64 variable.
目前已经采用python3环境搞了,python2已经被放弃了,求教!!!!
我打算开多个线程,每个线程一个lua_State。现在需要确认下是否线程安全。
python3的支持,替换掉plugin中的同名文件
plugin.zip
In containers.lua
RepeatedCompositeFieldContainer and RepeatedScalarFieldContainer should impl a MergeFrom function which is called from parent message's MergeFrom
I think you want <machine/endian.h> in pb.c
How to install a plugin for protoc in windows?
When parse message with a double field, it may cause crash, because incorrect data alignment will send signal sigbus in these machines.So modify lua_pb.c to use follow functions to parse float and double:
static float unpack_float(const uint8_t* buffer)
{
float fvalue;
memcpy(&fvalue, buffer, sizeof(float));
return fvalue;
uint32_t uvalue;
memcpy(&uvalue, buffer, sizeof(uint32_t));
uvalue = le32toh(uvalue);
return *(*float)&uvalue;
}
static double unpack_double(const uint8_t* buffer)
{
double dvalue;
memcpy(&dvalue, buffer, sizeof(double));
return dvalue;
uint64_t uvalue;
memcpy(&uvalue, buffer, sizeof(uint64_t));
uvalue = le64toh(uvalue);
return *(*double)&uvalue;
}
local msg1= person_pb.Person()
msg1.personid = 100
msg1.name = "zhag"
msg1.email = "[email protected]"
local pb_data = msg1:SerializeToString() -- Parse Example
print(msg1.personid)
print(msg1.name)
print(msg1.email)
print result is 100 [email protected]
local msg2 = person_pb.Person()
msg2:ParseFromString(pb_data)
print(msg2.personid)
print(msg2.name)
print(msg2.email)
print result is 0 [email protected]
why msg2.personid is 0 ??
def get_ref_name(self, type_name):
return (node.filename + '_pb.').upper() + node.get_local_name()
or you just get code like this:
local MESSAGE_PB = require("Message_pb")
Message_pb.MessageSingle.RegisterExtension(REQUESTLOGIN_UP_FIELD)
Failed to parse message in lua, when the message contains embedded field which is defined in another proto file.
the error message and stack:
[LUA-print] LUA ERROR: [string "pb/containers.lua"]:27: attempt to index field '_message_descriptor' (a nil value)
[LUA-print] stack traceback:
[string ".\script/main.lua"]:53: in function <[string ".\script/main.lua"]:50>
[string "pb/containers.lua"]:27: in function 'add'
[string "pb/decoder.lua"]:249: in function 'field_decoder'
[string "pb/protobuf.lua"]:684: in function '_InternalParse'
[string "pb/decoder.lua"]:271: in function 'field_decoder'
[string "pb/protobuf.lua"]:684: in function '_internal_parse'
[string "pb/protobuf.lua"]:693: in function 'merge_from_string'
[string "pb/protobuf.lua"]:702: in function 'ParseFromString'
protoc-gen-lua-binfixes this problem
I report one bug for enum auto generate in protoc-gen-lua script.
proto file:
message RegisterReq {
enum CmdId
{
CMD_ID = 1;
}
optional ......
}
example:
module.REGISTERREQ_CMDID_CMD_ID_ENUM = protobuf.EnumValueDescriptor()
module.REGISTERREQ_CMDID_CMD_ID_ENUM.name = 'CMD_ID'
module.REGISTERREQ_CMDID_CMD_ID_ENUM.index = 0
module.REGISTERREQ_CMDID_CMD_ID_ENUM.number = 1
module.REGISTERREQ_CMDID.values = {REGISTERREQ_CMDID_CMD_ID_ENUM} // error
//should be
module.REGISTERREQ_CMDID.values = {module.REGISTERREQ_CMDID_CMD_ID_ENUM}
I am not very sure how to fix this gracefully
in protoc-gen-lua script 249 code_gen_enum_item function
I just change return value from
return obj_name
to
return 'module.' + obj_name
and It works
I tried running test.lua after compiling you code but getting error.
$ lua -v
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
$ protoc --version
libprotoc 3.4.0
$ protoc --lua_out=./ person.proto
$ lua test.lua
lua: ../protobuf/encoder.lua:224: attempt to call upvalue '_EncodeSignedVarint' (a nil value)
stack traceback:
../protobuf/encoder.lua:224: in function <../protobuf/encoder.lua:219>
(tail call): ?
../protobuf/encoder.lua:257: in function '?'
../protobuf/protobuf.lua:302: in function '_AttachFieldHelpers'
../protobuf/protobuf.lua:904: in function 'Message'
./person_pb.lua:104: in main chunk
[C]: in function 'require'
test.lua:4: in main chunk
[C]: ?
Can you please guide me?
protobuf.lua中_internal_serialize函数实现,ListFields方法遍历是无序的,会导致结果不稳定,在某些情景下无法反序列化。
local _internal_serialize = function(self, write_bytes)
for field_descriptor, field_value in message_meta._member.ListFields(self) do
field_descriptor._encoder(write_bytes, field_value)
end
end
改动:根据field的key的index排序,再进行遍历。
local _internal_serialize = function(self, write_bytes)
local field_list = {}
for k, v in pairs(self._fields) do
field_list[k.index + 1] = k
end
local n = table.maxn(field_list)
for i = 1, n do
local descriptor = field_list[i]
if descriptor then
value = self._fields[descriptor]
if _IsPresent(descriptor, value) then
descriptor._encoder(write_bytes, value)
end
end
end
end
--lua_out: file seen without package. lua-protobuf currently requires a package on every proto file: chesssrv.proto
如题
message Person {
required int32 id = 1;
required string name = 2;
optional string email = 3;
extensions 10 to max;
}
message Phone {
extend Person { required Phone phones = 10;} // phones字段若是repeated就不会有问题
enum PHONE_TYPE{
MOBILE = 1;
HOME = 2;
}
repeated string num = 1; // 或是再加一个字段,序列化也没问题了
}
用法感觉也没错误啊:
local person= person_pb.Person()
person.id = 1000
person.name = "Alice"
person.email = "[email protected]"
local home = person.Extensions[person_pb.Phone.phones]
table.insert(home.num, "2147483647")
local data = person:SerializeToString()
local msg = person_pb.Person()
msg:ParseFromString(data)
print(msg)
plugin.zip
python3的支持,替换掉plugin中的同名文件
Could you, please, help me with compiling it for windows platform?
https://github.com/memoryoff/protoc-gen-lua-bin-fix
support both lua5.1 and lua5.3,it works.
Say I have a local msg = person_pb.Person()
, how can I get desciptor info from msg
?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.