[feat] 新增 New Option 可选项
This commit is contained in:
		
							parent
							
								
									e54629237c
								
							
						
					
					
						commit
						8270326a7b
					
				
							
								
								
									
										26
									
								
								condition.go
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								condition.go
									
									
									
									
									
								
							| @ -213,6 +213,28 @@ func (this Condition) GetFields(operator ConditionOperator, types ...ConditionTo | |||||||
| 	return result | 	return result | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func New(typ ConditionType, describe string) *Condition { | type Option func(option *Condition) | ||||||
| 	return &Condition{typ: typ, describe: describe} | 
 | ||||||
|  | func Type(v ConditionType) Option { | ||||||
|  | 	return func(option *Condition) { | ||||||
|  | 		option.typ = v | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func Describe(v string) Option { | ||||||
|  | 	return func(option *Condition) { | ||||||
|  | 		option.describe = v | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func New(opts ...Option) *Condition { | ||||||
|  | 	cond := &Condition{typ: AND} | ||||||
|  | 
 | ||||||
|  | 	if l := len(opts); l > 0 { | ||||||
|  | 		for i := 0; i < l; i++ { | ||||||
|  | 			opts[i](cond) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return cond | ||||||
| } | } | ||||||
|  | |||||||
| @ -17,26 +17,26 @@ type ConditionTokenType string | |||||||
| 
 | 
 | ||||||
| const ( | const ( | ||||||
| 	SQL    ConditionTokenType = "sql" | 	SQL    ConditionTokenType = "sql" | ||||||
| 	FUNC                      = "func" | 	FUNC   ConditionTokenType = "func" | ||||||
| 	PARAM                     = "param" | 	PARAM  ConditionTokenType = "param" | ||||||
| 	STRING                    = "string" | 	STRING ConditionTokenType = "string" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| const ( | const ( | ||||||
| 	IS_NULL     ConditionOperator = "IS NULL" | 	IS_NULL     ConditionOperator = "IS NULL" | ||||||
| 	IS_NOT_NULL                   = "IS NOT NULL" | 	IS_NOT_NULL ConditionOperator = "IS NOT NULL" | ||||||
| 	EQ                            = "=" | 	EQ          ConditionOperator = "=" | ||||||
| 	NE                            = "!=" | 	NE          ConditionOperator = "!=" | ||||||
| 	GT                            = ">" | 	GT          ConditionOperator = ">" | ||||||
| 	GE                            = ">=" | 	GE          ConditionOperator = ">=" | ||||||
| 	LT                            = "<" | 	LT          ConditionOperator = "<" | ||||||
| 	LE                            = "<=" | 	LE          ConditionOperator = "<=" | ||||||
| 	LIKE                          = "LIKE" | 	LIKE        ConditionOperator = "LIKE" | ||||||
| 	NOT_LIKE                      = "NOT LIKE" | 	NOT_LIKE    ConditionOperator = "NOT LIKE" | ||||||
| 	IN                            = "IN" | 	IN          ConditionOperator = "IN" | ||||||
| 	NOT_IN                        = "NOT IN" | 	NOT_IN      ConditionOperator = "NOT IN" | ||||||
| 	REGEXP                        = "REGEXP" | 	REGEXP      ConditionOperator = "REGEXP" | ||||||
| 	NOT_REGEXP                    = "NOT REGEXP" | 	NOT_REGEXP  ConditionOperator = "NOT REGEXP" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| type ConditionTokenValue interface { | type ConditionTokenValue interface { | ||||||
| @ -232,15 +232,50 @@ func (this ConditionExpr) IsIgnoreEmptyParma(m ConditionTokenValue) bool { | |||||||
| 	return false | 	return false | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func NewExpr(rResource, rField, token string, operator ConditionOperator, tType ConditionTokenType, ignoreEmptyParma bool, fn, fnParam string) *ConditionExpr { | type ExprOption func(option *ConditionExpr) | ||||||
| 	return &ConditionExpr{ | 
 | ||||||
| 		operator:          operator, | func Operator(v ConditionOperator) ExprOption { | ||||||
| 		field:             rField, | 	return func(option *ConditionExpr) { | ||||||
| 		fieldResource:     rResource, | 		option.operator = v | ||||||
| 		fieldSqlFunc:      fn, |  | ||||||
| 		fieldSqlFuncParam: fnParam, |  | ||||||
| 		ignoreEmptyParma:  ignoreEmptyParma, |  | ||||||
| 		tokenType:         tType, |  | ||||||
| 		token:             string(token), |  | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func TokenType(v ConditionTokenType) ExprOption { | ||||||
|  | 	return func(option *ConditionExpr) { | ||||||
|  | 		option.tokenType = v | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func IgnoreEmptyParma(v bool) ExprOption { | ||||||
|  | 	return func(option *ConditionExpr) { | ||||||
|  | 		option.ignoreEmptyParma = v | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func FieldSqlFn(fn, fnParam string) ExprOption { | ||||||
|  | 	return func(option *ConditionExpr) { | ||||||
|  | 		option.fieldSqlFunc = fn | ||||||
|  | 		option.fieldSqlFuncParam = fnParam | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func NewExpr(rResource, rField, token string, opts ...ExprOption) *ConditionExpr { | ||||||
|  | 	expr := &ConditionExpr{ | ||||||
|  | 		field:             rField, | ||||||
|  | 		fieldResource:     rResource, | ||||||
|  | 		token:             token, | ||||||
|  | 		tokenType:         STRING, | ||||||
|  | 		operator:          EQ, | ||||||
|  | 		ignoreEmptyParma:  false, | ||||||
|  | 		fieldSqlFunc:      "", | ||||||
|  | 		fieldSqlFuncParam: "", | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if l := len(opts); l > 0 { | ||||||
|  | 		for i := 0; i < l; i++ { | ||||||
|  | 			opts[i](expr) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return expr | ||||||
|  | } | ||||||
|  | |||||||
| @ -23,12 +23,11 @@ func TestEngine(t *testing.T) { | |||||||
| 		g:    req.NewGlobalParam(`{"age": 30}`, nil), | 		g:    req.NewGlobalParam(`{"age": 30}`, nil), | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	cond1 := condition.New(condition.AND, "条件1") | 	cond1 := condition.New(condition.Describe("条件1")) | ||||||
| 
 | 
 | ||||||
| 	cond1.SetExpr(condition.NewExpr( | 	cond1.SetExpr(condition.NewExpr( | ||||||
| 		"TestTable", "age", "age", | 		"TestTable", "age", "age", | ||||||
| 		condition.EQ, condition.PARAM, | 		condition.TokenType(condition.PARAM), | ||||||
| 		false, "", "", |  | ||||||
| 	)) | 	)) | ||||||
| 
 | 
 | ||||||
| 	engine.Case(cond1, func(data any, g req.GlobalParams) error { | 	engine.Case(cond1, func(data any, g req.GlobalParams) error { | ||||||
| @ -66,11 +65,10 @@ func TestRelationEngine(t *testing.T) { | |||||||
| 		}, | 		}, | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	cond1 := condition.New(condition.AND, "条件1"). | 	cond1 := condition.New(condition.Describe("条件1")). | ||||||
| 		SetExpr(condition.NewExpr( | 		SetExpr(condition.NewExpr( | ||||||
| 			"TestTable", "age", "age", | 			"TestTable", "age", "age", | ||||||
| 			condition.EQ, condition.PARAM, | 			condition.TokenType(condition.PARAM), | ||||||
| 			false, "", "", |  | ||||||
| 		)) | 		)) | ||||||
| 
 | 
 | ||||||
| 	engine.Case(cond1, func(data any, g req.GlobalParams) error { | 	engine.Case(cond1, func(data any, g req.GlobalParams) error { | ||||||
| @ -78,16 +76,14 @@ func TestRelationEngine(t *testing.T) { | |||||||
| 		return nil | 		return nil | ||||||
| 	}) | 	}) | ||||||
| 
 | 
 | ||||||
| 	cond2 := condition.New(condition.OR, "条件2"). | 	cond2 := condition.New(condition.Type(condition.OR), condition.Describe("条件2")). | ||||||
| 		SetExpr(condition.NewExpr( | 		SetExpr(condition.NewExpr( | ||||||
| 			"TestTableA", "age", "age", | 			"TestTableA", "age", "age", | ||||||
| 			condition.EQ, condition.PARAM, | 			condition.TokenType(condition.PARAM), | ||||||
| 			false, "", "", |  | ||||||
| 		)). | 		)). | ||||||
| 		SetExpr(condition.NewExpr( | 		SetExpr(condition.NewExpr( | ||||||
| 			"TestTable", "age", "age", | 			"TestTable", "age", "age", | ||||||
| 			condition.EQ, condition.PARAM, | 			condition.TokenType(condition.PARAM), | ||||||
| 			false, "", "", |  | ||||||
| 		)) | 		)) | ||||||
| 
 | 
 | ||||||
| 	engine.Case(cond2, func(data any, g req.GlobalParams) error { | 	engine.Case(cond2, func(data any, g req.GlobalParams) error { | ||||||
|  | |||||||
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							| @ -2,8 +2,6 @@ module git.fsdpf.net/go/condition | |||||||
| 
 | 
 | ||||||
| go 1.21 | go 1.21 | ||||||
| 
 | 
 | ||||||
| toolchain go1.21.5 |  | ||||||
| 
 |  | ||||||
| require ( | require ( | ||||||
| 	git.fsdpf.net/go/contracts v0.0.0-20240327025936-ee4b5fe3c6fe | 	git.fsdpf.net/go/contracts v0.0.0-20240327025936-ee4b5fe3c6fe | ||||||
| 	git.fsdpf.net/go/db v0.0.0-20230731125324-11651ea6640b | 	git.fsdpf.net/go/db v0.0.0-20230731125324-11651ea6640b | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user