Skip to content

Commit

Permalink
optimize: hz client
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF committed Dec 26, 2023
1 parent a327795 commit 19ffa49
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cmd/hz/generator/package_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ type Option struct {
type Options struct {
hostUrl string
enumAsInt bool
doer client.Doer
header http.Header
requestBodyBind bindRequestBodyFunc
Expand Down Expand Up @@ -375,6 +376,13 @@ func WithResponseResultDecider(decider ResponseResultDecider) Option {
}}
}
// WithQueryEnumAsInt is used to set enum as int for query parameters
func WithQueryEnumAsInt(enable bool) Option {
return Option{func(op *Options) {
op.enumAsInt = enable
}}
}
func withHostUrl(HostUrl string) Option {
return Option{func(op *Options) {
op.hostUrl = HostUrl
Expand All @@ -384,6 +392,7 @@ func withHostUrl(HostUrl string) Option {
// underlying client
type cli struct {
hostUrl string
enumAsInt bool
doer client.Doer
header http.Header
bindRequestBody bindRequestBodyFunc
Expand Down Expand Up @@ -419,6 +428,7 @@ func newClient(opts *Options) (*cli, error) {
c := &cli{
hostUrl: opts.hostUrl,
enumAsInt: opts.enumAsInt,
doer: opts.doer,
header: opts.header,
bindRequestBody: opts.requestBodyBind,
Expand Down Expand Up @@ -580,6 +590,12 @@ func (r *request) setQueryParam(param string, value interface{}) *request {
for index := 0; index < v.Len(); index++ {
r.queryParam.Add(param, fmt.Sprint(v.Index(index).Interface()))
}
case reflect.Int32, reflect.Int64:
if r.client.enumAsInt {
r.queryParam.Add(param, fmt.Sprintf("%d", v.Interface()))
} else {
r.queryParam.Add(param, fmt.Sprint(v))
}
default:
r.queryParam.Set(param, fmt.Sprint(v))
}
Expand Down

0 comments on commit 19ffa49

Please sign in to comment.