Skip to content
Permalink
Browse files

Handle arb length paths in GetFieldName

  • Loading branch information...
monopole committed Jun 8, 2018
1 parent 7c4ca21 commit de65503177337f9f3bf807f7ca3b9e3198db26aa
Showing with 46 additions and 39 deletions.
  1. +3 −5 pkg/app/application.go
  2. +14 −15 pkg/resource/resource.go
  3. +29 −19 pkg/resource/resource_test.go
@@ -22,7 +22,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"strings"

"github.com/ghodss/yaml"
"github.com/golang/glog"
@@ -316,10 +315,9 @@ func (a *applicationImpl) resolveRefVars(resources resmap.ResMap) (map[string]st
}

for _, refvar := range vars {
refGVKN := resource.NewResId(refvar.ObjRef.GroupVersionKind(), refvar.ObjRef.Name)
if r, found := resources[refGVKN]; found {
s, err := resource.GetFieldValue(
r.UnstructuredContent(), strings.Split(refvar.FieldRef.FieldPath, "."))
id := resource.NewResId(refvar.ObjRef.GroupVersionKind(), refvar.ObjRef.Name)
if r, found := resources[id]; found {
s, err := r.GetFieldValue(refvar.FieldRef.FieldPath)
if err != nil {
return nil, fmt.Errorf("failed to resolve referred var: %+v", refvar)
}
@@ -21,6 +21,8 @@ import (
"encoding/json"
"fmt"

"strings"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)
@@ -49,6 +51,13 @@ func NewBehaviorlessResource(u *unstructured.Unstructured) *Resource {
return &Resource{Unstructured: *u, b: BehaviorUnspecified}
}

// NewResource returns a new instance of Resource.
func NewResource(m map[string]interface{}) *Resource {
return &Resource{
Unstructured: unstructured.Unstructured{Object: m},
b: BehaviorUnspecified}
}

// Behavior returns the behavior for the resource.
func (r *Resource) Behavior() GenerationBehavior {
return r.b
@@ -94,21 +103,14 @@ func mergeStringMaps(maps ...map[string]string) map[string]string {
return result
}

func newUnstructuredFromObject(in runtime.Object) (*unstructured.Unstructured, error) {
marshaled, err := json.Marshal(in)
if err != nil {
return nil, err
}
var out unstructured.Unstructured
err = out.UnmarshalJSON(marshaled)
return &out, err
func (r *Resource) GetFieldValue(fieldPath string) (string, error) {
return getFieldValue(r.UnstructuredContent(), strings.Split(fieldPath, "."))
}

func GetFieldValue(m map[string]interface{}, pathToField []string) (string, error) {
func getFieldValue(m map[string]interface{}, pathToField []string) (string, error) {
if len(pathToField) == 0 {
return "", fmt.Errorf("Field not found")
}

if len(pathToField) == 1 {
if v, found := m[pathToField[0]]; found {
if s, ok := v.(string); ok {
@@ -118,13 +120,10 @@ func GetFieldValue(m map[string]interface{}, pathToField []string) (string, erro
}
return "", fmt.Errorf("field at given fieldpath does not exist")
}

curr, rest := pathToField[0], pathToField[1]

v := m[curr]
v := m[pathToField[0]]
switch typedV := v.(type) {
case map[string]interface{}:
return GetFieldValue(typedV, []string{rest})
return getFieldValue(typedV, pathToField[1:])
default:
return "", fmt.Errorf("%#v is not expected to be a primitive type", typedV)
}
@@ -20,46 +20,56 @@ import (
"testing"
)

func TestGetFieldAsString(t *testing.T) {
m := map[string]interface{}{
func TestGetFieldValue(t *testing.T) {
res := NewResource(map[string]interface{}{
"Kind": "Service",
"metadata": map[string]interface{}{
"labels": map[string]string{
"app": "application-name",
},
"name": "service-name",
},
}
"spec": map[string]interface{}{
"ports": map[string]interface{}{
"port": "80",
},
},
})

tests := []struct {
pathToField []string
expectedName string
expectedError bool
pathToField string
expectedValue string
errorExpected bool
}{
{
pathToField: []string{"Kind"},
expectedName: "Service",
expectedError: false,
pathToField: "Kind",
expectedValue: "Service",
errorExpected: false,
},
{
pathToField: "metadata.name",
expectedValue: "service-name",
errorExpected: false,
},
{
pathToField: []string{"metadata", "name"},
expectedName: "service-name",
expectedError: false,
pathToField: "metadata.non-existing-field",
expectedValue: "",
errorExpected: true,
},
{
pathToField: []string{"metadata", "non-existing-field"},
expectedName: "",
expectedError: true,
pathToField: "spec.ports.port",
expectedValue: "80",
errorExpected: false,
},
}

for _, test := range tests {
s, err := GetFieldValue(m, test.pathToField)
if test.expectedError && err == nil {
s, err := res.GetFieldValue(test.pathToField)
if test.errorExpected && err == nil {
t.Fatalf("should return error, but no error returned")
} else {
if test.expectedName != s {
t.Fatalf("Got:%s expected:%s", s, test.expectedName)
if test.expectedValue != s {
t.Fatalf("Got:%s expected:%s", s, test.expectedValue)
}
}
}

0 comments on commit de65503

Please sign in to comment.
You can’t perform that action at this time.