Expose path cleanup helper in loader pkg

This commit is contained in:
Torin Sandall
2017-09-21 15:49:10 -07:00
parent ece1bb6d38
commit c62d8129fb
2 changed files with 9 additions and 8 deletions
+6 -5
View File
@@ -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, "/")
}
+3 -3
View File
@@ -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)
}