diff --git a/go.mod b/go.mod index 7c4f87b..027526a 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.24 require ( github.com/bradleyjkemp/cupaloy v2.3.0+incompatible github.com/google/go-cmp v0.7.0 - github.com/samber/lo v1.50.0 + github.com/samber/lo v1.51.0 github.com/stretchr/testify v1.10.0 golang.org/x/crypto v0.39.0 golang.org/x/text v0.26.0 diff --git a/go.sum b/go.sum index 0325ca7..eb2cf45 100644 --- a/go.sum +++ b/go.sum @@ -4,14 +4,10 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= -github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= -github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/samber/lo v1.50.0 h1:XrG0xOeHs+4FQ8gJR97zDz5uOFMW7OwFWiFVzqopKgY= -github.com/samber/lo v1.50.0/go.mod h1:RjZyNk6WSnUFRKK6EyOhsRJMqft3G+pg7dCWHQCWvsc= +github.com/samber/lo v1.51.0 h1:kysRYLbHy/MB7kQZf5DSN50JHmMsNEdeY24VzJFu7wI= +github.com/samber/lo v1.51.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM= @@ -20,8 +16,7 @@ golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M= golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= -gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/vendor/github.com/samber/lo/README.md b/vendor/github.com/samber/lo/README.md index 62e3bc8..87d2550 100644 --- a/vendor/github.com/samber/lo/README.md +++ b/vendor/github.com/samber/lo/README.md @@ -12,11 +12,9 @@ ✨ **`samber/lo` is a Lodash-style Go library based on Go 1.18+ Generics.** -This project started as an experiment with the new generics implementation. It may look like [Lodash](https://github.com/lodash/lodash) in some aspects. I used to code with the fantastic ["go-funk"](https://github.com/thoas/go-funk) package, but "go-funk" uses reflection and therefore is not typesafe. +A utility library based on Go 1.18+ generics that makes it easier to work with slices, maps, strings, channels, and functions. It provides dozens of handy methods to simplify common coding tasks and improve code readability. It may look like [Lodash](https://github.com/lodash/lodash) in some aspects. -As expected, benchmarks demonstrate that generics are much faster than implementations based on the "reflect" package. Benchmarks also show similar performance gains compared to pure `for` loops. [See below](#-benchmark). - -In the future, 5 to 10 helpers will overlap with those coming into the Go standard library (under package names `slices` and `maps`). I feel this library is legitimate and offers many more valuable abstractions. +5 to 10 helpers may overlap with those from the Go standard library, in packages `slices` and `maps`. I feel this library is legitimate and offers many more valuable abstractions. **See also:** @@ -25,7 +23,7 @@ In the future, 5 to 10 helpers will overlap with those coming into the Go standa **Why this name?** -I wanted a **short name**, similar to "Lodash" and no Go package uses this name. +I wanted a **short name**, similar to "Lodash", and no Go package uses this name. ![lo](img/logo-full.png) @@ -60,8 +58,6 @@ names := lo.Uniq([]string{"Samuel", "John", "Samuel"}) // []string{"Samuel", "John"} ``` -Most of the time, the compiler will be able to infer the type so that you can call: `lo.Uniq([]string{...})`. - ### Tips for lazy developers I cannot recommend it, but in case you are too lazy for repeating `lo.` everywhere, you can import the entire library into the namespace. @@ -330,6 +326,8 @@ Error handling: - [TryWithErrorValue](#trywitherrorvalue) - [TryCatchWithErrorValue](#trycatchwitherrorvalue) - [ErrorsAs](#errorsas) +- [Assert](#assert) +- [Assertf](#assertf) Constraints: @@ -398,7 +396,7 @@ import lom "github.com/samber/lo/mutable" list := []int{1, 2, 3, 4} lom.Map(list, func(x int) int { - return i*2 + return x*2 }) // []int{2, 4, 6, 8} ``` @@ -1775,7 +1773,7 @@ str := lo.Capitalize("heLLO") ### Ellipsis -Trims and truncates a string to a specified length and appends an ellipsis if truncated. +Trims and truncates a string to a specified length **in bytes** and appends an ellipsis if truncated. If the string contains non-ASCII characters (which may occupy multiple bytes in UTF-8), truncating by byte length may split a character in the middle, potentially resulting in garbled output. ```go str := lo.Ellipsis(" Lorem Ipsum ", 5) @@ -2777,7 +2775,7 @@ Search the maximum time.Time of a collection. Returns zero value when the collection is empty. ```go -latest := lo.Latest([]time.Time{time.Now(), time.Time{}}) +latest := lo.Latest(time.Now(), time.Time{}) // 2023-04-01 01:02:03 +0000 UTC ``` @@ -2850,7 +2848,7 @@ last, ok := lo.Last([]int{}) ### LastOrEmpty -Returns the first element of a collection or zero value if empty. +Returns the last element of a collection or zero value if empty. ```go last := lo.LastOrEmpty([]int{1, 2, 3}) @@ -2862,7 +2860,7 @@ last := lo.LastOrEmpty([]int{}) ### LastOr -Returns the first element of a collection or the fallback value if empty. +Returns the last element of a collection or the fallback value if empty. ```go last := lo.LastOr([]int{1, 2, 3}, 245) @@ -4099,6 +4097,40 @@ if rateLimitErr, ok := lo.ErrorsAs[*RateLimitError](err); ok { [[play](https://go.dev/play/p/8wk5rH8UfrE)] +### Assert + +Does nothing when the condition is `true`, otherwise it panics with an optional message. + +Think twice before using it, given that [Go intentionally omits assertions from its standard library](https://go.dev/doc/faq#assertions). + +```go +age := getUserAge() + +lo.Assert(age >= 15) +``` + +```go +age := getUserAge() + +lo.Assert(age >= 15, "user age must be >= 15") +``` + +[[play](https://go.dev/play/p/Xv8LLKBMNwI)] + +### Assertf + +Like `Assert`, but with `fmt.Printf`-like formatting. + +Think twice before using it, given that [Go intentionally omits assertions from its standard library](https://go.dev/doc/faq#assertions). + +```go +age := getUserAge() + +lo.Assertf(age >= 15, "user age must be >= 15, got %d", age) +``` + +[[play](https://go.dev/play/p/TVPEmVcyrdY)] + ## 🛩 Benchmark We executed a simple benchmark with a dead-simple `lo.Map` loop: diff --git a/vendor/github.com/samber/lo/errors.go b/vendor/github.com/samber/lo/errors.go index 493580b..a234721 100644 --- a/vendor/github.com/samber/lo/errors.go +++ b/vendor/github.com/samber/lo/errors.go @@ -6,6 +6,8 @@ import ( "reflect" ) +const defaultAssertionFailureMessage = "assertion failed" + // Validate is a helper that creates an error when a condition is not met. // Play: https://go.dev/play/p/vPyh51XpCBt func Validate(ok bool, format string, args ...any) error { @@ -352,3 +354,28 @@ func ErrorsAs[T error](err error) (T, bool) { ok := errors.As(err, &t) return t, ok } + +// Assert does nothing when the condition is true, otherwise it panics with an optional message. +// Play: https://go.dev/play/p/Xv8LLKBMNwI +func Assert(condition bool, message ...string) { + if condition { + return + } + + panicMessage := defaultAssertionFailureMessage + if len(message) > 0 { + panicMessage = fmt.Sprintf("%s: %s", defaultAssertionFailureMessage, message[0]) + } + panic(panicMessage) +} + +// Assertf does nothing when the condition is true, otherwise it panics with a formatted message. +// Play: https://go.dev/play/p/TVPEmVcyrdY +func Assertf(condition bool, format string, args ...any) { + if condition { + return + } + + panicMessage := fmt.Sprintf("%s: %s", defaultAssertionFailureMessage, fmt.Sprintf(format, args...)) + panic(panicMessage) +} diff --git a/vendor/github.com/samber/lo/string.go b/vendor/github.com/samber/lo/string.go index 6a10955..923faa3 100644 --- a/vendor/github.com/samber/lo/string.go +++ b/vendor/github.com/samber/lo/string.go @@ -210,7 +210,9 @@ func Capitalize(str string) string { return cases.Title(language.English).String(str) } -// Ellipsis trims and truncates a string to a specified length and appends an ellipsis if truncated. +// Ellipsis trims and truncates a string to a specified length **in bytes** and appends an ellipsis +// if truncated. If the string contains non-ASCII characters (which may occupy multiple bytes in UTF-8), +// truncating by byte length may split a character in the middle, potentially resulting in garbled output. func Ellipsis(str string, length int) string { str = strings.TrimSpace(str) diff --git a/vendor/modules.txt b/vendor/modules.txt index 4f8e6fd..3ed4139 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -14,7 +14,7 @@ github.com/google/go-cmp/cmp/internal/value # github.com/pmezard/go-difflib v1.0.0 ## explicit github.com/pmezard/go-difflib/difflib -# github.com/samber/lo v1.50.0 +# github.com/samber/lo v1.51.0 ## explicit; go 1.18 github.com/samber/lo github.com/samber/lo/internal/constraints