-
Notifications
You must be signed in to change notification settings - Fork 313
feat: Allow yaml.SequenceNode type #1208
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
Changes from 8 commits
b146ed7
041ddb8
5ec51ee
5c6212f
2d8fa82
8d944e3
c434ba0
98c753c
c43b52d
c97f80a
646ea60
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ import ( | |
| "context" | ||
| "fmt" | ||
| "path/filepath" | ||
| "regexp" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
| "text/template" | ||
|
|
@@ -632,15 +634,45 @@ func setHelmValue(currentValues *yaml.Node, key string, value interface{}) error | |
|
|
||
| var err error | ||
| keys := strings.Split(key, ".") | ||
|
|
||
| // any-string[1] | ||
| pattern := `^(.*)\[(.*)\]$` | ||
| re := regexp.MustCompile(pattern) | ||
|
||
| for i, k := range keys { | ||
| if idx, found := findHelmValuesKey(current, k); found { | ||
| // pointer is needed to determine that the id has indeed been passed. | ||
| var idPtr *int | ||
| // by default, the search is based on the key without changes, but | ||
| // if string matches pattern, we consider it is an id in YAML list. | ||
| key := k | ||
| matches := re.FindStringSubmatch(k) | ||
| if matches != nil { | ||
| idStr := matches[2] | ||
| id, err := strconv.Atoi(idStr) | ||
| if err != nil { | ||
| return fmt.Errorf("id \"%s\" in yaml array must match pattern ^(.*)\\[(.*)\\]$", idStr) | ||
| } | ||
| idPtr = &id | ||
| key = matches[1] | ||
| } | ||
| if idx, found := findHelmValuesKey(current, key); found { | ||
| // Navigate deeper into the map | ||
| current = (*current).Content[idx] | ||
| // unpack one level of alias; an alias of an alias is not supported | ||
| if current.Kind == yaml.AliasNode { | ||
| current = current.Alias | ||
| } | ||
| if current.Kind != yaml.SequenceNode && idPtr != nil { | ||
| return fmt.Errorf("id %d provided when \"%s\" is not an yaml array", *idPtr, key) | ||
| } | ||
| if current.Kind == yaml.SequenceNode { | ||
| if idPtr == nil { | ||
| return fmt.Errorf("no id provided for yaml array \"%s\"", key) | ||
| } | ||
| currentContent := (*current).Content | ||
| if *idPtr < 0 || *idPtr >= len(currentContent) { | ||
| return fmt.Errorf("id %d is out of range [0, %d)", *idPtr, len(currentContent)) | ||
| } | ||
| current = (*current).Content[*idPtr] | ||
| } | ||
| if i == len(keys)-1 { | ||
| // If we're at the final key, set the value and return | ||
| if current.Kind == yaml.ScalarNode { | ||
|
|
||
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.
we may want to use a more specific pattern to avoid invalid values like
foo[x]:Uh oh!
There was an error while loading. Please reload this page.
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.
The original version looked like this, but it makes it harder to return a clear error like this . The problem is that the search is based on the key. If it doesn't fit the pattern, then the search will follow it entirely, which means it will fall into the else block and simply write this value. Should I take a different approach, such as checking the validity of the strings and parsing them separately?
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.
It's a small difference, in that case, let's just keep what you have now.