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

A seq field that is missing parses as an empty list #7

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/yanyl/core.nim
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,13 @@ proc get*[T](n: YNode, k: string, t: typedesc[Option[T]]): Option[T] =
else:
none(T)

proc get*[T](n: YNode, k: string, t: typedesc[seq[T]]): seq[T] =
expectYMap n:
let m = n.mapVal
if k in m:
ofYaml(m[k], seq[T])
else:
@[]

proc simplifyName(k: YamlNode): string =
case k.kind
Expand Down
2 changes: 1 addition & 1 deletion tests/tyaml_option.nim
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ check hekate.secondaryJob.isNone()

let hs = hekate.toYamlStr()
# toString shouldn't put nones in the maps
check hs.contains("secondaryJob") == false
check hs.contains("secondaryJob") == false
37 changes: 37 additions & 0 deletions tests/tyaml_seq.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import
yanyl,
test_utils/yaml_testing,
std/options,
strutils,
unittest

type
Person* = object of RootObj
id: int
names*: seq[string]
Team* = object of RootObj
id: int
members*: seq[Person]

deriveYamls:
Person
Team

var sample = """
id: 1
names:
- Bobby
"""
let bobby = ofYamlStr(sample, Person)
check bobby.id == 1
check bobby.names.len == 1
check bobby.names[0] == "Bobby"

var noNameYaml = """
id: 0
"""
let noName = ofYamlStr(noNameYaml, Person)
check noName.id == 0
check noName.names.len == 0