Hello,
I'm working on some serialization / deserialization using msgpack and I was
wondering if there was a way to use the macros system to iterate over field.
To illustrate here's what i'm ttrying to do :
type
Model = object
field1: string
field2: string
field3: int
FooObj = object
field1: Tensor[float]
field2: Table[string, float]
field3: int
proc newFooObj(model: Model): FooObj=
var field1: Tensor[float]
unpack(model.field1, field1)
var field2: Table[string, float]
unpack(model.field2, field2)
var field3: int
unpack(model.field3, field3)
result = FooOBj(field1: field1, field2: field2, field3: field3)
Run
Note that the name of the corresponding fields between type will always be
identical.
I have a dozen of type (for each serailized / deserialzed type) and each type
has lots of fields so writing by hand is doable by very tedious.
I'm not familiar with the macro system but I was wondering if there was a way
to iterate over the fields of an object with or without a bracket type (I've
used dumpTree and noticed a difference) and apply so I could simply pass 2 type
with identical field name and generate a procedure:
type
Model = object
field1: string
field2: string
field3: int
FooObj = object
field1: Tensor[float]
field2: Table[string, float]
field3: int
magicalProcGeneratingMacro[Model, FooObj](procName)
Run
I'm not experienced enough with Nim's macro system to see the solution (if
there is any) on my own or if I should just give up and do it by hand.