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

Can not parse seq of Components #11

Open
planetis-m opened this issue Mar 9, 2021 · 1 comment
Open

Can not parse seq of Components #11

planetis-m opened this issue Mar 9, 2021 · 1 comment

Comments

@planetis-m
Copy link
Owner

planetis-m commented Mar 9, 2021

import eminim, streams

type Component = ref object of RootObj
type Move = ref object of Component
  speed: float32
let s = newStringStream("""{"speed":20}""")
let a = Move(s.jsonTo(Component))
echo a.speed

From the marshal module docs:

Restriction: For objects, their type is not serialized. This means essentially that it does not work if the object has some other runtime type than its compiletime type.

@planetis-m
Copy link
Owner Author

planetis-m commented Jun 9, 2021

possible "solution": create a complicated macro that can understand object hierarchy:

type Component = ref object of RootObj
type Move = ref object of Component
  speed: float32

s.jsonTo(Component < Move | (Transform < Hierarchy))

Generates code that will recreate dst each time it matches the signature of a type.

type
  ObjHierarchy = enum
    Component
    Move
    Transform
    Hierarchy

var
  runtimeType: set[ObjHierarchy] = {Component}
eat(p, tkCurlyLe)
while p.tok != tkCurlyRi:
  if p.tok != tkString:
    raiseParseErr(p, "string literal as key")
  case p.a
  of "speed":
    discard getTok(p)
    eat(p, tkColon)
    if runtimeType * {Move} != {Move}:
      if Transform in runtimeType:
        when defined(emiLenient):
          skipJson(p)
        else: raiseParseErr(p, "invalid object hierarchy")
      dst = Move(field1: dst.field1, ...) # Recreate dst
    initFromJson(dst.speed, p)
  else:
    raiseParseErr(p, "valid object field")
  if p.tok != tkComma:
    break
  discard getTok(p)
eat(p, tkCurlyRi)

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