Skip to content

Commit

Permalink
add fn macro alternative for json interoperability
Browse files Browse the repository at this point in the history
The default `f{}` macro does not work if `json` is imported :/ See
issue #28 for reference.
  • Loading branch information
Vindaar committed Jan 29, 2020
1 parent 68bc413 commit fae97f4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/ggplotnim/formula.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1282,10 +1282,15 @@ proc buildFormula(n: NimNode): NimNode =
else:
raise newException(Exception, "Not implemented! " & $n.kind)

macro `{}`*(x, y: untyped): untyped =
if x.repr == "f":
macro `{}`*(x: untyped{ident}, y: untyped): untyped =
if x.strVal == "f":
result = buildFormula(y)

macro `fn`*(x: untyped): untyped =
let arg = if x.kind == nnkStmtList: x[0] else: x
expectKind arg, nnkCurly
result = buildFormula(arg[0])

proc unique*(v: PersistentVector[Value]): seq[Value] =
## returns a seq of all unique values in `v`
result = v.vToSeq.deduplicate
Expand Down
19 changes: 19 additions & 0 deletions tests/test_issue28.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import ggplotnim
import json

template accept(x) =
static: assert(compiles(x))

template reject(x) =
static: assert(not compiles(x))

accept:
let f = fn {"Channel" == "Ch 0"}
accept:
let f2 = fn({"Channel" == "Ch 0"})
accept:
let f3 = fn:
{"Channel" == "Ch 0"}

reject:
let f = f{"Channel" == "Ch 0"}

0 comments on commit fae97f4

Please sign in to comment.