Skip to content

Commit

Permalink
fix: Marshal() including "default=" tags in EnvSet (#27)
Browse files Browse the repository at this point in the history
* fix: Marshal() including "default=" tags in EnvSet

* chore: added tests

* chore: added more tests

* chore: added comment to clarify skipping of tag options in Marshal
  • Loading branch information
kaandesu authored Oct 28, 2024
1 parent 682abba commit 6b7f898
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
7 changes: 7 additions & 0 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,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
}
}
Expand Down
60 changes: 52 additions & 8 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,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"`

Expand Down Expand Up @@ -495,14 +503,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)
Expand Down Expand Up @@ -550,6 +562,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"])
}
Expand Down

0 comments on commit 6b7f898

Please sign in to comment.