mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parent
dbfb9c3a90
commit
199db81b23
@ -14,6 +14,7 @@ const fixed_time = time.Time{
|
|||||||
type StringAlias = string
|
type StringAlias = string
|
||||||
type BoolAlias = bool
|
type BoolAlias = bool
|
||||||
type IntAlias = int
|
type IntAlias = int
|
||||||
|
type TimeAlias = time.Time
|
||||||
type StructAlias = StructType[int]
|
type StructAlias = StructType[int]
|
||||||
|
|
||||||
type SumTypes = bool | int | string
|
type SumTypes = bool | int | string
|
||||||
@ -205,6 +206,9 @@ fn test_alias() {
|
|||||||
assert json.encode(StructType[IntAlias]{ val: 0 }) == '{"val":0}'
|
assert json.encode(StructType[IntAlias]{ val: 0 }) == '{"val":0}'
|
||||||
assert json.encode(StructType[IntAlias]{ val: 1 }) == '{"val":1}'
|
assert json.encode(StructType[IntAlias]{ val: 1 }) == '{"val":1}'
|
||||||
|
|
||||||
|
assert json.encode(StructType[TimeAlias]{}) == '{"val":"0000-00-00T00:00:00.000Z"}'
|
||||||
|
assert json.encode(StructType[TimeAlias]{ val: fixed_time }) == '{"val":"2022-03-11T13:54:25.000Z"}'
|
||||||
|
|
||||||
assert json.encode(StructType[StructAlias]{}) == '{"val":{"val":0}}'
|
assert json.encode(StructType[StructAlias]{}) == '{"val":{"val":0}}'
|
||||||
assert json.encode(StructType[StructAlias]{ val: StructType[int]{0} }) == '{"val":{"val":0}}'
|
assert json.encode(StructType[StructAlias]{ val: StructType[int]{0} }) == '{"val":{"val":0}}'
|
||||||
assert json.encode(StructType[StructAlias]{ val: StructType[int]{1} }) == '{"val":{"val":1}}'
|
assert json.encode(StructType[StructAlias]{ val: StructType[int]{1} }) == '{"val":{"val":1}}'
|
||||||
|
@ -259,23 +259,25 @@ fn (e &Encoder) encode_struct[U](val U, level int, mut wr io.Writer) ! {
|
|||||||
} $else $if field.is_enum {
|
} $else $if field.is_enum {
|
||||||
wr.write(int(val.$(field.name)).str().bytes())!
|
wr.write(int(val.$(field.name)).str().bytes())!
|
||||||
} $else $if field.is_alias {
|
} $else $if field.is_alias {
|
||||||
match field.unaliased_typ {
|
$if field.unaliased_typ is string {
|
||||||
typeof[string]().idx {
|
e.encode_string(value.str(), mut wr)!
|
||||||
e.encode_string(value.str(), mut wr)!
|
} $else $if field.unaliased_typ is time.Time {
|
||||||
}
|
parsed_time := val.$(field.name) as time.Time
|
||||||
typeof[bool]().idx, typeof[f32]().idx, typeof[f64]().idx, typeof[i8]().idx,
|
e.encode_string(parsed_time.format_rfc3339(), mut wr)!
|
||||||
typeof[i16]().idx, typeof[int]().idx, typeof[i64]().idx, typeof[u8]().idx,
|
} $else $if field.unaliased_typ in [bool, $Float, $Int] {
|
||||||
typeof[u16]().idx, typeof[u32]().idx, typeof[u64]().idx {
|
wr.write(value.str().bytes())!
|
||||||
wr.write(value.str().bytes())!
|
}
|
||||||
}
|
// FIXME
|
||||||
typeof[[]byte]().idx, typeof[[]int]().idx {
|
// $else $if field.unaliased_typ is $Array {
|
||||||
// FIXME - error: could not infer generic type `U` in call to `encode_array`
|
// // e.encode_array(value, level + 1, mut wr)!
|
||||||
// e.encode_array(value, level, mut wr)!
|
// } $else $if field.unaliased_typ is $Struct {
|
||||||
}
|
// // e.encode_struct(value, level + 1, mut wr)!
|
||||||
else {
|
// } $else $if field.unaliased_typ is $Enum {
|
||||||
e.encode_struct(value, level + 1, mut wr)!
|
// // wr.write(int(val.$(field.name)).str().bytes())!
|
||||||
// e.encode_value_with_level(value, level + 1, mut wr)!
|
// }
|
||||||
}
|
$else {
|
||||||
|
e.encode_struct(value, level + 1, mut wr)!
|
||||||
|
// return error('the alias ${typeof(val).name} cannot be encoded')
|
||||||
}
|
}
|
||||||
} $else {
|
} $else {
|
||||||
return error('type ${typeof(val).name} cannot be array encoded')
|
return error('type ${typeof(val).name} cannot be array encoded')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user