This commit is contained in:
2025-02-09 08:55:01 +01:00
parent 1d631fec21
commit 7b69719f0e
38 changed files with 798 additions and 918 deletions

View File

@@ -46,8 +46,8 @@ fn decode_struct[T](_ T, data string) !T {
should_skip = true
break
}
}
if ! should_skip {
}
if !should_skip {
$if field.is_struct {
$if field.typ !is time.Time {
if !field.name[0].is_capital() {

View File

@@ -28,7 +28,7 @@ pub fn encode[T](val T) !string {
$if T is $struct {
e.encode_struct[T](val)!
} $else $if T is $array {
//TODO: need to make comma separated list only works if int,u8,u16,i8... or string if string put all elements in \''...\'',...
// TODO: need to make comma separated list only works if int,u8,u16,i8... or string if string put all elements in \''...\'',...
e.add_child_list[T](val, 'TODO')
} $else {
return error('can only add elements for struct or array of structs. \n${val}')
@@ -138,8 +138,8 @@ pub fn (mut e Encoder) encode_struct[T](t T) ! {
break
}
}
if ! should_skip {
if !should_skip {
val := t.$(field.name)
// time is encoded in the above params encoding step so skip and dont treat as recursive struct
$if val is time.Time || val is ourtime.OurTime {

View File

@@ -5,23 +5,21 @@ import time
import v.reflection
struct MyStruct {
id int
id int
name string
//skip attributes would be best way how to do the encoding but can't get it to work
// skip attributes would be best way how to do the encoding but can't get it to work
other ?&Remark @[skip; str: skip]
}
//is the one we should skip
// is the one we should skip
struct Remark {
id int
id int
}
fn test_encode() ! {
mut o := MyStruct{
id: 1
name: 'test'
id: 1
name: 'test'
other: &Remark{
id: 123
}
@@ -29,17 +27,16 @@ fn test_encode() ! {
script := encode[MyStruct](o)!
assert script.trim_space() == "!!define.my_struct id:1 name:test"
assert script.trim_space() == '!!define.my_struct id:1 name:test'
println(script)
o2:=decode[MyStruct](script)!
o2 := decode[MyStruct](script)!
assert o2== MyStruct{
id: 1
assert o2 == MyStruct{
id: 1
name: 'test'
}
println(o2)
}

View File

@@ -17,10 +17,9 @@ pub fn (params Params) decode_struct[T](_ T) !T {
$if field.is_enum {
t.$(field.name) = params.get_int(field.name) or { 0 }
} $else {
//super annoying didn't find other way, then to ignore options
$if field.is_option{
}$else{
// super annoying didn't find other way, then to ignore options
$if field.is_option {
} $else {
if field.name[0].is_capital() {
// embed := params.decode_struct(t.$(field.name))!
t.$(field.name) = params.decode_struct(t.$(field.name))!

View File

@@ -103,12 +103,11 @@ fn test_decode() {
decoded_child := test_child_params.decode[TestChild]()!
assert decoded_child == test_child
//IMPORTANT OPTIONALS ARE NOT SUPPORTED AND WILL NOT BE ENCODED FOR NOW (unless we find ways how to deal with attributes to not encode skipped elements)
// IMPORTANT OPTIONALS ARE NOT SUPPORTED AND WILL NOT BE ENCODED FOR NOW (unless we find ways how to deal with attributes to not encode skipped elements)
// test recursive decode struct with child
decoded := test_params.decode[TestStruct]()!
assert decoded == test_struct
}
fn test_encode() {