mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Improve robutness of helper code
As per request, changed usage of pointer to int into plain int. Use -1 value where nil use to be used.
This commit is contained in:
@@ -31,7 +31,11 @@ import (
|
|||||||
// field of the first item in the foo list
|
// field of the first item in the foo list
|
||||||
type PathSection struct {
|
type PathSection struct {
|
||||||
fields []string
|
fields []string
|
||||||
idx *int
|
idx int
|
||||||
|
}
|
||||||
|
|
||||||
|
func newPathSection() PathSection {
|
||||||
|
return PathSection{idx: -1}
|
||||||
}
|
}
|
||||||
|
|
||||||
func appendNonEmpty(section *PathSection, field string) {
|
func appendNonEmpty(section *PathSection, field string) {
|
||||||
@@ -41,7 +45,7 @@ func appendNonEmpty(section *PathSection, field string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseFields(path string) ([]PathSection, error) {
|
func parseFields(path string) ([]PathSection, error) {
|
||||||
section := PathSection{}
|
section := newPathSection()
|
||||||
sectionset := []PathSection{}
|
sectionset := []PathSection{}
|
||||||
if !strings.Contains(path, "[") {
|
if !strings.Contains(path, "[") {
|
||||||
section.fields = strings.Split(path, ".")
|
section.fields = strings.Split(path, ".")
|
||||||
@@ -75,9 +79,9 @@ func parseFields(path string) ([]PathSection, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("invalid index %s", path)
|
return nil, fmt.Errorf("invalid index %s", path)
|
||||||
}
|
}
|
||||||
section.idx = &tmpIdx
|
section.idx = tmpIdx
|
||||||
sectionset = append(sectionset, section)
|
sectionset = append(sectionset, section)
|
||||||
section = PathSection{}
|
section = newPathSection()
|
||||||
|
|
||||||
start = i + 1
|
start = i + 1
|
||||||
insideParentheses = false
|
insideParentheses = false
|
||||||
|
|||||||
@@ -23,20 +23,15 @@ import (
|
|||||||
|
|
||||||
type PathSectionSlice []PathSection
|
type PathSectionSlice []PathSection
|
||||||
|
|
||||||
func buildPath(idx *int, fields ...string) PathSectionSlice {
|
func buildPath(idx int, fields ...string) PathSectionSlice {
|
||||||
return PathSectionSlice{PathSection{fields: fields, idx: idx}}
|
return PathSectionSlice{PathSection{fields: fields, idx: idx}}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a PathSectionSlice) addSection(idx *int, fields ...string) PathSectionSlice {
|
func (a PathSectionSlice) addSection(idx int, fields ...string) PathSectionSlice {
|
||||||
return append(a, PathSection{fields: fields, idx: idx})
|
return append(a, PathSection{fields: fields, idx: idx})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseField(t *testing.T) {
|
func TestParseField(t *testing.T) {
|
||||||
i := 1
|
|
||||||
var one *int = &i
|
|
||||||
j := 0
|
|
||||||
var zero *int = &j
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
pathToField string
|
pathToField string
|
||||||
@@ -47,55 +42,55 @@ func TestParseField(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "oneField",
|
name: "oneField",
|
||||||
pathToField: "Kind",
|
pathToField: "Kind",
|
||||||
expectedValue: buildPath(nil, "Kind"),
|
expectedValue: buildPath(-1, "Kind"),
|
||||||
errorExpected: false,
|
errorExpected: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "twoFields",
|
name: "twoFields",
|
||||||
pathToField: "metadata.name",
|
pathToField: "metadata.name",
|
||||||
expectedValue: buildPath(nil, "metadata", "name"),
|
expectedValue: buildPath(-1, "metadata", "name"),
|
||||||
errorExpected: false,
|
errorExpected: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "threeFields",
|
name: "threeFields",
|
||||||
pathToField: "spec.ports.port",
|
pathToField: "spec.ports.port",
|
||||||
expectedValue: buildPath(nil, "spec", "ports", "port"),
|
expectedValue: buildPath(-1, "spec", "ports", "port"),
|
||||||
errorExpected: false,
|
errorExpected: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "empty",
|
name: "empty",
|
||||||
pathToField: "",
|
pathToField: "",
|
||||||
expectedValue: buildPath(nil, ""),
|
expectedValue: buildPath(-1, ""),
|
||||||
errorExpected: false,
|
errorExpected: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "validStringIndex",
|
name: "validStringIndex",
|
||||||
pathToField: "that[1]",
|
pathToField: "that[1]",
|
||||||
expectedValue: buildPath(one, "that"),
|
expectedValue: buildPath(1, "that"),
|
||||||
errorExpected: false,
|
errorExpected: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "sliceInSlice",
|
name: "sliceInSlice",
|
||||||
pathToField: "that[1][0]",
|
pathToField: "that[1][0]",
|
||||||
expectedValue: buildPath(one, "that").addSection(zero),
|
expectedValue: buildPath(1, "that").addSection(0),
|
||||||
errorExpected: false,
|
errorExpected: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "validStructSubField",
|
name: "validStructSubField",
|
||||||
pathToField: "those[1].field2",
|
pathToField: "those[1].field2",
|
||||||
expectedValue: buildPath(one, "those").addSection(nil, "field2"),
|
expectedValue: buildPath(1, "those").addSection(-1, "field2"),
|
||||||
errorExpected: false,
|
errorExpected: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "validStructSubFieldIndex",
|
name: "validStructSubFieldIndex",
|
||||||
pathToField: "these[1].field2[0]",
|
pathToField: "these[1].field2[0]",
|
||||||
expectedValue: buildPath(one, "these").addSection(zero, "field2"),
|
expectedValue: buildPath(1, "these").addSection(0, "field2"),
|
||||||
errorExpected: false,
|
errorExpected: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "validStructSubFieldIndexSubfield",
|
name: "validStructSubFieldIndexSubfield",
|
||||||
pathToField: "complextree[1].field2[1].stringsubfield",
|
pathToField: "complextree[1].field2[1].stringsubfield",
|
||||||
expectedValue: buildPath(one, "complextree").addSection(one, "field2").addSection(nil, "stringsubfield"),
|
expectedValue: buildPath(1, "complextree").addSection(1, "field2").addSection(-1, "stringsubfield"),
|
||||||
errorExpected: false,
|
errorExpected: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -119,7 +114,7 @@ func TestParseField(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "validFieldsWithQuotes",
|
name: "validFieldsWithQuotes",
|
||||||
pathToField: "'complextree'[1].field2[1].'stringsubfield'",
|
pathToField: "'complextree'[1].field2[1].'stringsubfield'",
|
||||||
expectedValue: buildPath(one, "complextree").addSection(one, "field2").addSection(nil, "stringsubfield"),
|
expectedValue: buildPath(1, "complextree").addSection(1, "field2").addSection(-1, "stringsubfield"),
|
||||||
errorExpected: false,
|
errorExpected: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ func (fs *UnstructAdapter) selectSubtree(path string) (map[string]interface{}, [
|
|||||||
idx := sections[sectionIdx].idx
|
idx := sections[sectionIdx].idx
|
||||||
fields := sections[sectionIdx].fields
|
fields := sections[sectionIdx].fields
|
||||||
|
|
||||||
if idx == nil {
|
if idx == -1 {
|
||||||
// This section has no index
|
// This section has no index
|
||||||
return content, fields, true, nil
|
return content, fields, true, nil
|
||||||
}
|
}
|
||||||
@@ -107,24 +107,24 @@ func (fs *UnstructAdapter) selectSubtree(path string) (map[string]interface{}, [
|
|||||||
if !ok {
|
if !ok {
|
||||||
return content, fields, false, fmt.Errorf("%v is of the type %T, expected []interface{}", indexedField, indexedField)
|
return content, fields, false, fmt.Errorf("%v is of the type %T, expected []interface{}", indexedField, indexedField)
|
||||||
}
|
}
|
||||||
if *idx >= len(s) {
|
if idx >= len(s) {
|
||||||
return content, fields, false, fmt.Errorf("index %d is out of bounds", *idx)
|
return content, fields, false, fmt.Errorf("index %d is out of bounds", idx)
|
||||||
}
|
}
|
||||||
|
|
||||||
if sectionIdx == lastSectionIdx-1 {
|
if sectionIdx == lastSectionIdx-1 {
|
||||||
// This is the last section. Let's build a fake map
|
// This is the last section. Let's build a fake map
|
||||||
// to let the rest of the field extraction to work.
|
// to let the rest of the field extraction to work.
|
||||||
idxstring := fmt.Sprintf("[%v]", *idx)
|
idxstring := fmt.Sprintf("[%v]", idx)
|
||||||
newContent := map[string]interface{}{idxstring: s[*idx]}
|
newContent := map[string]interface{}{idxstring: s[idx]}
|
||||||
newFields := []string{idxstring}
|
newFields := []string{idxstring}
|
||||||
return newContent, newFields, true, nil
|
return newContent, newFields, true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
newContent, ok := s[*idx].(map[string]interface{})
|
newContent, ok := s[idx].(map[string]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
// Only map are supported here
|
// Only map are supported here
|
||||||
return content, fields, false,
|
return content, fields, false,
|
||||||
fmt.Errorf("%#v is expected to be of type map[string]interface{}", s[*idx])
|
fmt.Errorf("%#v is expected to be of type map[string]interface{}", s[idx])
|
||||||
}
|
}
|
||||||
content = newContent
|
content = newContent
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user