starwing / lua-protobuf

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

proto3怎么区分默认值和缺省值呢? #239

Open dwjcola opened 1 year ago

dwjcola commented 1 year ago

在设置pb.option("no_default_values")的情况下,无论是没赋值还是赋的默认值,decode出来都是nil,请问怎么区分呢? 另,试了 local parser = protoc.new(); parser.proto3_optional = true 之后在proto3中加入 optional 依然报错

starwing commented 1 year ago

你试试最新的master的版本,我这边是OK的:


local pb = require "pb"
local slice = require "pb.slice"
local buffer = require "pb.buffer"
local protoc = require "protoc"
local serpent = require "serpent"

local parser = protoc.new()
parser.proto3_optional = true
parser:load [[
syntax = "proto3";
message Test {
   optional uint32 foo = 1;
}
]]

print(serpent.block(pb.decode("Test", pb.encode("Test", {}))))
print(serpent.block(pb.decode("Test", pb.encode("Test", {foo = 0}))))

输出:

{} --[[table: 00000195450FBEF0]]
{
  foo = 0,
  optional_foo = "foo"
} --[[table: 00000195450FC830]]