This commit enhances the UnstructAdapter

* Added support for arbitrary data types rather than just strings
* Added support for integer index-able arrays
* Improve code coverage for kunstruct
  - kunstruct around 90%
  - helper at 100%
* Update expansion.Expand method to preserve the original type of the variable
* Ensure the int field such .spec.replicas can be used
  as a source in a first Deployment or as destination of a variable (in the
  second Deployment variable).
This commit is contained in:
Ian Howell
2019-06-13 15:32:58 -05:00
committed by Jerome Brette
parent 0dbe78149d
commit ed03818e20
14 changed files with 1203 additions and 108 deletions

View File

@@ -24,17 +24,17 @@ import (
)
type RefVarTransformer struct {
varMap map[string]string
varMap map[string]interface{}
replacementCounts map[string]int
fieldSpecs []config.FieldSpec
mappingFunc func(string) string
mappingFunc func(string) interface{}
}
// NewRefVarTransformer returns a new RefVarTransformer
// that replaces $(VAR) style variables with values.
// The fieldSpecs are the places to look for occurrences of $(VAR).
func NewRefVarTransformer(
varMap map[string]string, fs []config.FieldSpec) *RefVarTransformer {
varMap map[string]interface{}, fs []config.FieldSpec) *RefVarTransformer {
return &RefVarTransformer{
varMap: varMap,
fieldSpecs: fs,
@@ -48,7 +48,7 @@ func NewRefVarTransformer(
func (rv *RefVarTransformer) replaceVars(in interface{}) (interface{}, error) {
switch vt := in.(type) {
case []interface{}:
var xs []string
var xs []interface{}
for _, a := range in.([]interface{}) {
xs = append(xs, expansion.Expand(a.(string), rv.mappingFunc))
}
@@ -59,16 +59,26 @@ func (rv *RefVarTransformer) replaceVars(in interface{}) (interface{}, error) {
for k, v := range inMap {
s, ok := v.(string)
if !ok {
return nil, fmt.Errorf("%#v is expected to be %T", v, s)
// This field not contain a $(VAR) since it is not
// of string type. For instance .spec.replicas: 3 in
// a Deployment object
xs[k] = v
} else {
// This field can potentially contains a $(VAR) since it is
// of string type. For instance .spec.replicas: $(REPLICAS)
// in a Deployment object
xs[k] = expansion.Expand(s, rv.mappingFunc)
}
xs[k] = expansion.Expand(s, rv.mappingFunc)
}
return xs, nil
case interface{}:
s, ok := in.(string)
if !ok {
return nil, fmt.Errorf("%#v is expected to be %T", in, s)
// This field not contain a $(VAR) since it is not of string type.
return in, nil
}
// This field can potentially contain a $(VAR) since it is
// of string type.
return expansion.Expand(s, rv.mappingFunc), nil
case nil:
return nil, nil