json: fix decode codegen for []&type (fix #23007) (#23010)

This commit is contained in:
Felipe Pena 2024-11-30 03:40:14 -03:00 committed by GitHub
parent f2c281eadf
commit a66c4c9340
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 2 deletions

View file

@ -0,0 +1,10 @@
import json
struct Test {
a string
}
fn test_main() {
x := json.decode([]&Test, '[{"a":"a"}]') or { exit(1) }
assert x[0].a == 'a'
}

View file

@ -1021,7 +1021,7 @@ fn (mut g Gen) decode_array(utyp ast.Type, value_type ast.Type, fixed_array_size
s = '${styp} val = ${fn_name}((cJSON *)jsval); ' s = '${styp} val = ${fn_name}((cJSON *)jsval); '
} else if is_array_fixed_val { } else if is_array_fixed_val {
s = ' s = '
${result_name}_${styp} val2 = ${fn_name} ((cJSON *)jsval); ${result_name}_${styp.replace('*', '_ptr')} val2 = ${fn_name} ((cJSON *)jsval);
if(val2.is_error) { if(val2.is_error) {
${array_free_str} ${array_free_str}
return *(${result_name}_${ret_styp}*)&val2; return *(${result_name}_${ret_styp}*)&val2;
@ -1030,7 +1030,7 @@ fn (mut g Gen) decode_array(utyp ast.Type, value_type ast.Type, fixed_array_size
memcpy(&val, (${styp}*)val2.data, sizeof(${styp}));' memcpy(&val, (${styp}*)val2.data, sizeof(${styp}));'
} else { } else {
s = ' s = '
${result_name}_${styp} val2 = ${fn_name} ((cJSON *)jsval); ${result_name}_${styp.replace('*', '_ptr')} val2 = ${fn_name} ((cJSON *)jsval);
if(val2.is_error) { if(val2.is_error) {
${array_free_str} ${array_free_str}
return *(${result_name}_${ret_styp}*)&val2; return *(${result_name}_${ret_styp}*)&val2;