Skip to content

Commit 6be96d0

Browse files
authored
Merge pull request #221 from ExpandingMan/fix1
a few small fixes for codegen
2 parents 787898a + a6577ca commit 6be96d0

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ keywords = ["protobuf", "protoc"]
44
license = "MIT"
55
desc = "Julia protobuf implementation"
66
authors = ["Tomáš Drvoštěp <tomas.drvostep@gmail.com>"]
7-
version = "1.0.7"
7+
version = "1.0.8"
88

99
[deps]
1010
BufferedStreams = "e1450e63-4bb3-523b-b2a4-4ffa8c0fd77d"

src/codegen/names.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ const JULIA_RESERVED_KEYWORDS = Set{String}([
44
"ccall", "finally", "typealias", "break", "continue", "type",
55
"global", "module", "using", "import", "export", "const", "let",
66
"bitstype", "do", "baremodule", "importall", "immutable",
7-
"Type", "Enum", "Any", "DataType", "Base", "Set", "Method"
7+
"Type", "Enum", "Any", "DataType", "Base", "Set", "Method",
8+
"Array", "Vector", "Dict", "Union",
9+
"OneOf",
810
])
911

1012
_get_name(t::AbstractProtoType) = t.name
@@ -27,4 +29,4 @@ abstract_type_name(name::AbstractString) = string("var\"##Abstract", name, '"')
2729
jl_fieldname(@nospecialize(f::AbstractProtoFieldType)) = _safename(f.name)
2830
jl_fieldname(f::GroupType) = _safename(f.field_name)
2931

30-
_safe_namespace_string(ns::AbstractVector{<:AbstractString}) = string("var\"#$(first(ns))\"", '.', join(@view(ns[2:end]), '.'))
32+
_safe_namespace_string(ns::AbstractVector{<:AbstractString}) = string("var\"#$(first(ns))\"", '.', join(@view(ns[2:end]), '.'))

src/codegen/toplevel_definitions.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ function generate_struct(io, t::MessageType, ctx::Context)
8787
params_string = get_type_param_string(type_params)
8888

8989
print(io, "struct ", struct_name, length(t.fields) > 0 ? params_string : ' ', _maybe_subtype(abstract_base_name, ctx.options))
90-
length(t.fields) > 0 && println(io)
90+
# new line if there are fields, otherwise ensure that we have space before `end`
91+
length(t.fields) > 0 ? println(io) : print(io, ' ')
9192
for field in t.fields
9293
generate_struct_field(io, field, ctx, type_params)
9394
end
@@ -175,7 +176,7 @@ function translate(io, rp::ResolvedProtoFile, file_map::Dict{String,ResolvedProt
175176
println(io, "import ProtoBuf as PB")
176177
options.common_abstract_type && println(io, "using ProtoBuf: AbstractProtoBufMessage")
177178
println(io, "using ProtoBuf: OneOf")
178-
println(io, "using EnumX: @enumx")
179+
println(io, "using ProtoBuf.EnumX: @enumx")
179180
if (is_namespaced(p) || options.always_use_modules) && !isempty(p.definitions)
180181
len = 93
181182
for name in Iterators.map(_safename, p.sorted_definitions)
@@ -201,4 +202,4 @@ function translate(io, rp::ResolvedProtoFile, file_map::Dict{String,ResolvedProt
201202
codegen(io, p.definitions[def_name], ctx)
202203
end
203204
options.always_use_modules && !is_namespaced(p) && println(io, "end # module")
204-
end
205+
end

test/test_codegen.jl

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ end
6767
@test s == """
6868
import ProtoBuf as PB
6969
using ProtoBuf: OneOf
70-
using EnumX: @enumx"""
70+
using ProtoBuf.EnumX: @enumx"""
7171

7272
s, p, ctx = translate_simple_proto("", Options(always_use_modules=true))
7373
@test s == """
7474
module main_pb
7575
import ProtoBuf as PB
7676
using ProtoBuf: OneOf
77-
using EnumX: @enumx
77+
using ProtoBuf.EnumX: @enumx
7878
end # module"""
7979
end
8080

@@ -84,7 +84,7 @@ end
8484
import ProtoBuf as PB
8585
using ProtoBuf: AbstractProtoBufMessage
8686
using ProtoBuf: OneOf
87-
using EnumX: @enumx"""
87+
using ProtoBuf.EnumX: @enumx"""
8888
end
8989

9090
@testset "Minimal proto file with file imports" begin
@@ -93,7 +93,7 @@ end
9393
include("a_pb.jl")
9494
import ProtoBuf as PB
9595
using ProtoBuf: OneOf
96-
using EnumX: @enumx"""
96+
using ProtoBuf.EnumX: @enumx"""
9797

9898
s, p, ctx = translate_simple_proto("import \"path/to/a\";", Dict("path/to/a" => ""), Options(always_use_modules=true))
9999
@test s == """
@@ -102,7 +102,7 @@ end
102102
import a_pb
103103
import ProtoBuf as PB
104104
using ProtoBuf: OneOf
105-
using EnumX: @enumx
105+
using ProtoBuf.EnumX: @enumx
106106
end # module"""
107107
end
108108

@@ -113,7 +113,7 @@ end
113113
import .p
114114
import ProtoBuf as PB
115115
using ProtoBuf: OneOf
116-
using EnumX: @enumx"""
116+
using ProtoBuf.EnumX: @enumx"""
117117

118118
s, p, ctx = translate_simple_proto("import \"path/to/a\";", Dict("path/to/a" => "package p;"), Options(always_use_modules=true))
119119
@test s == """
@@ -122,7 +122,7 @@ end
122122
import .p
123123
import ProtoBuf as PB
124124
using ProtoBuf: OneOf
125-
using EnumX: @enumx
125+
using ProtoBuf.EnumX: @enumx
126126
end # module"""
127127
end
128128

@@ -312,6 +312,13 @@ end
312312
"""
313313
end
314314

315+
@testset "Empty struct with common abstract type" begin
316+
s, p, ctx = translate_simple_proto("message A { }", Options(always_use_modules=false, common_abstract_type=true))
317+
@test generate_struct_str(p.definitions["A"], ctx) == """
318+
struct A <: AbstractProtoBufMessage end
319+
"""
320+
end
321+
315322
@testset "OneOf field codegen" begin
316323
s, p, ctx = translate_simple_proto("message A { oneof a { int32 b = 1; } }", Options(parametrize_oneofs=true))
317324
@test occursin("""

0 commit comments

Comments
 (0)