I’m trying to create a macro that will help me build structs from a schema defined in a JSON document and while I have something that works, I would love a code review so I can learn how to do it better…
macro make_struct(struct_name, schema)
fields=[:($(Symbol(entry[1])) :: $(Symbol(titlecase(entry[2])))) for entry in eval(schema)]
return Expr(:type, false, eval(struct_name), Expr(:block, fields...))
end
And then I can verify how everything works here.
println(@macroexpand @make_struct :STRUCT_NAME [["x","int32"],["y","float32"]])
@make_struct :STRUCT_NAME [["x","int32"],["y","float32"]]
dump(STRUCT_NAME(1,2))
But my usage of eval
and building my expression with the Expr(...)
syntax feels like I’m missing a key point. Can anyone help me out?