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

support deep lookup #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions handlebars/builtins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,45 @@ var builtinsTests = []Test{
nil, nil, nil,
"",
},
{
"#lookup - should lookup in subexpression",
"{{#with (lookup data goodbyes)}}{{abc}}{{/with}}",
map[string]interface{}{"goodbyes": "foo", "data": map[string]interface{}{
"foo": map[string]string{"abc": "foo1"},
"bar": map[string]string{"abc": "bar1"}}},
nil, nil, nil,
"foo1",
},
{
"#lookup - should lookup array element deeply in subexpression",
"{{#each goodbyes}}{{#with (lookup ../data @index)}}{{foo}}{{bar}}{{/with}}{{/each}}",
map[string]interface{}{"goodbyes": []int{0, 1}, "data": []map[string]interface{}{
{"foo": "baz1", "bar": "bat1"},
{"foo": "baz2", "bar": "bat2"}}},
nil, nil, nil,
"baz1bat1baz2bat2",
},
{
"#lookup - should lookup map element deeply in subexpression",
"{{#each goodbyes}}{{#with (lookup ../data .)}}{{foo}}{{bar}}{{/with}}{{/each}}",
map[string]interface{}{"goodbyes": []string{"0", "1"}, "data": map[string]interface{}{
"0": map[string]string{"foo": "baz1", "bar": "bat1"},
"1": map[string]string{"foo": "baz2", "bar": "bat2"}}},
nil, nil, nil,
"baz1bat1baz2bat2",
},
{
"#lookup - should lookup struct element deeply in subexpression",
"{{#each goodbyes}}{{#with (lookup ../data .)}}{{foo}}{{bar}}{{/with}}{{/each}}",
map[string]interface{}{"goodbyes": []string{"One", "Two"}, "data": struct {
One map[string]string
Two map[string]string
}{
One: map[string]string{"foo": "baz1", "bar": "bat1"},
Two: map[string]string{"foo": "baz2", "bar": "bat2"}}},
nil, nil, nil,
"baz1bat1baz2bat2",
},
}

func TestBuiltins(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ func logHelper(message string) interface{} {

// #lookup helper
func lookupHelper(obj interface{}, field string, options *Options) interface{} {
return Str(options.Eval(obj, field))
return options.Eval(obj, field)
}

// #equal helper
Expand Down