From 7a62c6ae8af2f61d11c8afed2d3ef3f083ef271d Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Tue, 26 Sep 2023 11:22:54 -0500 Subject: [PATCH] Use new go 1.21 builtins --- envs/dates.go | 2 +- excellent/errors.go | 3 +-- flows/channel.go | 2 +- .../definition/legacy/expressions/functions.go | 4 +--- flows/environment.go | 2 +- flows/msg.go | 2 +- flows/runs/legacy.go | 2 +- utils/misc.go | 18 +----------------- utils/misc_test.go | 16 ---------------- 9 files changed, 8 insertions(+), 43 deletions(-) diff --git a/envs/dates.go b/envs/dates.go index 28b571545..f89e49649 100644 --- a/envs/dates.go +++ b/envs/dates.go @@ -158,7 +158,7 @@ func parseDate(env Environment, str string) (dates.Date, string, error) { str = strings.Trim(str, " \n\r\t") // try to parse as ISO date - asISO, err := time.ParseInLocation(iso8601DateOnlyFormat, str[0:utils.Min(len(iso8601DateOnlyFormat), len(str))], env.Timezone()) + asISO, err := time.ParseInLocation(iso8601DateOnlyFormat, str[0:min(len(iso8601DateOnlyFormat), len(str))], env.Timezone()) if err == nil { return dates.ExtractDate(asISO), str[len(iso8601DateOnlyFormat):], nil } diff --git a/excellent/errors.go b/excellent/errors.go index 3ed0a56f0..3774641a3 100644 --- a/excellent/errors.go +++ b/excellent/errors.go @@ -5,7 +5,6 @@ import ( "strings" "github.com/antlr/antlr4/runtime/Go/antlr/v4" - "github.com/nyaruka/goflow/utils" "github.com/pkg/errors" ) @@ -71,7 +70,7 @@ func (l *ErrorListener) SyntaxError(recognizer antlr.Recognizer, offendingSymbol // extract the part of the original expression where this error has occurred lines := strings.Split(l.expression, "\n") lineOfError := lines[line-1] - contextOfError := lineOfError[column:utils.Min(column+10, len(lineOfError))] + contextOfError := lineOfError[column:min(column+10, len(lineOfError))] l.errors = append(l.errors, errors.Errorf("syntax error at %s", contextOfError)) } diff --git a/flows/channel.go b/flows/channel.go index e83fd623a..1d78fed72 100644 --- a/flows/channel.go +++ b/flows/channel.go @@ -2,6 +2,7 @@ package flows import ( "fmt" + "slices" "strings" "github.com/nyaruka/gocommon/i18n" @@ -10,7 +11,6 @@ import ( "github.com/nyaruka/goflow/envs" "github.com/nyaruka/goflow/excellent/types" "github.com/nyaruka/goflow/utils" - "golang.org/x/exp/slices" ) // Channel represents a means for sending and receiving input during a flow run diff --git a/flows/definition/legacy/expressions/functions.go b/flows/definition/legacy/expressions/functions.go index 1d09fc1e5..beb4e2304 100644 --- a/flows/definition/legacy/expressions/functions.go +++ b/flows/definition/legacy/expressions/functions.go @@ -5,8 +5,6 @@ import ( "strconv" "strings" - "github.com/nyaruka/goflow/utils" - "github.com/pkg/errors" ) @@ -66,7 +64,7 @@ func asParamMigratorsWithDefaults(newName string, defaults []string, paramMigrat return "", errors.Errorf("don't know how to migrate call to %s with %d parameters", funcName, len(oldParams)) } - newParams := make([]string, utils.Max(len(oldParams), len(defaults))) + newParams := make([]string, max(len(oldParams), len(defaults))) for i := range newParams { var param string diff --git a/flows/environment.go b/flows/environment.go index 222369660..de85c3d96 100644 --- a/flows/environment.go +++ b/flows/environment.go @@ -2,13 +2,13 @@ package flows import ( "regexp" + "slices" "strings" "time" "github.com/nyaruka/gocommon/i18n" "github.com/nyaruka/goflow/assets" "github.com/nyaruka/goflow/envs" - "golang.org/x/exp/slices" ) type assetsEnvironment struct { diff --git a/flows/msg.go b/flows/msg.go index edf7ee0ca..924c00327 100644 --- a/flows/msg.go +++ b/flows/msg.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "slices" "github.com/go-playground/validator/v10" "github.com/nyaruka/gocommon/i18n" @@ -13,7 +14,6 @@ import ( "github.com/nyaruka/goflow/assets" "github.com/nyaruka/goflow/envs" "github.com/nyaruka/goflow/utils" - "golang.org/x/exp/slices" ) func init() { diff --git a/flows/runs/legacy.go b/flows/runs/legacy.go index b92cbed37..69d8a2da2 100644 --- a/flows/runs/legacy.go +++ b/flows/runs/legacy.go @@ -18,7 +18,7 @@ var invalidLegacyExtraKeyChars = regexp.MustCompile(`[^a-zA-Z0-9_]`) // keys in legacy @extra have non-word chars replaced with underscores and are limited to 255 chars func legacyExtraKey(key string) string { key = invalidLegacyExtraKeyChars.ReplaceAllString(strings.ToLower(key), "_") - return key[0:utils.Min(len(key), 255)] + return key[0:min(len(key), 255)] } type legacyExtra struct { diff --git a/utils/misc.go b/utils/misc.go index 05c4043b4..736b28cfb 100644 --- a/utils/misc.go +++ b/utils/misc.go @@ -2,10 +2,10 @@ package utils import ( "reflect" + "slices" "golang.org/x/exp/constraints" "golang.org/x/exp/maps" - "golang.org/x/exp/slices" ) // IsNil returns whether the given object is nil or an interface to a nil @@ -25,22 +25,6 @@ func IsNil(v any) bool { return false } -// Max returns the maximum of two values -func Max[T constraints.Ordered](x, y T) T { - if x > y { - return x - } - return y -} - -// Min returns the minimum of two values -func Min[T constraints.Ordered](x, y T) T { - if x < y { - return x - } - return y -} - // Set converts a slice to a set (a K > bool map) func Set[K constraints.Ordered](s []K) map[K]bool { m := make(map[K]bool, len(s)) diff --git a/utils/misc_test.go b/utils/misc_test.go index cb5f6f960..d0b43dca4 100644 --- a/utils/misc_test.go +++ b/utils/misc_test.go @@ -14,22 +14,6 @@ func TestIsNil(t *testing.T) { assert.False(t, utils.IsNil("")) } -func TestMax(t *testing.T) { - assert.Equal(t, 1, utils.Max(0, 1)) - assert.Equal(t, 1, utils.Max(1, 0)) - assert.Equal(t, 1, utils.Max(1, -1)) - - assert.Equal(t, uint16(1), utils.Max(uint16(0), uint16(1))) -} - -func TestMin(t *testing.T) { - assert.Equal(t, 0, utils.Min(0, 1)) - assert.Equal(t, 0, utils.Min(1, 0)) - assert.Equal(t, -1, utils.Min(1, -1)) - - assert.Equal(t, uint16(0), utils.Min(uint16(0), uint16(1))) -} - func TestSortedKeys(t *testing.T) { assert.Equal(t, []string{}, utils.SortedKeys(map[string]bool{})) assert.Equal(t, []string{"a", "x", "y"}, utils.SortedKeys(map[string]bool{"x": true, "y": true, "a": true}))