Skip to content

Commit

Permalink
Refactory prepare request query and multipart
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Cataldo committed Dec 6, 2023
1 parent 3e91091 commit 2735526
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 31 deletions.
34 changes: 10 additions & 24 deletions asaas/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/GabrielHCataldo/go-asaas/internal/util"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"reflect"
"strconv"
"strings"
)

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -193,35 +191,23 @@ 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 {
files = append(files, file)
}
}
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
Expand Down
1 change: 1 addition & 0 deletions asaas/test 1814882393.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unit test golang
1 change: 1 addition & 0 deletions asaas/test 1919830447.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unit test golang
39 changes: 32 additions & 7 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"encoding/json"
"fmt"
"os"
"path"
"reflect"
Expand Down Expand Up @@ -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]
}
Expand All @@ -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)
}

0 comments on commit 2735526

Please sign in to comment.