Call @enumx macro with NamedTuple constant as input

Hi, I have a NamedTuple and would like to create an EnumX from it, how can this be done?
CC @fredrikekre

Specifically, I’m using ProtoBuf.il to load an enum from Protocol Buffers.

And I want to create a new type using @enumx macro, where the input to the macro would be the NamedTuple key-value pairs as arguments.


Input:

const SrcProtoBufEnum = (;[
    Symbol("UNIVERSAL") => Int32(0),
    Symbol("WEB") => Int32(1),
    Symbol("IMAGES") => Int32(2),
    Symbol("LOCAL") => Int32(3),
    Symbol("NEWS") => Int32(4),
    Symbol("PRODUCTS") => Int32(5),
    Symbol("VIDEO") => Int32(6),
]...)

Output:

using EnumX
@enumx DstEnumX UNIVERSAL=0 WEB=1 IMAGES=2 LOCAL=3 NEWS=4 PRODUCTS=5 VIDEO=6
julia> using EnumX

julia> const SrcProtoBufEnum = (;[
           Symbol("UNIVERSAL") => Int32(0),
           Symbol("WEB") => Int32(1),
           Symbol("IMAGES") => Int32(2),
           Symbol("LOCAL") => Int32(3),
           Symbol("NEWS") => Int32(4),
           Symbol("PRODUCTS") => Int32(5),
           Symbol("VIDEO") => Int32(6),
       ]...);

julia> @eval @enumx DstEnumX $([:($k = $v) for (k, v) in pairs(SrcProtoBufEnum)]...)

julia> DstEnumX.T
Enum type DstEnumX.T <: Enum{Int32} with 7 instances:
 DstEnumX.UNIVERSAL = 0
 DstEnumX.WEB       = 1
 DstEnumX.IMAGES    = 2
 DstEnumX.LOCAL     = 3
 DstEnumX.NEWS      = 4
 DstEnumX.PRODUCTS  = 5
 DstEnumX.VIDEO     = 6
1 Like