wow, this works !
    
    
    import macros
    import json
    import tables
    import sequtils
    import print
    
    
    func typeStringFromJsonKind(k: JsonNodeKind): string =
      case k:
      of JBool: "bool"
      of JInt: "int"
      of JFloat: "float"
      of JString: "string"
      else:
        error("Unsupported json kind: " & $k)
        ""
    
    macro genTableJsonStr*(fields: static string, name: untyped): untyped =
      result = newStmtList()
      
      let props = parseJson(fields).getFields().pairs.toSeq()
      
      let params = nnkRecList.newTree(props.mapIt(nnkIdentDefs.newTree(ident 
$it[0], ident typeStringFromJsonKind(it[1].kind), newEmptyNode())))
      
      # Generate the base type
      let baseType = newTree(nnkTypeSection,
        newTree(nnkTypeDef,
          newTree(nnkPostfix,
            newIdentNode("*"),
            newTree(nnkAccQuoted, name)
          ),
          newEmptyNode(),
          newTree(nnkObjectTy,
            newEmptyNode(),
            newEmptyNode(),
            # # Replace the specific AST with a generic one :
            params
          )
        )
      )
      result.add(baseType)
      
      # Generate the table type
      let tableType = newTree(nnkTypeSection,
        newTree(nnkTypeDef,
          newTree(nnkPostfix,
            newIdentNode("*"),
            newTree(nnkAccQuoted,
              name,
              newIdentNode("Table")
            )
          ),
          newEmptyNode(),
          newTree(nnkObjectTy,
            newEmptyNode(),
            newEmptyNode(),
            newTree(nnkRecList,
              newTree(nnkIdentDefs,
                newTree(nnkPostfix, newIdentNode("*"), newIdentNode("list")),
                newTree(nnkBracketExpr,
                  newIdentNode("seq"),
                  newTree(nnkAccQuoted, name)
                ),
                newEmptyNode()
              )
            )
          )
        )
      )
      result.add(tableType)
    
    
    Run

but this version is still not working...
    
    
    macro genTableJson*(fields: static JsonNode, name: untyped): untyped =
      result = newStmtList()
      
      
      let props = fields.getFields().pairs.toSeq()
      
      let params = nnkRecList.newTree(props.mapIt(nnkIdentDefs.newTree(ident 
$it[0], ident typeStringFromJsonKind(it[1].kind), newEmptyNode())))
      
      
      # Generate the base type
      let baseType = newTree(nnkTypeSection,
        newTree(nnkTypeDef,
          newTree(nnkPostfix,
            newIdentNode("*"),
            newTree(nnkAccQuoted, name)
          ),
          newEmptyNode(),
          newTree(nnkObjectTy,
            newEmptyNode(),
            newEmptyNode(),
            # # Replace the specific AST with a generic one :
            params
          )
        )
      )
      result.add(baseType)
      
      # Generate the table type
      let tableType = newTree(nnkTypeSection,
        newTree(nnkTypeDef,
          newTree(nnkPostfix,
            newIdentNode("*"),
            newTree(nnkAccQuoted,
              name,
              newIdentNode("Table")
            )
          ),
          newEmptyNode(),
          newTree(nnkObjectTy,
            newEmptyNode(),
            newEmptyNode(),
            newTree(nnkRecList,
              newTree(nnkIdentDefs,
                newTree(nnkPostfix, newIdentNode("*"), newIdentNode("list")),
                newTree(nnkBracketExpr,
                  newIdentNode("seq"),
                  newTree(nnkAccQuoted, name)
                ),
                newEmptyNode()
              )
            )
          )
        )
      )
      result.add(tableType)
    
    
    Run

Reply via email to