mirror of https://github.com/status-im/NimYAML.git
Serialization: Support enum types
This commit is contained in:
parent
e967038d06
commit
e89a9a5b14
|
@ -212,8 +212,7 @@ Output:
|
||||||
- Add type hints for more scalar types
|
- Add type hints for more scalar types
|
||||||
* Serialization:
|
* Serialization:
|
||||||
- Support for more standard library types
|
- Support for more standard library types
|
||||||
- Support for enum types
|
- Support polymorphism
|
||||||
- Support polymorphism (requires ref and ptr types)
|
|
||||||
- Support variant objects
|
- Support variant objects
|
||||||
- Support transient fields (i.e. fields that will not be (de-)serialized on
|
- Support transient fields (i.e. fields that will not be (de-)serialized on
|
||||||
objects and tuples)
|
objects and tuples)
|
||||||
|
|
|
@ -7,6 +7,9 @@ type
|
||||||
i: int32
|
i: int32
|
||||||
b: bool
|
b: bool
|
||||||
|
|
||||||
|
TrafficLight = enum
|
||||||
|
tlGreen, tlYellow, tlRed
|
||||||
|
|
||||||
Person = object
|
Person = object
|
||||||
firstname, surname: string
|
firstname, surname: string
|
||||||
age: int32
|
age: int32
|
||||||
|
@ -89,6 +92,25 @@ suite "Serialization":
|
||||||
assertStringEqual "%YAML 1.2\n--- \n- [1, 2, 3]\n- [4, 5]\n- [6]",
|
assertStringEqual "%YAML 1.2\n--- \n- [1, 2, 3]\n- [4, 5]\n- [6]",
|
||||||
output.data
|
output.data
|
||||||
|
|
||||||
|
test "Serialization: Load Enum":
|
||||||
|
let input = newStringStream("- tlRed\n- tlGreen\n- tlYellow")
|
||||||
|
var
|
||||||
|
result: seq[TrafficLight]
|
||||||
|
parser = newYamlParser(tagLib)
|
||||||
|
events = parser.parse(input)
|
||||||
|
construct(events, result)
|
||||||
|
assert result.len == 3
|
||||||
|
assert result[0] == tlRed
|
||||||
|
assert result[1] == tlGreen
|
||||||
|
assert result[2] == tlYellow
|
||||||
|
|
||||||
|
test "Serialization: Serialize Enum":
|
||||||
|
let input = @[tlRed, tlGreen, tlYellow]
|
||||||
|
var output = newStringStream()
|
||||||
|
dump(input, output, psBlockOnly, tsNone)
|
||||||
|
assertStringEqual "%YAML 1.2\n--- \n- tlRed\n- tlGreen\n- tlYellow",
|
||||||
|
output.data
|
||||||
|
|
||||||
test "Serialization: Load Tuple":
|
test "Serialization: Load Tuple":
|
||||||
let input = newStringStream("str: value\ni: 42\nb: true")
|
let input = newStringStream("str: value\ni: 42\nb: true")
|
||||||
var
|
var
|
||||||
|
|
|
@ -605,7 +605,7 @@ proc serializeObject*[K, V](value: Table[K, V], ts: TagStyle,
|
||||||
yield event
|
yield event
|
||||||
yield YamlStreamEvent(kind: yamlEndMap)
|
yield YamlStreamEvent(kind: yamlEndMap)
|
||||||
|
|
||||||
template yamlTag*(T: typedesc[object]): expr =
|
template yamlTag*(T: typedesc[object|enum]): expr =
|
||||||
var uri = when compiles(yamlTagId(T)): yamlTagId(T) else:
|
var uri = when compiles(yamlTagId(T)): yamlTagId(T) else:
|
||||||
"!nim:custom:" & T.name
|
"!nim:custom:" & T.name
|
||||||
try:
|
try:
|
||||||
|
@ -663,6 +663,27 @@ proc serializeObject*[O: object|tuple](value: O, ts: TagStyle,
|
||||||
yield event
|
yield event
|
||||||
yield endMapEvent()
|
yield endMapEvent()
|
||||||
|
|
||||||
|
proc constructObject*[O: enum](s: YamlStream, c: ConstructionContext,
|
||||||
|
result: var O)
|
||||||
|
{.raises: [YamlConstructionError, YamlConstructionStreamError].} =
|
||||||
|
let e = s()
|
||||||
|
assert(not finished(s))
|
||||||
|
if e.kind != yamlScalar:
|
||||||
|
raise newException(YamlConstructionError, "Expected scalar, got " &
|
||||||
|
$e.kind)
|
||||||
|
try: result = parseEnum[O](e.scalarContent)
|
||||||
|
except ValueError:
|
||||||
|
var ex = newException(YamlConstructionError, "Cannot parse '" &
|
||||||
|
e.scalarContent & "' as " & type(O).name)
|
||||||
|
ex.parent = getCurrentException()
|
||||||
|
raise ex
|
||||||
|
|
||||||
|
proc serializeObject*[O: enum](value: O, ts: TagStyle,
|
||||||
|
c: SerializationContext):
|
||||||
|
YamlStream {.raises: [].} =
|
||||||
|
result = iterator(): YamlStreamEvent =
|
||||||
|
yield scalarEvent($value, presentTag(O, ts), yAnchorNone)
|
||||||
|
|
||||||
proc yamlTag*[O](T: typedesc[ref O]): TagId {.inline, raises: [].} = yamlTag(O)
|
proc yamlTag*[O](T: typedesc[ref O]): TagId {.inline, raises: [].} = yamlTag(O)
|
||||||
|
|
||||||
proc constructObject*[O](s: YamlStream, c: ConstructionContext,
|
proc constructObject*[O](s: YamlStream, c: ConstructionContext,
|
||||||
|
|
Loading…
Reference in New Issue