From 576cf11d814e321b58786db2d2c43f610164c0bc Mon Sep 17 00:00:00 2001 From: Felix Krause Date: Tue, 4 Apr 2023 19:29:20 +0200 Subject: [PATCH] Can load fields of parent types. ref #131 * dumping fields of parent types not working yet --- test/tserialization.nim | 13 ++++ yaml/serialization.nim | 159 ++++++++++++++++++++++++++-------------- 2 files changed, 117 insertions(+), 55 deletions(-) diff --git a/test/tserialization.nim b/test/tserialization.nim index b56bdb2..ae95f7d 100644 --- a/test/tserialization.nim +++ b/test/tserialization.nim @@ -61,6 +61,11 @@ type WithIgnoredField {.ignore: ["z"].} = object x, y: int + Parent = object of RootObj + i*: int + Child = object of Parent + s*: string + proc `$`(v: BetterInt): string {.borrow.} proc `==`(left, right: BetterInt): bool {.borrow.} @@ -458,6 +463,14 @@ suite "Serialization": assertStringEqual(yamlDirs & "!n!custom:Person \nfirstnamechar: P\nsurname: Pan\nage: 12\n", output) + test "Load object with inherited fields": + let input = + "i: 4\ns: hello" + var result: Child + load(input, result) + assert result.i == 4 + assert result.s == "hello" + test "Load custom variant object": let input = "---\n- - name: Bastet\n - kind: akCat\n - purringIntensity: 7\n" & diff --git a/yaml/serialization.nim b/yaml/serialization.nim index 57a6d4b..0527d9c 100644 --- a/yaml/serialization.nim +++ b/yaml/serialization.nim @@ -598,41 +598,65 @@ proc recListNode(n: NimNode): NimNode {.compileTime.} = if n.kind == nnkRecList: result = n[0] else: result = n -proc fieldCount(t: NimNode): int {.compiletime.} = - result = 0 - let tDesc = getType(getType(t)[1]) - if tDesc.kind == nnkBracketExpr: - # tuple - result = tDesc.len - 1 - else: - # object - for child in tDesc[2].children: - inc(result) - if child.kind == nnkRecCase: - for bIndex in 1..