Implement custom annotations (#6987)
This commit is contained in:
parent
aff787db69
commit
2c9e56a783
10 changed files with 254 additions and 42 deletions
5
tests/pragmas/custom_pragma.nim
Normal file
5
tests/pragmas/custom_pragma.nim
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# imported by tcustom_pragmas to test scoping
|
||||
|
||||
template serializationKey*(s: string) {.pragma.}
|
||||
template defaultValue*(V: typed) {.pragma.}
|
||||
template alternativeKey*(s: string = nil, V: typed) {.pragma.}
|
||||
43
tests/pragmas/tcustom_pragma.nim
Normal file
43
tests/pragmas/tcustom_pragma.nim
Normal 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")
|
||||
Loading…
Add table
Add a link
Reference in a new issue