Skip to content

Commit

Permalink
Alias variables block in the Target struct (#1748)
Browse files Browse the repository at this point in the history
## Changes
This PR aliases and overrides the schema associated with the variables
block in `target` to allow for directly specifying a variable value in
the JSON schema (without an levels of nesting). This is needed because
this direct value is resolved by dynamically parsing the configuration
tree.

https://github.com/databricks/cli/blob/ca6332a5a4325aff1be848536f45d13bd74d93b3/bundle/config/root.go#L424

## Tests
Existing unit tests.
  • Loading branch information
shreyas-goenka authored Sep 10, 2024
1 parent 28b39cd commit 5d2c0e3
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bundle/config/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestRootMergeTargetOverridesWithVariables(t *testing.T) {
},
Targets: map[string]*Target{
"development": {
Variables: map[string]*variable.Variable{
Variables: map[string]*variable.TargetVariable{
"foo": {
Default: "bar",
Description: "wrong",
Expand Down
21 changes: 20 additions & 1 deletion bundle/config/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,26 @@ type Target struct {
// Override default values or lookup name for defined variables
// Does not permit defining new variables or redefining existing ones
// in the scope of an target
Variables map[string]*variable.Variable `json:"variables,omitempty"`
//
// There are two valid ways to define a variable override in a target:
// 1. Direct value override. We normalize this to the variable.Variable
// struct format when loading the configuration YAML:
//
// variables:
// foo: "value"
//
// 2. Override matching the variable.Variable struct.
//
// variables:
// foo:
// default: "value"
//
// OR
//
// variables:
// foo:
// lookup: "resource_name"
Variables map[string]*variable.TargetVariable `json:"variables,omitempty"`

Git Git `json:"git,omitempty"`

Expand Down
5 changes: 5 additions & 0 deletions bundle/config/variable/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ const (
VariableTypeComplex VariableType = "complex"
)

// We alias it here to override the JSON schema associated with a variable value
// in a target override. This is because we allow for directly specifying the value
// in addition to the variable.Variable struct format in a target override.
type TargetVariable Variable

// 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
Expand Down
16 changes: 16 additions & 0 deletions bundle/internal/schema/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ func addInterpolationPatterns(typ reflect.Type, s jsonschema.Schema) jsonschema.
return s
}

// The variables block in a target override allows for directly specifying
// the value of the variable.
if typ == reflect.TypeOf(variable.TargetVariable{}) {
return jsonschema.Schema{
AnyOf: []jsonschema.Schema{
// We keep the original schema so that autocomplete suggestions
// continue to work.
s,
// All values are valid for a variable value, be it primitive types
// like string/bool or complex ones like objects/arrays. Thus we override
// the schema to allow all valid JSON values.
{},
},
}
}

switch s.Type {
case jsonschema.ArrayType, jsonschema.ObjectType:
// arrays and objects can have complex variable values specified.
Expand Down
39 changes: 38 additions & 1 deletion bundle/schema/jsonschema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5d2c0e3

Please sign in to comment.