cgen: cleanup code repetition in json.v

This commit is contained in:
Delyan Angelov 2023-12-20 15:00:08 +02:00
parent 85c5c86894
commit 2b74b64de4
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED

View File

@ -516,7 +516,7 @@ fn (mut g Gen) gen_sumtype_enc_dec(utyp ast.Type, sym ast.TypeSymbol, mut enc st
if number_is_met {
var_num := var_t.replace('__', '.')
last_num := last_number_type.replace('__', '.')
verror('json: can not decode `${sym.name}` sumtype, too many numeric types (conflict of `${last_num}` and `${var_num}`), you can try to use alias for `${var_num}` or compile v with `json_no_inline_sumtypes` flag')
verror_suggest_json_no_inline_sumtypes(sym.name, last_num, var_num)
}
number_is_met = true
last_number_type = var_t
@ -533,7 +533,7 @@ fn (mut g Gen) gen_sumtype_enc_dec(utyp ast.Type, sym ast.TypeSymbol, mut enc st
if var_t in ['string', 'rune'] {
if string_is_met {
var_num := var_t.replace('__', '.')
verror('json: can not decode `${sym.name}` sumtype, too many string types (conflict of `string` and `rune`), you can try to use alias for `${var_num}` or compile v with `json_no_inline_sumtypes` flag')
verror_suggest_json_no_inline_sumtypes(sym.name, 'string', var_num)
}
string_is_met = true
dec.writeln('\t\tif (cJSON_IsString(root)) {')
@ -575,7 +575,7 @@ fn (mut g Gen) gen_sumtype_enc_dec(utyp ast.Type, sym ast.TypeSymbol, mut enc st
if number_is_met {
var_num := var_t.replace('__', '.')
last_num := last_number_type.replace('__', '.')
verror('json: can not decode `${sym.name}` sumtype, too many numeric types (conflict of `${last_num}` and `${var_num}`), you can try to use alias for `${var_num}` or compile v with `json_no_inline_sumtypes` flag')
verror_suggest_json_no_inline_sumtypes(sym.name, last_num, var_num)
}
number_is_met = true
last_number_type = var_t
@ -1091,3 +1091,8 @@ fn (mut g Gen) encode_map(utyp ast.Type, key_type ast.Type, value_type ast.Type)
'
}
}
@[noreturn]
fn verror_suggest_json_no_inline_sumtypes(sumtype_name string, type_name1 string, type_name2 string) {
verror('json: can not decode `${sumtype_name}` sumtype, too many numeric types (conflict of `${type_name1}` and `${type_name2}`), you can try to use alias for `${type_name2}` or compile v with `json_no_inline_sumtypes` flag')
}