Add kunstruct impl of Get/SetDataMap, replace Resource.Merge

This commit is contained in:
jregan
2020-12-21 07:22:03 -08:00
parent 35087ed0cc
commit 735befef19
10 changed files with 280 additions and 48 deletions

View File

@@ -7,6 +7,7 @@ package kunstruct
import (
"encoding/json"
"fmt"
"log"
jsonpatch "github.com/evanphx/json-patch"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -249,6 +250,29 @@ func (fs *UnstructAdapter) GetStringMap(path string) (map[string]string, error)
return nil, NoFieldError{Field: path}
}
func (fs *UnstructAdapter) GetDataMap() map[string]string {
m, err := fs.GetStringMap("data")
if err != nil {
return map[string]string{}
}
return m
}
func (fs *UnstructAdapter) SetDataMap(m map[string]string) {
if m == nil {
unstructured.RemoveNestedField(fs.Object, "data")
return
}
s := make(map[string]interface{}, len(m))
for i, v := range m {
s[i] = v
}
err := unstructured.SetNestedMap(fs.Object, s, "data")
if err != nil {
log.Fatal(err)
}
}
// GetMap returns value at the given fieldpath.
func (fs *UnstructAdapter) GetMap(path string) (map[string]interface{}, error) {
content, fields, found, err := fs.selectSubtree(path)

View File

@@ -6,6 +6,8 @@ package kunstruct
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
var kunstructured = NewKunstructuredFactoryImpl().FromMap(map[string]interface{}{
@@ -557,3 +559,139 @@ func compareValues(t *testing.T, name string, pathToField string, expectedValue
t.Logf("%T value at `%s`", typedV, pathToField)
}
}
func TestKunstGetDataMap(t *testing.T) {
emptyMap := map[string]string{}
testCases := map[string]struct {
theMap map[string]interface{}
expected map[string]string
}{
"actuallyNil": {
theMap: nil,
expected: emptyMap,
},
"empty": {
theMap: map[string]interface{}{},
expected: emptyMap,
},
"mostlyEmpty": {
theMap: map[string]interface{}{
"hey": "there",
},
expected: emptyMap,
},
"noNameConfigMap": {
theMap: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
},
expected: emptyMap,
},
"configMap": {
theMap: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "winnie",
},
"data": map[string]interface{}{
"wine": "cabernet",
"truck": "ford",
"rocket": "falcon9",
"planet": "mars",
"city": "brownsville",
},
},
// order irrelevant, because assert.Equals is smart about maps.
expected: map[string]string{
"city": "brownsville",
"wine": "cabernet",
"planet": "mars",
"rocket": "falcon9",
"truck": "ford",
},
},
}
for n := range testCases {
tc := testCases[n]
t.Run(n, func(t *testing.T) {
kunstr := NewKunstructuredFactoryImpl().FromMap(tc.theMap)
m := kunstr.GetDataMap()
if !assert.Equal(t, tc.expected, m) {
t.FailNow()
}
})
}
}
func TestKunstSetDataMap(t *testing.T) {
testCases := map[string]struct {
theMap map[string]interface{}
input map[string]string
expected map[string]string
}{
"empty": {
theMap: map[string]interface{}{},
input: map[string]string{
"wine": "cabernet",
"truck": "ford",
},
expected: map[string]string{
"wine": "cabernet",
"truck": "ford",
},
},
"replace": {
theMap: map[string]interface{}{
"foo": 3,
"data": map[string]string{
"rocket": "falcon9",
"planet": "mars",
},
},
input: map[string]string{
"wine": "cabernet",
"truck": "ford",
},
expected: map[string]string{
"wine": "cabernet",
"truck": "ford",
},
},
"clear1": {
theMap: map[string]interface{}{
"foo": 3,
"data": map[string]string{
"rocket": "falcon9",
"planet": "mars",
},
},
input: map[string]string{},
expected: map[string]string{},
},
"clear2": {
theMap: map[string]interface{}{
"foo": 3,
"data": map[string]string{
"rocket": "falcon9",
"planet": "mars",
},
},
input: nil,
expected: map[string]string{},
},
}
for n := range testCases {
tc := testCases[n]
t.Run(n, func(t *testing.T) {
kunstr := NewKunstructuredFactoryImpl().FromMap(tc.theMap)
kunstr.SetDataMap(tc.input)
m := kunstr.GetDataMap()
if !assert.Equal(t, tc.expected, m) {
t.FailNow()
}
})
}
}