diff --git a/env.go b/env.go index 1a82ee4..db8665b 100644 --- a/env.go +++ b/env.go @@ -327,6 +327,13 @@ 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": + continue + } + } es[envKey] = envValue } } diff --git a/env_test.go b/env_test.go index d68ee29..70aad30 100644 --- a/env_test.go +++ b/env_test.go @@ -61,6 +61,14 @@ 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"` + + 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"` @@ -474,14 +482,18 @@ 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", + TagWithRequired: "foo", + TagWithSeparator: "val1&val2", + Duration: 3 * time.Minute, } environ, err := Marshal(&validStruct) @@ -529,6 +541,38 @@ 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["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.") + } + if environ["TYPE_DURATION"] != "3m0s" { t.Errorf("Expected field value to be '%s' but got '%s'", "3m0s", environ["TYPE_DURATION"]) }