From c4745ed29f0da7f41a77778d02203f0e7533a2f4 Mon Sep 17 00:00:00 2001 From: Chen Chen Date: Mon, 6 Aug 2018 16:41:15 +0800 Subject: [PATCH] support deep lookup --- handlebars/builtins_test.go | 39 +++++++++++++++++++++++++++++++++++++ helper.go | 2 +- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/handlebars/builtins_test.go b/handlebars/builtins_test.go index 1f986e4..ccab1e8 100644 --- a/handlebars/builtins_test.go +++ b/handlebars/builtins_test.go @@ -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) { diff --git a/helper.go b/helper.go index 3a5236e..8f12566 100644 --- a/helper.go +++ b/helper.go @@ -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