Skip to content

Commit

Permalink
multiple fixes in codec and codegen
Browse files Browse the repository at this point in the history
- removed duplicate using
- use relative package path with nested packages
- use @compat for Dict in generated code
- handle differences in convert in Julia 0.4
- writeproto now writes fields with default values if they are explicitly set (causes error with some readers otherwise)
  • Loading branch information
tanmaykm committed Feb 5, 2015
1 parent 6e3d587 commit a1ea37f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
@@ -1,6 +1,6 @@
The ProtoBuf.jl package is licensed under the MIT "Expat" License:

> Copyright (c) 2014: tan.
> Copyright (c) 2014: Tanmay Mohapatra.
>
> Permission is hereby granted, free of charge, to any person obtaining
> a copy of this software and associated documentation files (the
Expand Down
6 changes: 3 additions & 3 deletions src/codec.jl
Expand Up @@ -92,7 +92,7 @@ end
function _read_zigzag{T <: Integer}(io::IO, typ::Type{T})
zx = _read_uleb(io, UInt64)
# result is positive if zx is even
convert(typ, iseven(zx) ? (zx >>> 1) : -((zx+1) >>> 1))
convert(typ, iseven(zx) ? (zx >>> 1) : -signed((zx+1) >>> 1))
end


Expand Down Expand Up @@ -196,7 +196,7 @@ function _setmeta(meta::ProtoMeta, jtype::Type, ordered::Array{ProtoMetaAttribs,
end

function writeproto(io::IO, val, attrib::ProtoMetaAttribs)
!isempty(attrib.default) && isequal(val, attrib.default[1]) && (return 0)
#!isempty(attrib.default) && isequal(val, attrib.default[1]) && (return 0)
fld = attrib.fldnum
meta = attrib.meta
ptyp = attrib.ptyp
Expand Down Expand Up @@ -324,7 +324,7 @@ function readproto(io::IO, obj, meta::ProtoMeta=meta(typeof(obj)))
fld = attrib.fld
if !isfilled(obj, fld) && (length(attrib.default) > 0)
default = attrib.default[1]
setfield!(obj, fld, deepcopy(default))
setfield!(obj, fld, convert(fieldtype(typeof(obj), fld), deepcopy(default)))
fillset(obj, fld)
end
end
Expand Down
14 changes: 11 additions & 3 deletions src/gen.jl
Expand Up @@ -276,10 +276,10 @@ function generate(outio::IO, errio::IO, dtype::DescriptorProto, scope::Scope, ex
# generate the meta for this type if required
_d_fldnums = [1:length(fldnums)]
!isempty(reqflds) && println(io, "const __req_$(dtypename) = Symbol[$(join(reqflds, ','))]")
!isempty(defvals) && println(io, "const __val_$(dtypename) = [$(join(defvals, ", "))]")
!isempty(defvals) && println(io, "const __val_$(dtypename) = @compat Dict($(join(defvals, ", ")))")
(fldnums != _d_fldnums) && println(io, "const __fnum_$(dtypename) = Int[$(join(fldnums, ','))]")
!isempty(packedflds) && println(io, "const __pack_$(dtypename) = Symbol[$(join(packedflds, ','))]")
!isempty(wtypes) && println(io, "const __wtype_$(dtypename) = [$(join(wtypes, ", "))]")
!isempty(wtypes) && println(io, "const __wtype_$(dtypename) = @compat Dict($(join(wtypes, ", ")))")
if !isempty(reqflds) || !isempty(defvals) || (fldnums != [1:length(fldnums)]) || !isempty(packedflds) || !isempty(wtypes)
#logmsg("generating meta for type $(dtypename)")
print(io, "meta(t::Type{$dtypename}) = meta(t, ")
Expand Down Expand Up @@ -405,10 +405,16 @@ function generate(io::IO, errio::IO, protofile::FileDescriptorProto)
push!(depends, _packages[dependency])
end
end

fullscopename = scope.is_module ? fullname(scope) : ""
parentscope = (isdefined(scope, :parent) && scope.parent.is_module) ? fullname(scope.parent) : ""
for dependency in using_pkgs
(fullscopename == dependency) && continue
!isempty(parentscope) && startswith(dependency, parentscope) && (dependency = ".$(dependency[length(parentscope)+1:end])")
println(io, "using $dependency")
end
end
println(io, "using Compat")
println(io, "using ProtoBuf")
println(io, "import ProtoBuf.meta")
println(io, "")
Expand Down Expand Up @@ -507,7 +513,9 @@ function print_package(io::IO, s::Scope, indent="")
fname = string(protofile_name_to_module_name(f), ".jl")
println(io, "$(nested)include(\"$fname\")")
end
for c in children
for c in s.children
# check if already included
(c in children) || continue
print_package(io, c, nested)
end
println(io, "$(indent)end")
Expand Down

0 comments on commit a1ea37f

Please sign in to comment.