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

all: replace generic <> with [] - part 2 (#16536)

This commit is contained in:
yuyi
2022-11-27 00:23:26 +08:00
committed by GitHub
parent b19b97e7b1
commit ef5be22f81
297 changed files with 1959 additions and 1943 deletions

View File

@@ -48,14 +48,14 @@ fn main() {
//}
// Encode a struct/type to JSON
encoded_json := json2.encode<Person>(person2)
encoded_json := json2.encode[Person](person2)
}
```
## Using `decode<T>` and `encode<T>`
## Using `decode[T]` and `encode[T]`
> Codegen for this feature is still WIP.
> You need to manually define the methods before using the module to structs.
In order to use the `decode<T>` and `encode<T>` function, you need to explicitly define
In order to use the `decode[T]` and `encode[T]` function, you need to explicitly define
two methods: `from_json` and `to_json`. `from_json` accepts a `json2.Any` argument
and inside of it you need to map the fields you're going to put into the type.
As for `to_json` method, you just need to map the values into `json2.Any`
@@ -91,9 +91,9 @@ fn (p Person) to_json() string {
fn main() {
resp := os.read_file('./person.json')!
person := json2.decode<Person>(resp)!
person := json2.decode[Person](resp)!
println(person) // Person{name: 'Bob', age: 28, pets: ['Floof']}
person_json := json2.encode<Person>(person)
person_json := json2.encode[Person](person)
println(person_json) // {"name": "Bob", "age": 28, "pets": ["Floof"]}
}
```
@@ -128,7 +128,7 @@ fn (mut p Person) from_json(f json2.Any) {
### Null Values
`x.json2` has a separate `null` type for differentiating an undefined value and a null value.
To verify that the field you're accessing is a `null`, use `<typ> is json2.Null`.
To verify that the field you're accessing is a `null`, use `[typ] is json2.Null`.
```v ignore
fn (mut p Person) from_json(f json2.Any) {

View File

@@ -39,8 +39,8 @@ const escaped_chars = [(r'\b').bytes(), (r'\f').bytes(), (r'\n').bytes(),
(r'\r').bytes(), (r'\t').bytes()]
// encode_value encodes a value to the specific writer.
pub fn (e &Encoder) encode_value<T>(val T, mut wr io.Writer) ! {
e.encode_value_with_level<T>(val, 1, mut wr)!
pub fn (e &Encoder) encode_value[T](val T, mut wr io.Writer) ! {
e.encode_value_with_level[T](val, 1, mut wr)!
}
fn (e &Encoder) encode_newline(level int, mut wr io.Writer) ! {
@@ -132,7 +132,7 @@ fn (e &Encoder) encode_any(val Any, level int, mut wr io.Writer) ! {
}
}
fn (e &Encoder) encode_value_with_level<T>(val T, level int, mut wr io.Writer) ! {
fn (e &Encoder) encode_value_with_level[T](val T, level int, mut wr io.Writer) ! {
$if T is string {
e.encode_string(val, mut wr)!
} $else $if T is Any {
@@ -160,7 +160,7 @@ fn (e &Encoder) encode_value_with_level<T>(val T, level int, mut wr io.Writer) !
}
}
fn (e &Encoder) encode_struct<U>(val U, level int, mut wr io.Writer) ! {
fn (e &Encoder) encode_struct[U](val U, level int, mut wr io.Writer) ! {
wr.write([u8(`{`)])!
mut i := 0
mut fields_len := 0
@@ -221,7 +221,7 @@ fn (e &Encoder) encode_struct<U>(val U, level int, mut wr io.Writer) ! {
wr.write([u8(`}`)])!
}
fn (e &Encoder) encode_array<U>(val U, level int, mut wr io.Writer) ! {
fn (e &Encoder) encode_array[U](val U, level int, mut wr io.Writer) ! {
$if U is $Array {
wr.write([u8(`[`)])!
for i in 0 .. val.len {

View File

@@ -43,10 +43,10 @@ pub fn (mut x IntegerValues) from_json(f json2.Any) {
fn test_all_primitive_integer_types_are_encodable_and_decodable() {
f := IntegerValues{1, 2, 3, 4, -1, -2, -3, -4}
s := json2.encode<IntegerValues>(f)
s := json2.encode[IntegerValues](f)
dump(s)
assert s == '{"ux8":1,"ux16":2,"ux32":3,"ux64":4,"sx8":-1,"sx16":-2,"sx32":-3,"sx64":-4}'
x := json2.decode<IntegerValues>(s)!
x := json2.decode[IntegerValues](s)!
dump(x)
assert x == f
println('done')

View File

@@ -18,7 +18,7 @@ pub fn fast_raw_decode(src string) !Any {
}
// decode is a generic function that decodes a JSON string into the target type.
pub fn decode<T>(src string) !T {
pub fn decode[T](src string) !T {
res := raw_decode(src)!
mut typ := T{}
typ.from_json(res)
@@ -26,14 +26,14 @@ pub fn decode<T>(src string) !T {
}
// encode is a generic function that encodes a type into a JSON string.
pub fn encode<T>(val T) string {
pub fn encode[T](val T) string {
mut sb := strings.new_builder(64)
defer {
unsafe { sb.free() }
}
default_encoder.encode_value(val, mut sb) or {
dump(err)
default_encoder.encode_value<Null>(null, mut sb) or {}
default_encoder.encode_value[Null](null, mut sb) or {}
}
return sb.str()
}

View File

@@ -29,7 +29,7 @@ fn test_simple() {
name: 'João'
}
x := Employee{'Peter', 28, 95000.5, .worker, sub_employee}
s := json.encode<Employee>(x)
s := json.encode[Employee](x)
assert s == '{"name":"Peter","age":28,"salary":95000.5,"title":2,"sub_employee":{"name":"João","age":0,"salary":0.0,"title":0}}'
// y := json.decode<Employee>(s) or {
// println(err)
@@ -93,7 +93,7 @@ fn test_encode_user() {
pets: 'foo'
}
expected := '{"age":10,"nums":[1,2,3],"lastName":"Johnson","IsRegistered":true,"type":0,"pet_animals":"foo"}'
out := json.encode<User>(usr)
out := json.encode[User](usr)
// println(out)
assert out == expected
// Test json.encode on mutable pointers
@@ -136,7 +136,7 @@ struct Message {
fn test_encode_alias_struct() {
expected := '{"id":"118499178790780929","ij":999998888}'
msg := Message{'118499178790780929', 999998888}
out := json.encode<Message>(msg)
out := json.encode[Message](msg)
assert out == expected
}

View File

@@ -1,31 +1,31 @@
module json2
fn gen_workaround<T>(result T) T {
fn gen_workaround[T](result T) T {
return result
}
fn gen_workaround_result<T>(result T) ?T {
fn gen_workaround_result[T](result T) ?T {
return result
}
fn gen_workaround_optional<T>(result T) !T {
fn gen_workaround_optional[T](result T) !T {
return result
}
const (
string_type_idx = typeof(gen_workaround<string>(unsafe { nil })).idx
result_string_type_idx = typeof(gen_workaround_result<string>(unsafe { nil })).idx
optional_string_type_idx = typeof(gen_workaround_optional<string>(unsafe { nil })).idx
string_type_idx = typeof(gen_workaround[string](unsafe { nil })).idx
result_string_type_idx = typeof(gen_workaround_result[string](unsafe { nil })).idx
optional_string_type_idx = typeof(gen_workaround_optional[string](unsafe { nil })).idx
int_type_idx = typeof(gen_workaround<int>(unsafe { nil })).idx
result_int_type_idx = typeof(gen_workaround_result<int>(unsafe { nil })).idx
optional_int_type_idx = typeof(gen_workaround_optional<int>(unsafe { nil })).idx
int_type_idx = typeof(gen_workaround[int](unsafe { nil })).idx
result_int_type_idx = typeof(gen_workaround_result[int](unsafe { nil })).idx
optional_int_type_idx = typeof(gen_workaround_optional[int](unsafe { nil })).idx
int_array_type_idx = typeof(gen_workaround<[]int>(unsafe { nil })).idx
result_int_array_type_idx = typeof(gen_workaround_result<[]int>(unsafe { nil })).idx
optional_int_array_type_idx = typeof(gen_workaround_optional<[]int>(unsafe { nil })).idx
int_array_type_idx = typeof(gen_workaround[[]int](unsafe { nil })).idx
result_int_array_type_idx = typeof(gen_workaround_result[[]int](unsafe { nil })).idx
optional_int_array_type_idx = typeof(gen_workaround_optional[[]int](unsafe { nil })).idx
byte_array_type_idx = typeof(gen_workaround<[]byte>(unsafe { nil })).idx
result_byte_array_type_idx = typeof(gen_workaround_result<[]byte>(unsafe { nil })).idx
optional_byte_array_type_idx = typeof(gen_workaround_optional<[]byte>(unsafe { nil })).idx
byte_array_type_idx = typeof(gen_workaround[[]byte](unsafe { nil })).idx
result_byte_array_type_idx = typeof(gen_workaround_result[[]byte](unsafe { nil })).idx
optional_byte_array_type_idx = typeof(gen_workaround_optional[[]byte](unsafe { nil })).idx
)