mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: json sumtype inlining (#11961)
This commit is contained in:
committed by
GitHub
parent
430677a0c0
commit
c75271fcb7
@@ -86,10 +86,13 @@ $enc_fn_dec {
|
||||
return
|
||||
}
|
||||
enc.writeln('\to = cJSON_CreateObject();')
|
||||
if psym.info !is ast.Struct {
|
||||
if psym.info is ast.Struct {
|
||||
g.gen_struct_enc_dec(psym.info, styp, mut enc, mut dec)
|
||||
} else if psym.kind == .sum_type {
|
||||
verror('json: $sym.name aliased sumtypes does not work at the moment')
|
||||
} else {
|
||||
verror('json: $sym.name is not struct')
|
||||
}
|
||||
g.gen_struct_enc_dec(psym.info, styp, mut enc, mut dec)
|
||||
} else if sym.kind == .sum_type {
|
||||
enc.writeln('\to = cJSON_CreateObject();')
|
||||
// Sumtypes. Range through variants of sumtype
|
||||
@@ -118,46 +121,164 @@ $enc_fn_dec {
|
||||
[inline]
|
||||
fn (mut g Gen) gen_sumtype_enc_dec(sym ast.TypeSymbol, mut enc strings.Builder, mut dec strings.Builder) {
|
||||
info := sym.info as ast.SumType
|
||||
type_var := g.new_tmp_var()
|
||||
typ := sym.idx
|
||||
|
||||
// DECODING (inline)
|
||||
$if !json_no_inline_sumtypes ? {
|
||||
type_tmp := g.new_tmp_var()
|
||||
dec.writeln('\tif (cJSON_IsObject(root)) {')
|
||||
dec.writeln('\t\tcJSON* $type_tmp = js_get(root, "_type");')
|
||||
dec.writeln('\t\tif ($type_tmp != 0) {')
|
||||
dec.writeln('\t\t\tchar* $type_var = cJSON_GetStringValue($type_tmp);')
|
||||
// dec.writeln('\t\t\tcJSON_DeleteItemFromObjectCaseSensitive(root, "_type");')
|
||||
}
|
||||
|
||||
mut variant_types := []string{}
|
||||
mut variant_symbols := []ast.TypeSymbol{}
|
||||
mut at_least_one_prim := false
|
||||
for variant in info.variants {
|
||||
variant_typ := g.typ(variant)
|
||||
variant_types << variant_typ
|
||||
variant_sym := g.table.get_type_symbol(variant)
|
||||
variant_symbols << variant_sym
|
||||
at_least_one_prim = at_least_one_prim || is_js_prim(variant_typ)
|
||||
|| variant_sym.kind == .enum_ || variant_sym.name == 'time.Time'
|
||||
unmangled_variant_name := variant_sym.name.split('.').last()
|
||||
|
||||
// TODO: Do not generate dec/enc for 'time.Time', because we handle it by saving it as u64
|
||||
g.gen_json_for_type(variant)
|
||||
|
||||
// Helpers for decoding
|
||||
g.write_sumtype_casting_fn(variant, typ)
|
||||
g.definitions.writeln('static inline $sym.cname ${variant_typ}_to_sumtype_${sym.cname}($variant_typ* x);')
|
||||
|
||||
// ENCODING
|
||||
enc.writeln('\tif (val._typ == $variant) {')
|
||||
if variant_sym.kind == .enum_ {
|
||||
enc.writeln('\t\tcJSON_AddItemToObject(o, "$unmangled_variant_name", json__encode_u64(*val._$variant_typ));')
|
||||
} else if variant_sym.name == 'time.Time' {
|
||||
enc.writeln('\t\tcJSON_AddItemToObject(o, "$unmangled_variant_name", json__encode_i64(val._$variant_typ->_v_unix));')
|
||||
} else {
|
||||
enc.writeln('\t\tcJSON_AddItemToObject(o, "$unmangled_variant_name", json__encode_${variant_typ}(*val._$variant_typ));')
|
||||
$if json_no_inline_sumtypes ? {
|
||||
if variant_sym.kind == .enum_ {
|
||||
enc.writeln('\t\tcJSON_AddItemToObject(o, "$unmangled_variant_name", ${js_enc_name('u64')}(*val._$variant_typ));')
|
||||
} else if variant_sym.name == 'time.Time' {
|
||||
enc.writeln('\t\tcJSON_AddItemToObject(o, "$unmangled_variant_name", ${js_enc_name('i64')}(val._$variant_typ->_v_unix));')
|
||||
} else {
|
||||
enc.writeln('\t\tcJSON_AddItemToObject(o, "$unmangled_variant_name", ${js_enc_name(variant_typ)}(*val._$variant_typ));')
|
||||
}
|
||||
} $else {
|
||||
if is_js_prim(variant_typ) {
|
||||
enc.writeln('\t\to = ${js_enc_name(variant_typ)}(*val._$variant_typ);')
|
||||
} else if variant_sym.kind == .enum_ {
|
||||
enc.writeln('\t\to = ${js_enc_name('u64')}(*val._$variant_typ);')
|
||||
} else if variant_sym.name == 'time.Time' {
|
||||
enc.writeln('\t\tcJSON_AddItemToObject(o, "_type", cJSON_CreateString("$unmangled_variant_name"));')
|
||||
enc.writeln('\t\tcJSON_AddItemToObject(o, "value", ${js_enc_name('i64')}(val._$variant_typ->_v_unix));')
|
||||
} else {
|
||||
enc.writeln('\t\to = ${js_enc_name(variant_typ)}(*val._$variant_typ);')
|
||||
enc.writeln('\t\tcJSON_AddItemToObject(o, "_type", cJSON_CreateString("$unmangled_variant_name"));')
|
||||
}
|
||||
}
|
||||
enc.writeln('\t}')
|
||||
|
||||
// DECODING
|
||||
dec.writeln('\tif (strcmp("$unmangled_variant_name", root->child->string) == 0) {')
|
||||
tmp := g.new_tmp_var()
|
||||
if is_js_prim(variant_typ) {
|
||||
gen_js_get(variant_typ, tmp, unmangled_variant_name, mut dec, false)
|
||||
dec.writeln('\t\t$variant_typ value = (${js_dec_name(variant_typ)})(jsonroot_$tmp);')
|
||||
} else if variant_sym.kind == .enum_ {
|
||||
gen_js_get(variant_typ, tmp, unmangled_variant_name, mut dec, false)
|
||||
dec.writeln('\t\t$variant_typ value = json__decode_u64(jsonroot_$tmp);')
|
||||
} else if variant_sym.name == 'time.Time' {
|
||||
gen_js_get(variant_typ, tmp, unmangled_variant_name, mut dec, false)
|
||||
dec.writeln('\t\t$variant_typ value = time__unix(json__decode_i64(jsonroot_$tmp));')
|
||||
} else {
|
||||
gen_js_get_opt(js_dec_name(variant_typ), variant_typ, sym.cname, tmp, unmangled_variant_name, mut
|
||||
dec, false)
|
||||
dec.writeln('\t\t$variant_typ value = *($variant_typ*)(${tmp}.data);')
|
||||
$if json_no_inline_sumtypes ? {
|
||||
dec.writeln('\tif (strcmp("$unmangled_variant_name", root->child->string) == 0) {')
|
||||
if is_js_prim(variant_typ) {
|
||||
gen_js_get(variant_typ, tmp, unmangled_variant_name, mut dec, true)
|
||||
dec.writeln('\t\t$variant_typ value = ${js_dec_name(variant_typ)}(jsonroot_$tmp);')
|
||||
} else if variant_sym.kind == .enum_ {
|
||||
gen_js_get(variant_typ, tmp, unmangled_variant_name, mut dec, true)
|
||||
dec.writeln('\t\t$variant_typ value = ${js_dec_name('u64')}(jsonroot_$tmp);')
|
||||
} else if variant_sym.name == 'time.Time' {
|
||||
gen_js_get(variant_typ, tmp, unmangled_variant_name, mut dec, true)
|
||||
dec.writeln('\t\t$variant_typ value = time__unix(${js_dec_name('i64')}(jsonroot_$tmp));')
|
||||
} else {
|
||||
gen_js_get_opt(js_dec_name(variant_typ), variant_typ, sym.cname, tmp,
|
||||
unmangled_variant_name, mut dec, true)
|
||||
dec.writeln('\t\t$variant_typ value = *($variant_typ*)(${tmp}.data);')
|
||||
}
|
||||
dec.writeln('\t\tres = ${variant_typ}_to_sumtype_${sym.cname}(&value);')
|
||||
dec.writeln('\t}')
|
||||
} $else {
|
||||
if variant_sym.name == 'time.Time' {
|
||||
dec.writeln('\t\t\tif (strcmp("Time", $type_var) == 0) {')
|
||||
gen_js_get(sym.cname, tmp, 'value', mut dec, true)
|
||||
dec.writeln('\t\t\t\t$variant_typ $tmp = time__unix(${js_dec_name('i64')}(jsonroot_$tmp));')
|
||||
dec.writeln('\t\t\t\tres = ${variant_typ}_to_sumtype_${sym.cname}(&$tmp);')
|
||||
dec.writeln('\t\t\t}')
|
||||
} else if !is_js_prim(variant_typ) && variant_sym.kind != .enum_ {
|
||||
dec.writeln('\t\t\tif (strcmp("$unmangled_variant_name", $type_var) == 0) {')
|
||||
dec.writeln('\t\t\t\tOption_$variant_typ $tmp = ${js_dec_name(variant_typ)}(root);')
|
||||
dec.writeln('\t\t\t\tif (${tmp}.state != 0) {')
|
||||
dec.writeln('\t\t\t\t\treturn (Option_$sym.cname){ .state = ${tmp}.state, .err = ${tmp}.err, .data = {0} };')
|
||||
dec.writeln('\t\t\t\t}')
|
||||
dec.writeln('\t\t\t\tres = ${variant_typ}_to_sumtype_${sym.cname}(($variant_typ*)${tmp}.data);')
|
||||
dec.writeln('\t\t\t}')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DECODING (inline)
|
||||
$if !json_no_inline_sumtypes ? {
|
||||
dec.writeln('\t\t}')
|
||||
|
||||
mut number_is_met := false
|
||||
mut string_is_met := false
|
||||
mut last_number_type := ''
|
||||
|
||||
if at_least_one_prim {
|
||||
dec.writeln('\t} else {')
|
||||
|
||||
if 'bool' in variant_types {
|
||||
var_t := 'bool'
|
||||
dec.writeln('\t\tif (cJSON_IsBool(root)) {')
|
||||
dec.writeln('\t\t\t$var_t value = ${js_dec_name(var_t)}(root);')
|
||||
dec.writeln('\t\t\tres = ${var_t}_to_sumtype_${sym.cname}(&value);')
|
||||
dec.writeln('\t\t}')
|
||||
}
|
||||
|
||||
for i, var_t in variant_types {
|
||||
if variant_symbols[i].kind == .enum_ {
|
||||
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')
|
||||
}
|
||||
number_is_met = true
|
||||
last_number_type = var_t
|
||||
dec.writeln('\t\tif (cJSON_IsNumber(root)) {')
|
||||
dec.writeln('\t\t\t$var_t value = ${js_dec_name('u64')}(root);')
|
||||
dec.writeln('\t\t\tres = ${var_t}_to_sumtype_${sym.cname}(&value);')
|
||||
dec.writeln('\t\t}')
|
||||
}
|
||||
|
||||
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')
|
||||
}
|
||||
string_is_met = true
|
||||
dec.writeln('\t\tif (cJSON_IsString(root)) {')
|
||||
dec.writeln('\t\t\t$var_t value = ${js_dec_name(var_t)}(root);')
|
||||
dec.writeln('\t\t\tres = ${var_t}_to_sumtype_${sym.cname}(&value);')
|
||||
dec.writeln('\t\t}')
|
||||
}
|
||||
|
||||
if var_t in ['i64', 'int', 'i8', 'u64', 'u32', 'u16', 'byte', 'u8', 'rune', 'f64',
|
||||
'f32'] {
|
||||
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')
|
||||
}
|
||||
number_is_met = true
|
||||
last_number_type = var_t
|
||||
dec.writeln('\t\tif (cJSON_IsNumber(root)) {')
|
||||
dec.writeln('\t\t\t$var_t value = ${js_dec_name(var_t)}(root);')
|
||||
dec.writeln('\t\t\tres = ${var_t}_to_sumtype_${sym.cname}(&value);')
|
||||
dec.writeln('\t\t}')
|
||||
}
|
||||
}
|
||||
}
|
||||
dec.writeln('\t\tres = ${variant_typ}_to_sumtype_${sym.cname}(&value);')
|
||||
dec.writeln('\t}')
|
||||
}
|
||||
}
|
||||
@@ -294,8 +415,8 @@ fn js_dec_name(typ string) string {
|
||||
}
|
||||
|
||||
fn is_js_prim(typ string) bool {
|
||||
return typ in ['int', 'string', 'bool', 'f32', 'f64', 'i8', 'i16', 'i64', 'u16', 'u32', 'u64',
|
||||
'byte']
|
||||
return typ in ['int', 'rune', 'string', 'bool', 'f32', 'f64', 'i8', 'i16', 'i64', 'u8', 'u16',
|
||||
'u32', 'u64', 'byte']
|
||||
}
|
||||
|
||||
fn (mut g Gen) decode_array(value_type ast.Type) string {
|
||||
|
||||
Reference in New Issue
Block a user