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

json2: refactoring and fixes (#16893)

This commit is contained in:
Hitalo Souza 2023-01-15 07:30:33 -03:00 committed by GitHub
parent 4d2c767dcb
commit 28cbaf66b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 24 deletions

BIN
database.db Normal file

Binary file not shown.

View File

@ -91,6 +91,18 @@ fn test_option_types() {
assert json.encode(StructTypeOption[time.Time]{}) == '{}' assert json.encode(StructTypeOption[time.Time]{}) == '{}'
assert json.encode(StructTypeOption[time.Time]{ val: time.Time{} }) == '{"val":"0000-00-00T00:00:00.000Z"}' assert json.encode(StructTypeOption[time.Time]{ val: time.Time{} }) == '{"val":"0000-00-00T00:00:00.000Z"}'
assert json.encode(StructTypeOption[time.Time]{ val: fixed_time }) == '{"val":"2022-03-11T13:54:25.000Z"}' assert json.encode(StructTypeOption[time.Time]{ val: fixed_time }) == '{"val":"2022-03-11T13:54:25.000Z"}'
assert json.encode(StructTypeOption[StructType[int]]{
val: StructType[int]{
val: 1
}
}) == '{"val":{"val":1}}'
assert json.encode(StructTypeOption[Enumerates]{}) == '{}'
// assert json.encode(StructTypeOption[Enumerates]{ val: Enumerates.a }) == '{"val":0}'
// assert json.encode(StructTypeOption[Enumerates]{ val: Enumerates.d }) == '{"val":3}'
// assert json.encode(StructTypeOption[Enumerates]{ val: Enumerates.e }) == '{"val":99}'
// assert json.encode(StructTypeOption[Enumerates]{ val: Enumerates.f }) == '{"val":100}'
} }
fn test_array() { fn test_array() {

View File

@ -149,8 +149,7 @@ fn (e &Encoder) encode_struct[U](val U, level int, mut wr io.Writer) ! {
mut i := 0 mut i := 0
mut fields_len := 0 mut fields_len := 0
$for field in U.fields { $for field in U.fields {
value := val.$(field.name) if val.$(field.name).str() != 'Option(error: none)' {
if value.str() != 'Option(error: none)' {
fields_len++ fields_len++
} }
} }
@ -181,35 +180,24 @@ fn (e &Encoder) encode_struct[U](val U, level int, mut wr io.Writer) ! {
} }
$if field.typ is ?string { $if field.typ is ?string {
option_value := val.$(field.name) as ?string e.encode_string(val.$(field.name) ?.str()#[8..-2], mut wr)!
e.encode_string(option_value, mut wr)! } $else $if field.typ is ?bool || field.typ is ?f32 || field.typ is ?f64
} $else $if field.typ is ?bool { || field.typ is ?i8 || field.typ is ?i16 || field.typ is ?int
option_value := val.$(field.name) as ?bool || field.typ is ?i64 || field.typ is ?u8 || field.typ is ?u16
wr.write(Any(option_value).str().bytes())! || field.typ is ?u32 || field.typ is ?u64 {
} $else $if field.typ is ?f32 { wr.write(val.$(field.name) ?.str()#[7..-1].bytes())!
option_value := val.$(field.name) as ?f32
wr.write(Any(option_value).str().bytes())!
} $else $if field.typ is ?f64 {
option_value := val.$(field.name) as ?f64
wr.write(Any(option_value).str().bytes())!
} $else $if field.typ is ?i8 {
option_value := val.$(field.name) as ?i8
wr.write(Any(option_value).str().bytes())!
} $else $if field.typ is ?i16 {
option_value := val.$(field.name) as ?i16
wr.write(Any(option_value).str().bytes())!
} $else $if field.typ is ?int {
option_value := val.$(field.name) as ?int
wr.write(Any(option_value).int().str().bytes())!
} $else $if field.typ is ?time.Time { } $else $if field.typ is ?time.Time {
option_value := val.$(field.name) as ?time.Time option_value := val.$(field.name) as ?time.Time
parsed_time := option_value as time.Time parsed_time := option_value as time.Time
e.encode_string(parsed_time.format_rfc3339(), mut wr)! e.encode_string(parsed_time.format_rfc3339(), mut wr)!
} $else $if field.is_array { } $else $if field.is_array {
e.encode_array(value, level + 1, mut wr)! e.encode_array(value, level + 1, mut wr)!
} $else $if field.is_struct {
e.encode_struct(value, level + 1, mut wr)!
} $else $if field.is_enum { } $else $if field.is_enum {
option_value := val.$(field.name) as ?int // FIXME - checker and cast error
wr.write(Any(option_value).int().str().bytes())! // wr.write(int(val.$(field.name)?).str().bytes())!
return error('type ${typeof(val).name} cannot be encoded yet')
} $else $if field.is_alias { } $else $if field.is_alias {
match field.unaliased_typ { match field.unaliased_typ {
typeof[string]().idx { typeof[string]().idx {

0
vweb.sql Normal file
View File