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

json: fix omitempty for alias, struct, sumtype, array and map (#18012)

This commit is contained in:
Felipe Pena 2023-04-22 04:58:21 -03:00 committed by GitHub
parent 89b7bebc3c
commit 3d50663bcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 120 additions and 1 deletions

View File

@ -0,0 +1,104 @@
import json
struct OmitEmptyStruct {
bug Struct [omitempty]
}
type MyAlias = string
struct OmitEmptyAlias {
bug MyAlias [omitempty]
}
struct Struct {
name string
}
struct OmitEmptyMap {
bug map[string]string [omitempty]
}
struct OmitEmptyArray {
bug []string [omitempty]
}
type MySum = int | string
struct OmitEmptySumType {
bug MySum [omitempty]
}
fn main() {
test_struct()
test_alias()
test_map()
test_sumtype()
test_array()
}
fn test_struct() {
test2 := OmitEmptyStruct{
bug: Struct{}
}
encoded2 := json.encode(test2)
dump(encoded2)
test := OmitEmptyStruct{
bug: Struct{
name: 'mybug'
}
}
encoded := json.encode(test)
dump(encoded)
}
fn test_alias() {
test3 := OmitEmptyAlias{
bug: ''
}
encoded3 := json.encode(test3)
dump(encoded3)
test4 := OmitEmptyAlias{
bug: 'foo'
}
encoded4 := json.encode(test4)
dump(encoded4)
}
fn test_map() {
test3 := OmitEmptyMap{
bug: {}
}
encoded3 := json.encode(test3)
dump(encoded3)
test4 := OmitEmptyMap{
bug: {
'foo': 'bar'
}
}
encoded4 := json.encode(test4)
dump(encoded4)
}
fn test_sumtype() {
test3 := OmitEmptySumType{}
encoded3 := json.encode(test3)
dump(encoded3)
test4 := OmitEmptySumType{
bug: 1
}
encoded4 := json.encode(test4)
dump(encoded4)
}
fn test_array() {
test3 := OmitEmptyArray{
bug: []
}
encoded3 := json.encode(test3)
dump(encoded3)
test4 := OmitEmptyArray{
bug: ['1']
}
encoded4 := json.encode(test4)
dump(encoded4)
}

View File

@ -694,7 +694,22 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
} else if field.typ == ast.string_type { } else if field.typ == ast.string_type {
enc.writeln('${indent}if (${prefix_enc}${op}${c_name(field.name)}.len != 0)') enc.writeln('${indent}if (${prefix_enc}${op}${c_name(field.name)}.len != 0)')
} else { } else {
enc.writeln('${indent}if (${prefix_enc}${op}${c_name(field.name)} != ${g.type_default(field.typ)})') if field_sym.kind in [.alias, .sum_type, .map, .array, .struct_] {
ptr_typ := g.equality_fn(field.typ)
if field_sym.kind == .alias {
enc.writeln('${indent}if (!${ptr_typ}_alias_eq(${prefix_enc}${op}${c_name(field.name)}, ${g.type_default(field.typ)}))')
} else if field_sym.kind == .sum_type {
enc.writeln('${indent}if (${prefix_enc}${op}${c_name(field.name)}._typ != 0)')
} else if field_sym.kind == .map {
enc.writeln('${indent}if (!${ptr_typ}_map_eq(${prefix_enc}${op}${c_name(field.name)}, ${g.type_default(field.typ)}))')
} else if field_sym.kind == .array {
enc.writeln('${indent}if (!${ptr_typ}_arr_eq(${prefix_enc}${op}${c_name(field.name)}, ${g.type_default(field.typ)}))')
} else if field_sym.kind == .struct_ {
enc.writeln('${indent}if (!${ptr_typ}_struct_eq(${prefix_enc}${op}${c_name(field.name)}, ${g.type_default(field.typ)}))')
}
} else {
enc.writeln('${indent}if (${prefix_enc}${op}${c_name(field.name)} != ${g.type_default(field.typ)})')
}
} }
} }
if !is_js_prim(field_type) { if !is_js_prim(field_type) {