Skip to content

Commit

Permalink
Testing cleanKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
armon committed Apr 27, 2014
1 parent 0152e29 commit cf74f69
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
6 changes: 4 additions & 2 deletions resource/template/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,13 @@ func (t *TemplateResource) setVars() error {
if err != nil {
return err
}
t.Vars = t.cleanKeys(vars)
t.Vars = cleanKeys(vars)
return nil
}

func (t *TemplateResource) cleanKeys(vars map[string]interface{}) map[string]interface{} {
// cleanKeys is used to transform the path based keys we
// get from the StoreClient to a more friendly format.
func cleanKeys(vars map[string]interface{}) map[string]interface{} {
clean := make(map[string]interface{}, len(vars))
prefix := config.Prefix()
for key, val := range vars {
Expand Down
22 changes: 22 additions & 0 deletions resource/template/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,25 @@ func TestPathToKey(t *testing.T) {
}
}
}

func TestCleanKeys(t *testing.T) {
pre := map[string]interface{}{
"/my/cool_val/here": "test",
"/this_key": "foo",
"/prefix/key": "test",
}
config.SetPrefix("/prefix")
clean := cleanKeys(pre)
if len(clean) != len(pre) {
t.Fatalf("bad length")
}
if _, ok := clean["my_cool_val_here"]; !ok {
t.Fatalf("bad: %v", clean)
}
if _, ok := clean["this_key"]; !ok {
t.Fatalf("bad: %v", clean)
}
if _, ok := clean["key"]; !ok {
t.Fatalf("bad: %v", clean)
}
}

0 comments on commit cf74f69

Please sign in to comment.