1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen, json2: fix auto str option type generator, and json2 option type handling (#17388)

This commit is contained in:
Felipe Pena 2023-02-24 05:54:45 -03:00 committed by GitHub
parent 94ce753df3
commit 6b20bddd15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 5 deletions

View File

@ -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('*', '') mut field_styp := sftyp.replace('*', '')
field_styp_fn_name := if sym_has_str_method { field_styp_fn_name := if sym_has_str_method {
mut field_fn_name := if ftyp_noshared.has_flag(.option) { mut field_fn_name := if ftyp_noshared.has_flag(.option) {
'${field_styp}_str' g.get_str_fn(ftyp_noshared)
} else { } else {
left_cc_type := g.cc_type(ftyp_noshared, false) left_cc_type := g.cc_type(ftyp_noshared, false)
left_fn_name := util.no_dots(left_cc_type) left_fn_name := util.no_dots(left_cc_type)

View File

@ -15,7 +15,7 @@ mut:
name string name string
age ?int = 20 age ?int = 20
birthday time.Time birthday time.Time
deathday ?time.Time = none deathday ?time.Time
} }
fn main() { fn main() {
@ -39,11 +39,11 @@ mut:
name string name string
age ?int = 20 age ?int = 20
birthday time.Time birthday time.Time
deathday ?time.Time = none deathday ?time.Time
} }
fn main() { fn main() {
resp := '{"name": "Bob", "age": 20, "birthday": ${time.now()}}' resp := '{"name": "Bob", "age": 20, "birthday": "${time.now()}"}'
person := json2.decode[Person](resp)! person := json2.decode[Person](resp)!
/* /*
struct Person { struct Person {

View File

@ -52,19 +52,75 @@ pub fn decode[T](src string) !T {
} $else $if field.typ is i16 { } $else $if field.typ is i16 {
typ.$(field.name) = res[json_name]!.int() typ.$(field.name) = res[json_name]!.int()
} $else $if field.typ is i32 { } $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 { } $else $if field.typ is i64 {
typ.$(field.name) = res[json_name]!.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 { } $else $if field.typ is f32 {
typ.$(field.name) = res[json_name]!.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 { } $else $if field.typ is f64 {
typ.$(field.name) = res[json_name]!.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 { } $else $if field.typ is bool {
typ.$(field.name) = res[json_name]!.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 { } $else $if field.typ is string {
typ.$(field.name) = res[json_name]!.str() 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 { } $else $if field.typ is time.Time {
typ.$(field.name) = res[field.name]!.to_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 { } $else $if field.is_array {
// typ.$(field.name) = res[field.name]!.arr() // typ.$(field.name) = res[field.name]!.arr()
} $else $if field.is_struct { } $else $if field.is_struct {