diff --git a/lib/data/encoderhero/encoder_test.v b/lib/data/encoderhero/encoder_test.v index 52ed38f1..8ddb940f 100644 --- a/lib/data/encoderhero/encoder_test.v +++ b/lib/data/encoderhero/encoder_test.v @@ -1,6 +1,7 @@ module encoderhero import freeflowuniverse.herolib.data.paramsparser +import freeflowuniverse.herolib.data.ourtime import time import v.reflection @@ -13,6 +14,40 @@ struct Remark { text string } +struct Company { + name string + founded ourtime.OurTime + employees []Person +} + +const company = Company{ + name: "Tech Corp" + founded: ourtime.new('2022-12-05 20:14')! + employees: [ + person, + Person{ + id: 2 + name: "Alice" + age: 30 + birthday: time.new( + day: 20 + month: 6 + year: 1990 + ) + car: Car{ + name: "Alice's car" + year: 2018 + } + profiles: [ + Profile{ + platform: 'LinkedIn' + url: 'linkedin.com/alice' + }, + ] + }, + ] +} + struct Person { Base mut: @@ -43,7 +78,7 @@ struct Profile { const person_heroscript = " !!define.person id:1 name:Bob birthday:'2012-12-12 00:00:00' !!define.person.car name:'Bob\\'s car' year:2014 -!!define.person.car.insurance expiration:'0000-00-00 00:00:00' provider:'' +!!define.person.car.insurance provider:insurer !!define.person.profile platform:Github url:github.com/example " @@ -60,6 +95,9 @@ const person = Person{ car: Car{ name: "Bob's car" year: 2014 + insurance: Insurance { + provider: "insurer" + } } profiles: [ Profile{ @@ -69,7 +107,23 @@ const person = Person{ ] } +const company_script = " +!!define.company name:'Tech Corp' founded:'2022-12-05 20:14' +!!define.company.person id:1 name:Bob birthday:'2012-12-12 00:00:00' +!!define.company.person.car name:'Bob\'s car' year:2014 +!!define.company.person.car.insurance provider:insurer' + +!!define.company.person.profile platform:Github url:github.com/example + +!!define.company.person id:2 name:Alice birthday:'1990-06-20 00:00:00' +!!define.company.person.car name:'Alice\'s car' year:2018 +!!define.company.person.car.insurance + +!!define.company.person.profile platform:LinkedIn url:linkedin.com/alice +" + fn test_encode() ! { person_script := encode[Person](person)! assert person_script.trim_space() == person_heroscript.trim_space() -} + assert encode[Company](company)!.trim_space() == company_script.trim_space() +} \ No newline at end of file diff --git a/lib/data/paramsparser/params_export_import.v b/lib/data/paramsparser/params_export_import.v index 11ba9d8b..283428d7 100644 --- a/lib/data/paramsparser/params_export_import.v +++ b/lib/data/paramsparser/params_export_import.v @@ -192,7 +192,7 @@ fn (p Params) export_helper(args_ ExportArgs) ![]ParamExportItem { } fn val_is_empty(val string) bool { - return val == '' || val == '[]' + return val == '' || val == '[]' || val == '0000-00-00 00:00:00' } @[params] diff --git a/lib/data/paramsparser/params_export_test.v b/lib/data/paramsparser/params_export_test.v index 68d5cc31..2fde7ba3 100644 --- a/lib/data/paramsparser/params_export_test.v +++ b/lib/data/paramsparser/params_export_test.v @@ -290,4 +290,4 @@ fn test_export_text() { } paramsout := params.export() assert paramsout.trim_space() == "text:'This content contains the character \\' in it'" -} +} \ No newline at end of file diff --git a/lib/data/paramsparser/params_reflection.v b/lib/data/paramsparser/params_reflection.v index 96bf96f3..ad39e163 100644 --- a/lib/data/paramsparser/params_reflection.v +++ b/lib/data/paramsparser/params_reflection.v @@ -1,6 +1,7 @@ module paramsparser import time +import freeflowuniverse.herolib.data.ourtime import v.reflection // import freeflowuniverse.herolib.data.encoderhero // TODO: support more field types @@ -66,6 +67,13 @@ pub fn (params Params) decode_value[T](_ T, key string) !T { return time.Time{} } return time.parse(time_str)! + } $else $if T is ourtime.OurTime { + time_str := params.get(key)! + // todo: 'handle other null times' + if time_str == '0000-00-00 00:00:00' { + return ourtime.new('0000-00-00 00:00:00')! + } + return ourtime.new(time_str)! } $else $if T is $struct { child_params := params.get_params(key)! child := child_params.decode_struct(T{})! @@ -100,7 +108,7 @@ pub fn encode[T](t T, args EncodeArgs) !Params { key = field_attrs['alias'] } $if val is string || val is int || val is bool || val is i64 || val is u32 - || val is time.Time { + || val is time.Time || val is ourtime.OurTime { params.set(key, '${val}') } $else $if field.is_enum { params.set(key, '${int(val)}')