Yep, it's feasible!

For the most part I just need to change the proc that I call so that it ignores 
assigning to the discriminator-field of an object-variant, so that I can make 
sure that one only gets assigned once explicitly.

So basically:
    
    
    template getIterator(a: typed): untyped =
      when a is ref:
        a[].fieldPairs
      
      else:
        a.fieldPairs
    
    proc mapToVariant*(source: auto, target: var auto, ignoreFields: 
SomeSet[string] = initHashSet[string]()) =
      when source is ref:
        if source == nil:
          return
      
      for sourceName, sourceField in source.getIterator():
        for targetName, targetField in target.getIterator():
          when sourceName.eqIdent(targetName) and sourceField is 
typeof(targetField):
            if targetName notin ignoreFields:
              targetField = sourceField
    
    
    Run

And then for a given annotated proc definition such as this (where the 
parameter to the pragma defines which parameter in the proc-definition contains 
the `kind` to be used):
    
    
    proc myMapProc(a: A, myKind: Kind): B {.mapVariant: "myKind".} = discard
    
    
    Run

Will get turned into a proc such as this:
    
    
    proc myMapProc(a: A; myKind: Kind): B =
      result = B()
      result.kind = myKind
      a.mapToVariant(result, "kind")
      discard
    
    
    Run

I've got the code down, I'll need to take some time to write the necessary 
tests around it etc., but likely by sunday or so we'll have version `0.2.` with 
this added.

Reply via email to