From ac2689cec721eb94b49dfbfed310e2e0e3bdfa2b Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Thu, 21 Apr 2016 16:26:52 -0700 Subject: Put oneofs in the same table as fields. (#60) * Put oneofs in the same table as fields. Oneofs and fields are not allowed to have names that conflict, so we might as well put them all in the same table. This also allows an efficient operation that looks for both fields and oneofs in a single lookup. Added support for OneofDef to Lua to allow testing of this. * Addressed PR comments. --- upb/bindings/lua/upb.lua | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'upb/bindings/lua/upb.lua') diff --git a/upb/bindings/lua/upb.lua b/upb/bindings/lua/upb.lua index ee0ad4e..728852e 100644 --- a/upb/bindings/lua/upb.lua +++ b/upb/bindings/lua/upb.lua @@ -66,6 +66,7 @@ end local RealFieldDef = upb.FieldDef local RealEnumDef = upb.EnumDef local RealMessageDef = upb.MessageDef +local RealOneofDef = upb.OneofDef local RealSymbolTable = upb.SymbolTable -- FieldDef constructor; a wrapper around the real constructor that can @@ -136,6 +137,26 @@ upb.EnumDef = function(init) return e end +-- OneofDef constructor; a wrapper around the real constructor that can +-- set initial properties. +-- +-- User can specify initialization values like so: +-- upb.OneofDef{name="foo", fields={...}} +upb.OneofDef = function(init) + local o = RealOneofDef() + + if init then + for _, val in pairs(init.fields or {}) do + o:add(val) + end + init.fields = nil + + set_named(o, init) + end + + return o +end + -- SymbolTable constructor; a wrapper around the real constructor that can -- add an initial set of defs. upb.SymbolTable = function(defs) -- cgit v1.2.3