* fix: #2672 * chore: fix more cases * chore: update deps * chore: update deps * chore: refactor * chore: refactor * chore: refactor
This commit is contained in:
@@ -20,16 +20,16 @@ func (b noOpBreaker) Do(req func() error) error {
|
||||
return req()
|
||||
}
|
||||
|
||||
func (b noOpBreaker) DoWithAcceptable(req func() error, acceptable Acceptable) error {
|
||||
func (b noOpBreaker) DoWithAcceptable(req func() error, _ Acceptable) error {
|
||||
return req()
|
||||
}
|
||||
|
||||
func (b noOpBreaker) DoWithFallback(req func() error, fallback func(err error) error) error {
|
||||
func (b noOpBreaker) DoWithFallback(req func() error, _ func(err error) error) error {
|
||||
return req()
|
||||
}
|
||||
|
||||
func (b noOpBreaker) DoWithFallbackAcceptable(req func() error, fallback func(err error) error,
|
||||
acceptable Acceptable) error {
|
||||
func (b noOpBreaker) DoWithFallbackAcceptable(req func() error, _ func(err error) error,
|
||||
_ Acceptable) error {
|
||||
return req()
|
||||
}
|
||||
|
||||
@@ -38,5 +38,5 @@ type nopPromise struct{}
|
||||
func (p nopPromise) Accept() {
|
||||
}
|
||||
|
||||
func (p nopPromise) Reject(reason string) {
|
||||
func (p nopPromise) Reject(_ string) {
|
||||
}
|
||||
|
||||
@@ -735,8 +735,16 @@ func (u *Unmarshaler) generateMap(keyType, elemType reflect.Type, mapValue inter
|
||||
default:
|
||||
switch v := keythData.(type) {
|
||||
case bool:
|
||||
if dereffedElemKind != reflect.Bool {
|
||||
return emptyValue, errTypeMismatch
|
||||
}
|
||||
|
||||
targetValue.SetMapIndex(key, reflect.ValueOf(v))
|
||||
case string:
|
||||
if dereffedElemKind != reflect.String {
|
||||
return emptyValue, errTypeMismatch
|
||||
}
|
||||
|
||||
targetValue.SetMapIndex(key, reflect.ValueOf(v))
|
||||
case json.Number:
|
||||
target := reflect.New(dereffedElemType)
|
||||
@@ -746,6 +754,10 @@ func (u *Unmarshaler) generateMap(keyType, elemType reflect.Type, mapValue inter
|
||||
|
||||
targetValue.SetMapIndex(key, target.Elem())
|
||||
default:
|
||||
if dereffedElemKind != keythValue.Kind() {
|
||||
return emptyValue, errTypeMismatch
|
||||
}
|
||||
|
||||
targetValue.SetMapIndex(key, keythValue)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3563,6 +3563,71 @@ func TestGoogleUUID(t *testing.T) {
|
||||
assert.Equal(t, "6ba7b810-9dad-11d1-80b4-00c04fd430c2", val.Uidp.String())
|
||||
}
|
||||
|
||||
func TestUnmarshalJsonReaderWithTypeMismatchBool(t *testing.T) {
|
||||
var req struct {
|
||||
Params map[string]bool `json:"params"`
|
||||
}
|
||||
body := `{"params":{"a":"123"}}`
|
||||
assert.Equal(t, errTypeMismatch, UnmarshalJsonReader(strings.NewReader(body), &req))
|
||||
}
|
||||
|
||||
func TestUnmarshalJsonReaderWithTypeMismatchString(t *testing.T) {
|
||||
var req struct {
|
||||
Params map[string]string `json:"params"`
|
||||
}
|
||||
body := `{"params":{"a":{"a":123}}}`
|
||||
assert.Equal(t, errTypeMismatch, UnmarshalJsonReader(strings.NewReader(body), &req))
|
||||
}
|
||||
|
||||
func TestUnmarshalJsonReaderWithMismatchType(t *testing.T) {
|
||||
type Req struct {
|
||||
Params map[string]string `json:"params"`
|
||||
}
|
||||
|
||||
var req Req
|
||||
body := `{"params":{"a":{"a":123}}}`
|
||||
assert.Equal(t, errTypeMismatch, UnmarshalJsonReader(strings.NewReader(body), &req))
|
||||
}
|
||||
|
||||
func TestUnmarshalJsonReaderWithMismatchTypeBool(t *testing.T) {
|
||||
type Req struct {
|
||||
Params map[string]bool `json:"params"`
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
}{
|
||||
{
|
||||
name: "int",
|
||||
input: `{"params":{"a":123}}`,
|
||||
},
|
||||
{
|
||||
name: "int",
|
||||
input: `{"params":{"a":"123"}}`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
test := test
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
var req Req
|
||||
assert.Equal(t, errTypeMismatch, UnmarshalJsonReader(strings.NewReader(test.input), &req))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalJsonReaderWithMismatchTypeBoolMap(t *testing.T) {
|
||||
var req struct {
|
||||
Params map[string]string `json:"params"`
|
||||
}
|
||||
assert.Equal(t, errTypeMismatch, UnmarshalJsonMap(map[string]interface{}{
|
||||
"params": map[string]interface{}{
|
||||
"a": true,
|
||||
},
|
||||
}, &req))
|
||||
}
|
||||
|
||||
func BenchmarkDefaultValue(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
var a struct {
|
||||
|
||||
@@ -82,7 +82,14 @@ func ValidatePtr(v *reflect.Value) error {
|
||||
func convertType(kind reflect.Kind, str string) (interface{}, error) {
|
||||
switch kind {
|
||||
case reflect.Bool:
|
||||
return str == "1" || strings.ToLower(str) == "true", nil
|
||||
switch strings.ToLower(str) {
|
||||
case "1", "true":
|
||||
return true, nil
|
||||
case "0", "false":
|
||||
return false, nil
|
||||
default:
|
||||
return false, errTypeMismatch
|
||||
}
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
intValue, err := strconv.ParseInt(str, 10, 64)
|
||||
if err != nil {
|
||||
|
||||
@@ -4,7 +4,8 @@ import "github.com/zeromicro/go-zero/core/logx"
|
||||
|
||||
// Recover is used with defer to do cleanup on panics.
|
||||
// Use it like:
|
||||
// defer Recover(func() {})
|
||||
//
|
||||
// defer Recover(func() {})
|
||||
func Recover(cleanups ...func()) {
|
||||
for _, cleanup := range cleanups {
|
||||
cleanup()
|
||||
|
||||
@@ -34,7 +34,6 @@ func initialize() {
|
||||
|
||||
cores = uint64(len(cpus))
|
||||
quota = float64(len(cpus))
|
||||
|
||||
cq, err := cpuQuota()
|
||||
if err == nil {
|
||||
if cq != -1 {
|
||||
|
||||
Reference in New Issue
Block a user