From c62d8129fb5d4e97f4f688015d7c806cc417ee08 Mon Sep 17 00:00:00 2001 From: Torin Sandall Date: Thu, 21 Sep 2017 15:49:10 -0700 Subject: [PATCH] Expose path cleanup helper in loader pkg --- loader/loader.go | 11 ++++++----- loader/loader_test.go | 6 +++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/loader/loader.go b/loader/loader.go index 32750b71c8..0c0efecbfe 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -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, "/") -} diff --git a/loader/loader_test.go b/loader/loader_test.go index 5f7d611225..1de4c860a9 100644 --- a/loader/loader_test.go +++ b/loader/loader_test.go @@ -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) }