Skip to content

Commit

Permalink
optimize(hz): hz client behavior (#1038)
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF committed Jan 3, 2024
1 parent 88e2576 commit f7430e0
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion 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 @@ -573,13 +583,24 @@ func (r *request) setHeader(header, value string) *request {
return r
}
func (r *request) addHeader(header, value string) *request {
r.header.Add(header, value)
return r
}
func (r *request) setQueryParam(param string, value interface{}) *request {
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Slice, reflect.Array:
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 Expand Up @@ -911,7 +932,7 @@ func (s *{{$.ServiceName}}Client) {{$MethodInfo.Name}}(context context.Context,
setPathParams(map[string]string{
{{$MethodInfo.PathParamsCode}}
}).
setHeaders(map[string]string{
addHeaders(map[string]string{
{{$MethodInfo.HeaderParamsCode}}
}).
setFormParams(map[string]string{
Expand Down

0 comments on commit f7430e0

Please sign in to comment.