Make ge, le etc. work with the Hugo Version number
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 22 Feb 2018 08:15:12 +0000 (09:15 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 22 Feb 2018 08:15:12 +0000 (09:15 +0100)
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

compare/compare.go [new file with mode: 0644]
compare/eq.go [deleted file]
helpers/hugo.go
helpers/hugo_test.go
hugolib/hugo_info.go
hugolib/hugo_info_test.go [new file with mode: 0644]
tpl/compare/compare.go
tpl/compare/compare_test.go
tpl/compare/init.go
tpl/tplimpl/template_funcs_test.go

diff --git a/compare/compare.go b/compare/compare.go
new file mode 100644 (file)
index 0000000..19a5dea
--- /dev/null
@@ -0,0 +1,29 @@
+// Copyright 2017-present 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
+
+// Eqer can be used to determine if this value is equal to the other.
+// The semantics of equals is that the two value are interchangeable
+// in the Hugo templates.
+type Eqer interface {
+       Eq(other interface{}) bool
+}
+
+// Comparer can be used to compare two values.
+// This will be used when using the le, ge etc. operators in the templates.
+// Compare returns -1 if the given version is less than, 0 if equal and 1 if greater than
+// the running version.
+type Comparer interface {
+       Compare(other interface{}) int
+}
diff --git a/compare/eq.go b/compare/eq.go
deleted file mode 100644 (file)
index 6120c0b..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-// Copyright 2017-present 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
-
-// Eqer can be used to determine if this value is equal to the other.
-// The semantics of equals is that the two value are interchangeable
-// in the Hugo templates.
-type Eqer interface {
-       Eq(other interface{}) bool
-}
index c4c3f4a764e18c570b149e6dea02049f10159450..b9b42e6cfbe475605e183b9820b0393544ab605d 100644 (file)
@@ -18,6 +18,7 @@ import (
        "fmt"
        "strings"
 
+       "github.com/gohugoio/hugo/compare"
        "github.com/spf13/cast"
 )
 
@@ -34,10 +35,40 @@ type HugoVersion struct {
        Suffix string
 }
 
+var (
+       _ compare.Eqer     = (*HugoVersionString)(nil)
+       _ compare.Comparer = (*HugoVersionString)(nil)
+)
+
+type HugoVersionString string
+
 func (v HugoVersion) String() string {
        return hugoVersion(v.Number, v.PatchLevel, v.Suffix)
 }
 
+func (v HugoVersion) Version() HugoVersionString {
+       return HugoVersionString(v.String())
+}
+
+func (h HugoVersionString) String() string {
+       return string(h)
+}
+
+// Implements compare.Comparer
+func (h HugoVersionString) Compare(other interface{}) int {
+       v := MustParseHugoVersion(h.String())
+       return compareVersions(v.Number, v.PatchLevel, other)
+}
+
+// Implements compare.Eqer
+func (h HugoVersionString) Eq(other interface{}) bool {
+       s, err := cast.ToStringE(other)
+       if err != nil {
+               return false
+       }
+       return s == h.String()
+}
+
 // ParseHugoVersion parses a version string.
 func ParseHugoVersion(s string) (HugoVersion, error) {
        var vv HugoVersion
index 1f5e5193f91048e3626ff5e94ba6b8c66035d7b9..78742c7055ad58654cecd291a7ae668f925d5adc 100644 (file)
@@ -29,6 +29,11 @@ func TestHugoVersion(t *testing.T) {
        require.Equal(t, v.ReleaseVersion().String(), "0.21")
        require.Equal(t, "0.21-DEV", v.String())
        require.Equal(t, "0.22", v.Next().String())
+       nextVersionString := v.Next().Version()
+       require.Equal(t, "0.22", nextVersionString.String())
+       require.True(t, nextVersionString.Eq("0.22"))
+       require.False(t, nextVersionString.Eq("0.21"))
+       require.True(t, nextVersionString.Eq(nextVersionString))
        require.Equal(t, "0.20.3", v.NextPatchLevel(3).String())
 }
 
index 1e0c192e508397dcc71d8c6499b5ac27c143bf83..303231edb51e0041bb498cf5e5ded7d97b1687e5 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright 2015 The Hugo Authors. All rights reserved.
+// Copyright 2018 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.
@@ -33,7 +33,7 @@ var hugoInfo *HugoInfo
 
 // HugoInfo contains information about the current Hugo environment
 type HugoInfo struct {
-       Version    string
+       Version    helpers.HugoVersionString
        Generator  template.HTML
        CommitHash string
        BuildDate  string
@@ -41,7 +41,7 @@ type HugoInfo struct {
 
 func init() {
        hugoInfo = &HugoInfo{
-               Version:    helpers.CurrentHugoVersion.String(),
+               Version:    helpers.CurrentHugoVersion.Version(),
                CommitHash: CommitHash,
                BuildDate:  BuildDate,
                Generator:  template.HTML(fmt.Sprintf(`<meta name="generator" content="Hugo %s" />`, helpers.CurrentHugoVersion.String())),
diff --git a/hugolib/hugo_info_test.go b/hugolib/hugo_info_test.go
new file mode 100644 (file)
index 0000000..0a34330
--- /dev/null
@@ -0,0 +1,33 @@
+// Copyright 2018 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 hugolib
+
+import (
+       "fmt"
+       "testing"
+
+       "github.com/gohugoio/hugo/helpers"
+       "github.com/stretchr/testify/require"
+)
+
+func TestHugoInfo(t *testing.T) {
+       assert := require.New(t)
+
+       assert.Equal(helpers.CurrentHugoVersion.Version(), hugoInfo.Version)
+       assert.IsType(helpers.HugoVersionString(""), hugoInfo.Version)
+       assert.Equal(CommitHash, hugoInfo.CommitHash)
+       assert.Equal(BuildDate, hugoInfo.BuildDate)
+       assert.Contains(hugoInfo.Generator, fmt.Sprintf("Hugo %s", hugoInfo.Version))
+
+}
index 65228da995c18142e752b8c93171a1cdef6383dd..b228040cdc920068c8ad56eb47318ae8b213742c 100644 (file)
@@ -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)
index c9bc2ffe9324e66c76d583104f627d5575530a66..d201c995b57413ecf9209767a84127d0ddbccad8 100644 (file)
@@ -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
index 7d58cf9aba73dea9ce80b5af4b25d57932bc5ca6..f766ef890f90a27444f85c7043461b75cfc7b8b2 100644 (file)
@@ -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,
index 32f1a1a719400966caceef1ae470f8e47967efc6..b8390c5b92e8d64184507e383191c1b79008bb74 100644 (file)
@@ -80,12 +80,14 @@ func TestTemplateFuncsExamples(t *testing.T) {
        var data struct {
                Title   string
                Section string
+               Hugo    map[string]interface{}
                Params  map[string]interface{}
        }
 
        data.Title = "**BatMan**"
        data.Section = "blog"
        data.Params = map[string]interface{}{"langCode": "en"}
+       data.Hugo = map[string]interface{}{"Version": helpers.MustParseHugoVersion("0.36.1").Version()}
 
        for _, nsf := range internal.TemplateFuncsNamespaceRegistry {
                ns := nsf(d)