So I had this question before and I had an answer below the question which worked, but I just realized that the query I came out with does not work as planned.

Basically now if it works like this

(if the roleskey contains any of the roles in slice) and (if the tenantID is an empty string) or (if tenantIDKey is equal to tenantID)

But what I need is

(if the roleskey contains any of the roles in slice) AND (if the tenantID is an empty string OR if tenantIDKey is equal to tenantID)

Here is my current query:

query := bolthold.Where(roleskey).ContainsAny(bolthold.Slice(roles)...).And(tenantIDKey).Eq("").Or(bolthold.Where(tenantIDKey).Eq(tenantID))

Does anyone know how to solve this?

推荐答案

Try:

query := bolthold.
         Where(tenantIDKey).Eq("").
         Or(
           bolthold.
           Where(tenantIDKey).
           Eq(tenantID)
         ).
         And(roleskey).
         ContainsAny(bolthold.Slice(roles)...)

Or

query := bolthold.
         Where(tenantIDKey).ContainsAny("", tenantID).
         And(roleskey).
         ContainsAny(bolthold.Slice(roles)...)

Or

query := bolthold.
         Where(tenantIDKey).In("", tenantID).
         And(roleskey).
         ContainsAny(bolthold.Slice(roles)...)

Go相关问答推荐

如何使用GRPC UnaryClientInterceptor中的`maily`参数?

如何使用Promela建模语言对Golang RWLock进行建模

CGO Linux到Windows交叉编译中的未知类型名称

禁用Golang中的终端

带有条件的for循环中缺少RETURN语句

如何使用 Go 连接到非默认 firestore 数据库?

go aws-lambda 与 terraform 中的 exec 格式错误

如何将 goose 迁移与 pgx 一起使用?

在 Go 中将元数据从一个 JPEG 复制到另一个

使用Go和Operator SDK通过API调用设置Kubernetes Pods的安装步骤

错误!在为 age-viewer-go 运行 wails dev 或 wails build 命令时

如何将整数哈希细分为范围

errors.Wrap 和 errors.WithMessage 有什么区别

如何在时间范围内规范化数组的元素?

在 Gorm 的 AfterFind() 钩子中获取智能 Select struct 的值

如何编写一个以字符串或错误为参数的通用函数?

从 Go struct 中提取标签作为 reflect.Value

Gorilla/Mux 和 Websocket 竞赛条件,这安全吗?

Go switch vs if-else 效率

Merge字段两个相同类型的struct struct