guzba / sunny

JSON in Nim with Go-like field tags.
MIT License
14 stars 0 forks source link

Generics and custom field pragma #2

Open guzba opened 7 months ago

guzba commented 7 months ago

Elegantbeef pointed out that generics present a problem for the custom pragma approach used by Sunny for the Go-like field tags. Some notes:

type JsonField[S: static string] = distinct S
type MYType[T] = object
  x: T
proc jsonName(_: typedesc[MyType], _: JsonField["x"]): string = "Real name"

In the above example you cannot do x {.json: "MYJsonName".}: T As the pragma is lost in generic ast

import std/macros

template myPragma() {.pragma.}

type MyType[T] = object
  x: T

var a = MyType[int](x: 100)
assert not a.x.hasCustomPragma(myPragma)

From Discord as a last resort https://discord.com/channels/371759389889003530/371759389889003532/1214810826859487283

guzba commented 6 months ago

https://forum.nim-lang.org/t/11171