From 25fb063718fa5d56f3389a07014c01e8ce7db5ff Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 30 Dec 2024 14:22:50 -0300 Subject: [PATCH] fix --- .../json_encode_default_option_none_test.v | 11 +++++++++++ vlib/v/gen/c/cgen.v | 10 +++++++++- vlib/v/gen/c/json.v | 17 +++++++++++------ 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 vlib/json/tests/json_encode_default_option_none_test.v diff --git a/vlib/json/tests/json_encode_default_option_none_test.v b/vlib/json/tests/json_encode_default_option_none_test.v new file mode 100644 index 00000000000000..3add0e34251b9e --- /dev/null +++ b/vlib/json/tests/json_encode_default_option_none_test.v @@ -0,0 +1,11 @@ +module main + +import json + +struct Test { + id ?string = none +} + +fn test_main() { + assert json.encode(Test{}) == '{}' +} diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 738b4621b0debd..e23aedfb112f7c 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -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 @@ -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') diff --git a/vlib/v/gen/c/json.v b/vlib/v/gen/c/json.v index 2013abb461dfea..f3d126a7157c0d 100644 --- a/vlib/v/gen/c/json.v +++ b/vlib/v/gen/c/json.v @@ -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 { @@ -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' { @@ -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 { @@ -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 { @@ -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}') } @@ -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'))