Skip to content

Commit 38a5c45

Browse files
committed
refactor(net/gclient): 重构请求参数分类逻辑
- 将多个返回值改为结构体返回,提高代码可读性 - 新增 requestParams 结构体统一管理各类参数 - 修改 classifyRequestParams 方法返回 *requestParams 指针 - 更新所有参数访问方式从独立变量到结构体字段 - 简化错误返回逻辑,统一错误处理流程 - 优化参数分类和赋值的代码结构
1 parent 02639cc commit 38a5c45

File tree

1 file changed

+41
-37
lines changed

1 file changed

+41
-37
lines changed

net/gclient/gclient_request_obj.go

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -87,39 +87,39 @@ func (c *Client) DoRequestObj(ctx context.Context, req, res any) error {
8787
}
8888

8989
// Classify request parameters by `in` tag
90-
pathParams, queryParams, headerParams, cookieParams, bodyParams, err := c.classifyRequestParams(req)
90+
params, err := c.classifyRequestParams(req)
9191
if err != nil {
9292
return err
9393
}
9494

9595
// Backward compatibility: if path has placeholders but no path params were classified,
9696
// try to extract from all fields (for requests without `in` tags)
97-
if gstr.Contains(path, "{") && len(pathParams) == 0 {
97+
if gstr.Contains(path, "{") && len(params.path) == 0 {
9898
allParamsMap := gconv.Map(req)
9999
path = c.handlePathForObjRequest(path, allParamsMap)
100100
} else {
101101
// Replace path parameters
102-
path = c.handlePathForObjRequest(path, pathParams)
102+
path = c.handlePathForObjRequest(path, params.path)
103103
}
104104

105105
// Build client with parameters
106106
client := c
107-
if len(queryParams) > 0 {
108-
client = client.SetQueryMap(queryParams)
107+
if len(params.query) > 0 {
108+
client = client.SetQueryMap(params.query)
109109
}
110-
if len(headerParams) > 0 {
111-
client = client.SetHeaderMap(headerParams)
110+
if len(params.header) > 0 {
111+
client = client.SetHeaderMap(params.header)
112112
}
113-
if len(cookieParams) > 0 {
114-
for k, v := range cookieParams {
113+
if len(params.cookie) > 0 {
114+
for k, v := range params.cookie {
115115
client = client.SetCookie(k, v)
116116
}
117117
}
118118

119119
// Prepare body data
120120
var data any
121-
if len(bodyParams) > 0 {
122-
data = bodyParams
121+
if len(params.body) > 0 {
122+
data = params.body
123123
}
124124

125125
// Send request
@@ -163,6 +163,15 @@ func (c *Client) handlePathForObjRequest(path string, pathParams map[string]any)
163163
return path
164164
}
165165

166+
// requestParams holds classified request parameters by location
167+
type requestParams struct {
168+
path map[string]any
169+
query map[string]any
170+
header map[string]string
171+
cookie map[string]string
172+
body map[string]any
173+
}
174+
166175
// classifyRequestParams classifies request parameters by `in` tag.
167176
// It returns parameters categorized into path, query, header, cookie, and body.
168177
//
@@ -177,27 +186,22 @@ func (c *Client) handlePathForObjRequest(path string, pathParams map[string]any)
177186
// - Anonymous embedded structs are automatically flattened
178187
// - Named struct fields with `in:"query"` are flattened to query parameters
179188
// - Named struct fields without `in` tag are placed in body as-is
180-
func (c *Client) classifyRequestParams(req any) (
181-
pathParams map[string]any,
182-
queryParams map[string]any,
183-
headerParams map[string]string,
184-
cookieParams map[string]string,
185-
bodyParams map[string]any,
186-
err error,
187-
) {
188-
pathParams = make(map[string]any)
189-
queryParams = make(map[string]any)
190-
headerParams = make(map[string]string)
191-
cookieParams = make(map[string]string)
192-
bodyParams = make(map[string]any)
189+
func (c *Client) classifyRequestParams(req any) (*requestParams, error) {
190+
params := &requestParams{
191+
path: make(map[string]any),
192+
query: make(map[string]any),
193+
header: make(map[string]string),
194+
cookie: make(map[string]string),
195+
body: make(map[string]any),
196+
}
193197

194198
// Use RecursiveOptionEmbedded to automatically flatten anonymous embedded structs
195199
fields, err := gstructs.Fields(gstructs.FieldsInput{
196200
Pointer: req,
197201
RecursiveOption: gstructs.RecursiveOptionEmbedded,
198202
})
199203
if err != nil {
200-
return nil, nil, nil, nil, nil, err
204+
return nil, err
201205
}
202206

203207
for _, field := range fields {
@@ -220,60 +224,60 @@ func (c *Client) classifyRequestParams(req any) (
220224
switch inTag {
221225
case goai.ParameterInQuery:
222226
// Flatten struct fields to query parameters
223-
if err := flattenStructToMap(queryParams, fieldValue); err != nil {
224-
return nil, nil, nil, nil, nil, err
227+
if err := flattenStructToMap(params.query, fieldValue); err != nil {
228+
return nil, err
225229
}
226230
continue
227231

228232
case goai.ParameterInHeader:
229233
// Header doesn't support struct, serialize to JSON
230234
jsonBytes, _ := json.Marshal(fieldValue)
231-
headerParams[fieldName] = string(jsonBytes)
235+
params.header[fieldName] = string(jsonBytes)
232236
continue
233237

234238
case goai.ParameterInPath, goai.ParameterInCookie:
235239
// Path and Cookie don't support struct type
236-
return nil, nil, nil, nil, nil, gerror.Newf(
240+
return nil, gerror.Newf(
237241
`field "%s" with in:"%s" cannot be a struct type`,
238242
fieldName, inTag,
239243
)
240244
}
241245
}
242246
// Struct field without `in` tag goes to body
243-
bodyParams[fieldName] = fieldValue
247+
params.body[fieldName] = fieldValue
244248
continue
245249
}
246250

247251
// Handle regular fields (including flattened embedded fields)
248252
switch inTag {
249253
case goai.ParameterInPath:
250-
pathParams[fieldName] = fieldValue
254+
params.path[fieldName] = fieldValue
251255

252256
case goai.ParameterInQuery:
253257
// Handle map type (flatten to key[subkey] format)
254258
if reflectValue.IsValid() && reflectValue.Kind() == reflect.Map {
255259
for _, key := range reflectValue.MapKeys() {
256260
mapKey := fmt.Sprintf("%s[%s]", fieldName, key.String())
257-
queryParams[mapKey] = reflectValue.MapIndex(key).Interface()
261+
params.query[mapKey] = reflectValue.MapIndex(key).Interface()
258262
}
259263
} else {
260264
// Slice/array/primitive types are handled by SetQueryMap
261-
queryParams[fieldName] = fieldValue
265+
params.query[fieldName] = fieldValue
262266
}
263267

264268
case goai.ParameterInHeader:
265-
headerParams[fieldName] = gconv.String(fieldValue)
269+
params.header[fieldName] = gconv.String(fieldValue)
266270

267271
case goai.ParameterInCookie:
268-
cookieParams[fieldName] = gconv.String(fieldValue)
272+
params.cookie[fieldName] = gconv.String(fieldValue)
269273

270274
default:
271275
// No `in` tag, goes to body
272-
bodyParams[fieldName] = fieldValue
276+
params.body[fieldName] = fieldValue
273277
}
274278
}
275279

276-
return
280+
return params, nil
277281
}
278282

279283
// flattenStructToMap flattens struct fields to target map.

0 commit comments

Comments
 (0)