mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Merge pull request #467 from monopole/deleteSomeKunstructCode
Delete some kunstruct code in favor of apimachinery code.
This commit is contained in:
@@ -20,11 +20,8 @@ package kunstruct
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
@@ -83,30 +80,10 @@ func (fs *UnstructAdapter) SetMap(m map[string]interface{}) {
|
|||||||
|
|
||||||
// GetFieldValue returns value at the given fieldpath.
|
// GetFieldValue returns value at the given fieldpath.
|
||||||
func (fs *UnstructAdapter) GetFieldValue(path string) (string, error) {
|
func (fs *UnstructAdapter) GetFieldValue(path string) (string, error) {
|
||||||
s, err := getFieldValue(fs.UnstructuredContent(), strings.Split(path, "."))
|
s, found, err := unstructured.NestedString(
|
||||||
if err != nil {
|
fs.UnstructuredContent(), strings.Split(path, ".")...)
|
||||||
return "", errors.Wrapf(err, "at path '%s'", path)
|
if found || err != nil {
|
||||||
|
return s, err
|
||||||
}
|
}
|
||||||
return s, nil
|
return "", fmt.Errorf("no field named '%s'", path)
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,49 +76,49 @@ func TestGetFieldValue(t *testing.T) {
|
|||||||
name: "empty",
|
name: "empty",
|
||||||
pathToField: "",
|
pathToField: "",
|
||||||
errorExpected: true,
|
errorExpected: true,
|
||||||
errorMsg: "at path '': no field named ''",
|
errorMsg: "no field named ''",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "emptyDotEmpty",
|
name: "emptyDotEmpty",
|
||||||
pathToField: ".",
|
pathToField: ".",
|
||||||
errorExpected: true,
|
errorExpected: true,
|
||||||
errorMsg: "at path '.': no field named ''",
|
errorMsg: "no field named '.'",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "twoFieldsOneMissing",
|
name: "twoFieldsOneMissing",
|
||||||
pathToField: "metadata.banana",
|
pathToField: "metadata.banana",
|
||||||
errorExpected: true,
|
errorExpected: true,
|
||||||
errorMsg: "at path 'metadata.banana': no field named 'banana'",
|
errorMsg: "no field named 'metadata.banana'",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "deeperMissingField",
|
name: "deeperMissingField",
|
||||||
pathToField: "this.is.aDeep.field.that.does.not.exist",
|
pathToField: "this.is.aDeep.field.that.does.not.exist",
|
||||||
errorExpected: true,
|
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",
|
name: "emptyMap",
|
||||||
pathToField: "this.is.anEmptyMap",
|
pathToField: "this.is.anEmptyMap",
|
||||||
errorExpected: true,
|
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",
|
name: "numberAsValue",
|
||||||
pathToField: "this.is.aNumber",
|
pathToField: "this.is.aNumber",
|
||||||
errorExpected: true,
|
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",
|
name: "nilAsValue",
|
||||||
pathToField: "this.is.aNilValue",
|
pathToField: "this.is.aNilValue",
|
||||||
errorExpected: true,
|
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",
|
name: "unrecognizable",
|
||||||
pathToField: "this.is.unrecognizable.Name",
|
pathToField: "this.is.unrecognizable.Name",
|
||||||
errorExpected: true,
|
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