From 34f7c99adc7cb79164d9178f58e9621a9be76466 Mon Sep 17 00:00:00 2001 From: marrow16 Date: Sat, 6 Jan 2024 14:37:26 +0000 Subject: [PATCH] Use delimiter and separator --- writer.go | 10 +++++----- writer_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/writer.go b/writer.go index 8fd4159..852293d 100644 --- a/writer.go +++ b/writer.go @@ -145,9 +145,9 @@ func writeExampleValue(w io.Writer, name string, fv reflect.Value, fi *fieldInfo case reflect.Float32, reflect.Float64: eg = "0.0" case reflect.Slice: - eg = "value,value,..." + eg = fmt.Sprintf("value%svalue%s...", fi.delimiter, fi.delimiter) case reflect.Map: - eg = "key:value,key:value,..." + eg = fmt.Sprintf("key%svalue%skey%svalue%s...", fi.separator, fi.delimiter, fi.separator, fi.delimiter) } } _, err := w.Write([]byte(name + "=" + eg + "\n")) @@ -185,14 +185,14 @@ func writeActualValue(w io.Writer, name string, fv reflect.Value, fi *fieldInfo) for i := 0; i < fv.Len(); i++ { items = append(items, fmt.Sprintf("%v", fv.Index(i).Interface())) } - eg = strings.Join(items, ",") + eg = strings.Join(items, fi.delimiter) case reflect.Map: items := make([]string, 0) for _, mk := range fv.MapKeys() { mv := fv.MapIndex(mk) - items = append(items, fmt.Sprintf("%v:%v", mk.Interface(), mv.Interface())) + items = append(items, fmt.Sprintf("%v%s%v", mk.Interface(), fi.separator, mv.Interface())) } - eg = strings.Join(items, ",") + eg = strings.Join(items, fi.delimiter) } } } diff --git a/writer_test.go b/writer_test.go index 8d08c9c..4df21f3 100644 --- a/writer_test.go +++ b/writer_test.go @@ -121,6 +121,13 @@ func TestExample(t *testing.T) { Test []string }{}, expect: `TEST=value,value,... +`, + }, + { + cfg: &struct { + Test []string `env:"delimiter=;"` + }{}, + expect: `TEST=value;value;... `, }, { @@ -128,6 +135,13 @@ func TestExample(t *testing.T) { Test map[string]string }{}, expect: `TEST=key:value,key:value,... +`, + }, + { + cfg: &struct { + Test map[string]string `env:"delimiter=;,separator=','"` + }{}, + expect: `TEST=key,value;key,value;... `, }, { @@ -396,6 +410,18 @@ func TestExample(t *testing.T) { }, actual: true, expect: `TEST=foo:1.1 +`, + }, + { + cfg: &struct { + Test map[string]float32 `env:"separator=','"` + }{ + Test: map[string]float32{ + "foo": 1.1, + }, + }, + actual: true, + expect: `TEST=foo,1.1 `, }, {