Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: fix type_default for option type when the default expr is none #23320

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions vlib/json/tests/json_encode_default_option_none_test.v
spytheman marked this conversation as resolved.
Show resolved Hide resolved
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
Loading