2022-12-10 19:44:20 +03:00
|
|
|
import x.json2 as json
|
2022-12-11 17:54:28 +03:00
|
|
|
import time
|
|
|
|
|
|
|
|
const fixed_time = time.Time{
|
|
|
|
year: 2022
|
|
|
|
month: 3
|
|
|
|
day: 11
|
|
|
|
hour: 13
|
|
|
|
minute: 54
|
|
|
|
second: 25
|
2022-12-12 11:06:29 +03:00
|
|
|
unix: 1647006865
|
2022-12-11 17:54:28 +03:00
|
|
|
}
|
2022-12-10 19:44:20 +03:00
|
|
|
|
|
|
|
type StringAlias = string
|
|
|
|
type BoolAlias = bool
|
|
|
|
type IntAlias = int
|
2023-01-14 22:30:29 +03:00
|
|
|
type TimeAlias = time.Time
|
2023-01-09 23:43:01 +03:00
|
|
|
type StructAlias = StructType[int]
|
2023-01-16 00:52:48 +03:00
|
|
|
type EnumAlias = Enumerates
|
2022-12-10 19:44:20 +03:00
|
|
|
|
2023-01-21 21:26:55 +03:00
|
|
|
type SumTypes = StructType[string] | bool | int | string | time.Time
|
2022-12-10 19:44:20 +03:00
|
|
|
|
2022-12-11 17:54:28 +03:00
|
|
|
enum Enumerates {
|
|
|
|
a
|
|
|
|
b
|
|
|
|
c
|
|
|
|
d
|
|
|
|
e = 99
|
|
|
|
f
|
|
|
|
}
|
|
|
|
|
2022-12-10 19:44:20 +03:00
|
|
|
struct StructType[T] {
|
|
|
|
mut:
|
|
|
|
val T
|
|
|
|
}
|
|
|
|
|
2023-01-09 09:36:45 +03:00
|
|
|
struct StructTypeOption[T] {
|
2022-12-10 19:44:20 +03:00
|
|
|
mut:
|
|
|
|
val ?T
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StructTypePointer[T] {
|
|
|
|
mut:
|
|
|
|
val &T
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_types() {
|
|
|
|
assert json.encode(StructType[string]{}) == '{"val":""}'
|
|
|
|
assert json.encode(StructType[string]{ val: '' }) == '{"val":""}'
|
|
|
|
assert json.encode(StructType[string]{ val: 'a' }) == '{"val":"a"}'
|
|
|
|
|
|
|
|
assert json.encode(StructType[bool]{}) == '{"val":false}'
|
|
|
|
assert json.encode(StructType[bool]{ val: false }) == '{"val":false}'
|
|
|
|
assert json.encode(StructType[bool]{ val: true }) == '{"val":true}'
|
|
|
|
|
|
|
|
assert json.encode(StructType[int]{}) == '{"val":0}'
|
|
|
|
assert json.encode(StructType[int]{ val: 0 }) == '{"val":0}'
|
|
|
|
assert json.encode(StructType[int]{ val: 1 }) == '{"val":1}'
|
2022-12-11 17:54:28 +03:00
|
|
|
|
|
|
|
assert json.encode(StructType[time.Time]{}) == '{"val":"0000-00-00T00:00:00.000Z"}'
|
|
|
|
assert json.encode(StructType[time.Time]{ val: fixed_time }) == '{"val":"2022-03-11T13:54:25.000Z"}'
|
2023-01-05 16:17:38 +03:00
|
|
|
|
|
|
|
assert json.encode(StructType[StructType[int]]{
|
|
|
|
val: StructType[int]{
|
|
|
|
val: 1
|
|
|
|
}
|
|
|
|
}) == '{"val":{"val":1}}'
|
2023-01-11 11:18:45 +03:00
|
|
|
|
|
|
|
assert json.encode(StructType[Enumerates]{}) == '{"val":0}'
|
|
|
|
assert json.encode(StructType[Enumerates]{ val: Enumerates.a }) == '{"val":0}'
|
|
|
|
assert json.encode(StructType[Enumerates]{ val: Enumerates.d }) == '{"val":3}'
|
|
|
|
assert json.encode(StructType[Enumerates]{ val: Enumerates.e }) == '{"val":99}'
|
|
|
|
assert json.encode(StructType[Enumerates]{ val: Enumerates.f }) == '{"val":100}'
|
2022-12-10 19:44:20 +03:00
|
|
|
}
|
|
|
|
|
2023-01-09 09:36:45 +03:00
|
|
|
fn test_option_types() {
|
|
|
|
assert json.encode(StructTypeOption[string]{ val: none }) == '{}'
|
|
|
|
assert json.encode(StructTypeOption[string]{}) == '{}'
|
|
|
|
assert json.encode(StructTypeOption[string]{ val: '' }) == '{"val":""}'
|
|
|
|
assert json.encode(StructTypeOption[string]{ val: 'a' }) == '{"val":"a"}'
|
|
|
|
|
|
|
|
assert json.encode(StructTypeOption[bool]{ val: none }) == '{}'
|
|
|
|
assert json.encode(StructTypeOption[bool]{}) == '{}'
|
|
|
|
assert json.encode(StructTypeOption[bool]{ val: false }) == '{"val":false}'
|
|
|
|
assert json.encode(StructTypeOption[bool]{ val: true }) == '{"val":true}'
|
|
|
|
|
|
|
|
assert json.encode(StructTypeOption[int]{ val: none }) == '{}'
|
|
|
|
assert json.encode(StructTypeOption[int]{}) == '{}'
|
|
|
|
assert json.encode(StructTypeOption[int]{ val: 0 }) == '{"val":0}'
|
|
|
|
assert json.encode(StructTypeOption[int]{ val: 1 }) == '{"val":1}'
|
|
|
|
|
|
|
|
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: fixed_time }) == '{"val":"2022-03-11T13:54:25.000Z"}'
|
2023-01-15 13:30:33 +03:00
|
|
|
|
|
|
|
assert json.encode(StructTypeOption[StructType[int]]{
|
|
|
|
val: StructType[int]{
|
|
|
|
val: 1
|
|
|
|
}
|
|
|
|
}) == '{"val":{"val":1}}'
|
|
|
|
|
|
|
|
assert json.encode(StructTypeOption[Enumerates]{}) == '{}'
|
2022-12-10 19:44:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_array() {
|
|
|
|
assert json.encode(StructType[[]string]{}) == '{"val":[]}'
|
|
|
|
assert json.encode(StructType[[]string]{ val: [] }) == '{"val":[]}'
|
|
|
|
assert json.encode(StructType[[]string]{ val: ['0'] }) == '{"val":["0"]}'
|
|
|
|
assert json.encode(StructType[[]string]{ val: ['1'] }) == '{"val":["1"]}'
|
|
|
|
|
|
|
|
assert json.encode(StructType[[]int]{}) == '{"val":[]}'
|
|
|
|
assert json.encode(StructType[[]int]{ val: [] }) == '{"val":[]}'
|
|
|
|
assert json.encode(StructType[[]int]{ val: [0] }) == '{"val":[0]}'
|
|
|
|
assert json.encode(StructType[[]int]{ val: [1] }) == '{"val":[1]}'
|
|
|
|
assert json.encode(StructType[[]int]{ val: [0, 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}'
|
|
|
|
|
|
|
|
assert json.encode(StructType[[]byte]{}) == '{"val":[]}'
|
|
|
|
assert json.encode(StructType[[]byte]{ val: [] }) == '{"val":[]}'
|
|
|
|
assert json.encode(StructType[[]byte]{ val: [byte(0)] }) == '{"val":[0]}'
|
|
|
|
assert json.encode(StructType[[]byte]{ val: [byte(1)] }) == '{"val":[1]}'
|
|
|
|
assert json.encode(StructType[[]byte]{ val: [byte(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}'
|
|
|
|
|
|
|
|
assert json.encode(StructType[[]i64]{}) == '{"val":[]}'
|
|
|
|
assert json.encode(StructType[[]i64]{ val: [] }) == '{"val":[]}'
|
|
|
|
assert json.encode(StructType[[]i64]{ val: [i64(0)] }) == '{"val":[0]}'
|
|
|
|
assert json.encode(StructType[[]i64]{ val: [i64(1)] }) == '{"val":[1]}'
|
|
|
|
assert json.encode(StructType[[]i64]{ val: [i64(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}'
|
|
|
|
|
|
|
|
assert json.encode(StructType[[]u64]{}) == '{"val":[]}'
|
|
|
|
assert json.encode(StructType[[]u64]{ val: [] }) == '{"val":[]}'
|
|
|
|
assert json.encode(StructType[[]u64]{ val: [u64(0)] }) == '{"val":[0]}'
|
|
|
|
assert json.encode(StructType[[]u64]{ val: [u64(1)] }) == '{"val":[1]}'
|
|
|
|
assert json.encode(StructType[[]u64]{ val: [u64(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}'
|
|
|
|
|
|
|
|
assert json.encode(StructType[[]f64]{}) == '{"val":[]}'
|
|
|
|
assert json.encode(StructType[[]f64]{ val: [] }) == '{"val":[]}'
|
|
|
|
assert json.encode(StructType[[]f64]{ val: [f64(0)] }) == '{"val":[0.0]}'
|
|
|
|
assert json.encode(StructType[[]f64]{ val: [f64(1)] }) == '{"val":[1.0]}'
|
|
|
|
assert json.encode(StructType[[]f64]{ val: [f64(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0.0,1.0,0.0,2.0,3.0,2.0,5.0,1.0]}'
|
|
|
|
|
|
|
|
assert json.encode(StructType[[]bool]{}) == '{"val":[]}'
|
|
|
|
assert json.encode(StructType[[]bool]{ val: [] }) == '{"val":[]}'
|
|
|
|
assert json.encode(StructType[[]bool]{ val: [true] }) == '{"val":[true]}'
|
|
|
|
assert json.encode(StructType[[]bool]{ val: [false] }) == '{"val":[false]}'
|
|
|
|
assert json.encode(StructType[[]bool]{ val: [false, true, false] }) == '{"val":[false,true,false]}'
|
2023-01-05 16:17:38 +03:00
|
|
|
|
|
|
|
array_of_struct := [StructType[bool]{
|
|
|
|
val: true
|
|
|
|
}, StructType[bool]{
|
|
|
|
val: false
|
|
|
|
}]
|
|
|
|
assert json.encode(StructType[[]StructType[bool]]{ val: array_of_struct }) == '{"val":[{"val":true},{"val":false}]}'
|
2022-12-10 19:44:20 +03:00
|
|
|
}
|
|
|
|
|
2023-01-09 09:36:45 +03:00
|
|
|
fn test_option_array() {
|
|
|
|
assert json.encode(StructTypeOption[[]string]{}) == '{}'
|
|
|
|
assert json.encode(StructTypeOption[[]string]{ val: [] }) == '{"val":[]}'
|
|
|
|
assert json.encode(StructTypeOption[[]string]{ val: ['0'] }) == '{"val":["0"]}'
|
|
|
|
assert json.encode(StructTypeOption[[]string]{ val: ['1'] }) == '{"val":["1"]}'
|
|
|
|
|
|
|
|
assert json.encode(StructTypeOption[[]int]{}) == '{}'
|
|
|
|
assert json.encode(StructTypeOption[[]int]{ val: [] }) == '{"val":[]}'
|
|
|
|
assert json.encode(StructTypeOption[[]int]{ val: [0] }) == '{"val":[0]}'
|
|
|
|
assert json.encode(StructTypeOption[[]int]{ val: [1] }) == '{"val":[1]}'
|
|
|
|
assert json.encode(StructTypeOption[[]int]{ val: [0, 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}'
|
|
|
|
|
|
|
|
assert json.encode(StructTypeOption[[]byte]{}) == '{}'
|
|
|
|
assert json.encode(StructTypeOption[[]byte]{ val: [] }) == '{"val":[]}'
|
|
|
|
assert json.encode(StructTypeOption[[]byte]{ val: [byte(0)] }) == '{"val":[0]}'
|
|
|
|
assert json.encode(StructTypeOption[[]byte]{ val: [byte(1)] }) == '{"val":[1]}'
|
|
|
|
assert json.encode(StructTypeOption[[]byte]{ val: [byte(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}'
|
|
|
|
|
|
|
|
assert json.encode(StructTypeOption[[]i64]{}) == '{}'
|
|
|
|
assert json.encode(StructTypeOption[[]i64]{ val: [] }) == '{"val":[]}'
|
|
|
|
assert json.encode(StructTypeOption[[]i64]{ val: [i64(0)] }) == '{"val":[0]}'
|
|
|
|
assert json.encode(StructTypeOption[[]i64]{ val: [i64(1)] }) == '{"val":[1]}'
|
|
|
|
assert json.encode(StructTypeOption[[]i64]{ val: [i64(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}'
|
|
|
|
|
|
|
|
assert json.encode(StructTypeOption[[]u64]{}) == '{}'
|
|
|
|
assert json.encode(StructTypeOption[[]u64]{ val: [] }) == '{"val":[]}'
|
|
|
|
assert json.encode(StructTypeOption[[]u64]{ val: [u64(0)] }) == '{"val":[0]}'
|
|
|
|
assert json.encode(StructTypeOption[[]u64]{ val: [u64(1)] }) == '{"val":[1]}'
|
|
|
|
assert json.encode(StructTypeOption[[]u64]{ val: [u64(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0,1,0,2,3,2,5,1]}'
|
|
|
|
|
|
|
|
assert json.encode(StructTypeOption[[]f64]{}) == '{}'
|
|
|
|
assert json.encode(StructTypeOption[[]f64]{ val: [] }) == '{"val":[]}'
|
|
|
|
assert json.encode(StructTypeOption[[]f64]{ val: [f64(0)] }) == '{"val":[0.0]}'
|
|
|
|
assert json.encode(StructTypeOption[[]f64]{ val: [f64(1)] }) == '{"val":[1.0]}'
|
|
|
|
assert json.encode(StructTypeOption[[]f64]{ val: [f64(0), 1, 0, 2, 3, 2, 5, 1] }) == '{"val":[0.0,1.0,0.0,2.0,3.0,2.0,5.0,1.0]}'
|
|
|
|
|
|
|
|
assert json.encode(StructTypeOption[[]bool]{}) == '{}'
|
|
|
|
assert json.encode(StructTypeOption[[]bool]{ val: [] }) == '{"val":[]}'
|
|
|
|
assert json.encode(StructTypeOption[[]bool]{ val: [true] }) == '{"val":[true]}'
|
|
|
|
assert json.encode(StructTypeOption[[]bool]{ val: [false] }) == '{"val":[false]}'
|
|
|
|
assert json.encode(StructTypeOption[[]bool]{ val: [false, true, false] }) == '{"val":[false,true,false]}'
|
2023-01-05 16:17:38 +03:00
|
|
|
|
|
|
|
array_of_struct := [StructType[bool]{
|
|
|
|
val: true
|
|
|
|
}, StructType[bool]{
|
|
|
|
val: false
|
|
|
|
}]
|
2023-01-09 09:36:45 +03:00
|
|
|
assert json.encode(StructTypeOption[[]StructType[bool]]{ val: array_of_struct }) == '{"val":[{"val":true},{"val":false}]}'
|
2023-04-10 19:54:43 +03:00
|
|
|
|
|
|
|
// assert json.encode(StructTypeOption[[][]int]{
|
|
|
|
// val: [[0, 1], [0, 2, 3], [2], [5, 1]]
|
|
|
|
// }) == '{"val":[[0,1],[0,2,3],[2],[5,1]]}'
|
2022-12-10 19:44:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_alias() {
|
|
|
|
assert json.encode(StructType[StringAlias]{}) == '{"val":""}'
|
|
|
|
assert json.encode(StructType[StringAlias]{ val: '' }) == '{"val":""}'
|
|
|
|
assert json.encode(StructType[StringAlias]{ val: 'a' }) == '{"val":"a"}'
|
|
|
|
|
|
|
|
assert json.encode(StructType[BoolAlias]{}) == '{"val":false}'
|
|
|
|
assert json.encode(StructType[BoolAlias]{ val: false }) == '{"val":false}'
|
|
|
|
assert json.encode(StructType[BoolAlias]{ val: true }) == '{"val":true}'
|
|
|
|
|
|
|
|
assert json.encode(StructType[IntAlias]{}) == '{"val":0}'
|
|
|
|
assert json.encode(StructType[IntAlias]{ val: 0 }) == '{"val":0}'
|
|
|
|
assert json.encode(StructType[IntAlias]{ val: 1 }) == '{"val":1}'
|
2023-01-09 23:43:01 +03:00
|
|
|
|
2023-01-14 22:30:29 +03:00
|
|
|
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"}'
|
|
|
|
|
2023-01-09 23:43:01 +03:00
|
|
|
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]{1} }) == '{"val":{"val":1}}'
|
2022-12-10 19:44:20 +03:00
|
|
|
}
|
2023-01-21 21:26:55 +03:00
|
|
|
|
|
|
|
fn test_sumtypes() {
|
|
|
|
assert json.encode(StructType[SumTypes]{}) == '{}'
|
|
|
|
assert json.encode(StructType[SumTypes]{ val: '' }) == '{"val":""}'
|
|
|
|
assert json.encode(StructType[SumTypes]{ val: 'a' }) == '{"val":"a"}'
|
|
|
|
|
|
|
|
assert json.encode(StructType[SumTypes]{ val: false }) == '{"val":false}'
|
|
|
|
assert json.encode(StructType[SumTypes]{ val: true }) == '{"val":true}'
|
|
|
|
|
|
|
|
assert json.encode(StructType[SumTypes]{ val: 0 }) == '{"val":0}'
|
|
|
|
assert json.encode(StructType[SumTypes]{ val: 1 }) == '{"val":1}'
|
|
|
|
|
|
|
|
assert json.encode(StructType[SumTypes]{ val: fixed_time }) == '{"val":2022-03-11T13:54:25.000Z}'
|
|
|
|
|
|
|
|
assert json.encode(StructType[StructType[SumTypes]]{
|
|
|
|
val: StructType[SumTypes]{
|
|
|
|
val: 1
|
|
|
|
}
|
|
|
|
}) == '{"val":{"val":1}}'
|
|
|
|
|
|
|
|
// assert json.encode(StructType[SumTypes]{ val: StructType[string]{
|
|
|
|
// val: '111111'
|
|
|
|
// } }) == '{"val":1}'
|
|
|
|
|
|
|
|
assert json.encode(StructType[StructType[SumTypes]]{
|
|
|
|
val: StructType[SumTypes]{
|
|
|
|
val: 1
|
|
|
|
}
|
|
|
|
}) == '{"val":{"val":1}}'
|
|
|
|
}
|
2023-03-24 14:45:26 +03:00
|
|
|
|
|
|
|
fn test_maps() {
|
|
|
|
assert json.encode(StructType[map[string]map[string]int]{}) == '{"val":{}}'
|
|
|
|
assert json.encode(StructType[map[string]string]{
|
|
|
|
val: {
|
|
|
|
'1': '1'
|
|
|
|
}
|
|
|
|
}) == '{"val":{"1":"1"}}'
|
|
|
|
assert json.encode(StructType[map[string]int]{
|
|
|
|
val: {
|
|
|
|
'1': 1
|
|
|
|
}
|
|
|
|
}) == '{"val":{"1":1}}'
|
2023-04-10 18:57:02 +03:00
|
|
|
assert json.encode(StructType[map[string]map[string]int]{
|
|
|
|
val: {
|
|
|
|
'a': {
|
|
|
|
'1': 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}) == '{"val":{"a":{"1":1}}}'
|
2023-03-24 14:45:26 +03:00
|
|
|
}
|