Alias variables block in the `Target` struct (#1748)

## 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.

ca6332a5a4/bundle/config/root.go (L424)

## Tests
Existing unit tests.
This commit is contained in:
shreyas-goenka 2024-09-10 20:19:34 +05:30 committed by GitHub
parent 28b39cd3f7
commit 5d2c0e3885
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 80 additions and 3 deletions

View File

@ -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",

View File

@ -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"`

View File

@ -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

View File

@ -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.

View File

@ -642,6 +642,29 @@
}
]
},
"variable.TargetVariable": {
"anyOf": [
{
"type": "object",
"properties": {
"default": {
"$ref": "#/$defs/interface"
},
"description": {
"$ref": "#/$defs/string"
},
"lookup": {
"$ref": "#/$defs/github.com/databricks/cli/bundle/config/variable.Lookup"
},
"type": {
"$ref": "#/$defs/github.com/databricks/cli/bundle/config/variable.VariableType"
}
},
"additionalProperties": false
},
{}
]
},
"variable.Variable": {
"type": "object",
"properties": {
@ -995,7 +1018,7 @@
"$ref": "#/$defs/github.com/databricks/cli/bundle/config.Sync"
},
"variables": {
"$ref": "#/$defs/map/github.com/databricks/cli/bundle/config/variable.Variable"
"$ref": "#/$defs/map/github.com/databricks/cli/bundle/config/variable.TargetVariable"
},
"workspace": {
"$ref": "#/$defs/github.com/databricks/cli/bundle/config.Workspace"
@ -4993,6 +5016,20 @@
}
]
},
"variable.TargetVariable": {
"anyOf": [
{
"type": "object",
"additionalProperties": {
"$ref": "#/$defs/github.com/databricks/cli/bundle/config/variable.TargetVariable"
}
},
{
"type": "string",
"pattern": "\\$\\{(var(\\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\[[0-9]+\\])*)+)\\}"
}
]
},
"variable.Variable": {
"anyOf": [
{