chore: remove useless code (#4592)

This commit is contained in:
Kevin Wan
2025-01-22 22:55:52 +08:00
committed by GitHub
parent cbcacf31c1
commit 00e0db5def
3 changed files with 16 additions and 19 deletions

View File

@@ -610,19 +610,22 @@ func (u *Unmarshaler) processFieldNotFromString(fieldType reflect.Type, value re
case valueKind == reflect.String && typeKind == reflect.Map: case valueKind == reflect.String && typeKind == reflect.Map:
return u.fillMapFromString(value, mapValue) return u.fillMapFromString(value, mapValue)
case valueKind == reflect.String && typeKind == reflect.Slice: case valueKind == reflect.String && typeKind == reflect.Slice:
// try to find out if it's a byte slice, golang use []byte Marshal to base64 but there only SliceOf uint8/bytes can convert to []byte // try to find out if it's a byte slice,
// more details https://pkg.go.dev/encoding/json#Marshal // more details https://pkg.go.dev/encoding/json#Marshal
//> Array and slice values encode as JSON arrays, except that []byte encodes as a base64-encoded string, and a nil slice encodes as the null JSON value. // array and slice values encode as JSON arrays,
//and also u can find this https://stackoverflow.com/questions/34089750/marshal-byte-to-json-giving-a-strange-string // except that []byte encodes as a base64-encoded string,
// and a nil slice encoded as the null JSON value.
// https://stackoverflow.com/questions/34089750/marshal-byte-to-json-giving-a-strange-string
if fieldType.Elem().Kind() == reflect.Uint8 { if fieldType.Elem().Kind() == reflect.Uint8 {
strVal := mapValue.(string) // check whether string type, because the kind of some other types can be string
decodedBytes, err := base64.StdEncoding.DecodeString(strVal) if strVal, ok := mapValue.(string); ok {
// if err !=nil do next if decodedBytes, err := base64.StdEncoding.DecodeString(strVal); err == nil {
if err == nil { value.Set(reflect.ValueOf(decodedBytes))
value.Set(reflect.ValueOf(decodedBytes)) return nil
return nil }
} }
} }
return u.fillSliceFromString(fieldType, value, mapValue, fullName) return u.fillSliceFromString(fieldType, value, mapValue, fullName)
case valueKind == reflect.String && derefedFieldType == durationType: case valueKind == reflect.String && derefedFieldType == durationType:
return fillDurationValue(fieldType, value, mapValue.(string)) return fillDurationValue(fieldType, value, mapValue.(string))
@@ -748,15 +751,7 @@ func (u *Unmarshaler) processFieldTextUnmarshaler(fieldType reflect.Type, value
return true, tval.UnmarshalText(mv) return true, tval.UnmarshalText(mv)
} }
} }
//[]byte
if fieldType.Kind() == reflect.Slice && fieldType.Elem().Kind() == reflect.Uint8 {
b, err := base64.StdEncoding.DecodeString(mapValue.(string))
if err != nil {
return false, err
}
value.SetBytes(b)
return true, nil
}
return false, nil return false, nil
} }

View File

@@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/zeromicro/go-zero/core/jsonx"
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
@@ -14,6 +13,7 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/jsonx"
"github.com/zeromicro/go-zero/core/stringx" "github.com/zeromicro/go-zero/core/stringx"
) )
@@ -4874,6 +4874,7 @@ func TestUnmarshalJsonReaderMultiArray(t *testing.T) {
B [][]string `json:"b"` B [][]string `json:"b"`
C []byte `json:"c"` C []byte `json:"c"`
} }
var res testRes var res testRes
marshal := testRes{ marshal := testRes{
A: "133", A: "133",

View File

@@ -516,6 +516,7 @@ func TestParseJsonBody(t *testing.T) {
assert.Equal(t, "apple", v[0].Name) assert.Equal(t, "apple", v[0].Name)
assert.Equal(t, 18, v[0].Age) assert.Equal(t, 18, v[0].Age)
}) })
t.Run("bytes field", func(t *testing.T) { t.Run("bytes field", func(t *testing.T) {
type v struct { type v struct {
Signature []byte `json:"signature,optional"` Signature []byte `json:"signature,optional"`