go-gorm / go-gorm/datatypes

希望可以支持一下JSONQuery("key").NotEquals(...)

Open
#254 0 comments 0 reactions 1 assignee View on GitHub

@jinzhu is already working on this.

Since Apr 28, 2024.

Dominant language
Go
Stars
831
Forks
124
PR merge metrics
No merged PRs in 30d

Description

我尝试着复制JSONQuery代码自己修改NotEquals,发现gorm框架里面有各种各样的类型限制,导致我没法直接设计自己的NotEquals
所以希望架构能支持JSONQuery.NotEquals, 或者说至少支持我自定义实现JSONQuery等部类型的相关Condition接口

func exprToCondition(exprs ...clause.Expression) []Condition {
	conds := make([]Condition, 0, len(exprs))
	for _, e := range exprs {
		switch e := e.(type) {
		case *datatypes.JSONQueryExpression, *datatypes.JSONOverlapsExpression:
			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) {
	if len(conds) == 0 {
		return nil, nil
	}
	exprs := make([]clause.Expression, 0, len(conds))
	for _, cond := range conds {
		if cond == nil {
			continue
		}
		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
}

Motivation

Related Issues

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.