Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
chore: improve function readibility
Browse files Browse the repository at this point in the history
  • Loading branch information
YCK1130 committed Aug 22, 2024
1 parent 1830f04 commit 7a7600e
Showing 1 changed file with 36 additions and 29 deletions.
65 changes: 36 additions & 29 deletions application/jira/v0/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,32 +97,49 @@ func (e errBody) Message() string {
return strings.Join(e.Body.Msg, " ")
}

func turnToStringQueryParams(val any) string {
var stringVal string
switch val := val.(type) {
case string:
stringVal = val
case int:
stringVal = fmt.Sprintf("%d", val)
case bool:
stringVal = fmt.Sprintf("%t", val)
case []string:
stringVal = strings.Join(val, ",")
case []int:
var strVals []string
for _, v := range val {
strVals = append(strVals, fmt.Sprintf("%d", v))
}
stringVal = strings.Join(strVals, ",")
default:
return ""
}
return stringVal
}

func addQueryOptions(req *resty.Request, opt interface{}) error {
v := reflect.ValueOf(opt)
if v.Kind() == reflect.Ptr && v.IsNil() {
return nil
}
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() == reflect.Map {
for _, key := range v.MapKeys() {
if v.MapIndex(key).IsValid() && v.MapIndex(key).CanInterface() {
val := v.MapIndex(key).Interface()
var stringVal string
switch val := val.(type) {
case string:
stringVal = val
case int:
stringVal = fmt.Sprintf("%d", val)
case bool:
stringVal = fmt.Sprintf("%t", val)
default:
continue
}
if stringVal == fmt.Sprintf("%v", reflect.Zero(reflect.TypeOf(val))) {
continue
}
paramName := key.String()
req.SetQueryParam(paramName, stringVal)
if !v.MapIndex(key).IsValid() || !v.MapIndex(key).CanInterface() {
continue
}
val := v.MapIndex(key).Interface()
stringVal := turnToStringQueryParams(val)
if stringVal == fmt.Sprintf("%v", reflect.Zero(reflect.TypeOf(val))) {
continue
}
paramName := key.String()
req.SetQueryParam(paramName, stringVal)
}
} else if v.Kind() == reflect.Struct {
typeOfS := v.Type()
Expand All @@ -131,17 +148,7 @@ func addQueryOptions(req *resty.Request, opt interface{}) error {
continue
}
val := v.Field(i).Interface()
var stringVal string
switch val := val.(type) {
case string:
stringVal = val
case int:
stringVal = fmt.Sprintf("%d", val)
case bool:
stringVal = fmt.Sprintf("%t", val)
default:
continue
}
stringVal := turnToStringQueryParams(val)
if stringVal == fmt.Sprintf("%v", reflect.Zero(reflect.TypeOf(val))) {
continue
}
Expand Down

0 comments on commit 7a7600e

Please sign in to comment.