-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ea1a23
commit 1832221
Showing
6 changed files
with
214 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package search | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// pathMatch checks if the traversed yaml path matches with the user input path | ||
// checks if user input path is valid | ||
func (sr *SearchReplace) pathMatch(yamlPath string) bool { | ||
if sr.ByPath == "" { | ||
return false | ||
} | ||
inputElems := strings.Split(sr.ByPath, PathDelimiter) | ||
traversedElems := strings.Split(strings.Trim(yamlPath, PathDelimiter), PathDelimiter) | ||
if len(inputElems) != len(traversedElems) { | ||
return false | ||
} | ||
for i, inputElem := range inputElems { | ||
if inputElem != "*" && inputElem != traversedElems[i] { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// isAbsPath checks if input path is absolute and not a path expression | ||
// only supported path format is e.g. foo.bar.baz | ||
func isAbsPath(path string) bool { | ||
pathElem := strings.Split(path, PathDelimiter) | ||
if len(pathElem) == 0 { | ||
return false | ||
} | ||
for _, elem := range pathElem { | ||
// more checks can be added in future | ||
if elem == "" || strings.Contains(elem, "*") { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package search | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/golangplus/testing/assert" | ||
) | ||
|
||
type test struct { | ||
name string | ||
byPath string | ||
traversedPath string | ||
shouldMatch bool | ||
} | ||
|
||
var tests = []test{ | ||
{ | ||
name: "simple path match", | ||
byPath: "a.b.c", | ||
traversedPath: "a.b.c", | ||
shouldMatch: true, | ||
}, | ||
{ | ||
name: "simple path no match", | ||
byPath: "a.b.c", | ||
traversedPath: "a.c.b", | ||
shouldMatch: false, | ||
}, | ||
{ | ||
name: "simple path match with *", | ||
byPath: "a.*.c.*", | ||
traversedPath: "a.b.c.d", | ||
shouldMatch: true, | ||
}, | ||
{ | ||
name: "simple path no match with *", | ||
byPath: "a.*.c.*", | ||
traversedPath: "a.b.c", | ||
shouldMatch: false, | ||
}, | ||
{ | ||
name: "simple array path match", | ||
byPath: "a.c[0]", | ||
traversedPath: "a.c[0]", | ||
shouldMatch: true, | ||
}, | ||
} | ||
|
||
func TestPathMatch(t *testing.T) { | ||
for i := range tests { | ||
test := tests[i] | ||
t.Run(test.name, func(t *testing.T) { | ||
sr := SearchReplace{ | ||
ByPath: test.byPath, | ||
} | ||
actual := sr.pathMatch(test.traversedPath) | ||
if !assert.Equal(t, "", actual, test.shouldMatch) { | ||
t.FailNow() | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters