diff options
author | Bjørn Erik Pedersen <[email protected]> | 2018-02-22 09:15:12 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2018-02-22 09:15:12 +0100 |
commit | 0602135fd44b0cfa0a51b0ec6e451ae58ac95666 (patch) | |
tree | bb045df0360b26dbacf682fa227d5d4e18d57bdc /tpl/compare | |
parent | 55bd46a633d68f62e131457631ba839d6f876a55 (diff) | |
download | hugo-0602135fd44b0cfa0a51b0ec6e451ae58ac95666.tar.gz hugo-0602135fd44b0cfa0a51b0ec6e451ae58ac95666.zip |
Make ge, le etc. work with the Hugo Version number
This means that you can do something ala:
```html
{{ if ge .Hugo.Version "0.36" }}Reasonable new Hugo version!{{ end }}
```
The intented use is feature toggling, but please note that it will take some time and Hugo versions until this can be trusted. It does not work in older Hugo versions.
Fixes #4443
Diffstat (limited to 'tpl/compare')
-rw-r--r-- | tpl/compare/compare.go | 43 | ||||
-rw-r--r-- | tpl/compare/compare_test.go | 9 | ||||
-rw-r--r-- | tpl/compare/init.go | 4 |
3 files changed, 45 insertions, 11 deletions
diff --git a/tpl/compare/compare.go b/tpl/compare/compare.go index 65228da99..b228040cd 100644 --- a/tpl/compare/compare.go +++ b/tpl/compare/compare.go @@ -88,11 +88,12 @@ func (*Namespace) Default(dflt interface{}, given ...interface{}) (interface{}, // Eq returns the boolean truth of arg1 == arg2. func (*Namespace) Eq(x, y interface{}) bool { - // hugolib.Page implements compare.Eqer to make Page and PageOutput comparable. - if e1, ok := x.(compare.Eqer); ok { - if e2, ok := y.(compare.Eqer); ok { - return e1.Eq(e2) - } + if e, ok := x.(compare.Eqer); ok { + return e.Eq(y) + } + + if e, ok := y.(compare.Eqer); ok { + return e.Eq(x) } normalize := func(v interface{}) interface{} { @@ -120,25 +121,25 @@ func (n *Namespace) Ne(x, y interface{}) bool { // Ge returns the boolean truth of arg1 >= arg2. func (n *Namespace) Ge(a, b interface{}) bool { - left, right := n.compareGetFloat(a, b) + left, right := n.compareGet(a, b) return left >= right } // Gt returns the boolean truth of arg1 > arg2. func (n *Namespace) Gt(a, b interface{}) bool { - left, right := n.compareGetFloat(a, b) + left, right := n.compareGet(a, b) return left > right } // Le returns the boolean truth of arg1 <= arg2. func (n *Namespace) Le(a, b interface{}) bool { - left, right := n.compareGetFloat(a, b) + left, right := n.compareGet(a, b) return left <= right } // Lt returns the boolean truth of arg1 < arg2. func (n *Namespace) Lt(a, b interface{}) bool { - left, right := n.compareGetFloat(a, b) + left, right := n.compareGet(a, b) return left < right } @@ -151,7 +152,29 @@ func (n *Namespace) Conditional(condition bool, a, b interface{}) interface{} { return b } -func (*Namespace) compareGetFloat(a interface{}, b interface{}) (float64, float64) { +func (*Namespace) compareGet(a interface{}, b interface{}) (float64, float64) { + if ac, ok := a.(compare.Comparer); ok { + c := ac.Compare(b) + if c < 0 { + return 1, 0 + } else if c == 0 { + return 0, 0 + } else { + return 0, 1 + } + } + + if bc, ok := b.(compare.Comparer); ok { + c := bc.Compare(a) + if c < 0 { + return 0, 1 + } else if c == 0 { + return 0, 0 + } else { + return 1, 0 + } + } + 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 c9bc2ffe9..d201c995b 100644 --- a/tpl/compare/compare_test.go +++ b/tpl/compare/compare_test.go @@ -21,6 +21,8 @@ import ( "testing" "time" + "github.com/gohugoio/hugo/helpers" + "github.com/spf13/cast" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -171,6 +173,13 @@ func doTestCompare(t *testing.T, tp tstCompareType, funcUnderTest func(a, b inte {tstEqerType1("a"), tstEqerType2("a"), 0}, {tstEqerType2("a"), tstEqerType1("a"), 0}, {tstEqerType2("a"), tstEqerType1("b"), -1}, + {helpers.MustParseHugoVersion("0.32.1").Version(), helpers.MustParseHugoVersion("0.32").Version(), 1}, + {helpers.MustParseHugoVersion("0.35").Version(), helpers.MustParseHugoVersion("0.32").Version(), 1}, + {helpers.MustParseHugoVersion("0.36").Version(), helpers.MustParseHugoVersion("0.36").Version(), 0}, + {helpers.MustParseHugoVersion("0.32").Version(), helpers.MustParseHugoVersion("0.36").Version(), -1}, + {helpers.MustParseHugoVersion("0.32").Version(), "0.36", -1}, + {"0.36", helpers.MustParseHugoVersion("0.32").Version(), 1}, + {"0.36", helpers.MustParseHugoVersion("0.36").Version(), 0}, } { result := funcUnderTest(test.left, test.right) success := false diff --git a/tpl/compare/init.go b/tpl/compare/init.go index 7d58cf9ab..f766ef890 100644 --- a/tpl/compare/init.go +++ b/tpl/compare/init.go @@ -46,7 +46,9 @@ func init() { ns.AddMethodMapping(ctx.Ge, []string{"ge"}, - [][2]string{}, + [][2]string{ + {`{{ if ge .Hugo.Version "0.36" }}Reasonable new Hugo version!{{ end }}`, `Reasonable new Hugo version!`}, + }, ) ns.AddMethodMapping(ctx.Gt, |