diff --git a/asaas/request.go b/asaas/request.go index b2d151f..dcd928c 100644 --- a/asaas/request.go +++ b/asaas/request.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "fmt" "github.com/GabrielHCataldo/go-asaas/internal/util" "io" "mime/multipart" @@ -12,7 +11,6 @@ import ( "net/url" "os" "reflect" - "strconv" "strings" ) @@ -94,12 +92,12 @@ func (r request[T]) createHttpRequest(ctx context.Context, method string, path s for i := 0; i < rPayload.NumField(); i++ { f := rPayload.Field(i) ft := rtPayload.Field(i) - k := util.GetJsonFieldNameByReflect(ft) - v := util.GetValueByReflect(f) + k := util.GetJsonFieldNameByReflectField(ft) + v := util.GetValueByReflectField(f) if util.IsBlank(&k) || v == nil { continue } - params.Add(k, fmt.Sprintf(`%s`, v)) + params.Add(k, util.ConvertToString(v)) } encode := params.Encode() if util.IsNotBlank(&encode) { @@ -193,26 +191,14 @@ func (r request[T]) prepareMultipartPayload(payload any) (map[string][]io.Reader for i := 0; i < rPayload.NumField(); i++ { fd := rPayload.Field(i) ft := rtPayload.Field(i) - if fd.IsZero() || !fd.IsValid() { + k := util.GetJsonFieldNameByReflectField(ft) + vf := util.GetValueByReflectField(fd) + if vf == nil { continue } - k := util.GetJsonFieldNameByReflect(ft) - vf := util.GetValueByReflect(fd) - var b bool - var s string - var in int - var f *os.File - var fs []*os.File - var ok bool - if b, ok = vf.(bool); ok { - multipartPayload[k] = []io.Reader{strings.NewReader(strconv.FormatBool(b))} - } else if s, ok = vf.(string); ok { - multipartPayload[k] = []io.Reader{strings.NewReader(s)} - } else if in, ok = vf.(int); ok { - multipartPayload[k] = []io.Reader{strings.NewReader(strconv.Itoa(in))} - } else if f, ok = vf.(*os.File); ok && f != nil { + if f, fOk := vf.(*os.File); fOk && f != nil { multipartPayload[k] = []io.Reader{f} - } else if fs, ok = vf.([]*os.File); ok && fs != nil { + } else if fs, fsOk := vf.([]*os.File); fsOk && fs != nil { var files []io.Reader for _, file := range fs { if file != nil { @@ -220,8 +206,8 @@ func (r request[T]) prepareMultipartPayload(payload any) (map[string][]io.Reader } } multipartPayload[k] = files - } else if vf != nil { - multipartPayload[k] = []io.Reader{strings.NewReader(fmt.Sprintf(`%s`, vf))} + } else { + multipartPayload[k] = []io.Reader{strings.NewReader(util.ConvertToString(vf))} } } return multipartPayload, nil diff --git a/asaas/test 1814882393.txt b/asaas/test 1814882393.txt new file mode 100644 index 0000000..7cd8f77 --- /dev/null +++ b/asaas/test 1814882393.txt @@ -0,0 +1 @@ +unit test golang \ No newline at end of file diff --git a/asaas/test 1919830447.txt b/asaas/test 1919830447.txt new file mode 100644 index 0000000..7cd8f77 --- /dev/null +++ b/asaas/test 1919830447.txt @@ -0,0 +1 @@ +unit test golang \ No newline at end of file diff --git a/internal/util/util.go b/internal/util/util.go index d79afc5..e8cb354 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -2,6 +2,7 @@ package util import ( "encoding/json" + "fmt" "os" "path" "reflect" @@ -41,20 +42,29 @@ func GetSystemInfo(skipCaller int) (fileName string, line string, funcName strin return fileBase, strconv.Itoa(lineInt), nameFunc } -func GetValueByReflect(f reflect.Value) any { +func GetValueByReflectField(f reflect.Value) any { if f.IsZero() || !f.IsValid() { return nil } - v := f.Interface() - if x, ok := v.(*os.File); ok { - v = x + k := f.Kind() + if x, ok := f.Interface().(*os.File); ok { + return x } else if f.Kind() == reflect.Pointer { - v = f.Elem().Interface() + f = f.Elem() + k = f.Kind() } - return v + switch k { + case reflect.String: + return f.String() + case reflect.Int: + return f.Int() + case reflect.Bool: + return f.Bool() + } + return f.Interface() } -func GetJsonFieldNameByReflect(f reflect.StructField) string { +func GetJsonFieldNameByReflectField(f reflect.StructField) string { sk := strings.Split(f.Tag.Get("json"), ",") return sk[0] } @@ -76,3 +86,18 @@ func IsJson(v []byte) bool { } return false } + +func ConvertToString(v any) string { + if v == nil { + return "" + } else if i, iOk := v.(int); iOk { + return strconv.Itoa(i) + } else if b, bOk := v.(bool); bOk { + return strconv.FormatBool(b) + } else if f64, f64Ok := v.(float64); f64Ok { + return strconv.FormatFloat(f64, 'f', -1, 64) + } else if s, sOk := v.(string); sOk { + return s + } + return fmt.Sprintf(`"%s"`, v) +}