首页 > 后端开发 > Golang > 正文

在 Go Huma 中添加过滤查询参数

DDD
发布: 2024-11-30 09:36:01
转载
502人浏览过

据我所知,不幸的是,huma 不支持这样的数组查询过滤器:filters[]=filter1&filters[]=filter2(也不保留括号,例如filter=filter1&filter=filter2)。我遇到了这个 github 问题,它给出了一个用逗号 https://github.com/danielgtaylor/huma/issues/325, 分隔过滤器的示例,所以这就是我们最终所做的:filters=postcode:eq:rm7(ex,created:gt:2024-01-01

记录过滤器

与主体参数不同,主体参数可以简单地指定为结构,然后在文档中对其进行验证和生成,过滤器的文档和验证必须单独完成。

文档可以简单地添加到 huma.param 对象的描述属性下(在操作下):

parameters: []*huma.param{{
            name: "filters",
            in:   "query",
            description: "filter properties by various fields. separate filters by comma.\n\n" +
                "format: field:operator:value\n\n" +
                "supported fields:\n" +
                "- postcode (operator: eq)\n" +
                "- created (operators: gt, lt, gte, lte)\n",
            schema: &huma.schema{
                type: "string",
                items: &huma.schema{
                    type:    "string",
                    pattern: "^[a-za-z_]+:(eq|neq|gt|lt|gte|lte):[a-za-z0-9-:.]+$",
                },
                examples: []any{
                    "postcode:eq:rm7 8ex",
                    "created:gt:2024-01-01",
                },
            },
            required: false,
        }},
登录后复制

在 Go Huma 中添加过滤查询参数

我们现在可以定义 propertyfilterparams 结构进行验证:

蓝心千询
蓝心千询

蓝心千询是vivo推出的一个多功能AI智能助手

蓝心千询 34
查看详情 蓝心千询
type filterparam struct {
    field    string
    operator string
    value    interface{}
}

type propertyfilterparams struct {
    items []filterparam
}

func (s *propertyfilterparams) unmarshaltext(text []byte) error {
    equalityfields := []string{"postcode"}
    greatersmallerfields := []string{}
    datefields := []string{"created"}

    for _, item := range strings.split(string(text), ",") {
        filterparam, err := parseandvalidatefilteritem(item, equalityfields, greatersmallerfields, datefields)
        if err != nil {
            return err
        }
        s.items = append(s.items, filterparam)
    }

    return nil
}

func (s *propertyfilterparams) schema(registry huma.registry) *huma.schema {
    return &huma.schema{
        type: huma.typestring,
    }
}

func parseandvalidatefilteritem(item string, equalityfields []string, greatersmallerfields []string, datefields []string) (filterparam, error) {
    parts := strings.splitn(item, ":", 3)

    field := parts[0]
    operator := parts[1]
    value := parts[2]

    if contains(equalityfields, field) {
        if operator != "eq" && operator != "neq" {
            return filterparam{}, fmt.errorf("unsupported operator %s for field %s. only 'eq' and 'neq' are supported.", operator, field)
        }
    } else if contains(greatersmallerfields, field) {
        if !validation.isvalidcomparegreatersmalleroperator(operator) {
            return filterparam{}, fmt.errorf("unsupported operator %s for field %s. supported operators: eq, neq, gt, lt, gte, lte.", operator, field)
        }
    } else if contains(datefields, field) {
        if !validation.isvalidcomparegreatersmalleroperator(operator) {
            return filterparam{}, fmt.errorf("unsupported operator %s for field %s. supported operators: eq, neq, gt, lt, gte, lte.", operator, field)
        }
        if !validation.isvaliddate(value) {
            return filterparam{}, fmt.errorf("invalid date format: %s. expected: yyyy-mm-dd", value)
        }
    } else {
        return filterparam{}, fmt.errorf("unsupported filter field: %s", field)
    }

    return filterparam{field: field, operator: operator, value: value}, nil
}
登录后复制

我将 propertyfilterparams 添加到 propertyqueryparams 结构中:

type propertyqueryparams struct {
    paginationparams
    filter propertyfilterparams `query:"filters" doc:"filter properties by various fields"`
    sort   propertysortparams   `query:"sorts" doc:"sort properties by various fields"`
}
登录后复制

这就是将 propertyqueryparams 添加到路由的样子(请注意,操作代码本身,包括过滤器描述,位于 getallpropertyoperation 下 - 我没有粘贴完整的代码,但希望您能理解它的要点) 。如果验证失败,它将抛出 422 响应。我还添加了如何循环遍历通过的过滤器值:

huma.Register(api, getAllPropertyOperation(schema, "get-properties", "/properties", []string{"Properties"}),
        func(ctx context.Context, input *struct {
            models.Headers
            models.PropertyQueryParams
        }) (*models.MultiplePropertyOutput, error) {

            for _, filter := range input.Filter.Items {
                fmt.Println(filter)
            }

            return mockMultiplePropertyResponse(), err
        })
}
登录后复制

我希望这对某人有帮助。如果您找到更好的解决方案,请在评论中告诉我。

以上就是在 Go Huma 中添加过滤查询参数的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号