-
Notifications
You must be signed in to change notification settings - Fork 6
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: ensure --values
settings override internal defaults
#64
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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,103 @@ | ||
package maps | ||
|
||
import ( | ||
"fmt" | ||
"gopkg.in/yaml.v3" | ||
"os" | ||
"strings" | ||
) | ||
|
||
// FromSlice converts a slice of dot-delimited string values into a map[string]any. | ||
// For example: | ||
// "a.b.c=123","a.b.d=124" would return { "a": { "b": { "c": 123, "d": 124 } } } | ||
func FromSlice(slice []string) map[string]any { | ||
m := map[string]any{} | ||
|
||
for _, s := range slice { | ||
// s has the format of: | ||
// a.b.c=xyz | ||
parts := strings.SplitN(s, "=", 2) | ||
// a.b.c becomes [a, b, c] | ||
keys := strings.Split(parts[0], ".") | ||
// xyz | ||
value := parts[1] | ||
|
||
// pointer to the root of the map, | ||
// as this map will contain nested maps, this pointer will be | ||
// updated to point to the root of the nested maps as it iterates | ||
// through the for loop | ||
p := m | ||
for i, k := range keys { | ||
// last key, put the value into the map | ||
if i == len(keys)-1 { | ||
p[k] = value | ||
continue | ||
} | ||
// if the nested map doesn't exist, create it | ||
if _, ok := p[k]; !ok { | ||
p[k] = map[string]any{} | ||
} | ||
// cast the key to a map[string]any | ||
// and update the pointer to point to it | ||
p = p[k].(map[string]any) | ||
} | ||
} | ||
|
||
return m | ||
} | ||
|
||
// FromYAMLFile converts a yaml file into a map[string]any. | ||
func FromYAMLFile(path string) (map[string]any, error) { | ||
if path == "" { | ||
return map[string]any{}, nil | ||
} | ||
|
||
raw, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read file %s: %w", path, err) | ||
} | ||
var m map[string]any | ||
if err := yaml.Unmarshal(raw, &m); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal file %s: %w", path, err) | ||
} | ||
// ensure we don't return `nil, nil` | ||
if m == nil { | ||
return map[string]any{}, nil | ||
} | ||
return m, nil | ||
} | ||
|
||
// ToYAML converts the m map into a yaml string. | ||
// E.g. map[string]any{"a" : 1, "b", 2} becomes | ||
// a: 1 | ||
// b: 2 | ||
func ToYAML(m map[string]any) (string, error) { | ||
raw, err := yaml.Marshal(m) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to marshal map: %w", err) | ||
} | ||
return string(raw), nil | ||
} | ||
|
||
// Merge merges the override map into the base map. | ||
// Modifying the base map in place. | ||
func Merge(base, override map[string]any) { | ||
for k, overrideVal := range override { | ||
if baseVal, ok := base[k]; ok { | ||
// both maps have this key | ||
baseChild, baseChildIsMap := baseVal.(map[string]any) | ||
overrideChild, overrideChildIsMap := overrideVal.(map[string]any) | ||
|
||
if baseChildIsMap && overrideChildIsMap { | ||
// both values are maps, recurse | ||
Merge(baseChild, overrideChild) | ||
} else { | ||
// override base with override | ||
base[k] = overrideVal | ||
} | ||
} else { | ||
// only override has this key | ||
base[k] = overrideVal | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
Is this going to cause any issues if the value contains
=
as in a base64 encoded string (e.g.DEADBEEF==
)? Would you mind adding a test case for thatThere 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.
No, this is why I am using
SplitN
with2
, so it will only split on the first=
.If the key was base64 encoded, then that would be a problem.