Skip to content

Commit fecef5e

Browse files
committed
🩹fix: restrict supported types in SetValWithStruct
1 parent ba42a9f commit fecef5e

File tree

2 files changed

+11
-25
lines changed

2 files changed

+11
-25
lines changed

client/request.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"errors"
7-
"fmt"
87
"io"
98
"path/filepath"
109
"reflect"
@@ -930,11 +929,14 @@ func ReleaseFile(f *File) {
930929
filePool.Put(f)
931930
}
932931

933-
// SetValWithStruct sets values using a struct.
934-
// `p` is a structure that implements the WithStruct interface.
935-
// The field name is specified by `tagName`.
936-
// `v` is a struct containing some data.
937-
// Note: This method supports all values that can be converted to an interface.
932+
// SetValWithStruct stores the fields of `v` into `p`.
933+
// `tagName` specifies the key used to store into `p`. If not specified,
934+
// the field name is used by default.
935+
// `v` is a struct or a pointer to a struct containing some data.
936+
// Fields in `v` should be string, int, int8, int16, int32, int64, uint,
937+
// uint8, uint16, uint32, uint64, float32, float64, complex64,
938+
// complex128 or bool. Arrays or slices are inserted sequentially with the
939+
// same key. Other types are ignored.
938940
func SetValWithStruct(p WithStruct, tagName string, v any) {
939941
valueOfV := reflect.ValueOf(v)
940942
typeOfV := reflect.TypeOf(v)
@@ -955,7 +957,9 @@ func SetValWithStruct(p WithStruct, tagName string, v any) {
955957
p.Add(name, strconv.Itoa(int(val.Int())))
956958
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
957959
p.Add(name, strconv.FormatUint(val.Uint(), 10))
958-
case reflect.Complex128, reflect.Complex64:
960+
case reflect.Float32, reflect.Float64:
961+
p.Add(name, strconv.FormatFloat(val.Float(), 'f', -1, 64))
962+
case reflect.Complex64, reflect.Complex128:
959963
p.Add(name, strconv.FormatComplex(val.Complex(), 'f', -1, 128))
960964
case reflect.Bool:
961965
if val.Bool() {
@@ -965,16 +969,10 @@ func SetValWithStruct(p WithStruct, tagName string, v any) {
965969
}
966970
case reflect.String:
967971
p.Add(name, val.String())
968-
case reflect.Float32, reflect.Float64:
969-
p.Add(name, strconv.FormatFloat(val.Float(), 'f', -1, 64))
970972
case reflect.Slice, reflect.Array:
971973
for i := 0; i < val.Len(); i++ {
972974
setVal(name, val.Index(i))
973975
}
974-
default:
975-
if val.CanInterface() {
976-
p.Add(name, fmt.Sprintf("%#v", val.Interface()))
977-
}
978976
}
979977
}
980978

client/request_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,9 +1327,7 @@ func Test_SetValWithStruct(t *testing.T) {
13271327
TUint uint
13281328
TFloat float64
13291329
TComplex complex128
1330-
TFunc func()
13311330
TBool bool
1332-
TMap map[int]int
13331331
}
13341332

13351333
t.Run("the struct should be applied", func(t *testing.T) {
@@ -1348,8 +1346,6 @@ func Test_SetValWithStruct(t *testing.T) {
13481346
TBool: false,
13491347
TSlice: []string{"foo", "bar"},
13501348
TIntSlice: []int{0, 1, 2},
1351-
TFunc: func() {},
1352-
TMap: map[int]int{1: 2},
13531349
})
13541350

13551351
require.Equal(t, "", string(p.Peek("unexport")))
@@ -1359,8 +1355,6 @@ func Test_SetValWithStruct(t *testing.T) {
13591355
require.Equal(t, []byte("3.1"), p.Peek("TFloat"))
13601356
require.Equal(t, []byte("(3+4i)"), p.Peek("TComplex"))
13611357
require.Equal(t, []byte("false"), p.Peek("TBool"))
1362-
require.Contains(t, string(p.Peek("TFunc")), "(func())")
1363-
require.Equal(t, []byte("map[int]int{1:2}"), p.Peek("TMap"))
13641358
require.True(t, func() bool {
13651359
for _, v := range p.PeekMulti("TSlice") {
13661360
if string(v) == "foo" {
@@ -1485,8 +1479,6 @@ func Benchmark_SetValWithStruct(b *testing.B) {
14851479
TFloat float64
14861480
TComplex complex128
14871481
TBool bool
1488-
TFunc func()
1489-
TMap map[int]int
14901482
}
14911483

14921484
b.Run("the struct should be applied", func(b *testing.B) {
@@ -1508,8 +1500,6 @@ func Benchmark_SetValWithStruct(b *testing.B) {
15081500
TBool: false,
15091501
TSlice: []string{"foo", "bar"},
15101502
TIntSlice: []int{0, 1, 2},
1511-
TFunc: func() {},
1512-
TMap: map[int]int{1: 2},
15131503
})
15141504
}
15151505

@@ -1520,8 +1510,6 @@ func Benchmark_SetValWithStruct(b *testing.B) {
15201510
require.Equal(b, []byte("3.1"), p.Peek("TFloat"))
15211511
require.Equal(b, []byte("(3+4i)"), p.Peek("TComplex"))
15221512
require.Equal(b, []byte("false"), p.Peek("TBool"))
1523-
require.Contains(b, string(p.Peek("TFunc")), "(func())")
1524-
require.Equal(b, []byte("map[int]int{1:2}"), p.Peek("TMap"))
15251513
require.True(b, func() bool {
15261514
for _, v := range p.PeekMulti("TSlice") {
15271515
if string(v) == "foo" {

0 commit comments

Comments
 (0)