Fix tuple size check in std/jsonutils (#20637)
* Add test for tuple being invalid size * Test tuple size before accessing fields * Fix formatting for import * Fix not being able to build from csources_v1 Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
parent
98b2838a30
commit
d261135c5c
2 changed files with 19 additions and 3 deletions
|
|
@ -32,7 +32,7 @@ add a way to customize serialization, for e.g.:
|
||||||
|
|
||||||
import macros
|
import macros
|
||||||
from enumutils import symbolName
|
from enumutils import symbolName
|
||||||
from typetraits import OrdinalEnum
|
from typetraits import OrdinalEnum, tupleLen
|
||||||
|
|
||||||
when defined(nimPreviewSlimSystem):
|
when defined(nimPreviewSlimSystem):
|
||||||
import std/assertions
|
import std/assertions
|
||||||
|
|
@ -286,11 +286,21 @@ proc fromJson*[T](a: var T, b: JsonNode, opt = Joptions()) =
|
||||||
fromJsonFields(a, nil, b, seq[string].default, opt)
|
fromJsonFields(a, nil, b, seq[string].default, opt)
|
||||||
else:
|
else:
|
||||||
checkJson b.kind == JArray, $(b.kind) # we could customize whether to allow JNull
|
checkJson b.kind == JArray, $(b.kind) # we could customize whether to allow JNull
|
||||||
|
|
||||||
|
when compiles(tupleLen(T)):
|
||||||
|
let tupleSize = tupleLen(T)
|
||||||
|
else:
|
||||||
|
# Tuple len isn't in csources_v1 so using tupleLen would fail.
|
||||||
|
# Else branch basically never runs (tupleLen added in 1.1 and jsonutils in 1.4), but here for consistency
|
||||||
|
var tupleSize = 0
|
||||||
|
for val in fields(a):
|
||||||
|
tupleSize.inc
|
||||||
|
|
||||||
|
checkJson b.len == tupleSize, $(b.len, tupleSize, $T, b) # could customize
|
||||||
var i = 0
|
var i = 0
|
||||||
for val in fields(a):
|
for val in fields(a):
|
||||||
fromJson(val, b[i], opt)
|
fromJson(val, b[i], opt)
|
||||||
i.inc
|
i.inc
|
||||||
checkJson b.len == i, $(b.len, i, $T, b) # could customize
|
|
||||||
else:
|
else:
|
||||||
# checkJson not appropriate here
|
# checkJson not appropriate here
|
||||||
static: doAssert false, "not yet implemented: " & $T
|
static: doAssert false, "not yet implemented: " & $T
|
||||||
|
|
|
||||||
|
|
@ -410,6 +410,13 @@ template fn() =
|
||||||
doAssert foo.c == 0
|
doAssert foo.c == 0
|
||||||
doAssert foo.c0 == 42
|
doAssert foo.c0 == 42
|
||||||
|
|
||||||
|
|
||||||
|
block testInvalidTupleLength:
|
||||||
|
let json = parseJson("[0]")
|
||||||
|
# Should raise ValueError instead of index error
|
||||||
|
doAssertRaises(ValueError):
|
||||||
|
discard json.jsonTo((int, int))
|
||||||
|
|
||||||
type
|
type
|
||||||
InnerEnum = enum
|
InnerEnum = enum
|
||||||
A
|
A
|
||||||
|
|
@ -431,7 +438,6 @@ template fn() =
|
||||||
let json = inner.toJson(ToJsonOptions(enumMode: joptEnumSymbol))
|
let json = inner.toJson(ToJsonOptions(enumMode: joptEnumSymbol))
|
||||||
doAssert $json == """{"x":"hello","y":"A"}"""
|
doAssert $json == """{"x":"hello","y":"A"}"""
|
||||||
|
|
||||||
|
|
||||||
when false:
|
when false:
|
||||||
## TODO: Implement support for nested variant objects allowing the tests
|
## TODO: Implement support for nested variant objects allowing the tests
|
||||||
## bellow to pass.
|
## bellow to pass.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue