From 2b74b64de44d8ea4eecc57a4d3e2e15d015ee4b3 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Wed, 20 Dec 2023 15:00:08 +0200 Subject: [PATCH] cgen: cleanup code repetition in json.v --- vlib/v/gen/c/json.v | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/vlib/v/gen/c/json.v b/vlib/v/gen/c/json.v index 0e526d5712..9248d1520f 100644 --- a/vlib/v/gen/c/json.v +++ b/vlib/v/gen/c/json.v @@ -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') +}