You might want something like this:
    
    
    import macros
    
    type Foo = object
      a: int
      b: string
    
    macro getFields(x: typed): untyped =
      let impl = getImpl(x)
      let objFields = impl[2][2] # 2 is nnkObjectTy, 2 2 is nnkRecList
      expectKind objFields, nnkRecList
      result = nnkBracket.newTree()
      for f in objFields:
        expectKind f, nnkIdentDefs
        result.add nnkPar.newTree(newLit f[0].strVal, newLit f[1].strVal)
    
    for (fieldName, typeName) in getFields(Foo):
      echo "Field: ", fieldName, " with type name ", typeName
    
    
    Run

It returns a bracket of (field name, type name) tuples. Both as strings, since 
you can't mix strings with types in a tuple. For more complicated objects you'd 
have to recurse on the fields with complex types of course.

Reply via email to