1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

json2: fix encoding of struct fields that are aliases (#16925)

This commit is contained in:
Hitalo Souza 2023-01-09 17:43:01 -03:00 committed by GitHub
parent 0109fe66a6
commit 33a99fe833
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 20 deletions

View File

@ -14,6 +14,7 @@ const fixed_time = time.Time{
type StringAlias = string
type BoolAlias = bool
type IntAlias = int
type StructAlias = StructType[int]
type SumTypes = bool | int | string
@ -197,4 +198,8 @@ fn test_alias() {
assert json.encode(StructType[IntAlias]{}) == '{"val":0}'
assert json.encode(StructType[IntAlias]{ val: 0 }) == '{"val":0}'
assert json.encode(StructType[IntAlias]{ val: 1 }) == '{"val":1}'
assert json.encode(StructType[StructAlias]{}) == '{"val":{"val":0}}'
assert json.encode(StructType[StructAlias]{ val: StructType[int]{0} }) == '{"val":{"val":0}}'
assert json.encode(StructType[StructAlias]{ val: StructType[int]{1} }) == '{"val":{"val":1}}'
}

View File

@ -271,6 +271,7 @@ fn (e &Encoder) encode_struct[U](val U, level int, mut wr io.Writer) ! {
// e.encode_array(value, level, mut wr)!
}
else {
e.encode_struct(value, level + 1, mut wr)!
// e.encode_value_with_level(value, level + 1, mut wr)!
}
}

View File

@ -217,3 +217,22 @@ fn test_pretty() {
"c": "aliens"
}'
}
struct Aa {
sub AliasType
}
struct Bb {
a int
}
type AliasType = Bb
fn test_encode_alias_field() {
s := json.encode(Aa{
sub: Bb{
a: 1
}
})
assert s == '{"sub":{"a":1}}'
}

View File

@ -371,26 +371,6 @@ fn test_encode_sumtype_defined_ahead() {
assert ret == '{"value":0,"_type":"GPScale"}'
}
struct Aa {
sub AliasType
}
struct Bb {
a int
}
type AliasType = Bb
//! FIX: returning null
fn test_encode_alias_field() {
s := json.encode(Aa{
sub: Bb{
a: 1
}
})
assert s == '{"sub":{"a":1}}'
}
struct APrice {}
struct Association {