From fdff0d3af4670f7079e539fef4b92af2a866d02d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 19 Aug 2018 13:30:42 +0200 Subject: [PATCH] tpl/tplimpl: Fix .Site.Params case regression Fixes #5094 --- tpl/tplimpl/template_ast_transformers.go | 15 +++++---------- tpl/tplimpl/template_ast_transformers_test.go | 13 ++++++++++++- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/tpl/tplimpl/template_ast_transformers.go b/tpl/tplimpl/template_ast_transformers.go index c396a1f41..9faaa2249 100644 --- a/tpl/tplimpl/template_ast_transformers.go +++ b/tpl/tplimpl/template_ast_transformers.go @@ -213,16 +213,6 @@ func (d decl) indexOfReplacementStart(idents []string) int { // .Data.Params.someKey return -1 } - if !d.isKeyword(container) { - // where $pages ".Params.toc_hide" "!=" true - return -1 - } - } - if i < len(resolvedIdents)-1 { - next := resolvedIdents[i+1] - if !d.isKeyword(next) { - return -1 - } } paramFound = true @@ -298,6 +288,11 @@ func (d decl) resolveVariables(idents []string) ([]string, bool) { return nil, false } + if !d.isKeyword(replacement) { + // This can not be .Site.Params etc. + return nil, false + } + replacement = strings.TrimPrefix(replacement, ".") if replacement == "" { diff --git a/tpl/tplimpl/template_ast_transformers_test.go b/tpl/tplimpl/template_ast_transformers_test.go index cf3caeccd..46a037cec 100644 --- a/tpl/tplimpl/template_ast_transformers_test.go +++ b/tpl/tplimpl/template_ast_transformers_test.go @@ -24,7 +24,8 @@ import ( var ( testFuncs = map[string]interface{}{ - "Echo": func(v interface{}) interface{} { return v }, + "First": func(v ...interface{}) interface{} { return v[0] }, + "Echo": func(v interface{}) interface{} { return v }, "where": func(seq, key interface{}, args ...interface{}) (interface{}, error) { return map[string]interface{}{ "ByWeight": fmt.Sprintf("%v:%v:%v", seq, key, args), @@ -55,6 +56,9 @@ var ( "Language": map[string]interface{}{ "Params": map[string]interface{}{ "lower": "P22L", + "nested": map[string]interface{}{ + "lower": "P22L_nested", + }, }, }, "Data": map[string]interface{}{ @@ -84,6 +88,7 @@ P2_2: {{ $.Site.Params.LOWER }} P2_3: {{ $site.Params.LOWER }} P2_4: {{ $siteParams.LOWER }} P22: {{ .Site.Language.Params.LOWER }} +P22_nested: {{ .Site.Language.Params.NESTED.LOWER }} P3: {{ .Site.Data.Params.NOLOW }} P3_2: {{ $.Site.Data.Params.NOLOW }} P3_3: {{ $site.Data.Params.NOLOW }} @@ -135,6 +140,8 @@ PARAMS STRING: {{ $pages.ByWeight }} PARAMS STRING2: {{ with $pages }}{{ .ByWeight }}{{ end }} {{ $pages3 := where ".Params.TOC_HIDE" "!=" .Params.LOWER }} PARAMS STRING3: {{ $pages3.ByWeight }} +{{ $first := First .Pages .Site.Params.LOWER }} +PARAMS COMPOSITE: {{ $first.ByWeight }} ` ) @@ -168,6 +175,7 @@ func TestParamsKeysToLower(t *testing.T) { require.Contains(t, result, "P2_3: P2L") require.Contains(t, result, "P2_4: P2L") require.Contains(t, result, "P22: P22L") + require.Contains(t, result, "P22_nested: P22L_nested") require.Contains(t, result, "P3: P3H") require.Contains(t, result, "P3_2: P3H") require.Contains(t, result, "P3_3: P3H") @@ -195,6 +203,9 @@ func TestParamsKeysToLower(t *testing.T) { require.Contains(t, result, "PARAMS STRING2: foo:.Params.toc_hide:[!= true]") require.Contains(t, result, "PARAMS STRING3: .Params.TOC_HIDE:!=:[P1L]") + // Issue #5094 + require.Contains(t, result, "PARAMS COMPOSITE: [1 3]") + // Issue #5068 require.Contains(t, result, "PCurrentSection: pcurrentsection")