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

db, json, time, term: change optional to result (#16201)

This commit is contained in:
yuyi
2022-10-26 16:26:28 +08:00
committed by GitHub
parent 2a7420f572
commit 992b502198
57 changed files with 340 additions and 340 deletions

View File

@ -14,12 +14,12 @@ struct Employee {
title JobTitle
}
fn test_simple() ? {
fn test_simple() {
x := Employee{'Peter', 28, 95000.5, .worker}
s := json.encode(x)
// eprintln('Employee x: $s')
assert s == '{"name":"Peter","age":28,"salary":95000.5,"title":2}'
y := json.decode(Employee, s)?
y := json.decode(Employee, s)!
// eprintln('Employee y: $y')
assert y.name == 'Peter'
assert y.age == 28
@ -34,9 +34,9 @@ struct Price {
currency_id string [json: currencyId] = currency_id
}
fn test_field_with_default_expr() ? {
fn test_field_with_default_expr() {
data := '[{"net":1},{"net":2,"currencyId":"cjson"}]'
prices := json.decode([]Price, data)?
prices := json.decode([]Price, data)!
assert prices == [Price{
net: 1
currency_id: 'cconst'
@ -77,7 +77,7 @@ struct SomeGame {
other []Entity
}
fn test_encode_decode_sumtype() ? {
fn test_encode_decode_sumtype() {
t := time.now()
game := SomeGame{
title: 'Super Mega Game'
@ -97,7 +97,7 @@ fn test_encode_decode_sumtype() ? {
assert enc == '{"title":"Super Mega Game","player":{"name":"Monke","_type":"Human"},"other":[{"tag":"Pen","_type":"Item"},{"tag":"Cookie","_type":"Item"},1,"Stool",{"_type":"Time","value":$t.unix_time()}]}'
dec := json.decode(SomeGame, enc)?
dec := json.decode(SomeGame, enc)!
// eprintln('Decoded Game: $dec')
assert game.title == dec.title
@ -106,8 +106,8 @@ fn test_encode_decode_sumtype() ? {
assert (game.other[4] as time.Time).unix_time() == (dec.other[4] as time.Time).unix_time()
}
fn bar<T>(payload string) ?Bar { // ?T doesn't work currently
result := json.decode(T, payload)?
fn bar<T>(payload string) !Bar { // ?T doesn't work currently
result := json.decode(T, payload)!
return result
}
@ -135,11 +135,11 @@ struct User {
pets string [json: 'pet_animals'; raw]
}
fn test_parse_user() ? {
fn test_parse_user() {
s := '{"age": 10, "nums": [1,2,3], "type": 1, "lastName": "Johnson", "IsRegistered": true, "pet_animals": {"name": "Bob", "animal": "Dog"}}'
u2 := json.decode(User2, s)?
u2 := json.decode(User2, s)!
// println(u2)
u := json.decode(User, s)?
u := json.decode(User, s)!
// println(u)
assert u.age == 10
assert u.last_name == 'Johnson'
@ -152,7 +152,7 @@ fn test_parse_user() ? {
assert u.pets == '{"name":"Bob","animal":"Dog"}'
}
fn test_encode_decode_time() ? {
fn test_encode_decode_time() {
user := User2{
age: 25
reg_date: time.new_time(year: 2020, month: 12, day: 22, hour: 7, minute: 23)
@ -160,7 +160,7 @@ fn test_encode_decode_time() ? {
s := json.encode(user)
// println(s)
assert s.contains('"reg_date":1608621780')
user2 := json.decode(User2, s)?
user2 := json.decode(User2, s)!
assert user2.reg_date.str() == '2020-12-22 07:23:00'
// println(user2)
// println(user2.reg_date)
@ -219,8 +219,8 @@ struct Country {
name string
}
fn test_struct_in_struct() ? {
country := json.decode(Country, '{ "name": "UK", "cities": [{"name":"London"}, {"name":"Manchester"}]}')?
fn test_struct_in_struct() {
country := json.decode(Country, '{ "name": "UK", "cities": [{"name":"London"}, {"name":"Manchester"}]}')!
assert country.name == 'UK'
assert country.cities.len == 2
assert country.cities[0].name == 'London'
@ -241,14 +241,14 @@ fn test_encode_map() {
assert out == expected
}
fn test_parse_map() ? {
fn test_parse_map() {
expected := {
'one': 1
'two': 2
'three': 3
'four': 4
}
out := json.decode(map[string]int, '{"one":1,"two":2,"three":3,"four":4}')?
out := json.decode(map[string]int, '{"one":1,"two":2,"three":3,"four":4}')!
// println(out)
assert out == expected
}
@ -259,7 +259,7 @@ struct Data {
extra map[string]map[string]int
}
fn test_nested_type() ? {
fn test_nested_type() {
data_expected := '{"countries":[{"cities":[{"name":"London"},{"name":"Manchester"}],"name":"UK"},{"cities":[{"name":"Donlon"},{"name":"Termanches"}],"name":"KU"}],"users":{"Foo":{"age":10,"nums":[1,2,3],"lastName":"Johnson","IsRegistered":true,"type":0,"pet_animals":"little foo"},"Boo":{"age":20,"nums":[5,3,1],"lastName":"Smith","IsRegistered":false,"type":4,"pet_animals":"little boo"}},"extra":{"2":{"n1":2,"n2":4,"n3":8,"n4":16},"3":{"n1":3,"n2":9,"n3":27,"n4":81}}}'
data := Data{
countries: [
@ -308,7 +308,7 @@ fn test_nested_type() ? {
out := json.encode(data)
// println(out)
assert out == data_expected
data2 := json.decode(Data, data_expected)?
data2 := json.decode(Data, data_expected)!
assert data2.countries.len == data.countries.len
for i in 0 .. 1 {
assert data2.countries[i].name == data.countries[i].name
@ -338,11 +338,11 @@ pub:
data T
}
fn test_generic_struct() ? {
fn test_generic_struct() {
foo_int := Foo<int>{'bar', 12}
foo_enc := json.encode(foo_int)
assert foo_enc == '{"name":"bar","data":12}'
foo_dec := json.decode(Foo<int>, foo_enc)?
foo_dec := json.decode(Foo<int>, foo_enc)!
assert foo_dec.name == 'bar'
assert foo_dec.data == 12
}
@ -376,8 +376,8 @@ struct Message {
id ID
}
fn test_decode_alias_struct() ? {
msg := json.decode(Message, '{"id": "118499178790780929"}')?
fn test_decode_alias_struct() {
msg := json.decode(Message, '{"id": "118499178790780929"}')!
// hacky way of comparing aliased strings
assert msg.id.str() == '118499178790780929'
}
@ -394,20 +394,20 @@ struct List {
items []string
}
fn test_list() ? {
list := json.decode(List, '{"id": 1, "items": ["1", "2"]}')?
fn test_list() {
list := json.decode(List, '{"id": 1, "items": ["1", "2"]}')!
assert list.id == 1
assert list.items == ['1', '2']
}
fn test_list_no_id() ? {
list := json.decode(List, '{"items": ["1", "2"]}')?
fn test_list_no_id() {
list := json.decode(List, '{"items": ["1", "2"]}')!
assert list.id == 0
assert list.items == ['1', '2']
}
fn test_list_no_items() ? {
list := json.decode(List, '{"id": 1}')?
fn test_list_no_items() {
list := json.decode(List, '{"id": 1}')!
assert list.id == 1
assert list.items == []
}
@ -418,15 +418,15 @@ struct Info {
maps map[string]string
}
fn test_decode_null_object() ? {
info := json.decode(Info, '{"id": 22, "items": null, "maps": null}')?
fn test_decode_null_object() {
info := json.decode(Info, '{"id": 22, "items": null, "maps": null}')!
assert info.id == 22
assert '$info.items' == '[]'
assert '$info.maps' == '{}'
}
fn test_decode_missing_maps_field() ? {
info := json.decode(Info, '{"id": 22, "items": null}')?
fn test_decode_missing_maps_field() {
info := json.decode(Info, '{"id": 22, "items": null}')!
assert info.id == 22
assert '$info.items' == '[]'
assert '$info.maps' == '{}'