Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Marshal() including "default=" tags in EnvSet #27

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ func Marshal(v interface{}) (EnvSet, error) {
}

for _, envKey := range envKeys {
if strings.Contains(envKey, "=") {
kaandesu marked this conversation as resolved.
Show resolved Hide resolved
switch strings.ToLower(strings.SplitN(envKey, "=", 2)[0]) {
case "separator", "required", "default":
patricksanders marked this conversation as resolved.
Show resolved Hide resolved
continue
}
}
es[envKey] = envValue
}
}
Expand Down
37 changes: 29 additions & 8 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"])
}
Expand Down