REST API过滤器查询 - 处理或操作
我正在尝试创建一个不错的助手类来解析一些查询参数,以便它们可以用于过滤数据数组。
我正在为我的查询参数使用以下语法:
?filter [name,contrains,string] = foo //名称字段包含字符串“ foo”
?filter [id,gte,gte,number ] = 123 //项目字段大于或等于123
?filter [type,eq,string] = foo | bar //类型字段等于“ foo”或“ bar”
(您还可以在值之间用作和操作员之间的,
,但是在单个字段上,它没有很多用例,例如GT 1和2并不是一个好的过滤器)
这些查询参数可以组合,例如
?filter [name,contrains,string] = foo& exter [id,gte,number] = 123
,这将充当和条件,这意味着数据是返回与两个过滤器匹配的匹配。
到目前为止很棒。但是我想为多个单独的过滤器实现或条件。
?filter [] = ...
用于实际过滤器查询,因此我无法真正使用它来设置或条件。
我尝试找到使用类似于此过滤语法的Somethig的REST API实现,最接近的是Laravel,但是文档并没有详细介绍有关它的任何细节。
希望有人可以将我指出有关REST API过滤/帮助我实现或条件的好方法的良好资源。
I am trying to create a nice helper class to parse some query parameters so they can be used to filter an array of data.
I am using the following syntax for my query parameters:
?filter[name,contains,string]=foo // name field contains the string "foo"
?filter[id,gte,number]=123 // item field is greater then or equal to 123
?filter[type,eq,string]=foo|bar // type field is equal to "foo" or "bar"
(you can also use ,
between the values to act as an AND operator, but on a single field, it doesn't have many use cases eg gt 1 AND 2 isn't a great filter)
These query params can be combined, eg
?filter[name,contains,string]=foo&filter[id,gte,number]=123
And this will act as an AND condition, meaning data is returned that matches both filters.
Great so far. But I want to implement an OR condition for multiple separate filters.
?filter[]=...
is used for the actual filter query, so I can't really use that to set the OR condition.
I've tried finding REST API implementations that use somethig similar to this filtering syntax, the closest is Laravel, but the docs don't go into any details about it.
Hoping someone can point me to a good resource about REST API filtering/help me figure out a nice way to implement the OR condition.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,我认为,如果您想要所需的灵活性,最好定义适当的语法并接受该语法遵循的“滤波字符串”。这意味着您必须定义默认的语法和行为,解析它,然后将其转换为与您的基础存储系统匹配的查询(例如,SQL查询或ORM API调用)。
例如,这可能会使您的过滤器看起来像:
?filter = name :(“ foo”和id> = 123)或id< = 567
,事实证明有很多决定何时开始引入复杂的过滤操作。
有关更多阅读,Google的API过滤语法和准则为: https://google.aip.aip.aip.dev/160 EBNF语法是 https://google.aip.dev/assets/ MISC/EBNF-FILTERING.TXT
Unfortunately I think if you want the flexibility you're looking for, it'll probably be best to define a proper grammar and accept a "filter string" that follows that syntax. This would mean you have to define the grammar and behavior of defaults, parse it, and turn it into a query that matches your underlying storage system (e.g., a SQL query or ORM API calls).
For example, this might make your filters look something like:
?filter=name:("foo" AND id >= 123) OR id <= 567
And it turns out that there's quite a lot to decide when you start introducing complex filtering operations.
For more reading, Google's API filtering syntax and guidelines are: https://google.aip.dev/160 and the EBNF grammar is https://google.aip.dev/assets/misc/ebnf-filtering.txt