From 6b20bddd153ec8c951381ce579df5bc2ffb48a85 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Fri, 24 Feb 2023 05:54:45 -0300 Subject: [PATCH] cgen, json2: fix auto str option type generator, and json2 option type handling (#17388) --- vlib/v/gen/c/auto_str_methods.v | 2 +- vlib/x/json2/README.md | 6 ++-- vlib/x/json2/json2.v | 58 ++++++++++++++++++++++++++++++++- 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/vlib/v/gen/c/auto_str_methods.v b/vlib/v/gen/c/auto_str_methods.v index 1324a45386..5c5698032f 100644 --- a/vlib/v/gen/c/auto_str_methods.v +++ b/vlib/v/gen/c/auto_str_methods.v @@ -905,7 +905,7 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, typ_str string, mut field_styp := sftyp.replace('*', '') field_styp_fn_name := if sym_has_str_method { mut field_fn_name := if ftyp_noshared.has_flag(.option) { - '${field_styp}_str' + g.get_str_fn(ftyp_noshared) } else { left_cc_type := g.cc_type(ftyp_noshared, false) left_fn_name := util.no_dots(left_cc_type) diff --git a/vlib/x/json2/README.md b/vlib/x/json2/README.md index e22928f62c..a4841277cf 100644 --- a/vlib/x/json2/README.md +++ b/vlib/x/json2/README.md @@ -15,7 +15,7 @@ mut: name string age ?int = 20 birthday time.Time - deathday ?time.Time = none + deathday ?time.Time } fn main() { @@ -39,11 +39,11 @@ mut: name string age ?int = 20 birthday time.Time - deathday ?time.Time = none + deathday ?time.Time } fn main() { - resp := '{"name": "Bob", "age": 20, "birthday": ${time.now()}}' + resp := '{"name": "Bob", "age": 20, "birthday": "${time.now()}"}' person := json2.decode[Person](resp)! /* struct Person { diff --git a/vlib/x/json2/json2.v b/vlib/x/json2/json2.v index d082c1860d..0586a79ee5 100644 --- a/vlib/x/json2/json2.v +++ b/vlib/x/json2/json2.v @@ -52,19 +52,75 @@ pub fn decode[T](src string) !T { } $else $if field.typ is i16 { typ.$(field.name) = res[json_name]!.int() } $else $if field.typ is i32 { - // typ.$(field.name) = res[field.name]!.i32() + typ.$(field.name) = i32(res[field.name]!.int()) } $else $if field.typ is i64 { typ.$(field.name) = res[json_name]!.i64() + } $else $if field.typ is ?u8 { + if json_name in res { + typ.$(field.name) = ?u8(res[json_name]!.i64()) + } + } $else $if field.typ is ?i8 { + if json_name in res { + typ.$(field.name) = ?i8(res[json_name]!.i64()) + } + } $else $if field.typ is ?u16 { + if json_name in res { + typ.$(field.name) = ?u16(res[json_name]!.i64()) + } + } $else $if field.typ is ?i16 { + if json_name in res { + typ.$(field.name) = ?i16(res[json_name]!.i64()) + } + } $else $if field.typ is ?u32 { + if json_name in res { + typ.$(field.name) = ?u32(res[json_name]!.i64()) + } + } $else $if field.typ is ?i32 { + if json_name in res { + typ.$(field.name) = ?i32(res[json_name]!.i64()) + } + } $else $if field.typ is ?u64 { + if json_name in res { + typ.$(field.name) = ?u64(res[json_name]!.i64()) + } + } $else $if field.typ is ?i64 { + if json_name in res { + typ.$(field.name) = ?i64(res[json_name]!.i64()) + } + } $else $if field.typ is ?int { + if json_name in res { + typ.$(field.name) = ?int(res[json_name]!.i64()) + } } $else $if field.typ is f32 { typ.$(field.name) = res[json_name]!.f32() + } $else $if field.typ is ?f32 { + if json_name in res { + typ.$(field.name) = res[json_name]!.f32() + } } $else $if field.typ is f64 { typ.$(field.name) = res[json_name]!.f64() + } $else $if field.typ is ?f64 { + if json_name in res { + typ.$(field.name) = res[json_name]!.f64() + } } $else $if field.typ is bool { typ.$(field.name) = res[json_name]!.bool() + } $else $if field.typ is ?bool { + if json_name in res { + typ.$(field.name) = res[json_name]!.bool() + } } $else $if field.typ is string { typ.$(field.name) = res[json_name]!.str() + } $else $if field.typ is ?string { + if json_name in res { + typ.$(field.name) = res[json_name]!.str() + } } $else $if field.typ is time.Time { typ.$(field.name) = res[field.name]!.to_time()! + } $else $if field.typ is ?time.Time { + if json_name in res { + typ.$(field.name) = res[field.name]!.to_time()! + } } $else $if field.is_array { // typ.$(field.name) = res[field.name]!.arr() } $else $if field.is_struct {