2023-05-15 09:34:05 +00:00
|
|
|
package variable
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-06-26 10:25:32 +00:00
|
|
|
"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
|
|
|
|
|
|
|
|
type VariableType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
VariableTypeComplex VariableType = "complex"
|
2023-05-15 09:34:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// An input variable for the bundle config
|
|
|
|
type Variable struct {
|
2024-06-26 10:25:32 +00:00
|
|
|
// A type of the variable. This is used to validate the value of the variable
|
|
|
|
Type VariableType `json:"type,omitempty"`
|
|
|
|
|
2023-05-15 09:34:05 +00:00
|
|
|
// A default value which then makes the variable optional
|
2024-06-26 10:25:32 +00:00
|
|
|
Default VariableValue `json:"default,omitempty"`
|
2023-05-15 09:34:05 +00:00
|
|
|
|
|
|
|
// Documentation for this input variable
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
|
|
|
|
// This field stores the resolved value for the variable. The variable are
|
|
|
|
// resolved in the following priority order (from highest to lowest)
|
|
|
|
//
|
|
|
|
// 1. Command line flag. For example: `--var="foo=bar"`
|
2023-08-17 15:22:32 +00:00
|
|
|
// 2. Target variable. eg: BUNDLE_VAR_foo=bar
|
2023-05-15 12:07:18 +00:00
|
|
|
// 3. Default value as defined in the applicable environments block
|
|
|
|
// 4. Default value defined in variable definition
|
|
|
|
// 5. Throw error, since if no default value is defined, then the variable
|
2023-05-15 09:34:05 +00:00
|
|
|
// is required
|
2024-06-26 10:25:32 +00:00
|
|
|
Value VariableValue `json:"value,omitempty" bundle:"readonly"`
|
2024-01-04 21:04:42 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
Lookup *Lookup `json:"lookup,omitempty"`
|
2023-05-15 09:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// True if the variable has been assigned a default value. Variables without a
|
|
|
|
// a default value are by defination required
|
|
|
|
func (v *Variable) HasDefault() bool {
|
|
|
|
return v.Default != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// True if variable has already been assigned a value
|
|
|
|
func (v *Variable) HasValue() bool {
|
|
|
|
return v.Value != nil
|
|
|
|
}
|
|
|
|
|
2024-06-26 10:25:32 +00:00
|
|
|
func (v *Variable) Set(val VariableValue) error {
|
2023-05-15 09:34:05 +00:00
|
|
|
if v.HasValue() {
|
2024-06-26 10:25:32 +00:00
|
|
|
return fmt.Errorf("variable has already been assigned value: %s", v.Value)
|
2023-05-15 09:34:05 +00:00
|
|
|
}
|
2024-06-26 10:25:32 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-05-15 09:34:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-06-26 10:25:32 +00:00
|
|
|
|
|
|
|
func (v *Variable) IsComplex() bool {
|
|
|
|
return v.Type == VariableTypeComplex
|
|
|
|
}
|