-
Notifications
You must be signed in to change notification settings - Fork 491
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
feat(config): Env var config can now add new items #2653
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -460,9 +460,7 @@ func (c *Config) applyEnvOverridesToMap(prefix string, fieldDesc string, mapValu | |
if spec.Kind() == reflect.Ptr { | ||
s = spec.Elem() | ||
} | ||
|
||
var value string | ||
|
||
if s.Kind() != reflect.Struct { | ||
value = os.Getenv(prefix) | ||
// Skip any fields we don't have a value to set | ||
|
@@ -640,8 +638,28 @@ func (c *Config) applyEnvOverridesToStruct(prefix string, s reflect.Value) error | |
} | ||
} | ||
} else if f.Kind() == reflect.Map { | ||
for _, k := range f.MapKeys() { | ||
if err := c.applyEnvOverridesToMap(fmt.Sprintf("%s_%v", key, k), fieldName, f, k, f.MapIndex(k)); err != nil { | ||
keys := map[string]reflect.Value{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this only for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, and environmental variable config of kap. |
||
for _, kVal := range f.MapKeys() { | ||
keys[fmt.Sprintf("%s_%v", key, kVal)] = kVal | ||
} | ||
|
||
for _, env := range os.Environ() { | ||
if !strings.HasPrefix(env, key) { | ||
continue | ||
} | ||
fullKey := parseEnvKey(env) | ||
// we need to replace k with the correctly typed k, if it is in keys. | ||
k, ok := keys[fullKey] | ||
if !ok { | ||
if f.Type().Key().Kind() != reflect.String { | ||
return errors.New("This config field does not support creation from environmental variables; key must exist in the config first") | ||
} | ||
k = reflect.ValueOf(strings.TrimLeft(strings.TrimPrefix(fullKey, key), "_")) | ||
if k.String() == "" { | ||
continue | ||
} | ||
} | ||
if err := c.applyEnvOverridesToMap(fullKey, fieldName, f, k, reflect.New(f.Type().Elem())); err != nil { | ||
return err | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package server | ||
|
||
// parseEnvKey gives us the key from the environment string. | ||
func parseEnvKey(s string) string { | ||
// copied from how the stdlib splits the env strings | ||
for i := 0; i < len(s); i++ { | ||
if s[i] == '=' { | ||
return s[:i] | ||
} | ||
} | ||
return s | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happened here? Non-blocking, just weird.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
go mod tidy
the new version sorts differently, and maintains sorting.