mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-10 16:42:51 +00:00
Delete some kunstruct code in favor of apimachinery code.
This commit is contained in:
@@ -20,11 +20,8 @@ package kunstruct
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
@@ -83,30 +80,10 @@ func (fs *UnstructAdapter) SetMap(m map[string]interface{}) {
|
||||
|
||||
// GetFieldValue returns value at the given fieldpath.
|
||||
func (fs *UnstructAdapter) GetFieldValue(path string) (string, error) {
|
||||
s, err := getFieldValue(fs.UnstructuredContent(), strings.Split(path, "."))
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "at path '%s'", path)
|
||||
s, found, err := unstructured.NestedString(
|
||||
fs.UnstructuredContent(), strings.Split(path, ".")...)
|
||||
if found || err != nil {
|
||||
return s, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func getFieldValue(m map[string]interface{}, path []string) (string, error) {
|
||||
if len(path) == 0 {
|
||||
return "", fmt.Errorf("%v not found", path)
|
||||
}
|
||||
v, ok := m[path[0]]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("no field named '%s'", path[0])
|
||||
}
|
||||
if len(path) == 1 {
|
||||
if s, ok := v.(string); ok {
|
||||
return s, nil
|
||||
}
|
||||
return "", fmt.Errorf("value at '%v' not a string", path[0])
|
||||
}
|
||||
if deeper, ok := v.(map[string]interface{}); ok {
|
||||
return getFieldValue(deeper, path[1:])
|
||||
}
|
||||
return "", fmt.Errorf("Expected map at %v, but got %s=%v",
|
||||
path[0], reflect.TypeOf(v), v)
|
||||
return "", fmt.Errorf("no field named '%s'", path)
|
||||
}
|
||||
|
||||
@@ -76,49 +76,49 @@ func TestGetFieldValue(t *testing.T) {
|
||||
name: "empty",
|
||||
pathToField: "",
|
||||
errorExpected: true,
|
||||
errorMsg: "at path '': no field named ''",
|
||||
errorMsg: "no field named ''",
|
||||
},
|
||||
{
|
||||
name: "emptyDotEmpty",
|
||||
pathToField: ".",
|
||||
errorExpected: true,
|
||||
errorMsg: "at path '.': no field named ''",
|
||||
errorMsg: "no field named '.'",
|
||||
},
|
||||
{
|
||||
name: "twoFieldsOneMissing",
|
||||
pathToField: "metadata.banana",
|
||||
errorExpected: true,
|
||||
errorMsg: "at path 'metadata.banana': no field named 'banana'",
|
||||
errorMsg: "no field named 'metadata.banana'",
|
||||
},
|
||||
{
|
||||
name: "deeperMissingField",
|
||||
pathToField: "this.is.aDeep.field.that.does.not.exist",
|
||||
errorExpected: true,
|
||||
errorMsg: "at path 'this.is.aDeep.field.that.does.not.exist': no field named 'aDeep'",
|
||||
errorMsg: "no field named 'this.is.aDeep.field.that.does.not.exist'",
|
||||
},
|
||||
{
|
||||
name: "emptyMap",
|
||||
pathToField: "this.is.anEmptyMap",
|
||||
errorExpected: true,
|
||||
errorMsg: "at path 'this.is.anEmptyMap': value at 'anEmptyMap' not a string",
|
||||
errorMsg: ".this.is.anEmptyMap accessor error: map[] is of the type map[string]interface {}, expected string",
|
||||
},
|
||||
{
|
||||
name: "numberAsValue",
|
||||
pathToField: "this.is.aNumber",
|
||||
errorExpected: true,
|
||||
errorMsg: "at path 'this.is.aNumber': value at 'aNumber' not a string",
|
||||
errorMsg: ".this.is.aNumber accessor error: 1000 is of the type int, expected string",
|
||||
},
|
||||
{
|
||||
name: "nilAsValue",
|
||||
pathToField: "this.is.aNilValue",
|
||||
errorExpected: true,
|
||||
errorMsg: "at path 'this.is.aNilValue': value at 'aNilValue' not a string",
|
||||
errorMsg: ".this.is.aNilValue accessor error: <nil> is of the type <nil>, expected string",
|
||||
},
|
||||
{
|
||||
name: "unrecognizable",
|
||||
pathToField: "this.is.unrecognizable.Name",
|
||||
errorExpected: true,
|
||||
errorMsg: "at path 'this.is.unrecognizable.Name': Expected map at unrecognizable, but got testing.InternalExample={fooBar <nil> false}",
|
||||
errorMsg: ".this.is.unrecognizable.Name accessor error: {fooBar <nil> false} is of the type testing.InternalExample, expected map[string]interface{}",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user