Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support object variants #22

Open
planetis-m opened this issue Jun 7, 2022 · 1 comment
Open

Support object variants #22

planetis-m opened this issue Jun 7, 2022 · 1 comment

Comments

@planetis-m
Copy link
Owner

planetis-m commented Jun 7, 2022

suggested transformation:

type
  Fruit = enum
    Banana, Apple
  Bar = object
    case kind: Fruit
    of Banana:
      bad: float
      banana: int
    of Apple: apple: string

proc initFromJson(dst: var Bar; tree: JsonTree; n: NodePos) =
  verifyJsonKind(tree, n, {JObject})
  var kindTmp: Fruit
  initFromJson(kindTmp, tree, rawGet(tree, n, "kind"))
  dst = (typeof dst)(kind: kindTmp)
  for x in keys(tree, n):
    case dst.kind
    of Banana:
      case x.str
      of "bad":
        initFromJson(dst.bad, tree, x.firstSon)
      of "banana":
        initFromJson(dst.banana, tree, x.firstSon)
      else: discard
    of Apple:
      case x.str
      of "apple":
        initFromJson(dst.apple, tree, x.firstSon)
      else: discard

let x = parseJson("""{"kind":"Apple","apple":"world"}""")
var b: Bar
initFromJson(b, x, rootNodeId)
echo b
@planetis-m
Copy link
Owner Author

This is needlessly complicated due to recursive RecList. Let's wait until sum types land.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant