Closed zhenyamega closed 2 years ago
See the docs on variant object types, specifically this:
The value of a discriminator field must be loaded before any value of a field that depends on it. Therefore, a YAML mapping cannot be used to serialize variant object types - the YAML specification explicitly states that the order of key-value pairs in a mapping must not be used to convey content information. So, any variant object type is serialized as a list of key-value pairs.
This should answer 1.
Concerning 2, multiple case
constructs in an object are currently not supported. I am not entirely sure why, since it has been a long time since I wrote that code. I can't promise a timely fix but suggest you have a look at {.implicit.}
and check if that can solve your problem. Your example would become:
import yaml/serialization, streams
type
DataType = enum
Default
Sequence
Inner1 {.implicit.} = object
case kind: DataType
of Default: value: string
of Sequence: valueSeq: seq[string]
Inner2 {.implicit.} = object
case kind : DataType
of Default: value: string
of Sequence: valueSeq: seq[string]
Data = object
data: string
inner1: Inner1
inner2: Inner2
setTag(seq[string], "!list".Tag)
var node: Data
let s = newFileStream("in.yaml")
load(s, node)
s.close()
This can load the following YAML:
data: test
inner1: test # sets kind to Default and value to "test"
inner2: !list [test2, test3] # sets kind to Sequence.
I gave incorrect variable naming in the examples. I don't need variant object types. I need variant object fields and these variants may be several.
type
DataType = enum
Default
Type1
Type2
Config = object
name: string
case kind: DataType
of Type1:
param1: string
param2: string
of Type2:
param3: string
param4: seq[string]
else: discard
name: some_confing
kind: Type2
param3: str
param4:
- str1
- str2
That Config
type is loadable but you must put the fields in a sequence for the reason quoted above from the docs. It's either that or use an implicit type and wrap the inner params into objects.
1.
This object "Data" detection as Map
but this object "Data" detection as Seq
error:
Error: unhandled exception: While constructing Data: Expected yamlStartSeq, got yamlStartMap [YamlConstructionError]
Why did it become a sequence?
2.
with 1 enum we can work
main.nim
in.yaml
It work and output
test
but with 2 enum
main.nim
in.yaml
error
/root/.nimble/pkgs/yaml-0.16.0/yaml/serialization.nim(739, 16) Error: Unexpected kind of field 'kind2': nnkRecCase