Skip to content

Commit

Permalink
Use new go 1.21 builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Sep 26, 2023
1 parent aab6071 commit 7a62c6a
Show file tree
Hide file tree
Showing 9 changed files with 8 additions and 43 deletions.
2 changes: 1 addition & 1 deletion envs/dates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions excellent/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"

"github.com/antlr/antlr4/runtime/Go/antlr/v4"
"github.com/nyaruka/goflow/utils"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -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))
}
2 changes: 1 addition & 1 deletion flows/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package flows

import (
"fmt"
"slices"
"strings"

"github.com/nyaruka/gocommon/i18n"
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions flows/definition/legacy/expressions/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"strconv"
"strings"

"github.com/nyaruka/goflow/utils"

"github.com/pkg/errors"
)

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion flows/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion flows/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"slices"

"github.com/go-playground/validator/v10"
"github.com/nyaruka/gocommon/i18n"
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion flows/runs/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 1 addition & 17 deletions utils/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
16 changes: 0 additions & 16 deletions utils/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}))
Expand Down

0 comments on commit 7a62c6a

Please sign in to comment.