Skip to content

Commit

Permalink
chore: remove useless code (#4592)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Jan 22, 2025
1 parent cbcacf3 commit 00e0db5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
31 changes: 13 additions & 18 deletions core/mapping/unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,19 +610,22 @@ func (u *Unmarshaler) processFieldNotFromString(fieldType reflect.Type, value re
case valueKind == reflect.String && typeKind == reflect.Map:
return u.fillMapFromString(value, mapValue)
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
//> 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.
//and also u can find this https://stackoverflow.com/questions/34089750/marshal-byte-to-json-giving-a-strange-string
// array and slice values encode as JSON arrays,
// 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 {
strVal := mapValue.(string)
decodedBytes, err := base64.StdEncoding.DecodeString(strVal)
// if err !=nil do next
if err == nil {
value.Set(reflect.ValueOf(decodedBytes))
return nil
// check whether string type, because the kind of some other types can be string
if strVal, ok := mapValue.(string); ok {
if decodedBytes, err := base64.StdEncoding.DecodeString(strVal); err == nil {
value.Set(reflect.ValueOf(decodedBytes))
return nil
}
}
}

return u.fillSliceFromString(fieldType, value, mapValue, fullName)
case valueKind == reflect.String && derefedFieldType == durationType:
return fillDurationValue(fieldType, value, mapValue.(string))
Expand Down Expand Up @@ -748,15 +751,7 @@ func (u *Unmarshaler) processFieldTextUnmarshaler(fieldType reflect.Type, value
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
}

Expand Down
3 changes: 2 additions & 1 deletion core/mapping/unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/zeromicro/go-zero/core/jsonx"
"reflect"
"strconv"
"strings"
Expand All @@ -14,6 +13,7 @@ import (

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/jsonx"
"github.com/zeromicro/go-zero/core/stringx"
)

Expand Down Expand Up @@ -4874,6 +4874,7 @@ func TestUnmarshalJsonReaderMultiArray(t *testing.T) {
B [][]string `json:"b"`
C []byte `json:"c"`
}

var res testRes
marshal := testRes{
A: "133",
Expand Down
1 change: 1 addition & 0 deletions rest/httpx/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ func TestParseJsonBody(t *testing.T) {
assert.Equal(t, "apple", v[0].Name)
assert.Equal(t, 18, v[0].Age)
})

t.Run("bytes field", func(t *testing.T) {
type v struct {
Signature []byte `json:"signature,optional"`
Expand Down

0 comments on commit 00e0db5

Please sign in to comment.