summaryrefslogtreecommitdiffstats
path: root/metrics
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-04-02 10:30:24 +0200
committerGitHub <noreply@github.com>2019-04-02 10:30:24 +0200
commita55640de8e3944d3b9f64b15155148a0e35cb31e (patch)
tree3fe07277c5f7f675571c15851ce9fdc96a2bcecd /metrics
parent9225db636e2f9b75f992013a25c0b149d6bd8b0d (diff)
tpl: Allow the partial template func to return any type
This commit adds support for return values in partials. This means that you can now do this and similar: {{ $v := add . 42 }} {{ return $v }} Partials without a `return` statement will be rendered as before. This works for both `partial` and `partialCached`. Fixes #5783
Diffstat (limited to 'metrics')
-rw-r--r--metrics/metrics.go52
1 files changed, 44 insertions, 8 deletions
diff --git a/metrics/metrics.go b/metrics/metrics.go
index c83610a92..e67b16bda 100644
--- a/metrics/metrics.go
+++ b/metrics/metrics.go
@@ -23,6 +23,12 @@ import (
"strings"
"sync"
"time"
+
+ "github.com/gohugoio/hugo/compare"
+
+ "github.com/gohugoio/hugo/common/hreflect"
+
+ "github.com/spf13/cast"
)
// The Provider interface defines an interface for measuring metrics.
@@ -35,20 +41,20 @@ type Provider interface {
WriteMetrics(w io.Writer)
// TrackValue tracks the value for diff calculations etc.
- TrackValue(key, value string)
+ TrackValue(key string, value interface{})
// Reset clears the metric store.
Reset()
}
type diff struct {
- baseline string
+ baseline interface{}
count int
simSum int
}
-func (d *diff) add(v string) *diff {
- if d.baseline == "" {
+func (d *diff) add(v interface{}) *diff {
+ if !hreflect.IsTruthful(v) {
d.baseline = v
d.count = 1
d.simSum = 100 // If we get only one it is very cache friendly.
@@ -90,7 +96,7 @@ func (s *Store) Reset() {
}
// TrackValue tracks the value for diff calculations etc.
-func (s *Store) TrackValue(key, value string) {
+func (s *Store) TrackValue(key string, value interface{}) {
if !s.calculateHints {
return
}
@@ -191,13 +197,43 @@ func (b bySum) Less(i, j int) bool { return b[i].sum > b[j].sum }
// howSimilar is a naive diff implementation that returns
// a number between 0-100 indicating how similar a and b are.
-// 100 is when all words in a also exists in b.
-func howSimilar(a, b string) int {
-
+func howSimilar(a, b interface{}) int {
if a == b {
return 100
}
+ as, err1 := cast.ToStringE(a)
+ bs, err2 := cast.ToStringE(b)
+
+ if err1 == nil && err2 == nil {
+ return howSimilarStrings(as, bs)
+ }
+
+ if err1 != err2 {
+ return 0
+ }
+
+ e1, ok1 := a.(compare.Eqer)
+ e2, ok2 := b.(compare.Eqer)
+ if ok1 && ok2 && e1.Eq(e2) {
+ return 100
+ }
+
+ // TODO(bep) implement ProbablyEq for Pages etc.
+ pe1, pok1 := a.(compare.ProbablyEqer)
+ pe2, pok2 := b.(compare.ProbablyEqer)
+ if pok1 && pok2 && pe1.ProbablyEq(pe2) {
+ return 90
+ }
+
+ return 0
+}
+
+// howSimilar is a naive diff implementation that returns
+// a number between 0-100 indicating how similar a and b are.
+// 100 is when all words in a also exists in b.
+func howSimilarStrings(a, b string) int {
+
// Give some weight to the word positions.
const partitionSize = 4