Implement custom annotations (#6987)

This commit is contained in:
cooldome 2018-01-09 14:25:22 +00:00 • committed by Andreas Rumpf
commit 2c9e56a783
10 changed files with 254 additions and 42 deletions

View file

@ -0,0 +1,43 @@
import macros
block:
template myAttr() {.pragma.}
proc myProc():int {.myAttr.} = 2
const myAttrIdx = myProc.hasCustomPragma(myAttr)
static:
assert(myAttrIdx)
block:
template myAttr(a: string) {.pragma.}
type MyObj = object
myField1, myField2 {.myAttr: "hi".}: int
var o: MyObj
static:
assert o.myField2.hasCustomPragma(myAttr)
assert(not o.myField1.hasCustomPragma(myAttr))
import custom_pragma
block: # A bit more advanced case
type
Subfield = object
c {.serializationKey: "cc".}: float
MySerializable = object
a {.serializationKey"asdf", defaultValue: 5.} : int
b {.custom_pragma.defaultValue"hello".} : int
field: Subfield
d {.alternativeKey("df", 5).}: float
e {.alternativeKey(V = 5).}: seq[bool]
var s: MySerializable
const aDefVal = s.a.getCustomPragmaVal(defaultValue)
static: assert(aDefVal == 5)
const aSerKey = s.a.getCustomPragmaVal(serializationKey)
static: assert(aSerKey == "asdf")
const cSerKey = getCustomPragmaVal(s.field.c, serializationKey)
static: assert(cSerKey == "cc")