-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add regex pattern for resource and definition (#9)
* feat: add fields for regex pattern on resource and definition * feat: update path explorer to accept regex pattern * fix: error due to change of explore paths * docs: update documentation and example * fix: update distribution build to trim path
- Loading branch information
Showing
8 changed files
with
109 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,23 @@ | ||
package core | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/gojek/optimus-extension-valor/registry/explorer" | ||
) | ||
|
||
// ExplorePaths explores the given root path for the type and format | ||
func ExplorePaths(rootPath, _type, format string) ([]string, error) { | ||
func ExplorePaths(rootPath, _type, format, regexPattern string) ([]string, error) { | ||
exPath, err := explorer.Explorers.Get(_type) | ||
if err != nil { | ||
return nil, err | ||
} | ||
reg, err := regexp.Compile(regexPattern) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return exPath(rootPath, func(path string) bool { | ||
return strings.HasSuffix(path, format) | ||
return reg.MatchString(path) && strings.HasSuffix(path, format) | ||
}) | ||
} |
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,75 @@ | ||
package core_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gojek/optimus-extension-valor/core" | ||
"github.com/gojek/optimus-extension-valor/registry/explorer" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestExplorePaths(t *testing.T) { | ||
originalExplorer := explorer.Explorers | ||
defer func() { explorer.Explorers = originalExplorer }() | ||
|
||
const ( | ||
rootPath = "." | ||
_type = "virtual" | ||
format = "go" | ||
) | ||
|
||
t.Run("should return nil and error if explorer registry returns error", func(t *testing.T) { | ||
_type := "invalid_type" | ||
pattern := `child_.+` | ||
|
||
actualPaths, actualErr := core.ExplorePaths(rootPath, _type, format, pattern) | ||
|
||
assert.Nil(t, actualPaths) | ||
assert.Error(t, actualErr) | ||
}) | ||
|
||
explorer.Explorers.Register(_type, func(root string, filter func(string) bool) ([]string, error) { | ||
paths := []string{ | ||
"./core/testing/root.go", | ||
"./core/testing/child_1.go", | ||
"./core/testing/child_2.json", | ||
} | ||
|
||
var validPaths []string | ||
for _, p := range paths { | ||
if filter(p) { | ||
validPaths = append(validPaths, p) | ||
} | ||
} | ||
|
||
return validPaths, nil | ||
}) | ||
|
||
t.Run("should return nil and error if regex pattern is invalid", func(t *testing.T) { | ||
pattern := "*" | ||
|
||
actualPaths, actualErr := core.ExplorePaths(rootPath, _type, format, pattern) | ||
|
||
assert.Nil(t, actualPaths) | ||
assert.Error(t, actualErr) | ||
}) | ||
|
||
t.Run("should return all paths and nil if regex pattern is empty", func(t *testing.T) { | ||
pattern := "" | ||
|
||
actualPaths, actualErr := core.ExplorePaths(rootPath, _type, format, pattern) | ||
|
||
assert.Len(t, actualPaths, 2) | ||
assert.NoError(t, actualErr) | ||
}) | ||
|
||
t.Run("should return as expected paths and nil if given regex pattern and format", func(t *testing.T) { | ||
pattern := `child_.+` | ||
|
||
actualPaths, actualErr := core.ExplorePaths(rootPath, _type, format, pattern) | ||
|
||
assert.Len(t, actualPaths, 1) | ||
assert.NoError(t, actualErr) | ||
}) | ||
} |
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
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