Skip to content

Commit

Permalink
Fix displayed existing keys
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsauter committed Nov 6, 2023
1 parent 8799fdf commit b9b3992
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ func collectDataFromMatchingFiles(baseDir, glob string, data map[string]interfac
type visitFunc func(path []string, key any, value any) string

func assembleRef(path []string, key, value any) string {
if len(path) == 0 {
return fmt.Sprintf(".%s", key)
}
return fmt.Sprintf(".%s.%s", strings.Join(path, "."), key)
}

Expand Down
23 changes: 23 additions & 0 deletions cmd/render/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/opendevstack/ods-pipeline-adoc/internal/testhelper"
)

Expand Down Expand Up @@ -101,3 +102,25 @@ func TestRenderFailsOnMissingKeys(t *testing.T) {
t.Errorf("Error must list valid references, got:\n%s", err)
}
}

func TestWalkMap(t *testing.T) {
got := []string{}
data := map[string]any{
"foo": "bar",
"baz": map[string]any{
"one": 1,
"two": map[string]any{
"x": "y",
},
},
}
walkMap(data, &got, []string{}, assembleRef)
want := []string{
".foo",
".baz.one",
".baz.two.x",
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("walkMap() mismatch (-want +got):\n%s", diff)
}
}

0 comments on commit b9b3992

Please sign in to comment.