Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for array index selector #85

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/mustache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func main() {
rootCmd.Flags().StringVar(&layoutFile, "layout", "", "location of layout file")
rootCmd.Flags().StringVar(&overrideFile, "override", "", "location of data.yml override yml")
rootCmd.Flags().BoolVar(&mustache.AllowMissingVariables, "allow-missing-variables", true, "allow missing variables")
rootCmd.Flags().BoolVar(&mustache.Experimental, "experimental", false, "allow experimental features")

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
Expand Down
17 changes: 17 additions & 0 deletions mustache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ var (
// is true (the default), an empty string is emitted. If it is false, an error
// is generated instead.
AllowMissingVariables = true
// Experimental defines the behavior for experimental features (e.g. commonly
// used, but not part of the spec). If it is false (default) it will ignore
// the code paths, if true, it will allow you to leverage the experimental features.
Experimental = false
)

// RenderFunc is provided to lambda functions for rendering.
Expand Down Expand Up @@ -515,6 +519,19 @@ Outer:
return ret, nil
}
continue Outer
case reflect.Slice:
if !Experimental {
continue Outer
}
index, err := strconv.Atoi(name)
if err != nil {
return v, err
}
ret := av.Index(index)
if ret.IsValid() {
return ret, nil
}
continue Outer
default:
continue Outer
}
Expand Down
61 changes: 52 additions & 9 deletions mustache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,15 @@ func TestPartial(t *testing.T) {
}

/*
func TestSectionPartial(t *testing.T) {
filename := path.Join(path.Join(os.Getenv("PWD"), "tests"), "test3.mustache")
expected := "Mike\nJoe\n"
context := map[string]interface{}{"users": []User{{"Mike", 1}, {"Joe", 2}}}
output := RenderFile(filename, context)
if output != expected {
t.Fatalf("testSectionPartial expected %q got %q", expected, output)
}
}
func TestSectionPartial(t *testing.T) {
filename := path.Join(path.Join(os.Getenv("PWD"), "tests"), "test3.mustache")
expected := "Mike\nJoe\n"
context := map[string]interface{}{"users": []User{{"Mike", 1}, {"Joe", 2}}}
output := RenderFile(filename, context)
if output != expected {
t.Fatalf("testSectionPartial expected %q got %q", expected, output)
}
}
*/
func TestMultiContext(t *testing.T) {
output, err := Render(`{{hello}} {{World}}`, map[string]string{"hello": "hello"}, struct{ World string }{"world"})
Expand Down Expand Up @@ -716,3 +716,46 @@ func compareTags(t *testing.T, actual []Tag, expected []tag) {
}
}
}

var experimental = []Test{
{`{{a.b.0.c}}{{a.b.1.c}}`,
map[string]interface{}{
"a": map[string]interface{}{
"b": []map[string]interface{}{
{
"c": "hello",
},
{
"c": "ehlo",
},
},
},
},
"helloehlo",
nil,
},
}

func TestExperimental(t *testing.T) {
// Default behavior, Experimental=false
for _, test := range experimental {
output, err := Render(test.tmpl, test.context)
if err != nil {
t.Errorf("%q expected %q but got error %q", test.tmpl, test.expected, err.Error())
} else if output != "" {
t.Errorf("%q expected %q got %q", test.tmpl, test.expected, output)
}
}

// Now set Experimental=true and test again
Experimental = true
defer func() { Experimental = false }()
for _, test := range experimental {
output, err := Render(test.tmpl, test.context)
if err != nil {
t.Errorf("%s expected %s but got error %s", test.tmpl, test.expected, err.Error())
} else if output != test.expected {
t.Errorf("%q expected %q got %q", test.tmpl, test.expected, output)
}
}
}