8 Star 67 Fork 21

gorm/gen

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
condition.go 1.32 KB
一键复制 编辑 原始数据 按行查看 历史
riverchu 提交于 2021-09-11 21:19 . feat: refactor condition
package gen
import (
"fmt"
"gorm.io/datatypes"
"gorm.io/gen/field"
"gorm.io/gorm/clause"
)
func Cond(exprs ...clause.Expression) []Condition {
return exprToCondition(exprs...)
}
var _ Condition = &condContainer{}
type condContainer struct {
value interface{}
err error
}
func (c *condContainer) BeCond() interface{} { return c.value }
func (c *condContainer) CondError() error { return c.err }
func exprToCondition(exprs ...clause.Expression) []Condition {
conds := make([]Condition, 0, len(exprs))
for _, e := range exprs {
switch e := e.(type) {
case *datatypes.JSONQueryExpression:
conds = append(conds, &condContainer{value: e})
default:
conds = append(conds, &condContainer{err: fmt.Errorf("unsupported Expression %T to converted to Condition", e)})
}
}
return conds
}
func condToExpression(conds []Condition) ([]clause.Expression, error) {
exprs := make([]clause.Expression, 0, len(conds))
for _, cond := range conds {
if err := cond.CondError(); err != nil {
return nil, err
}
switch cond.(type) {
case *condContainer, field.Expr, subQuery:
default:
return nil, fmt.Errorf("unsupported condition: %+v", cond)
}
switch e := cond.BeCond().(type) {
case []clause.Expression:
exprs = append(exprs, e...)
case clause.Expression:
exprs = append(exprs, e)
}
}
return exprs, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/gorm/gen.git
git@gitee.com:gorm/gen.git
gorm
gen
gen
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385