-
Notifications
You must be signed in to change notification settings - Fork 177
Fixed variable override in target with full variable syntax #1749
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 1 commit
9298634
e144def
e884a3d
1f1ef8c
dae3c8f
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 |
|---|---|---|
|
|
@@ -406,6 +406,22 @@ func (r *Root) MergeTargetOverrides(name string) error { | |
| return r.updateWithDynamicValue(root) | ||
| } | ||
|
|
||
| var variableKeywords = []string{"default", "lookup"} | ||
|
|
||
| // isFullVariableDef checks if the given value is a variable definition. | ||
| // A variable definition is a map with one of the following | ||
| // keys: "default", "value", "lookup". | ||
| func isFullVariableDef(v dyn.Value) bool { | ||
| for _, keyword := range variableKeywords { | ||
| vv, err := dyn.Get(v, keyword) | ||
| if err == nil && vv.Kind() != dyn.KindInvalid { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| // rewriteShorthands performs lightweight rewriting of the configuration | ||
| // tree where we allow users to write a shorthand and must rewrite to the full form. | ||
| func rewriteShorthands(v dyn.Value) (dyn.Value, error) { | ||
|
|
@@ -433,30 +449,29 @@ func rewriteShorthands(v dyn.Value) (dyn.Value, error) { | |
| }, variable.Locations()), nil | ||
|
|
||
| case dyn.KindMap, dyn.KindSequence: | ||
| lookup, err := dyn.Get(variable, "lookup") | ||
| // If lookup is set, we don't want to rewrite the variable and return it as is. | ||
| if err == nil && lookup.Kind() != dyn.KindInvalid { | ||
| return variable, nil | ||
| } | ||
|
|
||
| // Check if the original definition of variable has a type field. | ||
| // If it has a type field, it means the shorthand is a value of a complex type. | ||
| // Type might not be found if the variable overriden in a separate file | ||
| // and configuration is not merged yet. | ||
| typeV, err := dyn.GetByPath(v, p.Append(dyn.Key("type"))) | ||
| if err != nil { | ||
| return dyn.NewValue(map[string]dyn.Value{ | ||
| "default": variable, | ||
| }, variable.Locations()), nil | ||
| if err == nil && typeV.MustString() == "complex" { | ||
| if typeV.MustString() == "complex" { | ||
|
andrewnester marked this conversation as resolved.
Outdated
|
||
| return dyn.NewValue(map[string]dyn.Value{ | ||
| "type": typeV, | ||
| "default": variable, | ||
| }, variable.Locations()), nil | ||
|
Contributor
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. This is the only way to override for complex variables?
Contributor
Author
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. Good catch, we support both shorthand and full one. Ambiguity can be if someone defined a complex variable and overrides it with Then we can't really understand if it's shorthand or full syntax |
||
| } | ||
| } | ||
|
|
||
| if typeV.MustString() == "complex" { | ||
| return dyn.NewValue(map[string]dyn.Value{ | ||
| "type": typeV, | ||
| "default": variable, | ||
| }, variable.Locations()), nil | ||
| // If it's a full variable definition, leave it as is. | ||
| if isFullVariableDef(variable) { | ||
| return variable, nil | ||
| } | ||
|
|
||
| return variable, nil | ||
| // If it's a shorthand, rewrite it to a full variable definition. | ||
| return dyn.NewValue(map[string]dyn.Value{ | ||
| "default": variable, | ||
| }, variable.Locations()), nil | ||
|
|
||
| default: | ||
| return variable, nil | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.