Skip to content

Commit

Permalink
mark some generics slice&map functions deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Apr 3, 2024
1 parent 9f27922 commit a200129
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 33 deletions.
18 changes: 18 additions & 0 deletions helper/map.go → helper/deprecated_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package helper

// MapValues returns all the values of the map.
//
// DEPRECATED!!!
func MapValues[M ~map[K]V, K comparable, V any](maps M) []V {
values := make([]V, 0, len(maps))
for _, v := range maps {
Expand All @@ -24,6 +26,8 @@ func MapValues[M ~map[K]V, K comparable, V any](maps M) []V {
}

// MapKeys returns all the keys of the map.
//
// DEPRECATED!!!
func MapKeys[M ~map[K]V, K comparable, V any](maps M) []K {
keys := make([]K, 0, len(maps))
for k := range maps {
Expand All @@ -33,6 +37,8 @@ func MapKeys[M ~map[K]V, K comparable, V any](maps M) []K {
}

// MapKeysFunc returns all the keys of the map by the conversion function.
//
// DEPRECATED!!!
func MapKeysFunc[M ~map[K]V, T any, K comparable, V any](maps M, convert func(K) T) []T {
keys := make([]T, 0, len(maps))
for k := range maps {
Expand All @@ -42,6 +48,8 @@ func MapKeysFunc[M ~map[K]V, T any, K comparable, V any](maps M, convert func(K)
}

// MapValues returns all the values of the map by the conversion function.
//
// DEPRECATED!!!
func MapValuesFunc[M ~map[K]V, T any, K comparable, V any](maps M, convert func(V) T) []T {
values := make([]T, 0, len(maps))
for _, v := range maps {
Expand All @@ -50,6 +58,7 @@ func MapValuesFunc[M ~map[K]V, T any, K comparable, V any](maps M, convert func(
return values
}

// DEPRECATED!!!
func ToMapWithIndex[S ~[]E, K comparable, V, E any](s S, convert func(int, E) (K, V)) map[K]V {
_len := len(s)
maps := make(map[K]V, _len)
Expand All @@ -60,26 +69,35 @@ func ToMapWithIndex[S ~[]E, K comparable, V, E any](s S, convert func(int, E) (K
return maps
}

// DEPRECATED!!!
func ToMap[S ~[]E, K comparable, V, E any](s S, convert func(E) (K, V)) map[K]V {
return ToMapWithIndex(s, func(_ int, e E) (K, V) { return convert(e) })
}

// ToSetMap converts a slice s to a set map.
//
// DEPRECATED!!!
func ToSetMap[S ~[]T, T comparable](s S) map[T]struct{} {
return ToMap(s, func(e T) (T, struct{}) { return e, struct{}{} })
}

// ToBoolMap converts a slice s to a bool map.
//
// DEPRECATED!!!
func ToBoolMap[S ~[]T, T comparable](s S) map[T]bool {
return ToMap(s, func(e T) (T, bool) { return e, true })
}

// ToSetMapFunc converts a slice s to a set map by a conversion function.
//
// DEPRECATED!!!
func ToSetMapFunc[S ~[]T, K comparable, T any](s S, convert func(T) K) map[K]struct{} {
return ToMap(s, func(e T) (K, struct{}) { return convert(e), struct{}{} })
}

// ToBoolMapFunc converts a slice s to a bool map by a conversion function.
//
// DEPRECATED!!!
func ToBoolMapFunc[S ~[]T, K comparable, T any](s S, convert func(T) K) map[K]bool {
return ToMap(s, func(e T) (K, bool) { return convert(e), true })
}
File renamed without changes.
25 changes: 25 additions & 0 deletions helper/deprecated_must.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2023 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package helper

// MustV returns value if err is nil. Or, panic with err instead.
//
// DEPRECATED!!!
func MustV[T any](value T, err error) T {
if err != nil {
panic(err)
}
return value
}
43 changes: 43 additions & 0 deletions helper/deprecated_must_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2023 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package helper

import (
"errors"
"fmt"
"testing"
)

func TestMustV(t *testing.T) {
func() {
defer func() {
if r := recover(); r != nil {
t.Errorf("unexpect a panic, but got one: %v", r)
}
}()
MustV(123, nil)
}()

func() {
defer func() {
if r := recover(); r == nil {
t.Errorf("expect a panic, but got not")
} else if s := fmt.Sprint(r); s != "test" {
t.Errorf("expect '%s', but got '%s'", "test", s)
}
}()
MustV(123, errors.New("test"))
}()
}
4 changes: 4 additions & 0 deletions helper/slice.go → helper/deprecated_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package helper
// If both cap and defaultCap are equal to 0, it is equal to make(S, len).
// If cap is equal to 0, use defaultCap as cap instead, which is equal to
// make(S, len, defaultCap).
//
// DEPRECATED!!!
func MakeSlice[S ~[]E, E any, I ~int | ~int64](len, cap, defaultCap I) S {
if cap == 0 {
if cap = defaultCap; cap == 0 {
Expand All @@ -36,6 +38,8 @@ func MakeSlice[S ~[]E, E any, I ~int | ~int64](len, cap, defaultCap I) S {
//
// If no arguments, return nil.
// If all the arguments are empty, return a empty slice with cap==0.
//
// DEPRECATED!!!
func MergeSlice[S ~[]E, E any](ss ...S) S {
switch _len := len(ss); _len {
case 0:
Expand Down
File renamed without changes.
8 changes: 0 additions & 8 deletions helper/must.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,3 @@ func Must(err error) {
panic(err)
}
}

// MustV returns value if err is nil. Or, panic with err instead.
func MustV[T any](value T, err error) T {
if err != nil {
panic(err)
}
return value
}
22 changes: 0 additions & 22 deletions helper/must_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,3 @@ func TestMust(t *testing.T) {
Must(errors.New("test"))
}()
}

func TestMustV(t *testing.T) {
func() {
defer func() {
if r := recover(); r != nil {
t.Errorf("unexpect a panic, but got one: %v", r)
}
}()
MustV(123, nil)
}()

func() {
defer func() {
if r := recover(); r == nil {
t.Errorf("expect a panic, but got not")
} else if s := fmt.Sprint(r); s != "test" {
t.Errorf("expect '%s', but got '%s'", "test", s)
}
}()
MustV(123, errors.New("test"))
}()
}
22 changes: 19 additions & 3 deletions http/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"net/http"
"slices"

"github.com/xgfone/go-apiserver/helper"
"github.com/xgfone/go-apiserver/http/middleware/context"
"github.com/xgfone/go-apiserver/http/middleware/logger"
"github.com/xgfone/go-apiserver/http/middleware/recover"
Expand Down Expand Up @@ -109,12 +108,12 @@ func (ms Middlewares) AppendFunc(m ...MiddlewareFunc) Middlewares {
// Insert inserts a set of middlewares into the front
// and return a new middleware slice.
func (ms Middlewares) Insert(m ...Middleware) Middlewares {
return helper.MergeSlice(m, ms)
return mergeMiddlewares(m, ms)
}

// Append appends a set of middlewares and return a new middleware slice.
func (ms Middlewares) Append(m ...Middleware) Middlewares {
return helper.MergeSlice(ms, m)
return mergeMiddlewares(ms, m)
}

// Handler implements the interface Middleware.
Expand Down Expand Up @@ -162,3 +161,20 @@ func Sort(ms []Middleware) {
func getPriority(m Middleware) int {
return m.(interface{ Priority() int }).Priority()
}

func mergeMiddlewares(mws1, mws2 Middlewares) Middlewares {
len1, len2 := len(mws1), len(mws2)
switch {
case len1 == 0:
return mws2

case len2 == 0:
return mws1

default:
vs := make(Middlewares, len1+len2)
copy(vs, mws1)
copy(vs[len1:], mws2)
return vs
}
}

0 comments on commit a200129

Please sign in to comment.