Schemas: seperate procs for unembeddables
This commit is contained in:
parent
54c28c90ce
commit
171502f1d2
|
@ -692,11 +692,14 @@ proc collectRefImports(imports: PNode; scm: Schema) =
|
||||||
|
|
||||||
proc renderNimModule*(scm: Schema): string =
|
proc renderNimModule*(scm: Schema): string =
|
||||||
## Construct and render a Nim module from a `Schema`.
|
## Construct and render a Nim module from a `Schema`.
|
||||||
|
proc mergeType(x: var PNode; y: PNode) =
|
||||||
|
if x.isNil: x = y
|
||||||
|
else: x = nn(nkInfix, ident"|", x, y)
|
||||||
var
|
var
|
||||||
typeDefs: TypeTable
|
typeDefs: TypeTable
|
||||||
typeSection = newNode nkTypeSection
|
typeSection = newNode nkTypeSection
|
||||||
procs: seq[PNode]
|
procs: seq[PNode]
|
||||||
megaType: PNode
|
unembeddableType, embeddableType: PNode
|
||||||
for name, def in scm.data.definitions.pairs:
|
for name, def in scm.data.definitions.pairs:
|
||||||
if isLiteral(scm, def):
|
if isLiteral(scm, def):
|
||||||
generateConstProcs(procs, scm, name, def)
|
generateConstProcs(procs, scm, name, def)
|
||||||
|
@ -705,11 +708,10 @@ proc renderNimModule*(scm: Schema): string =
|
||||||
name[0] = name[0].toUpperAscii
|
name[0] = name[0].toUpperAscii
|
||||||
var defIdent = parameterize(ident(name), isEmbeddable(scm, def))
|
var defIdent = parameterize(ident(name), isEmbeddable(scm, def))
|
||||||
if not isSymbolEnum(scm, def):
|
if not isSymbolEnum(scm, def):
|
||||||
if megaType.isNil:
|
if isEmbeddable(scm, def):
|
||||||
megaType = defIdent
|
mergeType(embeddableType, defIdent)
|
||||||
else:
|
else:
|
||||||
megaType = nn(nkInfix,
|
mergeType(unembeddableType, defIdent)
|
||||||
ident"|", megaType, defIdent)
|
|
||||||
let typeSpec = nimTypeOf(scm, typeDefs, def, name)
|
let typeSpec = nimTypeOf(scm, typeDefs, def, name)
|
||||||
typeDefs[name] = typeDef(scm, name, def, typeSpec.node)
|
typeDefs[name] = typeDef(scm, name, def, typeSpec.node)
|
||||||
generateProcs(procs, scm, name, def)
|
generateProcs(procs, scm, name, def)
|
||||||
|
@ -723,6 +725,7 @@ proc renderNimModule*(scm: Schema): string =
|
||||||
if isEmbeddable(scm):
|
if isEmbeddable(scm):
|
||||||
nn(nkGenericParams, nn(nkIdentDefs, ident"E", newEmpty(), newEmpty()))
|
nn(nkGenericParams, nn(nkIdentDefs, ident"E", newEmpty(), newEmpty()))
|
||||||
else: newEmpty()
|
else: newEmpty()
|
||||||
|
if not embeddableType.isNil:
|
||||||
procs.add nn(nkProcDef,
|
procs.add nn(nkProcDef,
|
||||||
"$".toFieldIdent,
|
"$".toFieldIdent,
|
||||||
newEmpty(),
|
newEmpty(),
|
||||||
|
@ -731,7 +734,7 @@ proc renderNimModule*(scm: Schema): string =
|
||||||
ident"string",
|
ident"string",
|
||||||
nn(nkIdentDefs,
|
nn(nkIdentDefs,
|
||||||
ident"x",
|
ident"x",
|
||||||
megaType,
|
embeddableType,
|
||||||
newEmpty())),
|
newEmpty())),
|
||||||
newEmpty(),
|
newEmpty(),
|
||||||
newEmpty(),
|
newEmpty(),
|
||||||
|
@ -746,13 +749,44 @@ proc renderNimModule*(scm: Schema): string =
|
||||||
nn(nkBracketExpr, ident"seq", ident"byte"),
|
nn(nkBracketExpr, ident"seq", ident"byte"),
|
||||||
nn(nkIdentDefs,
|
nn(nkIdentDefs,
|
||||||
ident"x",
|
ident"x",
|
||||||
megaType,
|
embeddableType,
|
||||||
newEmpty())),
|
newEmpty())),
|
||||||
newEmpty(),
|
newEmpty(),
|
||||||
newEmpty(),
|
newEmpty(),
|
||||||
nn(nkStmtList,
|
nn(nkStmtList,
|
||||||
nn(nkCall, ident"encode", nn(nkCall,
|
nn(nkCall, ident"encode", nn(nkCall,
|
||||||
ident"toPreserve", ident"x", ident"E"))))
|
ident"toPreserve", ident"x", ident"E"))))
|
||||||
|
if not unembeddableType.isNil:
|
||||||
|
procs.add nn(nkProcDef,
|
||||||
|
"$".toFieldIdent,
|
||||||
|
newEmpty(),
|
||||||
|
newEmpty(),
|
||||||
|
nn(nkFormalParams,
|
||||||
|
ident"string",
|
||||||
|
nn(nkIdentDefs,
|
||||||
|
ident"x",
|
||||||
|
unembeddableType,
|
||||||
|
newEmpty())),
|
||||||
|
newEmpty(),
|
||||||
|
newEmpty(),
|
||||||
|
nn(nkStmtList,
|
||||||
|
nn(nkCall, ident"$",
|
||||||
|
nn(nkCall, ident"toPreserve", ident"x"))))
|
||||||
|
procs.add nn(nkProcDef,
|
||||||
|
"encode".ident.toExport,
|
||||||
|
newEmpty(),
|
||||||
|
newEmpty(),
|
||||||
|
nn(nkFormalParams,
|
||||||
|
nn(nkBracketExpr, ident"seq", ident"byte"),
|
||||||
|
nn(nkIdentDefs,
|
||||||
|
ident"x",
|
||||||
|
unembeddableType,
|
||||||
|
newEmpty())),
|
||||||
|
newEmpty(),
|
||||||
|
newEmpty(),
|
||||||
|
nn(nkStmtList,
|
||||||
|
nn(nkCall, ident"encode", nn(nkCall,
|
||||||
|
ident"toPreserve", ident"x"))))
|
||||||
var module = newNode(nkStmtList).add(
|
var module = newNode(nkStmtList).add(
|
||||||
imports,
|
imports,
|
||||||
typeSection
|
typeSection
|
||||||
|
|
|
@ -199,7 +199,7 @@ proc `$`*(x: Ref | ModulePath | Bundle | CompoundPattern | Modules |
|
||||||
Binding): string =
|
Binding): string =
|
||||||
`$`(toPreserve(x))
|
`$`(toPreserve(x))
|
||||||
|
|
||||||
proc encode*[E](x: Ref | ModulePath | Bundle | CompoundPattern | Modules |
|
proc encode*(x: Ref | ModulePath | Bundle | CompoundPattern | Modules |
|
||||||
EmbeddedTypeName |
|
EmbeddedTypeName |
|
||||||
Definitions |
|
Definitions |
|
||||||
DictionaryEntries |
|
DictionaryEntries |
|
||||||
|
@ -211,4 +211,4 @@ proc encode*[E](x: Ref | ModulePath | Bundle | CompoundPattern | Modules |
|
||||||
Schema |
|
Schema |
|
||||||
Pattern |
|
Pattern |
|
||||||
Binding): seq[byte] =
|
Binding): seq[byte] =
|
||||||
encode(toPreserve(x, E))
|
encode(toPreserve(x))
|
||||||
|
|
Loading…
Reference in New Issue