From 5bc983e0bf89243d21354ff9a5fb2db49318d794 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Tue, 3 Oct 2017 09:35:28 +0200 Subject: [PATCH] added lookuptables.nim helper module --- karax/lookuptables.nim | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 karax/lookuptables.nim diff --git a/karax/lookuptables.nim b/karax/lookuptables.nim new file mode 100644 index 0000000..c888b49 --- /dev/null +++ b/karax/lookuptables.nim @@ -0,0 +1,24 @@ + +import macros, kbase +from strutils import `%` + +macro fieldNamesAsArray*(t: typed; pattern = "$1"): untyped = + var impl = getTypeImpl(getTypeImpl(t)[1]) + if impl.kind == nnkRefTy: impl = getTypeImpl(impl[0]) + expectKind impl, nnkObjectTy + impl = impl[2] + expectKind impl, nnkRecList + result = newTree(nnkBracket) + for x in impl: + expectKind x, nnkIdentDefs + result.add(newCall(bindSym"kstring", newLit(pattern.strVal % $x[0]))) + +when isMainModule: + type + MyObject = ref object + x*: string + next*: int + more, here*: float + + const myObjectFields = fieldNamesAsArray(MyObject, "MyObject_$1") + for f in myObjectFields: echo f