feat: go 1.21

This commit is contained in:
Pete Davison
2023-08-29 21:04:01 +00:00
committed by Andrey Nering
parent 81148c312e
commit 8d0754af4d
11 changed files with 52 additions and 29 deletions

26
internal/exp/maps.go Normal file
View File

@@ -0,0 +1,26 @@
// This package is intended as a place to copy functions from the
// golang.org/x/exp package. Copying these functions allows us to rely on our
// own code instead of an external package that may change unpredictably in the
// future.
//
// It also prevents problems with transitive dependencies whereby a
// package that imports Task (and therefore our version of golang.org/x/exp)
// cannot import a different version of golang.org/x/exp.
//
// Finally, it serves as a place to track functions that may be able to be
// removed in the future if they are added to the standard library. This is also
// why this package is under the internal directory since these functions are
// not intended to be used outside of Task.
package exp
import "cmp"
// Keys is a copy of https://pkg.go.dev/golang.org/x/exp@v0.0.0-20240103183307-be819d1f06fc/maps#Keys.
// This is not yet included in the standard library. See https://github.com/golang/go/issues/61538.
func Keys[K cmp.Ordered, V any](m map[K]V) []K {
var keys []K
for key := range m {
keys = append(keys, key)
}
return keys
}

View File

@@ -5,12 +5,12 @@ import (
"io"
"os"
"path"
"slices"
"strings"
"text/tabwriter"
"github.com/joho/godotenv"
"github.com/spf13/pflag"
"golang.org/x/exp/slices"
"github.com/go-task/task/v3/internal/logger"
)

View File

@@ -4,11 +4,11 @@ import (
"bufio"
"io"
"os"
"slices"
"strconv"
"strings"
"github.com/fatih/color"
"golang.org/x/exp/slices"
"github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/term"

View File

@@ -1,26 +1,26 @@
package omap
import (
"cmp"
"fmt"
"slices"
"golang.org/x/exp/constraints"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
"github.com/go-task/task/v3/internal/deepcopy"
"github.com/go-task/task/v3/internal/exp"
)
// An OrderedMap is a wrapper around a regular map that maintains an ordered
// list of the map's keys. This allows you to run deterministic and ordered
// operations on the map such as printing/serializing/iterating.
type OrderedMap[K constraints.Ordered, V any] struct {
type OrderedMap[K cmp.Ordered, V any] struct {
s []K
m map[K]V
}
// New will create a new OrderedMap of the given type and return it.
func New[K constraints.Ordered, V any]() OrderedMap[K, V] {
func New[K cmp.Ordered, V any]() OrderedMap[K, V] {
return OrderedMap[K, V]{
s: make([]K, 0),
m: make(map[K]V),
@@ -29,14 +29,14 @@ func New[K constraints.Ordered, V any]() OrderedMap[K, V] {
// FromMap will create a new OrderedMap from the given map. Since Golang maps
// are unordered, the order of the created OrderedMap will be random.
func FromMap[K constraints.Ordered, V any](m map[K]V) OrderedMap[K, V] {
func FromMap[K cmp.Ordered, V any](m map[K]V) OrderedMap[K, V] {
om := New[K, V]()
om.m = m
om.s = maps.Keys(m)
om.s = exp.Keys(m)
return om
}
func FromMapWithOrder[K constraints.Ordered, V any](m map[K]V, order []K) OrderedMap[K, V] {
func FromMapWithOrder[K cmp.Ordered, V any](m map[K]V, order []K) OrderedMap[K, V] {
om := New[K, V]()
if len(m) != len(order) {
panic("length of map and order must be equal")

View File

@@ -43,13 +43,7 @@ func TestSortFunc(t *testing.T) {
om.Set(1, "one")
om.Set(2, "two")
om.SortFunc(func(a, b int) int {
if a < b {
return 1
}
if a > b {
return -1
}
return 0
return b - a
})
assert.Equal(t, []int{3, 2, 1}, om.s)
}

View File

@@ -1,11 +1,11 @@
package slicesext
import (
"golang.org/x/exp/constraints"
"golang.org/x/exp/slices"
"cmp"
"slices"
)
func UniqueJoin[T constraints.Ordered](ss ...[]T) []T {
func UniqueJoin[T cmp.Ordered](ss ...[]T) []T {
var length int
for _, s := range ss {
length += len(s)

View File

@@ -2,11 +2,10 @@ package templater
import (
"bytes"
"maps"
"strings"
"text/template"
"golang.org/x/exp/maps"
"github.com/go-task/task/v3/taskfile/ast"
)