JuliaIO / ProtoBuf.jl

Julia protobuf implementation
Other
205 stars 55 forks source link

reflection without __protobuf_jl_internal_meta? #225

Closed cschiefe closed 1 year ago

cschiefe commented 1 year ago

Hello, I'm porting some code to run with ProtoBuf.jl v 1.0.9 and ran into some trouble now that __protobuf_jl_internal_meta is gone. We need a way to get the expected types of each pb message's fields. The default_values() work quite well until we get to nested protobufs, such as the following:

message ChatPb
{
        string Contents = 1;
        ColorPb Color = 2;
}

message ColorPb
{
        int32 R = 1;
        int32 G = 2;
        int32 B = 3;
        int32 A = 4;
}
julia> msg = default_values(ChatPb)
ChatPb(Contents="", Color=nothing)

It seems as though the default value of the Color field should == ColorPb(). Was this an oversight or perhaps there is another way to get the intended type of the Color field at runtime?

Drvi commented 1 year ago

Hey @cschiefe! The default value choice of nothing was deliberate, the protobuf language guide has the following to say about default values:

[...] For message fields, the field is not set. Its exact value is language-dependent. See the generated code guide for details.

Nothing is our way of saying that the field is "not set".

You should be able to get the type information using Julia reflection capabilities, e.g.:

julia> struct A
           a::Int
           b
       end

julia> fieldnames(A)
(:a, :b)

julia> fieldtypes(A)
(Int64, Any)
cschiefe commented 1 year ago

Thanks - I'll go with that and de-unionize Union{Nothing, ColorPb} to get the info needed.