Skip to content

Commit

Permalink
Merge pull request #150 from JuliaIO/tan/codec
Browse files Browse the repository at this point in the history
track default values, fix oneof code generation
  • Loading branch information
tanmaykm authored Nov 23, 2020
2 parents ef609a9 + 353f2dd commit b40136a
Show file tree
Hide file tree
Showing 19 changed files with 155 additions and 78 deletions.
6 changes: 5 additions & 1 deletion src/codec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ function setdefaultproperties!(obj::ProtoType, meta::ProtoMeta=meta(typeof(obj))
default = attrib.default[1]
setproperty!(obj, fld, convert(attrib.jtyp, deepcopy(default)))
@debug("readproto set default", typ=typeof(obj), fld, default)
_markdefaultproperty!(obj, fld)
end
end
obj
Expand Down Expand Up @@ -680,10 +681,13 @@ function setproperty!(obj::ProtoType, fld::Symbol, val)
_unset_oneof(obj, objmeta, fld)
fldtype = symdict[fld].jtyp
obj.__protobuf_jl_internal_values[fld] = isa(val, fldtype) ? val : convert(fldtype, val)
delete!(obj.__protobuf_jl_internal_defaultset, fld)
else
setfield!(obj, fld, val)
end
end
_markdefaultproperty!(obj::ProtoType, fld::Symbol) = push!(obj.__protobuf_jl_internal_defaultset, fld)
isdefaultproperty(obj::ProtoType, fld::Symbol) = fld in obj.__protobuf_jl_internal_defaultset

function clear(obj::ProtoType)
empty!(obj.__protobuf_jl_internal_values)
Expand All @@ -702,4 +706,4 @@ function lookup(en::T, val) where {T <: NamedTuple}
(val == getproperty(en, name)) && return name
end
error("Enum has no value $val")
end
end
5 changes: 3 additions & 2 deletions src/gen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ function generate_msgtype(outio::IO, errio::IO, dtype::DescriptorProto, scope::S
if hasproperty(dtype, :field)
for fld_idx in 1:length(dtype.field)
field = dtype.field[fld_idx]
if hasproperty(field, :oneof_index) && !isempty(oneofs)
if hasproperty(field, :oneof_index) && !isempty(oneofs) && !ProtoBuf.isdefaultproperty(field, :oneof_index)
oneof_idx = field.oneof_index + 1
oneofs[fld_idx] = oneof_idx
end
Expand Down Expand Up @@ -468,9 +468,10 @@ function generate_msgtype(outio::IO, errio::IO, dtype::DescriptorProto, scope::S
mutable struct $(dtypename) <: ProtoType
__protobuf_jl_internal_meta::ProtoMeta
__protobuf_jl_internal_values::Dict{Symbol,Any}
__protobuf_jl_internal_defaultset::Set{Symbol}
function $(dtypename)(; kwargs...)
obj = new(meta($(dtypename)), Dict{Symbol,Any}())
obj = new(meta($(dtypename)), Dict{Symbol,Any}(), Set{Symbol}())
values = obj.__protobuf_jl_internal_values
symdict = obj.__protobuf_jl_internal_meta.symdict
for nv in kwargs
Expand Down
3 changes: 2 additions & 1 deletion src/google/any_pb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
mutable struct _Any <: ProtoType
__protobuf_jl_internal_meta::ProtoMeta
__protobuf_jl_internal_values::Dict{Symbol,Any}
__protobuf_jl_internal_defaultset::Set{Symbol}

function _Any(; kwargs...)
obj = new(meta(_Any), Dict{Symbol,Any}())
obj = new(meta(_Any), Dict{Symbol,Any}(), Set{Symbol}())
values = obj.__protobuf_jl_internal_values
symdict = obj.__protobuf_jl_internal_meta.symdict
for nv in kwargs
Expand Down
9 changes: 6 additions & 3 deletions src/google/api_pb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
mutable struct Method <: ProtoType
__protobuf_jl_internal_meta::ProtoMeta
__protobuf_jl_internal_values::Dict{Symbol,Any}
__protobuf_jl_internal_defaultset::Set{Symbol}

function Method(; kwargs...)
obj = new(meta(Method), Dict{Symbol,Any}())
obj = new(meta(Method), Dict{Symbol,Any}(), Set{Symbol}())
values = obj.__protobuf_jl_internal_values
symdict = obj.__protobuf_jl_internal_meta.symdict
for nv in kwargs
Expand Down Expand Up @@ -50,9 +51,10 @@ end
mutable struct Mixin <: ProtoType
__protobuf_jl_internal_meta::ProtoMeta
__protobuf_jl_internal_values::Dict{Symbol,Any}
__protobuf_jl_internal_defaultset::Set{Symbol}

function Mixin(; kwargs...)
obj = new(meta(Mixin), Dict{Symbol,Any}())
obj = new(meta(Mixin), Dict{Symbol,Any}(), Set{Symbol}())
values = obj.__protobuf_jl_internal_values
symdict = obj.__protobuf_jl_internal_meta.symdict
for nv in kwargs
Expand Down Expand Up @@ -88,9 +90,10 @@ end
mutable struct Api <: ProtoType
__protobuf_jl_internal_meta::ProtoMeta
__protobuf_jl_internal_values::Dict{Symbol,Any}
__protobuf_jl_internal_defaultset::Set{Symbol}

function Api(; kwargs...)
obj = new(meta(Api), Dict{Symbol,Any}())
obj = new(meta(Api), Dict{Symbol,Any}(), Set{Symbol}())
values = obj.__protobuf_jl_internal_values
symdict = obj.__protobuf_jl_internal_meta.symdict
for nv in kwargs
Expand Down
Loading

0 comments on commit b40136a

Please sign in to comment.