Skip to content

Commit

Permalink
regex as seprate path part
Browse files Browse the repository at this point in the history
  • Loading branch information
sethiyash committed Jan 16, 2023
1 parent 7e0968b commit 7130ae9
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 75 deletions.
12 changes: 6 additions & 6 deletions pkg/kapp/resources/matcher_empty_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func (m EmptyFieldMatcher) check(obj interface{}, path Path) bool {
return true
}

case part.IndexAndRegex != nil:
case part.ArrayIndex != nil:
switch {
case part.IndexAndRegex.All != nil:
case part.ArrayIndex.All != nil:
typedObj, ok := obj.([]interface{})
if !ok {
return obj == nil
Expand All @@ -50,21 +50,21 @@ func (m EmptyFieldMatcher) check(obj interface{}, path Path) bool {

return true

case part.IndexAndRegex.Index != nil:
case part.ArrayIndex.Index != nil:
typedObj, ok := obj.([]interface{})
if !ok {
return obj == nil
}

if *part.IndexAndRegex.Index < len(typedObj) {
obj = typedObj[*part.IndexAndRegex.Index]
if *part.ArrayIndex.Index < len(typedObj) {
obj = typedObj[*part.ArrayIndex.Index]
} else {
// Index not found, it's empty
return true
}

default:
panic(fmt.Sprintf("Unknown array index: %#v", part.IndexAndRegex))
panic(fmt.Sprintf("Unknown array index: %#v", part.ArrayIndex))
}

default:
Expand Down
40 changes: 21 additions & 19 deletions pkg/kapp/resources/mod_field_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ func (t FieldCopyMod) apply(obj interface{}, path Path, fullPath Path, srcs map[
typedObj[*part.MapKey] = obj
}

case part.IndexAndRegex != nil:
if isLast && part.IndexAndRegex.Regex == nil {
case part.ArrayIndex != nil:
if isLast && part.RegexObj == nil {
return false, fmt.Errorf("Expected last part of the path to be map key")
}

switch {
case part.IndexAndRegex.All != nil:
case part.ArrayIndex.All != nil:
typedObj, ok := obj.([]interface{})
if !ok {
return false, fmt.Errorf("Unexpected non-array found: %T", obj)
Expand All @@ -91,7 +91,7 @@ func (t FieldCopyMod) apply(obj interface{}, path Path, fullPath Path, srcs map[
objI := objI

newFullPath := append([]*PathPart{}, fullPath...)
newFullPath[len(newFullPath)-1] = &PathPart{IndexAndRegex: &PathPartIndexAndRegex{Index: &objI}}
newFullPath[len(newFullPath)-1] = &PathPart{ArrayIndex: &PathPartArrayIndex{Index: &objI}}

updated, err := t.apply(obj, path[i+1:], newFullPath, srcs)
if err != nil {
Expand All @@ -104,20 +104,25 @@ func (t FieldCopyMod) apply(obj interface{}, path Path, fullPath Path, srcs map[

return anyUpdated, nil // dealt with children, get out

case part.IndexAndRegex.Index != nil:
case part.ArrayIndex.Index != nil:
typedObj, ok := obj.([]interface{})
if !ok {
return false, fmt.Errorf("Unexpected non-array found: %T", obj)
}

if *part.IndexAndRegex.Index < len(typedObj) {
obj = typedObj[*part.IndexAndRegex.Index]
if *part.ArrayIndex.Index < len(typedObj) {
obj = typedObj[*part.ArrayIndex.Index]
return t.apply(obj, path[i+1:], fullPath, srcs)
}

return false, nil // index not found, nothing to append to

case part.IndexAndRegex.Regex != nil:
default:
panic(fmt.Sprintf("Unknown array index: %#v", part.RegexObj))
}

case part.RegexObj != nil:
if part.RegexObj.Regex != nil {
matchedKeys, err := t.matchRegexWithSources(path, srcs)
if err != nil {
return false, err
Expand All @@ -135,10 +140,7 @@ func (t FieldCopyMod) apply(obj interface{}, path Path, fullPath Path, srcs map[
}
}
return allUpdated, nil
default:
panic(fmt.Sprintf("Unknown array index: %#v", part.IndexAndRegex))
}

default:
panic(fmt.Sprintf("Unexpected path part: %#v", part))
}
Expand Down Expand Up @@ -190,26 +192,26 @@ func (t FieldCopyMod) obtainValue(obj interface{}, path Path) (interface{}, bool
return nil, false, nil // index is not found return
}

case part.IndexAndRegex != nil:
case part.ArrayIndex != nil:
if isLast {
return nil, false, fmt.Errorf("Expected last part of the path to be map key")
}

switch {
case part.IndexAndRegex.Index != nil:
case part.ArrayIndex.Index != nil:
typedObj, ok := obj.([]interface{})
if !ok {
return nil, false, fmt.Errorf("Unexpected non-array found: %T", obj)
}

if *part.IndexAndRegex.Index < len(typedObj) {
obj = typedObj[*part.IndexAndRegex.Index]
if *part.ArrayIndex.Index < len(typedObj) {
obj = typedObj[*part.ArrayIndex.Index]
} else {
return nil, false, nil // index not found, return
}

default:
panic(fmt.Sprintf("Unknown array index: %#v", part.IndexAndRegex))
panic(fmt.Sprintf("Unknown array index: %#v", part.ArrayIndex))
}

default:
Expand Down Expand Up @@ -245,10 +247,10 @@ func (t FieldCopyMod) obtainMatchingRegexKeys(obj interface{}, path Path) ([]str
return matchedKeys, nil
}

case part.IndexAndRegex != nil:
case part.RegexObj.Regex != nil:
switch {
case part.IndexAndRegex.Regex != nil:
regex, err := regexp.Compile(*part.IndexAndRegex.Regex)
case part.RegexObj.Regex != nil:
regex, err := regexp.Compile(*part.RegexObj.Regex)
if err != nil {
return matchedKeys, err
}
Expand Down
39 changes: 19 additions & 20 deletions pkg/kapp/resources/mod_field_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ func (t FieldRemoveMod) apply(obj interface{}, path Path) error {
return nil // map key is not found, nothing to remove
}

case part.IndexAndRegex != nil:
if isLast && part.IndexAndRegex.Regex == nil {
case part.ArrayIndex != nil:
if isLast && part.RegexObj.Regex == nil {
return fmt.Errorf("Expected last part of the path to be map key")
}

switch {
case part.IndexAndRegex.All != nil:
case part.ArrayIndex.All != nil:
typedObj, ok := obj.([]interface{})
if !ok {
return fmt.Errorf("Unexpected non-array found: %T", obj)
Expand All @@ -75,37 +75,36 @@ func (t FieldRemoveMod) apply(obj interface{}, path Path) error {
return err
}
}

return nil // dealt with children, get out

case part.IndexAndRegex.Index != nil:
case part.ArrayIndex.Index != nil:
typedObj, ok := obj.([]interface{})
if !ok {
return fmt.Errorf("Unexpected non-array found: %T", obj)
}

if *part.IndexAndRegex.Index < len(typedObj) {
obj = typedObj[*part.IndexAndRegex.Index]
if *part.ArrayIndex.Index < len(typedObj) {
obj = typedObj[*part.ArrayIndex.Index]
} else {
return nil // index not found, nothing to remove
}

case part.IndexAndRegex.Regex != nil:
matchedKeys, err := t.obtainMatchingRegexKeys(obj, part)
default:
panic(fmt.Sprintf("Unknown array index: %#v", part.ArrayIndex))
}
case part.RegexObj.Regex != nil:
matchedKeys, err := t.obtainMatchingRegexKeys(obj, part)
if err != nil {
return err
}
for _, key := range matchedKeys {
newPath := append(Path{&PathPart{MapKey: &key}}, path[i+1:]...)
err := t.apply(obj, newPath)
if err != nil {
return err
}
for _, key := range matchedKeys {
newPath := append(Path{&PathPart{MapKey: &key}}, path[i+1:]...)
err := t.apply(obj, newPath)
if err != nil {
return err
}
}
return nil
default:
panic(fmt.Sprintf("Unknown array index: %#v", part.IndexAndRegex))
}
return nil

default:
panic(fmt.Sprintf("Unexpected path part: %#v", part))
Expand All @@ -117,7 +116,7 @@ func (t FieldRemoveMod) apply(obj interface{}, path Path) error {

func (t FieldRemoveMod) obtainMatchingRegexKeys(obj interface{}, part *PathPart) ([]string, error) {
var matchedKeys []string
regex, err := regexp.Compile(*part.IndexAndRegex.Regex)
regex, err := regexp.Compile(*part.RegexObj.Regex)
if err != nil {
return matchedKeys, err
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/kapp/resources/mod_object_ref_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func (t ObjectRefSetMod) apply(obj interface{}, path Path) error {
return nil
}

case part.IndexAndRegex != nil:
case part.ArrayIndex != nil:
switch {
case part.IndexAndRegex.All != nil:
case part.ArrayIndex.All != nil:
if obj == nil {
return nil
}
Expand All @@ -61,20 +61,20 @@ func (t ObjectRefSetMod) apply(obj interface{}, path Path) error {

return nil // dealt with children, get out

case part.IndexAndRegex.Index != nil:
case part.ArrayIndex.Index != nil:
typedObj, ok := obj.([]interface{})
if !ok {
return fmt.Errorf("Unexpected non-array found: %T", obj)
}

if *part.IndexAndRegex.Index < len(typedObj) {
return t.apply(typedObj[*part.IndexAndRegex.Index], path[i+1:])
if *part.ArrayIndex.Index < len(typedObj) {
return t.apply(typedObj[*part.ArrayIndex.Index], path[i+1:])
}

return nil // index not found, nothing to append to

default:
panic(fmt.Sprintf("Unknown array index: %#v", part.IndexAndRegex))
panic(fmt.Sprintf("Unknown array index: %#v", part.ArrayIndex))
}

default:
Expand Down
35 changes: 21 additions & 14 deletions pkg/kapp/resources/mod_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ type ResourceModWithMultiple interface {
type Path []*PathPart

type PathPart struct {
MapKey *string
IndexAndRegex *PathPartIndexAndRegex
MapKey *string
RegexObj *PathPartRegex
ArrayIndex *PathPartArrayIndex
}

var _ json.Unmarshaler = &PathPart{}

type PathPartIndexAndRegex struct {
type PathPartArrayIndex struct {
Index *int
All *bool `json:"allIndexes"`
All *bool `json:"allIndexes"`
}

type PathPartRegex struct {
Regex *string `json:"regex"`
}

Expand Down Expand Up @@ -76,7 +80,7 @@ func (p Path) AsString() string {

func (p Path) ContainsNonMapKeys() bool {
for _, part := range p {
if part.MapKey == nil && part.IndexAndRegex.Regex == nil {
if part.MapKey == nil {
return true
}
}
Expand All @@ -88,38 +92,41 @@ func NewPathPartFromString(str string) *PathPart {
}

func NewPathPartFromIndex(i int) *PathPart {
return &PathPart{IndexAndRegex: &PathPartIndexAndRegex{Index: &i}}
return &PathPart{ArrayIndex: &PathPartArrayIndex{Index: &i}}
}

func NewPathPartFromIndexAll() *PathPart {
trueBool := true
return &PathPart{IndexAndRegex: &PathPartIndexAndRegex{All: &trueBool}}
return &PathPart{ArrayIndex: &PathPartArrayIndex{All: &trueBool}}
}

func (p *PathPart) AsString() string {
switch {
case p.MapKey != nil:
return *p.MapKey
case p.IndexAndRegex != nil && p.IndexAndRegex.Index != nil:
return fmt.Sprintf("%d", *p.IndexAndRegex.Index)
case p.IndexAndRegex != nil && p.IndexAndRegex.All != nil:
case p.ArrayIndex != nil && p.ArrayIndex.Index != nil:
return fmt.Sprintf("%d", *p.ArrayIndex.Index)
case p.ArrayIndex != nil && p.ArrayIndex.All != nil:
return "(all)"
case p.IndexAndRegex != nil && p.IndexAndRegex.Regex != nil:
return *p.IndexAndRegex.Regex
case p.RegexObj != nil && p.RegexObj.Regex != nil:
return *p.RegexObj.Regex
default:
panic("Unknown path part")
}
}

func (p *PathPart) UnmarshalJSON(data []byte) error {
var str string
var idx PathPartIndexAndRegex
var idx PathPartArrayIndex
var regx PathPartRegex

switch {
case json.Unmarshal(data, &str) == nil:
p.MapKey = &str
case json.Unmarshal(data, &regx) == nil && regx.Regex != nil:
p.RegexObj = &regx
case json.Unmarshal(data, &idx) == nil:
p.IndexAndRegex = &idx
p.ArrayIndex = &idx
default:
return fmt.Errorf("Unknown path part")
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/kapp/resources/mod_string_map_append.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func (t StringMapAppendMod) apply(obj interface{}, path Path) error {
typedObj[*part.MapKey] = obj
}

case part.IndexAndRegex != nil:
case part.ArrayIndex != nil:
switch {
case part.IndexAndRegex.All != nil:
case part.ArrayIndex.All != nil:
typedObj, ok := obj.([]interface{})
if !ok {
return fmt.Errorf("Unexpected non-array found: %T", obj)
Expand All @@ -68,20 +68,20 @@ func (t StringMapAppendMod) apply(obj interface{}, path Path) error {

return nil // dealt with children, get out

case part.IndexAndRegex.Index != nil:
case part.ArrayIndex.Index != nil:
typedObj, ok := obj.([]interface{})
if !ok {
return fmt.Errorf("Unexpected non-array found: %T", obj)
}

if *part.IndexAndRegex.Index < len(typedObj) {
return t.apply(typedObj[*part.IndexAndRegex.Index], path[i+1:])
if *part.ArrayIndex.Index < len(typedObj) {
return t.apply(typedObj[*part.ArrayIndex.Index], path[i+1:])
}

return nil // index not found, nothing to append to

default:
panic(fmt.Sprintf("Unknown array index: %#v", part.IndexAndRegex))
panic(fmt.Sprintf("Unknown array index: %#v", part.ArrayIndex))
}

default:
Expand Down
Loading

0 comments on commit 7130ae9

Please sign in to comment.