feat(net/gclient): added query parameter handling function and complete the parameter handling function of DoRequestObj#4639
Open
LanceAdd wants to merge 19 commits into
Open
Conversation
- 新增 queryParams 字段用于存储查询参数映射 - 实现 Query、QueryParams 和 QueryPair 链式调用方法 - 添加 SetQuery、SetQueryMap 和 SetQueryParams 设置方法 - 支持结构体参数自动转换为查询参数 - 支持切片和数组类型的参数处理 - 实现 nil 值参数跳过逻辑 - 添加 URL 参数合并和覆盖功能 - 完善查询参数的单元测试覆盖
gqcn
requested changes
Jan 21, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds comprehensive query parameter handling capabilities to the gclient HTTP client, enabling developers to set query parameters through multiple convenient methods using various data types including structs, maps, slices, and basic types.
Changes:
- Added
queryParamsfield to the Client struct to store query parameters - Implemented three configuration methods (SetQuery, SetQueryMap, SetQueryParams) and three chaining methods (Query, QueryParams, QueryPair)
- Enhanced request preparation logic to process and merge query parameters with proper nil/empty value handling and type conversion using reflection
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| net/gclient/gclient.go | Added queryParams field to Client struct, initialized in New(), and cloned in Clone() method |
| net/gclient/gclient_config.go | Implemented SetQuery, SetQueryMap, and SetQueryParams configuration methods |
| net/gclient/gclient_chain.go | Added Query, QueryParams, and QueryPair chaining methods for fluent API usage |
| net/gclient/gclient_request.go | Enhanced prepareRequest with query parameter processing logic using reflection to handle slices/arrays and proper URL merging |
| net/gclient/gclient_z_unit_query_params_test.go | Comprehensive test suite covering basic types, structs, slices/arrays, URL merging, chaining, special characters, and integration scenarios |
| net/gclient/gclient_z_unit_query_nil_test.go | Extensive tests for nil value handling including nil pointers, nil slices, empty slices/arrays, and mixed scenarios |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- 移除不必要的 url2 包导入 - 直接操作 URL 结构体字段而非重新解析 URL 字符串 - 提高高吞吐量应用的性能避免不必要的字符串操作 - 更新反射指针解引用注释说明完全解引用所有层级指针 - 简化查询参数编码直接设置到请求 URL 对象
- 新增mergeQueryParams函数统一处理URL查询参数合并 - 新增normalizeURL函数标准化URL前缀和协议 - 新增buildRequestParams函数构建请求参数 - 新增enhanceRequest函数增强请求上下文、头部和认证 - 新增createNormalPostRequest函数创建普通POST请求 - 新增createMultipartRequest函数创建多部分表单请求 - 新增createGetRequest函数创建GET请求并处理查询参数 - 新增createPostRequest函数创建POST/PUT/DELETE请求 - 优化prepareRequest函数结构化请求构建流程 - 改进参数处理优先级:queryParams > dataParams > urlParams - 修复noUrlEncode标志对查询参数编码的影响 - 提升代码可读性和维护性
- 移除了单部分参数的空字符串赋值逻辑 - 添加了完整的查询参数与数据参数交互测试 - 验证了Query/QueryParams/QueryPair方法与Get方法数据参数的集成 - 确保查询参数优先级高于数据参数 - 测试了多种参数类型的组合场景
- 移除 unused empty 包依赖 - 在 SetQueryParam 方法中过滤 nil 值以保持一致性 - 在 SetQueryParams 方法中过滤 nil 值以保持一致性 - 简化 IsNil 检查逻辑直接使用 != nil 判断
…确保空参数不会影响服务器端的默认值处理逻辑
- 新增GetMergedURL方法用于获取合并后的URL而不发送请求 - 添加GetMergedURL单元测试验证不同HTTP方法的行为 - 完善所有HTTP方法的查询参数功能测试 - 支持结构体、切片、数组等多种查询参数类型 - 实现查询参数与URL参数的合并逻辑 - 添加查询参数与请求体数据的组合测试 - 新增GetBodyMap方法仅获取请求体参数
- 提取URL参数解析逻辑到独立函数parseUnEncodedParams - 提取查询字符串构建逻辑到独立函数buildUnEncodedQuery - 添加getMediaType方法安全解析Content-Type头部 - 将上下文参数传递给相关请求构建函数 - 移除重复的参数合并代码并优化流程 - 添加内部日志记录用于调试目的
- 实现 in 标签参数分类,支持 path、query、header、cookie 和 body 参数 - 添加路径参数替换和查询参数映射功能 - 支持嵌入结构体字段自动展平 - 实现命名结构体字段特殊处理逻辑 - 添加对切片和映射类型查询参数的支持 - 新增文件上传参数处理机制 - 完善错误处理和参数验证逻辑 - 补充单元测试覆盖各种参数组合场景
- 将多个返回值改为结构体返回,提高代码可读性 - 新增 requestParams 结构体统一管理各类参数 - 修改 classifyRequestParams 方法返回 *requestParams 指针 - 更新所有参数访问方式从独立变量到结构体字段 - 简化错误返回逻辑,统一错误处理流程 - 优化参数分类和赋值的代码结构
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat(net/gclient): added query parameter handling function
描述
DoRequestObj参数处理问题,通过结构化的方式支持多种参数类型的分类和处理,实现了OpenApi风格的参数位置标记,使用in标签指定参数位置,支持 5 种参数类型:path、query、header、cookie、body变更内容
1.
Client结构体变更Client结构体中添加了queryParams字段用于存储查询参数 (map[string]any)New()函数中初始化queryParams为make(map[string]any)Clone()方法中添加queryParams的深拷贝逻辑2. 配置方法新增 (
gclient_config.go)SetQuery: 设置单个查询参数SetQueryMap: 批量设置查询参数SetQueryParams: 通过结构体或映射设置查询参数3. 链式调用方法新增 (
gclient_chain.go)Query: 通过映射设置查询参数QueryParams: 通过结构体或映射设置查询参数QueryPair: 设置单个查询参数4. 请求处理逻辑增强 (
gclient_request.go)prepareRequest方法中添加查询参数处理逻辑url2 "net/url"包解析URL以避免命名冲突nil值和空切片的特殊处理URL中已有参数的智能合并机制API 描述
配置方法
SetQuery(key string, value any) *Client: 设置单个查询参数,支持任意类型值SetQueryMap(m map[string]any) *Client: 批量设置查询参数,接受映射类型参数SetQueryParams(params any) *Client: 设置查询参数,支持结构体、映射等类型,使用gconv.Map进行转换链式调用方法
Query(m map[string]any) *Client: 通过映射设置查询参数,创建新的Client实例QueryParams(params any) *Client: 通过结构体或映射设置查询参数,创建新的Client实例QueryPair(key string, value any) *Client: 设置单个查询参数,创建新的Client实例功能特性
1. 多数据类型支持
string,int,bool,float等gconv.Map转换为映射map[string]any)2. 智能参数合并
queryValues.Add和queryValues.Set)3. 链式调用支持
gclient的设计风格实现细节
在
prepareRequest方法中,查询参数处理的关键逻辑:URL以获取现有的查询参数queryParams映射URL使用示例
1. 基础使用
2. 结构体传参
3. 单个参数设置
4. 链式调用
5. 切片和数组类型使用示例
6. 与其他配置组合使用
7. URL参数合并示例
向后兼容性
性能考量
URL处理函数,效率高complete the parameter handling function of DoRequestObj
📋 概述
为
gclient.DoRequestObj方法增加了基于in标签的参数分类功能,支持将请求对象的字段自动分类到不同的 HTTP 请求位置(路径、查询、请求头、Cookie、请求体),提供更灵活和类型安全的 HTTP 客户端使用方式。✨ 核心功能
1. 支持
in标签参数分类现在可以使用
in标签指定参数的位置,支持 5 种参数类型:g.Meta 支持的标签:
path- 请求路径(必需)method- HTTP 方法(必需)mime- Content-Type 头(可选,如 "application/json")实际请求效果:
使用
mime标签控制请求格式:mime:默认使用表单数据格式(application/x-www-form-urlencoded)mime:"application/json":使用 JSON 格式发送请求体mime:"multipart/form-data":使用 multipart 格式(用于文件上传)2. 支持的数据类型
in:"path"):基本类型(int, string, bool 等)in:"query"):基本类型、切片、数组in:"header"):字符串类型in:"cookie"):字符串类型类型限制:
3. 请求体参数的结构体处理
对于请求体参数,支持两种结构体使用方式:
匿名嵌入结构体 - 字段展平:
命名结构体字段 - 保持嵌套:
说明:
mime:"application/json"标签可以指定使用 JSON 格式发送请求体mime时,默认使用表单数据格式(application/x-www-form-urlencoded)4. 清晰的参数分类规则
in标签的字段:按标签指定的位置分类in标签的字段:默认放入请求体in标签)🎯 使用场景
这个功能特别适合以下场景: