summaryrefslogtreecommitdiffstats
path: root/tpl/compare
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-04-30 19:33:19 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-05-01 15:13:41 +0200
commita3bf118eaa0796892047bb7456fe89824e423f27 (patch)
treeabe34c808992bc84e4fb6476c670ac246eca52c7 /tpl/compare
parent656155736721547fef45466a28295f6a563cdd1f (diff)
tpl/compare: Make it a package that stands on its own
See #3042
Diffstat (limited to 'tpl/compare')
-rw-r--r--tpl/compare/compare.go35
-rw-r--r--tpl/compare/compare_test.go17
-rw-r--r--tpl/compare/init.go49
3 files changed, 81 insertions, 20 deletions
diff --git a/tpl/compare/compare.go b/tpl/compare/compare.go
index 8b7a96bf0..1482c0afe 100644
--- a/tpl/compare/compare.go
+++ b/tpl/compare/compare.go
@@ -20,11 +20,20 @@ import (
"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
// is not. "Set" in this context means non-zero for numeric types and times;
// non-zero length for strings, arrays, slices, and maps;
// 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
// argument when the key is missing: {{ index . "key" | default "foo" }}
// 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.
-func Eq(x, y interface{}) bool {
+func (*Namespace) Eq(x, y interface{}) bool {
normalize := func(v interface{}) interface{} {
vv := reflect.ValueOf(v)
switch vv.Kind() {
@@ -95,35 +104,35 @@ func Eq(x, y interface{}) bool {
}
// Ne returns the boolean truth of arg1 != arg2.
-func Ne(x, y interface{}) bool {
- return !Eq(x, y)
+func (n *Namespace) Ne(x, y interface{}) bool {
+ return !n.Eq(x, y)
}
// Ge returns the boolean truth of arg1 >= arg2.
-func Ge(a, b interface{}) bool {
- left, right := compareGetFloat(a, b)
+func (n *Namespace) Ge(a, b interface{}) bool {
+ left, right := n.compareGetFloat(a, b)
return left >= right
}
// Gt returns the boolean truth of arg1 > arg2.
-func Gt(a, b interface{}) bool {
- left, right := compareGetFloat(a, b)
+func (n *Namespace) Gt(a, b interface{}) bool {
+ left, right := n.compareGetFloat(a, b)
return left > right
}
// Le returns the boolean truth of arg1 <= arg2.
-func Le(a, b interface{}) bool {
- left, right := compareGetFloat(a, b)
+func (n *Namespace) Le(a, b interface{}) bool {
+ left, right := n.compareGetFloat(a, b)
return left <= right
}
// Lt returns the boolean truth of arg1 < arg2.
-func Lt(a, b interface{}) bool {
- left, right := compareGetFloat(a, b)
+func (n *Namespace) Lt(a, b interface{}) bool {
+ left, right := n.compareGetFloat(a, b)
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 leftStr, rightStr *string
av := reflect.ValueOf(a)
diff --git a/tpl/compare/compare_test.go b/tpl/compare/compare_test.go
index d40a6fe5f..57f061f4d 100644
--- a/tpl/compare/compare_test.go
+++ b/tpl/compare/compare_test.go
@@ -46,6 +46,7 @@ func TestDefaultFunc(t *testing.T) {
then := time.Now()
now := time.Now()
+ ns := New()
for i, test := range []struct {
dflt interface{}
@@ -91,7 +92,7 @@ func TestDefaultFunc(t *testing.T) {
} {
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)
assert.Equal(t, result, test.expect, errMsg)
@@ -101,16 +102,18 @@ func TestDefaultFunc(t *testing.T) {
func TestCompare(t *testing.T) {
t.Parallel()
+ n := New()
+
for _, test := range []struct {
tstCompareType
funcUnderTest func(a, b interface{}) bool
}{
- {tstGt, Gt},
- {tstLt, Lt},
- {tstGe, Ge},
- {tstLe, Le},
- {tstEq, Eq},
- {tstNe, Ne},
+ {tstGt, n.Gt},
+ {tstLt, n.Lt},
+ {tstGe, n.Ge},
+ {tstLe, n.Le},
+ {tstEq, n.Eq},
+ {tstNe, n.Ne},
} {
doTestCompare(t, test.tstCompareType, test.funcUnderTest)
}
diff --git a/tpl/compare/init.go b/tpl/compare/init.go
new file mode 100644
index 000000000..d8d30d6f4
--- /dev/null
+++ b/tpl/compare/init.go
@@ -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)
+}