Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Dec 30, 2024
1 parent d9e505b commit 25fb063
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
11 changes: 11 additions & 0 deletions vlib/json/tests/json_encode_default_option_none_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module main

import json

struct Test {
id ?string = none
}

fn test_main() {
assert json.encode(Test{}) == '{}'
}
10 changes: 9 additions & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,14 @@ fn (mut g Gen) expr_string(expr ast.Expr) string {
return g.out.cut_to(pos).trim_space()
}

fn (mut g Gen) expr_string_opt(typ ast.Type, expr ast.Expr) string {
expr_str := g.expr_string(expr)
if expr is ast.None {
return '(${g.styp(typ)}){.state=2, .err=${expr_str}, .data={EMPTY_STRUCT_INITIALIZATION}}'
}
return expr_str
}

fn (mut g Gen) expr_string_with_cast(expr ast.Expr, typ ast.Type, exp ast.Type) string {
pos := g.out.len
// pos2 := g.out_parallel[g.out_idx].len
Expand Down Expand Up @@ -7034,7 +7042,7 @@ fn (mut g Gen) type_default_impl(typ_ ast.Type, decode_sumtype bool) string {
}
}
} else {
default_str := g.expr_string(field.default_expr)
default_str := g.expr_string_opt(field.typ, field.default_expr)
if default_str.count('\n') > 1 {
g.type_default_vars.writeln(default_str.all_before_last('\n'))
expr_str = default_str.all_after_last('\n')
Expand Down
17 changes: 11 additions & 6 deletions vlib/v/gen/c/json.v
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,8 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${dec_name}(jsonroot_${tmp});')
if field.has_default_expr {
dec.writeln('\t} else {')
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string(field.default_expr)};')
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string_opt(field.typ,
field.default_expr)};')
}
dec.writeln('\t}')
} else if field_sym.kind == .enum {
Expand Down Expand Up @@ -755,7 +756,8 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
}
if field.has_default_expr {
dec.writeln('\t} else {')
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string(field.default_expr)};')
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string_opt(field.typ,
field.default_expr)};')
}
dec.writeln('\t}')
} else if field_sym.name == 'time.Time' {
Expand All @@ -767,7 +769,8 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = time__unix(json__decode_u64(jsonroot_${tmp}));')
if field.has_default_expr {
dec.writeln('\t} else {')
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string(field.default_expr)};')
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string_opt(field.typ,
field.default_expr)};')
}
dec.writeln('\t}')
} else if field_sym.kind == .alias {
Expand All @@ -788,7 +791,8 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${parent_dec_name} (jsonroot_${tmp});')
if field.has_default_expr {
dec.writeln('\t} else {')
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string(field.default_expr)};')
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string_opt(field.typ,
field.default_expr)};')
}
dec.writeln('\t}')
} else {
Expand All @@ -799,7 +803,8 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = *(${field_type}*) ${tmp}.data;')
if field.has_default_expr {
dec.writeln('\t} else {')
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string(field.default_expr)};')
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${g.expr_string_opt(field.typ,
field.default_expr)};')
}
dec.writeln('\t}')
}
Expand Down Expand Up @@ -838,7 +843,7 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
}
if field.has_default_expr {
dec.writeln('\t} else {')
default_str := g.expr_string(field.default_expr)
default_str := g.expr_string_opt(field.typ, field.default_expr)
lines := default_str.count('\n')
if lines > 1 {
dec.writeln(default_str.all_before_last('\n'))
Expand Down

0 comments on commit 25fb063

Please sign in to comment.