From 50c436372ee642161cc4e258e772ec9c105dd322 Mon Sep 17 00:00:00 2001 From: kaan Date: Tue, 10 Sep 2024 21:40:41 +0200 Subject: [PATCH 1/4] fix: Marshal() including "default=" tags in EnvSet --- env.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/env.go b/env.go index 1a82ee4..774cd5d 100644 --- a/env.go +++ b/env.go @@ -327,6 +327,12 @@ func Marshal(v interface{}) (EnvSet, error) { } for _, envKey := range envKeys { + if strings.Contains(envKey, "=") { + switch strings.ToLower(strings.SplitN(envKey, "=", 2)[0]) { + case "separator", "required", "default": + continue + } + } es[envKey] = envValue } } From 0bcfde26ef3754e546d05668f147ef5565b0db73 Mon Sep 17 00:00:00 2001 From: kaan Date: Tue, 10 Sep 2024 22:23:35 +0200 Subject: [PATCH 2/4] chore: added tests --- env_test.go | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/env_test.go b/env_test.go index d68ee29..59e17c1 100644 --- a/env_test.go +++ b/env_test.go @@ -61,6 +61,9 @@ type ValidStruct struct { MultipleTags string `env:"npm_config_cache,NPM_CONFIG_CACHE"` + MultipleTagsWithDefault string `env:"multiple_tags_with_default,MULTIPLE_TAGS_WITH_DEFAULT,default=default_tags_value"` + + TagWithDefault string `env:"tag_with_default,default=default_tag_value"` // time.Duration is supported Duration time.Duration `env:"TYPE_DURATION"` @@ -474,14 +477,16 @@ func TestMarshal(t *testing.T) { }{ Workspace: "/mnt/builds/slave/workspace/test", }, - Extra: "extra", - Int: 1, - Uint: 4294967295, - Float32: float32(2.3), - Float64: 4.5, - Bool: true, - MultipleTags: "foobar", - Duration: 3 * time.Minute, + Extra: "extra", + Int: 1, + Uint: 4294967295, + Float32: float32(2.3), + Float64: 4.5, + Bool: true, + MultipleTags: "foobar", + MultipleTagsWithDefault: "baz", + TagWithDefault: "bar", + Duration: 3 * time.Minute, } environ, err := Marshal(&validStruct) @@ -529,6 +534,22 @@ func TestMarshal(t *testing.T) { t.Errorf("Expected field value to be '%s' but got '%s'", "foobar", environ["NPM_CONFIG_CACHE"]) } + if environ["multiple_tags_with_default"] != "baz" { + t.Errorf("Expected field value to be '%s' but got '%s'", "baz", environ["multiple_tags_with_default"]) + } + + if environ["default=default_tags_value"] != "" { + t.Errorf("'default=default_tags_value' not expected to be a valid field value.") + } + + if environ["tag_with_default"] != "bar" { + t.Errorf("Expected field value to be '%s' but got '%s'", "bar", environ["tag_with_default"]) + } + + if environ["default=default_tag_value"] != "" { + t.Errorf("'default=default_tag_value' not expected to be a valid field value.") + } + if environ["TYPE_DURATION"] != "3m0s" { t.Errorf("Expected field value to be '%s' but got '%s'", "3m0s", environ["TYPE_DURATION"]) } From a7adfd984ec767047ddda9945578bb17705b4cc8 Mon Sep 17 00:00:00 2001 From: kaan Date: Sat, 26 Oct 2024 11:52:34 +0200 Subject: [PATCH 3/4] chore: added more tests --- env_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/env_test.go b/env_test.go index 59e17c1..70aad30 100644 --- a/env_test.go +++ b/env_test.go @@ -64,6 +64,11 @@ type ValidStruct struct { MultipleTagsWithDefault string `env:"multiple_tags_with_default,MULTIPLE_TAGS_WITH_DEFAULT,default=default_tags_value"` TagWithDefault string `env:"tag_with_default,default=default_tag_value"` + + TagWithRequired string `env:"tag_with_required,required=false"` + + TagWithSeparator string `env:"tag_with_separator,separator=&"` + // time.Duration is supported Duration time.Duration `env:"TYPE_DURATION"` @@ -486,6 +491,8 @@ func TestMarshal(t *testing.T) { MultipleTags: "foobar", MultipleTagsWithDefault: "baz", TagWithDefault: "bar", + TagWithRequired: "foo", + TagWithSeparator: "val1&val2", Duration: 3 * time.Minute, } @@ -546,6 +553,22 @@ func TestMarshal(t *testing.T) { t.Errorf("Expected field value to be '%s' but got '%s'", "bar", environ["tag_with_default"]) } + if environ["tag_with_required"] != "foo" { + t.Errorf("Expected field value to be '%s' but got '%s'", "foo", environ["tag_with_required"]) + } + + if environ["tag_with_separator"] != "val1&val2" { + t.Errorf("Expected field value to be '%s' but got '%s'", "val1&val2", environ["tag_with_separator"]) + } + + if environ["required=true"] != "" { + t.Errorf("'required=true' not expected to be a valid field value.") + } + + if environ["separator=&"] != "" { + t.Errorf("'separator=&' not expected to be a valid field value.") + } + if environ["default=default_tag_value"] != "" { t.Errorf("'default=default_tag_value' not expected to be a valid field value.") } From b88a6ced74a01254b91ba1af5f28bdf206eeb00e Mon Sep 17 00:00:00 2001 From: kaan Date: Sat, 26 Oct 2024 12:00:05 +0200 Subject: [PATCH 4/4] chore: added comment to clarify skipping of tag options in Marshal --- env.go | 1 + 1 file changed, 1 insertion(+) diff --git a/env.go b/env.go index 774cd5d..db8665b 100644 --- a/env.go +++ b/env.go @@ -327,6 +327,7 @@ func Marshal(v interface{}) (EnvSet, error) { } for _, envKey := range envKeys { + // Skip keys with '=', as they represent tag options and not environment variable names. if strings.Contains(envKey, "=") { switch strings.ToLower(strings.SplitN(envKey, "=", 2)[0]) { case "separator", "required", "default":