diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-03-17 22:03:27 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-03-17 22:03:27 +0100 |
commit | b80853de90b10171155b8f3fde47d64ec7bfa0dd (patch) | |
tree | 435d3dbf7a495a0c6ce64c9769e037179aa0d27b /tpl/strings | |
parent | 423594e03a906ef4150f433666ff588b022c3c92 (diff) |
all: gofmt -w -r 'interface{} -> any' .
Updates #9687
Diffstat (limited to 'tpl/strings')
-rw-r--r-- | tpl/strings/init.go | 2 | ||||
-rw-r--r-- | tpl/strings/regexp.go | 4 | ||||
-rw-r--r-- | tpl/strings/regexp_test.go | 18 | ||||
-rw-r--r-- | tpl/strings/strings.go | 46 | ||||
-rw-r--r-- | tpl/strings/strings_test.go | 114 | ||||
-rw-r--r-- | tpl/strings/truncate.go | 4 | ||||
-rw-r--r-- | tpl/strings/truncate_test.go | 8 |
7 files changed, 98 insertions, 98 deletions
diff --git a/tpl/strings/init.go b/tpl/strings/init.go index 384a5cda7..a11246e1c 100644 --- a/tpl/strings/init.go +++ b/tpl/strings/init.go @@ -26,7 +26,7 @@ func init() { ns := &internal.TemplateFuncsNamespace{ Name: name, - Context: func(args ...interface{}) (interface{}, error) { return ctx, nil }, + Context: func(args ...any) (any, error) { return ctx, nil }, } ns.AddMethodMapping(ctx.Chomp, diff --git a/tpl/strings/regexp.go b/tpl/strings/regexp.go index c6d731a0d..5b6a812d4 100644 --- a/tpl/strings/regexp.go +++ b/tpl/strings/regexp.go @@ -22,7 +22,7 @@ import ( // FindRE returns a list of strings that match the regular expression. By default all matches // will be included. The number of matches can be limited with an optional third parameter. -func (ns *Namespace) FindRE(expr string, content interface{}, limit ...interface{}) ([]string, error) { +func (ns *Namespace) FindRE(expr string, content any, limit ...any) ([]string, error) { re, err := reCache.Get(expr) if err != nil { return nil, err @@ -48,7 +48,7 @@ func (ns *Namespace) FindRE(expr string, content interface{}, limit ...interface // ReplaceRE returns a copy of s, replacing all matches of the regular // expression pattern with the replacement text repl. The number of replacements // can be limited with an optional fourth parameter. -func (ns *Namespace) ReplaceRE(pattern, repl, s interface{}, n ...interface{}) (_ string, err error) { +func (ns *Namespace) ReplaceRE(pattern, repl, s any, n ...any) (_ string, err error) { sp, err := cast.ToStringE(pattern) if err != nil { return diff --git a/tpl/strings/regexp_test.go b/tpl/strings/regexp_test.go index 433181f67..9ac098c17 100644 --- a/tpl/strings/regexp_test.go +++ b/tpl/strings/regexp_test.go @@ -25,9 +25,9 @@ func TestFindRE(t *testing.T) { for _, test := range []struct { expr string - content interface{} - limit interface{} - expect interface{} + content any + limit any + expect any }{ {"[G|g]o", "Hugo is a static site generator written in Go.", 2, []string{"go", "Go"}}, {"[G|g]o", "Hugo is a static site generator written in Go.", -1, []string{"go", "Go"}}, @@ -55,16 +55,16 @@ func TestReplaceRE(t *testing.T) { c := qt.New(t) for _, test := range []struct { - pattern interface{} - repl interface{} - s interface{} - n []interface{} - expect interface{} + pattern any + repl any + s any + n []any + expect any }{ {"^https?://([^/]+).*", "$1", "http://gohugo.io/docs", nil, "gohugo.io"}, {"^https?://([^/]+).*", "$2", "http://gohugo.io/docs", nil, ""}, {"(ab)", "AB", "aabbaab", nil, "aABbaAB"}, - {"(ab)", "AB", "aabbaab", []interface{}{1}, "aABbaab"}, + {"(ab)", "AB", "aabbaab", []any{1}, "aABbaab"}, // errors {"(ab", "AB", "aabb", nil, false}, // invalid re {tstNoStringer{}, "$2", "http://gohugo.io/docs", nil, false}, diff --git a/tpl/strings/strings.go b/tpl/strings/strings.go index 2575b2fee..af7c34a59 100644 --- a/tpl/strings/strings.go +++ b/tpl/strings/strings.go @@ -45,7 +45,7 @@ type Namespace struct { } // CountRunes returns the number of runes in s, excluding whitespace. -func (ns *Namespace) CountRunes(s interface{}) (int, error) { +func (ns *Namespace) CountRunes(s any) (int, error) { ss, err := cast.ToStringE(s) if err != nil { return 0, _errors.Wrap(err, "Failed to convert content to string") @@ -62,7 +62,7 @@ func (ns *Namespace) CountRunes(s interface{}) (int, error) { } // RuneCount returns the number of runes in s. -func (ns *Namespace) RuneCount(s interface{}) (int, error) { +func (ns *Namespace) RuneCount(s any) (int, error) { ss, err := cast.ToStringE(s) if err != nil { return 0, _errors.Wrap(err, "Failed to convert content to string") @@ -71,7 +71,7 @@ func (ns *Namespace) RuneCount(s interface{}) (int, error) { } // CountWords returns the approximate word count in s. -func (ns *Namespace) CountWords(s interface{}) (int, error) { +func (ns *Namespace) CountWords(s any) (int, error) { ss, err := cast.ToStringE(s) if err != nil { return 0, _errors.Wrap(err, "Failed to convert content to string") @@ -101,7 +101,7 @@ func (ns *Namespace) CountWords(s interface{}) (int, error) { // Count counts the number of non-overlapping instances of substr in s. // If substr is an empty string, Count returns 1 + the number of Unicode code points in s. -func (ns *Namespace) Count(substr, s interface{}) (int, error) { +func (ns *Namespace) Count(substr, s any) (int, error) { substrs, err := cast.ToStringE(substr) if err != nil { return 0, _errors.Wrap(err, "Failed to convert substr to string") @@ -114,7 +114,7 @@ func (ns *Namespace) Count(substr, s interface{}) (int, error) { } // Chomp returns a copy of s with all trailing newline characters removed. -func (ns *Namespace) Chomp(s interface{}) (interface{}, error) { +func (ns *Namespace) Chomp(s any) (any, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -130,7 +130,7 @@ func (ns *Namespace) Chomp(s interface{}) (interface{}, error) { } // Contains reports whether substr is in s. -func (ns *Namespace) Contains(s, substr interface{}) (bool, error) { +func (ns *Namespace) Contains(s, substr any) (bool, error) { ss, err := cast.ToStringE(s) if err != nil { return false, err @@ -145,7 +145,7 @@ func (ns *Namespace) Contains(s, substr interface{}) (bool, error) { } // ContainsAny reports whether any Unicode code points in chars are within s. -func (ns *Namespace) ContainsAny(s, chars interface{}) (bool, error) { +func (ns *Namespace) ContainsAny(s, chars any) (bool, error) { ss, err := cast.ToStringE(s) if err != nil { return false, err @@ -160,7 +160,7 @@ func (ns *Namespace) ContainsAny(s, chars interface{}) (bool, error) { } // HasPrefix tests whether the input s begins with prefix. -func (ns *Namespace) HasPrefix(s, prefix interface{}) (bool, error) { +func (ns *Namespace) HasPrefix(s, prefix any) (bool, error) { ss, err := cast.ToStringE(s) if err != nil { return false, err @@ -175,7 +175,7 @@ func (ns *Namespace) HasPrefix(s, prefix interface{}) (bool, error) { } // HasSuffix tests whether the input s begins with suffix. -func (ns *Namespace) HasSuffix(s, suffix interface{}) (bool, error) { +func (ns *Namespace) HasSuffix(s, suffix any) (bool, error) { ss, err := cast.ToStringE(s) if err != nil { return false, err @@ -192,7 +192,7 @@ func (ns *Namespace) HasSuffix(s, suffix interface{}) (bool, error) { // Replace returns a copy of the string s with all occurrences of old replaced // with new. The number of replacements can be limited with an optional fourth // parameter. -func (ns *Namespace) Replace(s, old, new interface{}, limit ...interface{}) (string, error) { +func (ns *Namespace) Replace(s, old, new any, limit ...any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -223,7 +223,7 @@ func (ns *Namespace) Replace(s, old, new interface{}, limit ...interface{}) (str // SliceString slices a string by specifying a half-open range with // two indices, start and end. 1 and 4 creates a slice including elements 1 through 3. // The end index can be omitted, it defaults to the string's length. -func (ns *Namespace) SliceString(a interface{}, startEnd ...interface{}) (string, error) { +func (ns *Namespace) SliceString(a any, startEnd ...any) (string, error) { aStr, err := cast.ToStringE(a) if err != nil { return "", err @@ -267,7 +267,7 @@ func (ns *Namespace) SliceString(a interface{}, startEnd ...interface{}) (string } // Split slices an input string into all substrings separated by delimiter. -func (ns *Namespace) Split(a interface{}, delimiter string) ([]string, error) { +func (ns *Namespace) Split(a any, delimiter string) ([]string, error) { aStr, err := cast.ToStringE(a) if err != nil { return []string{}, err @@ -288,7 +288,7 @@ func (ns *Namespace) Split(a interface{}, delimiter string) ([]string, error) { // In addition, borrowing from the extended behavior described at http://php.net/substr, // if length is given and is negative, then that many characters will be omitted from // the end of string. -func (ns *Namespace) Substr(a interface{}, nums ...interface{}) (string, error) { +func (ns *Namespace) Substr(a any, nums ...any) (string, error) { s, err := cast.ToStringE(a) if err != nil { return "", err @@ -363,7 +363,7 @@ func (ns *Namespace) Substr(a interface{}, nums ...interface{}) (string, error) // Title returns a copy of the input s with all Unicode letters that begin words // mapped to their title case. -func (ns *Namespace) Title(s interface{}) (string, error) { +func (ns *Namespace) Title(s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -373,7 +373,7 @@ func (ns *Namespace) Title(s interface{}) (string, error) { } // FirstUpper returns a string with the first character as upper case. -func (ns *Namespace) FirstUpper(s interface{}) (string, error) { +func (ns *Namespace) FirstUpper(s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -384,7 +384,7 @@ func (ns *Namespace) FirstUpper(s interface{}) (string, error) { // ToLower returns a copy of the input s with all Unicode letters mapped to their // lower case. -func (ns *Namespace) ToLower(s interface{}) (string, error) { +func (ns *Namespace) ToLower(s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -395,7 +395,7 @@ func (ns *Namespace) ToLower(s interface{}) (string, error) { // ToUpper returns a copy of the input s with all Unicode letters mapped to their // upper case. -func (ns *Namespace) ToUpper(s interface{}) (string, error) { +func (ns *Namespace) ToUpper(s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -406,7 +406,7 @@ func (ns *Namespace) ToUpper(s interface{}) (string, error) { // Trim returns a string with all leading and trailing characters defined // contained in cutset removed. -func (ns *Namespace) Trim(s, cutset interface{}) (string, error) { +func (ns *Namespace) Trim(s, cutset any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -422,7 +422,7 @@ func (ns *Namespace) Trim(s, cutset interface{}) (string, error) { // TrimLeft returns a slice of the string s with all leading characters // contained in cutset removed. -func (ns *Namespace) TrimLeft(cutset, s interface{}) (string, error) { +func (ns *Namespace) TrimLeft(cutset, s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -438,7 +438,7 @@ func (ns *Namespace) TrimLeft(cutset, s interface{}) (string, error) { // TrimPrefix returns s without the provided leading prefix string. If s doesn't // start with prefix, s is returned unchanged. -func (ns *Namespace) TrimPrefix(prefix, s interface{}) (string, error) { +func (ns *Namespace) TrimPrefix(prefix, s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -454,7 +454,7 @@ func (ns *Namespace) TrimPrefix(prefix, s interface{}) (string, error) { // TrimRight returns a slice of the string s with all trailing characters // contained in cutset removed. -func (ns *Namespace) TrimRight(cutset, s interface{}) (string, error) { +func (ns *Namespace) TrimRight(cutset, s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -470,7 +470,7 @@ func (ns *Namespace) TrimRight(cutset, s interface{}) (string, error) { // TrimSuffix returns s without the provided trailing suffix string. If s // doesn't end with suffix, s is returned unchanged. -func (ns *Namespace) TrimSuffix(suffix, s interface{}) (string, error) { +func (ns *Namespace) TrimSuffix(suffix, s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err @@ -485,7 +485,7 @@ func (ns *Namespace) TrimSuffix(suffix, s interface{}) (string, error) { } // Repeat returns a new string consisting of count copies of the string s. -func (ns *Namespace) Repeat(n, s interface{}) (string, error) { +func (ns *Namespace) Repeat(n, s any) (string, error) { ss, err := cast.ToStringE(s) if err != nil { return "", err diff --git a/tpl/strings/strings_test.go b/tpl/strings/strings_test.go index 18c033793..7e3960934 100644 --- a/tpl/strings/strings_test.go +++ b/tpl/strings/strings_test.go @@ -33,8 +33,8 @@ func TestChomp(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - expect interface{} + s any + expect any }{ {"\n a\n", "\n a"}, {"\n a\n\n", "\n a"}, @@ -68,8 +68,8 @@ func TestContains(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - substr interface{} + s any + substr any expect bool isErr bool }{ @@ -106,8 +106,8 @@ func TestContainsAny(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - substr interface{} + s any + substr any expect bool isErr bool }{ @@ -150,8 +150,8 @@ func TestCountRunes(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - expect interface{} + s any + expect any }{ {"foo bar", 6}, {"旁边", 2}, @@ -177,8 +177,8 @@ func TestRuneCount(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - expect interface{} + s any + expect any }{ {"foo bar", 7}, {"旁边", 2}, @@ -204,8 +204,8 @@ func TestCountWords(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - expect interface{} + s any + expect any }{ {"Do Be Do Be Do", 5}, {"旁边", 2}, @@ -234,9 +234,9 @@ func TestHasPrefix(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - prefix interface{} - expect interface{} + s any + prefix any + expect any isErr bool }{ {"abcd", "ab", true, false}, @@ -268,9 +268,9 @@ func TestHasSuffix(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - suffix interface{} - expect interface{} + s any + suffix any + expect any isErr bool }{ {"abcd", "cd", true, false}, @@ -302,11 +302,11 @@ func TestReplace(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - old interface{} - new interface{} - limit interface{} - expect interface{} + s any + old any + new any + limit any + expect any }{ {"aab", "a", "b", nil, "bbb"}, {"11a11", 1, 2, nil, "22a22"}, @@ -346,10 +346,10 @@ func TestSliceString(t *testing.T) { var err error for _, test := range []struct { - v1 interface{} - v2 interface{} - v3 interface{} - expect interface{} + v1 any + v2 any + v3 any + expect any }{ {"abc", 1, 2, "b"}, {"abc", 1, 3, "bc"}, @@ -408,9 +408,9 @@ func TestSplit(t *testing.T) { c := qt.New(t) for _, test := range []struct { - v1 interface{} + v1 any v2 string - expect interface{} + expect any }{ {"a, b", ", ", []string{"a", "b"}}, {"a & b & c", " & ", []string{"a", "b", "c"}}, @@ -437,10 +437,10 @@ func TestSubstr(t *testing.T) { var err error for _, test := range []struct { - v1 interface{} - v2 interface{} - v3 interface{} - expect interface{} + v1 any + v2 any + v3 any + expect any }{ {"abc", 1, 2, "bc"}, {"abc", 0, 1, "a"}, @@ -511,8 +511,8 @@ func TestTitle(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - expect interface{} + s any + expect any }{ {"test", "Test"}, {template.HTML("hypertext"), "Hypertext"}, @@ -538,8 +538,8 @@ func TestToLower(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - expect interface{} + s any + expect any }{ {"TEST", "test"}, {template.HTML("LoWeR"), "lower"}, @@ -565,8 +565,8 @@ func TestToUpper(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - expect interface{} + s any + expect any }{ {"test", "TEST"}, {template.HTML("UpPeR"), "UPPER"}, @@ -592,9 +592,9 @@ func TestTrim(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - cutset interface{} - expect interface{} + s any + cutset any + expect any }{ {"abba", "a", "bb"}, {"abba", "ab", ""}, @@ -626,9 +626,9 @@ func TestTrimLeft(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - cutset interface{} - expect interface{} + s any + cutset any + expect any }{ {"abba", "a", "bba"}, {"abba", "ab", ""}, @@ -661,9 +661,9 @@ func TestTrimPrefix(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - prefix interface{} - expect interface{} + s any + prefix any + expect any }{ {"aabbaa", "a", "abbaa"}, {"aabb", "b", "aabb"}, @@ -691,9 +691,9 @@ func TestTrimRight(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - cutset interface{} - expect interface{} + s any + cutset any + expect any }{ {"abba", "a", "abb"}, {"abba", "ab", ""}, @@ -726,9 +726,9 @@ func TestTrimSuffix(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - suffix interface{} - expect interface{} + s any + suffix any + expect any }{ {"aabbaa", "a", "aabba"}, {"aabb", "b", "aab"}, @@ -756,9 +756,9 @@ func TestRepeat(t *testing.T) { c := qt.New(t) for _, test := range []struct { - s interface{} - n interface{} - expect interface{} + s any + n any + expect any }{ {"yo", "2", "yoyo"}, {"~", "16", "~~~~~~~~~~~~~~~~"}, diff --git a/tpl/strings/truncate.go b/tpl/strings/truncate.go index 6e3a50ed2..dd6267280 100644 --- a/tpl/strings/truncate.go +++ b/tpl/strings/truncate.go @@ -40,12 +40,12 @@ type htmlTag struct { } // Truncate truncates a given string to the specified length. -func (ns *Namespace) Truncate(a interface{}, options ...interface{}) (template.HTML, error) { +func (ns *Namespace) Truncate(a any, options ...any) (template.HTML, error) { length, err := cast.ToIntE(a) if err != nil { return "", err } - var textParam interface{} + var textParam any var ellipsis string switch len(options) { diff --git a/tpl/strings/truncate_test.go b/tpl/strings/truncate_test.go index 51743e935..f7d5d132d 100644 --- a/tpl/strings/truncate_test.go +++ b/tpl/strings/truncate_test.go @@ -25,10 +25,10 @@ func TestTruncate(t *testing.T) { var err error cases := []struct { - v1 interface{} - v2 interface{} - v3 interface{} - want interface{} + v1 any + v2 any + v3 any + want any isErr bool }{ {10, "I am a test sentence", nil, template.HTML("I am a …"), false}, |