From a2de5b6239a3f883b572f2fb4827db1222e959d8 Mon Sep 17 00:00:00 2001 From: tcn <87722291+heyimtcn@users.noreply.github.com> Date: Sat, 25 May 2024 13:48:10 +0200 Subject: [PATCH 1/7] toml.v fix decode_struct not working with `toml: ` --- vlib/toml/toml.v | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/vlib/toml/toml.v b/vlib/toml/toml.v index 35dda7df835974..833dc79462dcbe 100644 --- a/vlib/toml/toml.v +++ b/vlib/toml/toml.v @@ -33,7 +33,13 @@ pub fn decode[T](toml_txt string) !T { fn decode_struct[T](doc Any, mut typ T) { $for field in T.fields { - value := doc.value(field.name) + mut field_name := field.name + for attr in field.attrs { + if attr.starts_with('toml:') { + field_name = attr.all_after(':').trim_space() + } + } + value := doc.value(field_name) $if field.is_enum { typ.$(field.name) = value.int() } $else $if field.typ is string { From efd3e9a1f9b60680f50d332c099c3f2ba7b92f5b Mon Sep 17 00:00:00 2001 From: tcn <87722291+heyimtcn@users.noreply.github.com> Date: Sat, 25 May 2024 21:59:18 +0200 Subject: [PATCH 2/7] Update toml.v --- vlib/toml/toml.v | 214 ++++++++++++++++++++++++++--------------------- 1 file changed, 117 insertions(+), 97 deletions(-) diff --git a/vlib/toml/toml.v b/vlib/toml/toml.v index 833dc79462dcbe..2a324c92e018b5 100644 --- a/vlib/toml/toml.v +++ b/vlib/toml/toml.v @@ -34,110 +34,117 @@ pub fn decode[T](toml_txt string) !T { fn decode_struct[T](doc Any, mut typ T) { $for field in T.fields { mut field_name := field.name + mut skip := false for attr in field.attrs { + if attr == 'skip' { + skip = true + break + } if attr.starts_with('toml:') { field_name = attr.all_after(':').trim_space() } } - value := doc.value(field_name) - $if field.is_enum { - typ.$(field.name) = value.int() - } $else $if field.typ is string { - typ.$(field.name) = value.string() - } $else $if field.typ is bool { - typ.$(field.name) = value.bool() - } $else $if field.typ is int { - typ.$(field.name) = value.int() - } $else $if field.typ is i64 { - typ.$(field.name) = value.i64() - } $else $if field.typ is u64 { - typ.$(field.name) = value.u64() - } $else $if field.typ is f32 { - typ.$(field.name) = value.f32() - } $else $if field.typ is f64 { - typ.$(field.name) = value.f64() - } $else $if field.typ is DateTime { - typ.$(field.name) = value.datetime() - } $else $if field.typ is Date { - typ.$(field.name) = value.date() - } $else $if field.typ is Time { - typ.$(field.name) = value.time() - } $else $if field.is_array { - arr := value.array() - match field.typ { - []string { typ.$(field.name) = arr.as_strings() } - []int { typ.$(field.name) = arr.map(it.int()) } - []i64 { typ.$(field.name) = arr.map(it.i64()) } - []u64 { typ.$(field.name) = arr.map(it.u64()) } - []f32 { typ.$(field.name) = arr.map(it.f32()) } - []f64 { typ.$(field.name) = arr.map(it.f64()) } - []bool { typ.$(field.name) = arr.map(it.bool()) } - []DateTime { typ.$(field.name) = arr.map(it.datetime()) } - []Date { typ.$(field.name) = arr.map(it.date()) } - []Time { typ.$(field.name) = arr.map(it.time()) } - else {} - } - } $else $if field.is_map { - mut mmap := value.as_map() - match field.typ { - map[string]string { - typ.$(field.name) = mmap.as_strings() - } - // Should be cleaned up to use the more modern lambda syntax - // |k, v| k, v.int() - // Unfortunately lambdas have issues with multiple return at the time of writing - map[string]int { - typ.$(field.name) = maps.to_map[string, Any, string, int](mmap, fn (k string, v Any) (string, int) { - return k, v.int() - }) - } - map[string]i64 { - typ.$(field.name) = maps.to_map[string, Any, string, i64](mmap, fn (k string, v Any) (string, i64) { - return k, v.i64() - }) - } - map[string]u64 { - typ.$(field.name) = maps.to_map[string, Any, string, u64](mmap, fn (k string, v Any) (string, u64) { - return k, v.u64() - }) - } - map[string]f32 { - typ.$(field.name) = maps.to_map[string, Any, string, f32](mmap, fn (k string, v Any) (string, f32) { - return k, v.f32() - }) - } - map[string]f64 { - typ.$(field.name) = maps.to_map[string, Any, string, f64](mmap, fn (k string, v Any) (string, f64) { - return k, v.f64() - }) - } - map[string]bool { - typ.$(field.name) = maps.to_map[string, Any, string, bool](mmap, fn (k string, v Any) (string, bool) { - return k, v.bool() - }) - } - map[string]DateTime { - typ.$(field.name) = maps.to_map[string, Any, string, DateTime](mmap, - fn (k string, v Any) (string, DateTime) { - return k, v.datetime() - }) + if !skip { + value := doc.value(field_name) + $if field.is_enum { + typ.$(field.name) = value.int() + } $else $if field.typ is string { + typ.$(field.name) = value.string() + } $else $if field.typ is bool { + typ.$(field.name) = value.bool() + } $else $if field.typ is int { + typ.$(field.name) = value.int() + } $else $if field.typ is i64 { + typ.$(field.name) = value.i64() + } $else $if field.typ is u64 { + typ.$(field.name) = value.u64() + } $else $if field.typ is f32 { + typ.$(field.name) = value.f32() + } $else $if field.typ is f64 { + typ.$(field.name) = value.f64() + } $else $if field.typ is DateTime { + typ.$(field.name) = value.datetime() + } $else $if field.typ is Date { + typ.$(field.name) = value.date() + } $else $if field.typ is Time { + typ.$(field.name) = value.time() + } $else $if field.is_array { + arr := value.array() + match field.typ { + []string { typ.$(field.name) = arr.as_strings() } + []int { typ.$(field.name) = arr.map(it.int()) } + []i64 { typ.$(field.name) = arr.map(it.i64()) } + []u64 { typ.$(field.name) = arr.map(it.u64()) } + []f32 { typ.$(field.name) = arr.map(it.f32()) } + []f64 { typ.$(field.name) = arr.map(it.f64()) } + []bool { typ.$(field.name) = arr.map(it.bool()) } + []DateTime { typ.$(field.name) = arr.map(it.datetime()) } + []Date { typ.$(field.name) = arr.map(it.date()) } + []Time { typ.$(field.name) = arr.map(it.time()) } + else {} } - map[string]Date { - typ.$(field.name) = maps.to_map[string, Any, string, Date](mmap, fn (k string, v Any) (string, Date) { - return k, v.date() - }) + } $else $if field.is_map { + mut mmap := value.as_map() + match field.typ { + map[string]string { + typ.$(field.name) = mmap.as_strings() + } + // Should be cleaned up to use the more modern lambda syntax + // |k, v| k, v.int() + // Unfortunately lambdas have issues with multiple return at the time of writing + map[string]int { + typ.$(field.name) = maps.to_map[string, Any, string, int](mmap, fn (k string, v Any) (string, int) { + return k, v.int() + }) + } + map[string]i64 { + typ.$(field.name) = maps.to_map[string, Any, string, i64](mmap, fn (k string, v Any) (string, i64) { + return k, v.i64() + }) + } + map[string]u64 { + typ.$(field.name) = maps.to_map[string, Any, string, u64](mmap, fn (k string, v Any) (string, u64) { + return k, v.u64() + }) + } + map[string]f32 { + typ.$(field.name) = maps.to_map[string, Any, string, f32](mmap, fn (k string, v Any) (string, f32) { + return k, v.f32() + }) + } + map[string]f64 { + typ.$(field.name) = maps.to_map[string, Any, string, f64](mmap, fn (k string, v Any) (string, f64) { + return k, v.f64() + }) + } + map[string]bool { + typ.$(field.name) = maps.to_map[string, Any, string, bool](mmap, fn (k string, v Any) (string, bool) { + return k, v.bool() + }) + } + map[string]DateTime { + typ.$(field.name) = maps.to_map[string, Any, string, DateTime](mmap, + fn (k string, v Any) (string, DateTime) { + return k, v.datetime() + }) + } + map[string]Date { + typ.$(field.name) = maps.to_map[string, Any, string, Date](mmap, fn (k string, v Any) (string, Date) { + return k, v.date() + }) + } + map[string]Time { + typ.$(field.name) = maps.to_map[string, Any, string, Time](mmap, fn (k string, v Any) (string, Time) { + return k, v.time() + }) + } + else {} } - map[string]Time { - typ.$(field.name) = maps.to_map[string, Any, string, Time](mmap, fn (k string, v Any) (string, Time) { - return k, v.time() - }) - } - else {} + } $else $if field.is_struct { + mut s := typ.$(field.name) + decode_struct(value, mut s) + typ.$(field.name) = s } - } $else $if field.is_struct { - mut s := typ.$(field.name) - decode_struct(value, mut s) - typ.$(field.name) = s } } } @@ -157,7 +164,20 @@ pub fn encode[T](typ T) string { fn encode_struct[T](typ T) map[string]Any { mut mp := map[string]Any{} $for field in T.fields { - mp[field.name] = to_any(typ.$(field.name)) + mut skip := false + mut field_name := field.name + for attr in field.attrs { + if attr == 'skip' { + skip = true + break + } + if attr.starts_with('toml:') { + field_name = attr.all_after(':').trim_space() + } + } + if !skip { + mp[field_name] = to_any(typ.$(field.name)) + } } return mp } From c69f90e74971aa83da38682047be9925c19f8f48 Mon Sep 17 00:00:00 2001 From: tcn <87722291+heyimtcn@users.noreply.github.com> Date: Sat, 25 May 2024 22:00:33 +0200 Subject: [PATCH 3/7] Add test --- vlib/toml/tests/toml_attrs_test.v | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 vlib/toml/tests/toml_attrs_test.v diff --git a/vlib/toml/tests/toml_attrs_test.v b/vlib/toml/tests/toml_attrs_test.v new file mode 100644 index 00000000000000..2962fef17cd99c --- /dev/null +++ b/vlib/toml/tests/toml_attrs_test.v @@ -0,0 +1,15 @@ +import toml + +struct TestStruct { + foo int + bar bool @[skip] + baz string = 'def' @[toml: barbaz] +} + +fn test_toml_attr_encode() { + assert toml.encode(TestStruct{}) == 'foo = 0\nbarbaz = "def"' +} + +fn test_toml_attr_decode() { + assert toml.decode[TestStruct]('foo = 0\nbarbaz = "def"')! == TestStruct{} +} \ No newline at end of file From d511533a57b5569c4ef25cc3291cbfabcd5c8755 Mon Sep 17 00:00:00 2001 From: tcn <87722291+heyimtcn@users.noreply.github.com> Date: Sat, 25 May 2024 22:28:52 +0200 Subject: [PATCH 4/7] fmt toml.v --- vlib/toml/toml.v | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/vlib/toml/toml.v b/vlib/toml/toml.v index 2a324c92e018b5..f3b1bb2e1436e8 100644 --- a/vlib/toml/toml.v +++ b/vlib/toml/toml.v @@ -93,32 +93,38 @@ fn decode_struct[T](doc Any, mut typ T) { // |k, v| k, v.int() // Unfortunately lambdas have issues with multiple return at the time of writing map[string]int { - typ.$(field.name) = maps.to_map[string, Any, string, int](mmap, fn (k string, v Any) (string, int) { + typ.$(field.name) = maps.to_map[string, Any, string, int](mmap, + fn (k string, v Any) (string, int) { return k, v.int() }) } map[string]i64 { - typ.$(field.name) = maps.to_map[string, Any, string, i64](mmap, fn (k string, v Any) (string, i64) { + typ.$(field.name) = maps.to_map[string, Any, string, i64](mmap, + fn (k string, v Any) (string, i64) { return k, v.i64() }) } map[string]u64 { - typ.$(field.name) = maps.to_map[string, Any, string, u64](mmap, fn (k string, v Any) (string, u64) { + typ.$(field.name) = maps.to_map[string, Any, string, u64](mmap, + fn (k string, v Any) (string, u64) { return k, v.u64() }) } map[string]f32 { - typ.$(field.name) = maps.to_map[string, Any, string, f32](mmap, fn (k string, v Any) (string, f32) { + typ.$(field.name) = maps.to_map[string, Any, string, f32](mmap, + fn (k string, v Any) (string, f32) { return k, v.f32() }) } map[string]f64 { - typ.$(field.name) = maps.to_map[string, Any, string, f64](mmap, fn (k string, v Any) (string, f64) { + typ.$(field.name) = maps.to_map[string, Any, string, f64](mmap, + fn (k string, v Any) (string, f64) { return k, v.f64() }) } map[string]bool { - typ.$(field.name) = maps.to_map[string, Any, string, bool](mmap, fn (k string, v Any) (string, bool) { + typ.$(field.name) = maps.to_map[string, Any, string, bool](mmap, + fn (k string, v Any) (string, bool) { return k, v.bool() }) } @@ -129,12 +135,14 @@ fn decode_struct[T](doc Any, mut typ T) { }) } map[string]Date { - typ.$(field.name) = maps.to_map[string, Any, string, Date](mmap, fn (k string, v Any) (string, Date) { + typ.$(field.name) = maps.to_map[string, Any, string, Date](mmap, + fn (k string, v Any) (string, Date) { return k, v.date() }) } map[string]Time { - typ.$(field.name) = maps.to_map[string, Any, string, Time](mmap, fn (k string, v Any) (string, Time) { + typ.$(field.name) = maps.to_map[string, Any, string, Time](mmap, + fn (k string, v Any) (string, Time) { return k, v.time() }) } From b6c7c3c7809f726b82112c9a2503e8ebf5444391 Mon Sep 17 00:00:00 2001 From: tcn <87722291+heyimtcn@users.noreply.github.com> Date: Sat, 25 May 2024 22:29:46 +0200 Subject: [PATCH 5/7] fmt toml_attrs_test.v --- vlib/toml/tests/toml_attrs_test.v | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vlib/toml/tests/toml_attrs_test.v b/vlib/toml/tests/toml_attrs_test.v index 2962fef17cd99c..f0fa6a775e1074 100644 --- a/vlib/toml/tests/toml_attrs_test.v +++ b/vlib/toml/tests/toml_attrs_test.v @@ -2,8 +2,8 @@ import toml struct TestStruct { foo int - bar bool @[skip] - baz string = 'def' @[toml: barbaz] + bar bool @[skip] + baz string = 'def' @[toml: barbaz] } fn test_toml_attr_encode() { @@ -12,4 +12,4 @@ fn test_toml_attr_encode() { fn test_toml_attr_decode() { assert toml.decode[TestStruct]('foo = 0\nbarbaz = "def"')! == TestStruct{} -} \ No newline at end of file +} From 4181a336acc706c72dbbf0411188c84b7e7dd06d Mon Sep 17 00:00:00 2001 From: tcn <87722291+heyimtcn@users.noreply.github.com> Date: Sun, 26 May 2024 00:19:45 +0200 Subject: [PATCH 6/7] fix fmt for toml_attrs_test.v? From f571fa07f1d7b5918038a3b1938b09860f8256e2 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 26 May 2024 05:02:15 +0300 Subject: [PATCH 7/7] run `./v fmt -w vlib/toml/` --- vlib/toml/tests/toml_attrs_test.v | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/vlib/toml/tests/toml_attrs_test.v b/vlib/toml/tests/toml_attrs_test.v index f0fa6a775e1074..772f9e7476c68e 100644 --- a/vlib/toml/tests/toml_attrs_test.v +++ b/vlib/toml/tests/toml_attrs_test.v @@ -1,15 +1,15 @@ -import toml - -struct TestStruct { - foo int - bar bool @[skip] - baz string = 'def' @[toml: barbaz] -} - -fn test_toml_attr_encode() { - assert toml.encode(TestStruct{}) == 'foo = 0\nbarbaz = "def"' -} - -fn test_toml_attr_decode() { - assert toml.decode[TestStruct]('foo = 0\nbarbaz = "def"')! == TestStruct{} -} +import toml + +struct TestStruct { + foo int + bar bool @[skip] + baz string = 'def' @[toml: barbaz] +} + +fn test_toml_attr_encode() { + assert toml.encode(TestStruct{}) == 'foo = 0\nbarbaz = "def"' +} + +fn test_toml_attr_decode() { + assert toml.decode[TestStruct]('foo = 0\nbarbaz = "def"')! == TestStruct{} +}