diff options
| author | Grégoire Duchêne <gduchene@awhk.org> | 2025-05-25 09:31:58 +0100 |
|---|---|---|
| committer | Grégoire Duchêne <gduchene@awhk.org> | 2025-05-25 09:31:58 +0100 |
| commit | bca1b0ccdece7b992cc88712aca36a9a127b1381 (patch) | |
| tree | a91d7028610d66c8f8f52bde5bbb3aeb41c3ff1f | |
| parent | 64fe7a6ad302af4d9b35c86f262c4ff41ed980fd (diff) | |
Use modern APIs and retire MapKeys
| -rw-r--r-- | flag.go | 15 | ||||
| -rw-r--r-- | go.mod | 4 | ||||
| -rw-r--r-- | go.sum | 4 | ||||
| -rw-r--r-- | http.go | 7 | ||||
| -rw-r--r-- | util.go | 14 | ||||
| -rw-r--r-- | util_test.go | 8 |
6 files changed, 13 insertions, 39 deletions
@@ -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 { @@ -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 @@ -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= @@ -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) @@ -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()}} |
