mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-13 03:42:35 -06:00
Expose path cleanup helper in loader pkg
This commit is contained in:
+6
-5
@@ -71,6 +71,11 @@ func Rego(path string) (*RegoFile, error) {
|
||||
return loadRego(path, bs)
|
||||
}
|
||||
|
||||
// CleanPath returns the normalized version of a path that can be used as an identifier.
|
||||
func CleanPath(path string) string {
|
||||
return strings.Trim(path, "/")
|
||||
}
|
||||
|
||||
// Paths returns a sorted list of files contained at path. If recurse is true
|
||||
// and path is a directory, then Paths will walk the directory structure
|
||||
// recursively and list files at each level.
|
||||
@@ -100,7 +105,7 @@ func SplitPrefix(path string) ([]string, string) {
|
||||
func (l *Result) merge(path string, result interface{}) error {
|
||||
switch result := result.(type) {
|
||||
case *RegoFile:
|
||||
l.Modules[normalizeModuleID(path)] = result
|
||||
l.Modules[CleanPath(path)] = result
|
||||
default:
|
||||
obj, ok := makeDir(l.path, result)
|
||||
if !ok {
|
||||
@@ -286,7 +291,3 @@ func makeDir(path []string, x interface{}) (map[string]interface{}, bool) {
|
||||
}
|
||||
return makeDir(path[:len(path)-1], map[string]interface{}{path[len(path)-1]: x})
|
||||
}
|
||||
|
||||
func normalizeModuleID(x string) string {
|
||||
return strings.Trim(x, "/")
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ p = true { true }`}
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
expected := ast.MustParseModule(files["/foo.rego"])
|
||||
if !expected.Equal(loaded.Modules[normalizeModuleID(moduleFile)].Parsed) {
|
||||
if !expected.Equal(loaded.Modules[CleanPath(moduleFile)].Parsed) {
|
||||
t.Fatalf("Expected:\n%v\n\nGot:\n%v", expected, loaded.Modules[moduleFile])
|
||||
}
|
||||
})
|
||||
@@ -141,8 +141,8 @@ func TestLoadDirRecursive(t *testing.T) {
|
||||
}
|
||||
mod1 := ast.MustParseModule(files["/a/e.rego"])
|
||||
mod2 := ast.MustParseModule(files["/b/d/e.rego"])
|
||||
expectedMod1 := loaded.Modules[normalizeModuleID(filepath.Join(rootDir, "/a/e.rego"))].Parsed
|
||||
expectedMod2 := loaded.Modules[normalizeModuleID(filepath.Join(rootDir, "/b/d/e.rego"))].Parsed
|
||||
expectedMod1 := loaded.Modules[CleanPath(filepath.Join(rootDir, "/a/e.rego"))].Parsed
|
||||
expectedMod2 := loaded.Modules[CleanPath(filepath.Join(rootDir, "/b/d/e.rego"))].Parsed
|
||||
if !mod1.Equal(expectedMod1) {
|
||||
t.Fatalf("Expected:\n%v\n\nGot:\n%v", expectedMod1, mod1)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user