4
4
"bytes"
5
5
"context"
6
6
"errors"
7
- "fmt"
8
7
"io"
9
8
"path/filepath"
10
9
"reflect"
@@ -930,11 +929,14 @@ func ReleaseFile(f *File) {
930
929
filePool .Put (f )
931
930
}
932
931
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.
938
940
func SetValWithStruct (p WithStruct , tagName string , v any ) {
939
941
valueOfV := reflect .ValueOf (v )
940
942
typeOfV := reflect .TypeOf (v )
@@ -955,7 +957,9 @@ func SetValWithStruct(p WithStruct, tagName string, v any) {
955
957
p .Add (name , strconv .Itoa (int (val .Int ())))
956
958
case reflect .Uint , reflect .Uint8 , reflect .Uint16 , reflect .Uint32 , reflect .Uint64 , reflect .Uintptr :
957
959
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 :
959
963
p .Add (name , strconv .FormatComplex (val .Complex (), 'f' , - 1 , 128 ))
960
964
case reflect .Bool :
961
965
if val .Bool () {
@@ -965,16 +969,10 @@ func SetValWithStruct(p WithStruct, tagName string, v any) {
965
969
}
966
970
case reflect .String :
967
971
p .Add (name , val .String ())
968
- case reflect .Float32 , reflect .Float64 :
969
- p .Add (name , strconv .FormatFloat (val .Float (), 'f' , - 1 , 64 ))
970
972
case reflect .Slice , reflect .Array :
971
973
for i := 0 ; i < val .Len (); i ++ {
972
974
setVal (name , val .Index (i ))
973
975
}
974
- default :
975
- if val .CanInterface () {
976
- p .Add (name , fmt .Sprintf ("%#v" , val .Interface ()))
977
- }
978
976
}
979
977
}
980
978
0 commit comments