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

First class support for object variants #13

Open
planetis-m opened this issue Jun 9, 2021 · 2 comments
Open

First class support for object variants #13

planetis-m opened this issue Jun 9, 2021 · 2 comments

Comments

@planetis-m
Copy link
Owner

planetis-m commented Jun 9, 2021

Requires #12 to be implemented.

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

let s = newStringStream("""{"kind":"Apple","apple":"world"}""")
let a = s.jsonTo(Bar)
proc initFromJson(dst: var Bar, p: var JsonParser) =
  eat(p, tkCurlyLe)
  while p.tok != tkCurlyRi:
    if p.tok != tkString:
      raiseParseErr(p, "string literal as key")
    case p.a
    of "shared":
      discard getTok(p)
      eat(p, tkColon)
      initFromJson(dst.apple, p)
    of "kind":
      discard getTok(p)
      eat(p, tkColon)
      var kindTmp`gensym0: Fruit
      initFromJson(kindTmp`gensym0, p)
      if dst.kind != kindTmp`gensym0:
        case dst.kind
        of Banana:
          if onceBad or onceBanana:
            raiseParseErr(p, "valid object field")
          if onceShared:
            dst = (typeof dst)(kind: kindTmp`gensym0, shared: dst.shared)
        of Apple:
          if onceApple:
            raiseParseErr(p, "valid object field")
          if onceShared:
            dst = (typeof dst)(kind: kindTmp`gensym0, shared: dst.shared)
        else: discard
    of "bad":
      if dst.kind != Banana: # not shared
        raiseParseErr(p, "valid object field")
      discard getTok(p)
      eat(p, tkColon)
      initFromJson(dst.bad, p)
    of "banana":
      if dst.kind != Banana:
        if onceApple:
          raiseParseErr(p, "valid object field")
        if onceShared:
          dst = (typeof dst)(kind: Banana, shared: dst.shared)
      discard getTok(p)
      eat(p, tkColon)
      initFromJson(dst.banana, p)
    of "apple":
      if dst.kind != Apple:
        if onceBad or onceBanana:
          raiseParseErr(p, "valid object field")
        if onceShared:
          dst = (typeof dst)(kind: Apple, shared: dst.shared)
      discard getTok(p)
      eat(p, tkColon)
      initFromJson(dst.apple, p)
    else:
      raiseParseErr(p, "valid object field")
    if p.tok != tkComma:
      break
    discard getTok(p)
  eat(p, tkCurlyRi)
@planetis-m
Copy link
Owner Author

planetis-m commented Jun 9, 2021

Pretty bad, what if kind: "Apple" is not discovered yet. It would error on a valid json. Fixed.

@planetis-m
Copy link
Owner Author

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