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.
938940func 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
0 commit comments