aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrégoire Duchêne <gduchene@awhk.org>2025-05-25 09:31:58 +0100
committerGrégoire Duchêne <gduchene@awhk.org>2025-05-25 09:31:58 +0100
commitbca1b0ccdece7b992cc88712aca36a9a127b1381 (patch)
treea91d7028610d66c8f8f52bde5bbb3aeb41c3ff1f
parent64fe7a6ad302af4d9b35c86f262c4ff41ed980fd (diff)
Use modern APIs and retire MapKeys
-rw-r--r--flag.go15
-rw-r--r--go.mod4
-rw-r--r--go.sum4
-rw-r--r--http.go7
-rw-r--r--util.go14
-rw-r--r--util_test.go8
6 files changed, 13 insertions, 39 deletions
diff --git a/flag.go b/flag.go
index 7db895b..71d6ee7 100644
--- a/flag.go
+++ b/flag.go
@@ -7,8 +7,10 @@ import (
"errors"
"flag"
"fmt"
+ "maps"
"os"
"regexp"
+ "slices"
"strconv"
"strings"
"sync/atomic"
@@ -163,7 +165,7 @@ func ParseProtobufEnum[T ~int32](values map[string]int32) ParseFunc[T] {
return func(s string) (T, error) {
val, found := values[strings.ToUpper(s)]
if !found {
- return 0, UnknownEnumValueError[string]{s, MapKeys(values)}
+ return 0, UnknownEnumValueError[string]{s, slices.Collect(maps.Keys(values))}
}
return T(val), nil
}
@@ -180,10 +182,8 @@ func ParseString(s string) (string, error) { return s, nil }
// Note that unlike ParseProtobufEnum, comparison is case-sensitive.
func ParseStringEnum(values ...string) ParseFunc[string] {
return func(s string) (string, error) {
- for _, val := range values {
- if s == val {
- return s, nil
- }
+ if slices.Contains(values, s) {
+ return s, nil
}
return "", UnknownEnumValueError[string]{s, values}
}
@@ -251,10 +251,7 @@ func (f flagFeature) Set(s string) error {
}
func (f flagFeature) String() string {
- if f.Enabled() {
- return "true"
- }
- return "false"
+ return strconv.FormatBool(f.Enabled())
}
type flagValue[T any] struct {
diff --git a/go.mod b/go.mod
index cd0d1f4..1afc5cc 100644
--- a/go.mod
+++ b/go.mod
@@ -1,5 +1,5 @@
module go.awhk.org/core
-go 1.18
+go 1.23
-require github.com/google/go-cmp v0.6.0
+require github.com/google/go-cmp v0.7.0
diff --git a/go.sum b/go.sum
index 5a8d551..40e761a 100644
--- a/go.sum
+++ b/go.sum
@@ -1,2 +1,2 @@
-github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
-github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
+github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
+github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
diff --git a/http.go b/http.go
index 8b57133..8fd89a9 100644
--- a/http.go
+++ b/http.go
@@ -5,6 +5,7 @@ package core
import (
"net/http"
+ "slices"
"sort"
"strings"
)
@@ -36,10 +37,8 @@ func FilterHTTPMethod(methods ...string) HTTPFilterFunc {
sort.Strings(methods)
allowed := strings.Join(methods, ", ")
return func(w http.ResponseWriter, req *http.Request) bool {
- for _, method := range methods {
- if method == req.Method {
- return false
- }
+ if slices.Contains(methods, req.Method) {
+ return false
}
w.Header().Set("Allow", allowed)
w.WriteHeader(http.StatusMethodNotAllowed)
diff --git a/util.go b/util.go
index 47a785a..ec2a3dd 100644
--- a/util.go
+++ b/util.go
@@ -3,20 +3,6 @@
package core
-// MapKeys returns a slice containing all the keys of the map supplied.
-// It basically is https://pkg.go.dev/golang.org/x/exp/maps#Keys, but
-// that package is still unstable.
-func MapKeys[T ~map[K]V, K comparable, V any](m T) []K {
- if len(m) == 0 {
- return nil
- }
- ret := make([]K, 0, len(m))
- for k := range m {
- ret = append(ret, k)
- }
- return ret
-}
-
// Must panics if err is not nil. It returns val otherwise.
func Must[T any](val T, err error) T {
if err != nil {
diff --git a/util_test.go b/util_test.go
index 4ed1dde..1098b21 100644
--- a/util_test.go
+++ b/util_test.go
@@ -13,14 +13,6 @@ import (
"go.awhk.org/core"
)
-func TestMapKeys(s *testing.T) {
- t := core.T{T: s, Options: cmp.Options{sortStrings}}
-
- t.AssertEqual(([]string)(nil), core.MapKeys[map[string]int](nil))
- t.AssertEqual(([]string)(nil), core.MapKeys(map[string]int{}))
- t.AssertEqual([]string{"bar", "foo"}, core.MapKeys(map[string]int{"foo": 1, "bar": 2}))
-}
-
func TestMust(s *testing.T) {
t := core.T{T: s, Options: []cmp.Option{cmpopts.EquateErrors()}}