如何动态构建查询过滤器
How to dynamically build query filters
我正在使用 Go 设置我自己的 API。由于我如何编写代码以动态 create/apply 查询过滤器,我现在有点卡住了。它有效,但我想知道是否有更好的方法来完成下面的场景。
例如,我有一个带有复选框的搜索页面(1 个用于电子邮件,1 个用于姓名)以缩小搜索范围。
// If I checked the email, the query would be like this
query findOne() {
user(func: type(user)) @filter(eq(email, "john.doe@email.com")) {
name
email
age
home_address
}
}
// If name checkedbox is also checked, it would be like this
query findOne() {
user(func: type(user)) @filter(eq(email, "john") OR eq(name, "john")) {
name
email
age
home_address
}
}
这是我目前得到的结果,我认为有更好的方法来做到这一点:
func (s *Service) GetUser(email, name string) (*Users, error) {
c := db.NewClient()
defer db.Close()
var u Users
var filter string
if email != "" && mobileNumber != "" {
filter = fmt.Sprintf(`eq(email, "%s") OR eq(mobileNumber, "%s")`, email, mobileNumber)
} else if email != "" && mobileNumber == "" {
filter = fmt.Sprintf(`eq(email, "%s")`, email)
} else if email == "" && mobileNumber != "" {
filter = fmt.Sprintf(`eq(mobileNumber, "%s")`, mobileNumber)
}
q := fmt.Sprintf(`query findOne() {
users(func: type("user")) @filter(%s) {
name
email
home_address
contact_number
}
}`, filter)
ctx := context.Background()
res, err := c.NewTxn().Query(ctx, q)
if err != nil {
return nil, err
}
if err = json.Unmarshal(res.Json, &u); err != nil {
return nil, err
}
return &u, nil
}
有没有更好的方法来代替创建长条件?
我建议重构您的过滤器逻辑,如下所述:
package main
import (
"fmt"
"strings"
)
func getQuery(key, val string, filters *[]string) {
if val != "" {
*filters = append(*filters, fmt.Sprintf(`eq("%s", "%s")`, key, val))
}
}
func main() {
var filters []string
email := "demo@demo.com"
mobileNumber := "123456789"
getQuery("email", email, &filters)
getQuery("mobileNumber", mobileNumber, &filters)
filter := strings.Join(filters, " OR ")
fmt.Println(filter)
}
这是它的反射版本。基本上它枚举字段,获取值和字段名称以构建基于它们的字符串数组。请注意,我经验不足,它可能还需要一些改进。
import (
"fmt"
"reflect"
"strings"
)
type User struct {
Id int
FullName string
Phone string
Mail string
}
func main() {
u := &User{Id: 10, FullName: "John", Mail: "john@mail"}
u2 := struct {
id int
name string
}{10, "john"};
// inline struct
q := getQuery(&u2, "OR")
fmt.Println(q)
// typed struct
q = getQuery(u, "AND")
fmt.Println(q)
}
func getQuery(target interface{}, join string) string {
var filters []string
val := reflect.ValueOf(target).Elem()
for i := 0; i < val.NumField(); i++ {
value := val.Field(i)
s :=fmt.Sprintf("%v",value);
// this little trick is to check if it is an empty value
// so don't generate empty condition expressions
if s == "" {
continue
}
fieldType := val.Type().Field(i)
filters = append(filters, fmt.Sprintf(" eq(%s, %v) ", fieldType.Name, value))
}
return strings.Join(filters, join)
}
我正在使用 Go 设置我自己的 API。由于我如何编写代码以动态 create/apply 查询过滤器,我现在有点卡住了。它有效,但我想知道是否有更好的方法来完成下面的场景。
例如,我有一个带有复选框的搜索页面(1 个用于电子邮件,1 个用于姓名)以缩小搜索范围。
// If I checked the email, the query would be like this
query findOne() {
user(func: type(user)) @filter(eq(email, "john.doe@email.com")) {
name
email
age
home_address
}
}
// If name checkedbox is also checked, it would be like this
query findOne() {
user(func: type(user)) @filter(eq(email, "john") OR eq(name, "john")) {
name
email
age
home_address
}
}
这是我目前得到的结果,我认为有更好的方法来做到这一点:
func (s *Service) GetUser(email, name string) (*Users, error) {
c := db.NewClient()
defer db.Close()
var u Users
var filter string
if email != "" && mobileNumber != "" {
filter = fmt.Sprintf(`eq(email, "%s") OR eq(mobileNumber, "%s")`, email, mobileNumber)
} else if email != "" && mobileNumber == "" {
filter = fmt.Sprintf(`eq(email, "%s")`, email)
} else if email == "" && mobileNumber != "" {
filter = fmt.Sprintf(`eq(mobileNumber, "%s")`, mobileNumber)
}
q := fmt.Sprintf(`query findOne() {
users(func: type("user")) @filter(%s) {
name
email
home_address
contact_number
}
}`, filter)
ctx := context.Background()
res, err := c.NewTxn().Query(ctx, q)
if err != nil {
return nil, err
}
if err = json.Unmarshal(res.Json, &u); err != nil {
return nil, err
}
return &u, nil
}
有没有更好的方法来代替创建长条件?
我建议重构您的过滤器逻辑,如下所述:
package main
import (
"fmt"
"strings"
)
func getQuery(key, val string, filters *[]string) {
if val != "" {
*filters = append(*filters, fmt.Sprintf(`eq("%s", "%s")`, key, val))
}
}
func main() {
var filters []string
email := "demo@demo.com"
mobileNumber := "123456789"
getQuery("email", email, &filters)
getQuery("mobileNumber", mobileNumber, &filters)
filter := strings.Join(filters, " OR ")
fmt.Println(filter)
}
这是它的反射版本。基本上它枚举字段,获取值和字段名称以构建基于它们的字符串数组。请注意,我经验不足,它可能还需要一些改进。
import (
"fmt"
"reflect"
"strings"
)
type User struct {
Id int
FullName string
Phone string
Mail string
}
func main() {
u := &User{Id: 10, FullName: "John", Mail: "john@mail"}
u2 := struct {
id int
name string
}{10, "john"};
// inline struct
q := getQuery(&u2, "OR")
fmt.Println(q)
// typed struct
q = getQuery(u, "AND")
fmt.Println(q)
}
func getQuery(target interface{}, join string) string {
var filters []string
val := reflect.ValueOf(target).Elem()
for i := 0; i < val.NumField(); i++ {
value := val.Field(i)
s :=fmt.Sprintf("%v",value);
// this little trick is to check if it is an empty value
// so don't generate empty condition expressions
if s == "" {
continue
}
fieldType := val.Type().Field(i)
filters = append(filters, fmt.Sprintf(" eq(%s, %v) ", fieldType.Name, value))
}
return strings.Join(filters, join)
}