mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-14 02:10:00 +08:00
update:Use the official slice operate func (#4841)
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -15,7 +16,6 @@ import (
|
|||||||
"github.com/zeromicro/go-zero/core/jsonx"
|
"github.com/zeromicro/go-zero/core/jsonx"
|
||||||
"github.com/zeromicro/go-zero/core/lang"
|
"github.com/zeromicro/go-zero/core/lang"
|
||||||
"github.com/zeromicro/go-zero/core/proc"
|
"github.com/zeromicro/go-zero/core/proc"
|
||||||
"github.com/zeromicro/go-zero/core/stringx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -894,7 +894,7 @@ func (u *Unmarshaler) processNamedFieldWithValueFromString(fieldType reflect.Typ
|
|||||||
valueKind.String())
|
valueKind.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
if !stringx.Contains(options, checkValue) {
|
if !slices.Contains(options, checkValue) {
|
||||||
return fmt.Errorf(`value "%s" for field %q is not defined in options "%v"`,
|
return fmt.Errorf(`value "%s" for field %q is not defined in options "%v"`,
|
||||||
mapValue, key, options)
|
mapValue, key, options)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -634,11 +635,11 @@ func validateValueInOptions(val any, options []string) error {
|
|||||||
if len(options) > 0 {
|
if len(options) > 0 {
|
||||||
switch v := val.(type) {
|
switch v := val.(type) {
|
||||||
case string:
|
case string:
|
||||||
if !stringx.Contains(options, v) {
|
if !slices.Contains(options, v) {
|
||||||
return fmt.Errorf(`error: value %q is not defined in options "%v"`, v, options)
|
return fmt.Errorf(`error: value %q is not defined in options "%v"`, v, options)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
if !stringx.Contains(options, Repr(v)) {
|
if !slices.Contains(options, Repr(v)) {
|
||||||
return fmt.Errorf(`error: value "%v" is not defined in options "%v"`, val, options)
|
return fmt.Errorf(`error: value "%v" is not defined in options "%v"`, val, options)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package stringx
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"slices"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/lang"
|
"github.com/zeromicro/go-zero/core/lang"
|
||||||
@@ -14,17 +15,6 @@ var (
|
|||||||
ErrInvalidStopPosition = errors.New("stop position is invalid")
|
ErrInvalidStopPosition = errors.New("stop position is invalid")
|
||||||
)
|
)
|
||||||
|
|
||||||
// Contains checks if str is in list.
|
|
||||||
func Contains(list []string, str string) bool {
|
|
||||||
for _, each := range list {
|
|
||||||
if each == str {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter filters chars from s with given filter function.
|
// Filter filters chars from s with given filter function.
|
||||||
func Filter(s string, filter func(r rune) bool) string {
|
func Filter(s string, filter func(r rune) bool) string {
|
||||||
var n int
|
var n int
|
||||||
@@ -123,11 +113,7 @@ func Remove(strings []string, strs ...string) []string {
|
|||||||
// Reverse reverses s.
|
// Reverse reverses s.
|
||||||
func Reverse(s string) string {
|
func Reverse(s string) string {
|
||||||
runes := []rune(s)
|
runes := []rune(s)
|
||||||
|
slices.Reverse(runes)
|
||||||
for from, to := 0, len(runes)-1; from < to; from, to = from+1, to-1 {
|
|
||||||
runes[from], runes[to] = runes[to], runes[from]
|
|
||||||
}
|
|
||||||
|
|
||||||
return string(runes)
|
return string(runes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,28 +41,6 @@ func TestNotEmpty(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContainsString(t *testing.T) {
|
|
||||||
cases := []struct {
|
|
||||||
slice []string
|
|
||||||
value string
|
|
||||||
expect bool
|
|
||||||
}{
|
|
||||||
{[]string{"1"}, "1", true},
|
|
||||||
{[]string{"1"}, "2", false},
|
|
||||||
{[]string{"1", "2"}, "1", true},
|
|
||||||
{[]string{"1", "2"}, "3", false},
|
|
||||||
{nil, "3", false},
|
|
||||||
{nil, "", false},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, each := range cases {
|
|
||||||
t.Run(path.Join(each.slice...), func(t *testing.T) {
|
|
||||||
actual := Contains(each.slice, each.value)
|
|
||||||
assert.Equal(t, each.expect, actual)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFilter(t *testing.T) {
|
func TestFilter(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
input string
|
input string
|
||||||
|
|||||||
Reference in New Issue
Block a user