JuliaIO / ProtoBuf.jl

Julia protobuf implementation
Other
205 stars 55 forks source link

Add type assertions when encoding OneOf members #223

Closed Drvi closed 2 years ago

Drvi commented 2 years ago

This changes codegen for encode and _encode_size methods for OneOf types. This should help type inference, definitely in the case when we don't parameterize the parent struct. E.g. this changes code generated in tests for "complex_message_pb.jl" from:

...
 if isnothing(x.oneof_field);
    elseif x.oneof_field.name === :oneof_bytes_field
        PB.encode(e, 17, x.oneof_field[])
    elseif x.oneof_field.name === :oneof_string_field
        PB.encode(e, 18, x.oneof_field[])
    elseif x.oneof_field.name === :oneof_uint32_field
        PB.encode(e, 19, x.oneof_field[])
    elseif x.oneof_field.name === :oneof_uint64_field
        PB.encode(e, 20, x.oneof_field[])
    elseif x.oneof_field.name === :oneof_int32_field
        PB.encode(e, 21, x.oneof_field[])
...

to

...
 if isnothing(x.oneof_field);
    elseif x.oneof_field.name === :oneof_bytes_field
        PB.encode(e, 17, x.oneof_field[]::Vector{UInt8})
    elseif x.oneof_field.name === :oneof_string_field
        PB.encode(e, 18, x.oneof_field[]::String)
    elseif x.oneof_field.name === :oneof_uint32_field
        PB.encode(e, 19, x.oneof_field[]::UInt32)
    elseif x.oneof_field.name === :oneof_uint64_field
        PB.encode(e, 20, x.oneof_field[]::UInt64)
    elseif x.oneof_field.name === :oneof_int32_field
        PB.encode(e, 21, x.oneof_field[]::Int32)
...
codecov[bot] commented 2 years ago

Codecov Report

Base: 89.04% // Head: 89.04% // No change to project coverage :thumbsup:

Coverage data is based on head (51962d9) compared to base (787898a). Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #223 +/- ## ======================================= Coverage 89.04% 89.04% ======================================= Files 25 25 Lines 2794 2794 ======================================= Hits 2488 2488 Misses 306 306 ``` | [Impacted Files](https://codecov.io/gh/JuliaIO/ProtoBuf.jl/pull/223?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=JuliaIO) | Coverage Δ | | |---|---|---| | [src/codegen/encode\_methods.jl](https://codecov.io/gh/JuliaIO/ProtoBuf.jl/pull/223/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=JuliaIO#diff-c3JjL2NvZGVnZW4vZW5jb2RlX21ldGhvZHMuamw=) | `97.70% <100.00%> (ø)` | | | [src/codegen/names.jl](https://codecov.io/gh/JuliaIO/ProtoBuf.jl/pull/223/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=JuliaIO#diff-c3JjL2NvZGVnZW4vbmFtZXMuamw=) | `100.00% <0.00%> (ø)` | | | [src/codegen/toplevel\_definitions.jl](https://codecov.io/gh/JuliaIO/ProtoBuf.jl/pull/223/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=JuliaIO#diff-c3JjL2NvZGVnZW4vdG9wbGV2ZWxfZGVmaW5pdGlvbnMuamw=) | `96.50% <0.00%> (ø)` | | Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=JuliaIO). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=JuliaIO)

:umbrella: View full report at Codecov.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.