@@ -627,8 +627,12 @@ fn (mut g Gen) gen_sumtype_enc_dec(utyp ast.Type, sym ast.TypeSymbol, mut enc st
627
627
}
628
628
}
629
629
630
- fn (mut g Gen) gen_prim_type_validation (name string , typ ast.Type, tmp string , ret_styp string , mut dec strings.Builder) {
631
- none_check := if typ.has_flag (.option) { 'cJSON_IsNull(jsonroot_${tmp} ) || ' } else { '' }
630
+ fn (mut g Gen) gen_prim_type_validation (name string , typ ast.Type, tmp string , is_required bool , ret_styp string , mut dec strings.Builder) {
631
+ none_check := if ! is_required {
632
+ 'cJSON_IsNull(jsonroot_${tmp} ) || '
633
+ } else {
634
+ ''
635
+ }
632
636
type_check := if typ.is_int () || typ.is_float () {
633
637
'${none_check} cJSON_IsNumber(jsonroot_${tmp} ) || (cJSON_IsString(jsonroot_${tmp} ) && strlen(jsonroot_${tmp} ->valuestring))'
634
638
} else if typ.is_string () {
@@ -724,7 +728,7 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
724
728
if utyp.has_flag (.option) {
725
729
dec.writeln ('\t\t res.state = 0;' )
726
730
}
727
- g.gen_prim_type_validation (field.name, field.typ, tmp, '${result_name} _${styp} ' , mut
731
+ g.gen_prim_type_validation (field.name, field.typ, tmp, is_required, '${result_name} _${styp} ' , mut
728
732
dec)
729
733
dec.writeln ('\t\t ${prefix}${op}${c_name(field.name)} = ${dec_name} (jsonroot_${tmp} );' )
730
734
if field.has_default_expr {
@@ -791,8 +795,8 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
791
795
tmp := g.new_tmp_var ()
792
796
gen_js_get (styp, tmp, name, mut dec, is_required)
793
797
dec.writeln ('\t if (jsonroot_${tmp} ) {' )
794
- g.gen_prim_type_validation (field.name, parent_type, tmp, ' ${result_name} _ ${styp} ' , mut
795
- dec)
798
+ g.gen_prim_type_validation (field.name, parent_type, tmp, is_required,
799
+ ' ${result_name} _ ${styp} ' , mut dec)
796
800
dec.writeln ('\t\t ${prefix}${op}${c_name(field.name)} = ${parent_dec_name} (jsonroot_${tmp} );' )
797
801
if field.has_default_expr {
798
802
dec.writeln ('\t } else {' )
@@ -832,8 +836,8 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
832
836
gen_js_get_opt (dec_name, field_type, styp, tmp, name, mut dec, is_required)
833
837
dec.writeln ('\t if (jsonroot_${tmp} ) {' )
834
838
if is_js_prim (g.styp (field.typ.clear_option_and_result ())) {
835
- g.gen_prim_type_validation (field.name, field.typ, tmp, ' ${result_name} _ ${styp} ' , mut
836
- dec)
839
+ g.gen_prim_type_validation (field.name, field.typ, tmp, is_required,
840
+ ' ${result_name} _ ${styp} ' , mut dec)
837
841
}
838
842
if field.typ.has_flag (.option) {
839
843
dec.writeln ('\t\t vmemcpy(&${prefix}${op}${c_name(field.name)} , (${field_type} *)${tmp} .data, sizeof(${field_type} ));' )
0 commit comments