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
|
|
|
|
|
|
|
|
type SumTypes = bool | int | string
|
|
|
|
|
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.decode[StructType[string]]('{"val": ""}')!.val == ''
|
|
|
|
assert json.decode[StructType[string]]('{"val": "0"}')!.val == '0'
|
|
|
|
assert json.decode[StructType[string]]('{"val": "1"}')!.val == '1'
|
|
|
|
assert json.decode[StructType[string]]('{"val": "2"}')!.val == '2'
|
|
|
|
assert json.decode[StructType[string]]('{"val": 0}')!.val == '0'
|
|
|
|
assert json.decode[StructType[string]]('{"val": 1}')!.val == '1'
|
|
|
|
assert json.decode[StructType[string]]('{"val": 2}')!.val == '2'
|
|
|
|
assert json.decode[StructType[string]]('{"val": "true"}')!.val == 'true'
|
|
|
|
assert json.decode[StructType[string]]('{"val": "false"}')!.val == 'false'
|
|
|
|
assert json.decode[StructType[string]]('{"val": true}')!.val == 'true'
|
|
|
|
assert json.decode[StructType[string]]('{"val": false}')!.val == 'false'
|
|
|
|
|
|
|
|
assert json.decode[StructType[bool]]('{"val": ""}')!.val == false
|
|
|
|
assert json.decode[StructType[bool]]('{"val": "0"}')!.val == false
|
|
|
|
assert json.decode[StructType[bool]]('{"val": "1"}')!.val == true
|
|
|
|
assert json.decode[StructType[bool]]('{"val": "2"}')!.val == true
|
|
|
|
assert json.decode[StructType[bool]]('{"val": 0}')!.val == false
|
|
|
|
assert json.decode[StructType[bool]]('{"val": 1}')!.val == true
|
|
|
|
assert json.decode[StructType[bool]]('{"val": 2}')!.val == true
|
|
|
|
assert json.decode[StructType[bool]]('{"val": "true"}')!.val == true
|
|
|
|
assert json.decode[StructType[bool]]('{"val": "false"}')!.val == false
|
|
|
|
assert json.decode[StructType[bool]]('{"val": true}')!.val == true
|
|
|
|
assert json.decode[StructType[bool]]('{"val": false}')!.val == false
|
|
|
|
|
|
|
|
assert json.decode[StructType[int]]('{"val": ""}')!.val == 0
|
|
|
|
assert json.decode[StructType[int]]('{"val": "0"}')!.val == 0
|
|
|
|
assert json.decode[StructType[int]]('{"val": "1"}')!.val == 1
|
|
|
|
assert json.decode[StructType[int]]('{"val": "2"}')!.val == 2
|
|
|
|
assert json.decode[StructType[int]]('{"val": 0}')!.val == 0
|
|
|
|
assert json.decode[StructType[int]]('{"val": 1}')!.val == 1
|
|
|
|
assert json.decode[StructType[int]]('{"val": 2}')!.val == 2
|
|
|
|
assert json.decode[StructType[int]]('{"val": "true"}')!.val == 1
|
|
|
|
assert json.decode[StructType[int]]('{"val": "false"}')!.val == 0
|
|
|
|
assert json.decode[StructType[int]]('{"val": true}')!.val == 1
|
|
|
|
assert json.decode[StructType[int]]('{"val": false}')!.val == 0
|
2022-12-12 11:06:29 +03:00
|
|
|
|
|
|
|
assert json.decode[StructType[time.Time]]('{"val": "2022-03-11T13:54:25.000Z"}')!.val == fixed_time
|
2022-12-20 17:34:04 +03:00
|
|
|
assert json.decode[StructType[time.Time]]('{"val": "2001-01-05"}')!.val.year == 2001
|
|
|
|
assert json.decode[StructType[time.Time]]('{"val": "2001-01-05"}')!.val.month == 1
|
|
|
|
assert json.decode[StructType[time.Time]]('{"val": "2001-01-05"}')!.val.day == 5
|
|
|
|
assert json.decode[StructType[time.Time]]('{"val": "2001-01-05"}')!.val.hour == 0
|
|
|
|
assert json.decode[StructType[time.Time]]('{"val": "2001-01-05"}')!.val.minute == 0
|
|
|
|
assert json.decode[StructType[time.Time]]('{"val": "2001-01-05"}')!.val.second == 0
|
2022-12-12 11:06:29 +03:00
|
|
|
assert json.decode[StructType[time.Time]]('{"val": "2022-03-11 13:54:25.000"}')!.val == fixed_time
|
|
|
|
assert json.decode[StructType[time.Time]]('{"val": 1647006865}')!.val == fixed_time
|
|
|
|
assert json.decode[StructType[time.Time]]('{"val": "1647006865"}')!.val == fixed_time
|
|
|
|
if x := json.decode[StructType[time.Time]]('{"val": "invalid time"}') {
|
|
|
|
assert false
|
|
|
|
} else {
|
|
|
|
// dump(err)
|
|
|
|
assert true
|
|
|
|
}
|
2022-12-10 19:44:20 +03:00
|
|
|
}
|