tpl/compare: Make it a package that stands on its own

See #3042
This commit is contained in:
Bjørn Erik Pedersen 2017-04-30 19:33:19 +02:00
parent 6561557367
commit a3bf118eaa
6 changed files with 87 additions and 33 deletions

View file

@ -23,6 +23,8 @@ import (
"github.com/spf13/hugo/tpl/compare" "github.com/spf13/hugo/tpl/compare"
) )
var comp = compare.New()
// Sort returns a sorted sequence. // Sort returns a sorted sequence.
func (ns *Namespace) Sort(seq interface{}, args ...interface{}) (interface{}, error) { func (ns *Namespace) Sort(seq interface{}, args ...interface{}) (interface{}, error) {
if seq == nil { if seq == nil {
@ -129,15 +131,15 @@ func (p pairList) Less(i, j int) bool {
if iv.IsValid() { if iv.IsValid() {
if jv.IsValid() { if jv.IsValid() {
// can only call Interface() on valid reflect Values // can only call Interface() on valid reflect Values
return compare.Lt(iv.Interface(), jv.Interface()) return comp.Lt(iv.Interface(), jv.Interface())
} }
// if j is invalid, test i against i's zero value // if j is invalid, test i against i's zero value
return compare.Lt(iv.Interface(), reflect.Zero(iv.Type())) return comp.Lt(iv.Interface(), reflect.Zero(iv.Type()))
} }
if jv.IsValid() { if jv.IsValid() {
// if i is invalid, test j against j's zero value // if i is invalid, test j against j's zero value
return compare.Lt(reflect.Zero(jv.Type()), jv.Interface()) return comp.Lt(reflect.Zero(jv.Type()), jv.Interface())
} }
return false return false

View file

@ -20,11 +20,20 @@ import (
"time" "time"
) )
// New returns a new instance of the compare-namespaced template functions.
func New() *Namespace {
return &Namespace{}
}
// Namespace provides template functions for the "compare" namespace.
type Namespace struct {
}
// Default checks whether a given value is set and returns a default value if it // Default checks whether a given value is set and returns a default value if it
// is not. "Set" in this context means non-zero for numeric types and times; // is not. "Set" in this context means non-zero for numeric types and times;
// non-zero length for strings, arrays, slices, and maps; // non-zero length for strings, arrays, slices, and maps;
// any boolean or struct value; or non-nil for any other types. // any boolean or struct value; or non-nil for any other types.
func Default(dflt interface{}, given ...interface{}) (interface{}, error) { func (*Namespace) Default(dflt interface{}, given ...interface{}) (interface{}, error) {
// given is variadic because the following construct will not pass a piped // given is variadic because the following construct will not pass a piped
// argument when the key is missing: {{ index . "key" | default "foo" }} // argument when the key is missing: {{ index . "key" | default "foo" }}
// The Go template will complain that we got 1 argument when we expectd 2. // The Go template will complain that we got 1 argument when we expectd 2.
@ -75,7 +84,7 @@ func Default(dflt interface{}, given ...interface{}) (interface{}, error) {
} }
// Eq returns the boolean truth of arg1 == arg2. // Eq returns the boolean truth of arg1 == arg2.
func Eq(x, y interface{}) bool { func (*Namespace) Eq(x, y interface{}) bool {
normalize := func(v interface{}) interface{} { normalize := func(v interface{}) interface{} {
vv := reflect.ValueOf(v) vv := reflect.ValueOf(v)
switch vv.Kind() { switch vv.Kind() {
@ -95,35 +104,35 @@ func Eq(x, y interface{}) bool {
} }
// Ne returns the boolean truth of arg1 != arg2. // Ne returns the boolean truth of arg1 != arg2.
func Ne(x, y interface{}) bool { func (n *Namespace) Ne(x, y interface{}) bool {
return !Eq(x, y) return !n.Eq(x, y)
} }
// Ge returns the boolean truth of arg1 >= arg2. // Ge returns the boolean truth of arg1 >= arg2.
func Ge(a, b interface{}) bool { func (n *Namespace) Ge(a, b interface{}) bool {
left, right := compareGetFloat(a, b) left, right := n.compareGetFloat(a, b)
return left >= right return left >= right
} }
// Gt returns the boolean truth of arg1 > arg2. // Gt returns the boolean truth of arg1 > arg2.
func Gt(a, b interface{}) bool { func (n *Namespace) Gt(a, b interface{}) bool {
left, right := compareGetFloat(a, b) left, right := n.compareGetFloat(a, b)
return left > right return left > right
} }
// Le returns the boolean truth of arg1 <= arg2. // Le returns the boolean truth of arg1 <= arg2.
func Le(a, b interface{}) bool { func (n *Namespace) Le(a, b interface{}) bool {
left, right := compareGetFloat(a, b) left, right := n.compareGetFloat(a, b)
return left <= right return left <= right
} }
// Lt returns the boolean truth of arg1 < arg2. // Lt returns the boolean truth of arg1 < arg2.
func Lt(a, b interface{}) bool { func (n *Namespace) Lt(a, b interface{}) bool {
left, right := compareGetFloat(a, b) left, right := n.compareGetFloat(a, b)
return left < right return left < right
} }
func compareGetFloat(a interface{}, b interface{}) (float64, float64) { func (*Namespace) compareGetFloat(a interface{}, b interface{}) (float64, float64) {
var left, right float64 var left, right float64
var leftStr, rightStr *string var leftStr, rightStr *string
av := reflect.ValueOf(a) av := reflect.ValueOf(a)

View file

@ -46,6 +46,7 @@ func TestDefaultFunc(t *testing.T) {
then := time.Now() then := time.Now()
now := time.Now() now := time.Now()
ns := New()
for i, test := range []struct { for i, test := range []struct {
dflt interface{} dflt interface{}
@ -91,7 +92,7 @@ func TestDefaultFunc(t *testing.T) {
} { } {
errMsg := fmt.Sprintf("[%d] %v", i, test) errMsg := fmt.Sprintf("[%d] %v", i, test)
result, err := Default(test.dflt, test.given) result, err := ns.Default(test.dflt, test.given)
require.NoError(t, err, errMsg) require.NoError(t, err, errMsg)
assert.Equal(t, result, test.expect, errMsg) assert.Equal(t, result, test.expect, errMsg)
@ -101,16 +102,18 @@ func TestDefaultFunc(t *testing.T) {
func TestCompare(t *testing.T) { func TestCompare(t *testing.T) {
t.Parallel() t.Parallel()
n := New()
for _, test := range []struct { for _, test := range []struct {
tstCompareType tstCompareType
funcUnderTest func(a, b interface{}) bool funcUnderTest func(a, b interface{}) bool
}{ }{
{tstGt, Gt}, {tstGt, n.Gt},
{tstLt, Lt}, {tstLt, n.Lt},
{tstGe, Ge}, {tstGe, n.Ge},
{tstLe, Le}, {tstLe, n.Le},
{tstEq, Eq}, {tstEq, n.Eq},
{tstNe, Ne}, {tstNe, n.Ne},
} { } {
doTestCompare(t, test.tstCompareType, test.funcUnderTest) doTestCompare(t, test.tstCompareType, test.funcUnderTest)
} }

49
tpl/compare/init.go Normal file
View file

@ -0,0 +1,49 @@
// Copyright 2017 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package compare
import (
"github.com/spf13/hugo/deps"
"github.com/spf13/hugo/tpl/internal"
)
const name = "compare"
func init() {
f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
ctx := New()
examples := [][2]string{
{`eq: {{ if eq .Section "blog" }}current{{ end }}`, `eq: current`},
}
return &internal.TemplateFuncsNamespace{
Name: name,
Context: func() interface{} { return ctx },
Aliases: map[string]interface{}{
"default": ctx.Default,
"eq": ctx.Eq,
"ge": ctx.Ge,
"gt": ctx.Gt,
"le": ctx.Le,
"lt": ctx.Lt,
"ne": ctx.Ne,
},
Examples: examples,
}
}
internal.AddTemplateFuncsNamespace(f)
}

View file

@ -21,10 +21,10 @@ import (
"sync" "sync"
"github.com/spf13/cast" "github.com/spf13/cast"
"github.com/spf13/hugo/tpl/compare"
"github.com/spf13/hugo/tpl/internal" "github.com/spf13/hugo/tpl/internal"
// Init the namespaces // Init the namespaces
_ "github.com/spf13/hugo/tpl/compare"
_ "github.com/spf13/hugo/tpl/data" _ "github.com/spf13/hugo/tpl/data"
_ "github.com/spf13/hugo/tpl/lang" _ "github.com/spf13/hugo/tpl/lang"
_ "github.com/spf13/hugo/tpl/math" _ "github.com/spf13/hugo/tpl/math"
@ -99,17 +99,13 @@ func (t *templateFuncster) initFuncMap() {
"apply": t.collections.Apply, "apply": t.collections.Apply,
"base64Decode": t.encoding.Base64Decode, "base64Decode": t.encoding.Base64Decode,
"base64Encode": t.encoding.Base64Encode, "base64Encode": t.encoding.Base64Encode,
"default": compare.Default,
"dateFormat": t.time.Format, "dateFormat": t.time.Format,
"delimit": t.collections.Delimit, "delimit": t.collections.Delimit,
"dict": t.collections.Dictionary, "dict": t.collections.Dictionary,
"echoParam": t.collections.EchoParam, "echoParam": t.collections.EchoParam,
"emojify": t.transform.Emojify, "emojify": t.transform.Emojify,
"eq": compare.Eq,
"first": t.collections.First, "first": t.collections.First,
"ge": compare.Ge,
"getenv": t.os.Getenv, "getenv": t.os.Getenv,
"gt": compare.Gt,
"highlight": t.transform.Highlight, "highlight": t.transform.Highlight,
"htmlEscape": t.transform.HTMLEscape, "htmlEscape": t.transform.HTMLEscape,
"htmlUnescape": t.transform.HTMLUnescape, "htmlUnescape": t.transform.HTMLUnescape,
@ -123,11 +119,8 @@ func (t *templateFuncster) initFuncMap() {
"isset": t.collections.IsSet, "isset": t.collections.IsSet,
"jsonify": t.encoding.Jsonify, "jsonify": t.encoding.Jsonify,
"last": t.collections.Last, "last": t.collections.Last,
"le": compare.Le,
"lt": compare.Lt,
"markdownify": t.transform.Markdownify, "markdownify": t.transform.Markdownify,
"md5": t.crypto.MD5, "md5": t.crypto.MD5,
"ne": compare.Ne,
"now": t.time.Now, "now": t.time.Now,
"partial": t.partial, "partial": t.partial,
"partialCached": t.partialCached, "partialCached": t.partialCached,

View file

@ -132,7 +132,6 @@ dateFormat: {{ dateFormat "Monday, Jan 2, 2006" "2015-01-21" }}
delimit: {{ delimit (slice "A" "B" "C") ", " " and " }} delimit: {{ delimit (slice "A" "B" "C") ", " " and " }}
echoParam: {{ echoParam .Params "langCode" }} echoParam: {{ echoParam .Params "langCode" }}
emojify: {{ "I :heart: Hugo" | emojify }} emojify: {{ "I :heart: Hugo" | emojify }}
eq: {{ if eq .Section "blog" }}current{{ end }}
htmlEscape 1: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | safeHTML}} htmlEscape 1: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | safeHTML}}
htmlEscape 2: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>"}} htmlEscape 2: {{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>"}}
htmlUnescape 1: {{htmlUnescape "Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | safeHTML}} htmlUnescape 1: {{htmlUnescape "Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | safeHTML}}
@ -189,7 +188,6 @@ dateFormat: Wednesday, Jan 21, 2015
delimit: A, B and C delimit: A, B and C
echoParam: en echoParam: en
emojify: I Hugo emojify: I Hugo
eq: current
htmlEscape 1: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt; htmlEscape 1: Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;
htmlEscape 2: Cathal Garvey &amp;amp; The Sunshine Band &amp;lt;cathal@foo.bar&amp;gt; htmlEscape 2: Cathal Garvey &amp;amp; The Sunshine Band &amp;lt;cathal@foo.bar&amp;gt;
htmlUnescape 1: Cathal Garvey & The Sunshine Band <cathal@foo.bar> htmlUnescape 1: Cathal Garvey & The Sunshine Band <cathal@foo.bar>