-
Notifications
You must be signed in to change notification settings - Fork 57
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
Added support for complex variables #1467
Changes from 5 commits
f8b286b
f8b7d64
26d1d5a
4503657
aff20cf
f5d7127
d8f50f2
f7eba5f
443b338
ddd14eb
0c4bf71
44b17cd
95c0032
59dba11
327d05e
2e3c97b
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 |
---|---|---|
|
@@ -2,12 +2,27 @@ package variable | |
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
// We are using `any` because since introduction of complex variables, | ||
// variables can be of any type. | ||
// Type alias is used to make it easier to understand the code. | ||
type VariableValue = any | ||
andrewnester marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
type VariableType string | ||
|
||
const ( | ||
VariableTypeComplex VariableType = "complex" | ||
) | ||
|
||
// An input variable for the bundle config | ||
type Variable struct { | ||
// A type of the variable. This is used to validate the value of the variable | ||
Type VariableType `json:"type,omitempty"` | ||
|
||
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. Would we want to constrain the type of primitive types as well? And otherwise, could we get away with not storing this at all? 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. We indeed could get away with not having this but there are few benefits of having this
|
||
// A default value which then makes the variable optional | ||
Default *string `json:"default,omitempty"` | ||
Default VariableValue `json:"default,omitempty"` | ||
|
||
// Documentation for this input variable | ||
Description string `json:"description,omitempty"` | ||
|
@@ -21,7 +36,7 @@ type Variable struct { | |
// 4. Default value defined in variable definition | ||
// 5. Throw error, since if no default value is defined, then the variable | ||
// is required | ||
Value *string `json:"value,omitempty" bundle:"readonly"` | ||
Value VariableValue `json:"value,omitempty" bundle:"readonly"` | ||
|
||
// The value of this field will be used to lookup the resource by name | ||
// And assign the value of the variable to ID of the resource found. | ||
|
@@ -39,10 +54,24 @@ func (v *Variable) HasValue() bool { | |
return v.Value != nil | ||
} | ||
|
||
func (v *Variable) Set(val string) error { | ||
func (v *Variable) Set(val VariableValue) error { | ||
if v.HasValue() { | ||
return fmt.Errorf("variable has already been assigned value: %s", *v.Value) | ||
return fmt.Errorf("variable has already been assigned value: %s", v.Value) | ||
} | ||
v.Value = &val | ||
|
||
rv := reflect.ValueOf(val) | ||
switch rv.Kind() { | ||
case reflect.Struct, reflect.Array, reflect.Slice, reflect.Map: | ||
if v.Type != VariableTypeComplex { | ||
return fmt.Errorf("variable type is not complex") | ||
} | ||
} | ||
|
||
v.Value = val | ||
|
||
return nil | ||
} | ||
|
||
func (v *Variable) IsComplex() bool { | ||
return v.Type == VariableTypeComplex | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package config_tests | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/databricks/cli/bundle" | ||
"github.com/databricks/cli/bundle/config/mutator" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestComplexVariables(t *testing.T) { | ||
andrewnester marked this conversation as resolved.
Show resolved
Hide resolved
|
||
b, diags := loadTargetWithDiags("variables/complex", "default") | ||
require.Empty(t, diags) | ||
|
||
diags = bundle.Apply(context.Background(), b, bundle.Seq( | ||
mutator.SetVariables(), | ||
mutator.ResolveVariableReferences( | ||
"variables", | ||
), | ||
andrewnester marked this conversation as resolved.
Show resolved
Hide resolved
|
||
)) | ||
require.NoError(t, diags.Error()) | ||
|
||
require.Equal(t, "13.2.x-scala2.11", b.Config.Resources.Jobs["my_job"].JobClusters[0].NewCluster.SparkVersion) | ||
require.Equal(t, 2, b.Config.Resources.Jobs["my_job"].JobClusters[0].NewCluster.NumWorkers) | ||
require.Equal(t, "true", b.Config.Resources.Jobs["my_job"].JobClusters[0].NewCluster.SparkConf["spark.speculation"]) | ||
} | ||
|
||
func TestComplexVariablesOverride(t *testing.T) { | ||
b, diags := loadTargetWithDiags("variables/complex", "dev") | ||
require.Empty(t, diags) | ||
|
||
diags = bundle.Apply(context.Background(), b, bundle.Seq( | ||
mutator.SetVariables(), | ||
mutator.ResolveVariableReferences( | ||
"variables", | ||
), | ||
)) | ||
require.NoError(t, diags.Error()) | ||
|
||
require.Equal(t, "14.2.x-scala2.11", b.Config.Resources.Jobs["my_job"].JobClusters[0].NewCluster.SparkVersion) | ||
require.Equal(t, 4, b.Config.Resources.Jobs["my_job"].JobClusters[0].NewCluster.NumWorkers) | ||
require.Equal(t, "false", b.Config.Resources.Jobs["my_job"].JobClusters[0].NewCluster.SparkConf["spark.speculation"]) | ||
} |
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.
Does the PR include test coverage for this, i.e. both ways of setting the variable, as well as defining a top level complex variable and then specifying a different type in the override?
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.
Can you TAL at this?