Suggest: 类型判断 switch v.(type) 可以写得更简单一点 #1663

Closed
opened 2020-04-25 03:26:49 +00:00 by azhai · 1 comment
Contributor

以这段代码 internal/statements/statement.go 第 211 行为例

原来的代码

// And add Where & and statement
func (statement *Statement) And(query interface{}, args ...interface{}) *Statement {
	switch query.(type) {
	case string:
		cond := builder.Expr(query.(string), args...)
		statement.cond = statement.cond.And(cond)
	case map[string]interface{}:
		queryMap := query.(map[string]interface{})
		newMap := make(map[string]interface{})
		for k, v := range queryMap {
			newMap[statement.quote(k)] = v
		}
		statement.cond = statement.cond.And(builder.Eq(newMap))
	case builder.Cond:
		cond := query.(builder.Cond)
		statement.cond = statement.cond.And(cond)
		for _, v := range args {
			if vv, ok := v.(builder.Cond); ok {
				statement.cond = statement.cond.And(vv)
			}
		}
	default:
		statement.LastError = ErrConditionType
	}

	return statement
}

可以改写为

// And add Where & and statement
func (statement *Statement) And(query interface{}, args ...interface{}) *Statement {
	switch qr := query.(type) {
	case string:
		cond := builder.Expr(qr, args...)
		statement.cond = statement.cond.And(cond)
	case map[string]interface{}:
		cond := make(builder.Eq)
		for k, v := range qr {
			cond[statement.quote(k)] = v
		}
		statement.cond = statement.cond.And(cond)
	case builder.Cond:
		statement.cond = statement.cond.And(qr)
		for _, v := range args {
			if vv, ok := v.(builder.Cond); ok {
				statement.cond = statement.cond.And(vv)
			}
		}
	default:
		statement.LastError = ErrConditionType
	}

	return statement
}
以这段代码 internal/statements/statement.go 第 211 行为例 原来的代码 ```go // And add Where & and statement func (statement *Statement) And(query interface{}, args ...interface{}) *Statement { switch query.(type) { case string: cond := builder.Expr(query.(string), args...) statement.cond = statement.cond.And(cond) case map[string]interface{}: queryMap := query.(map[string]interface{}) newMap := make(map[string]interface{}) for k, v := range queryMap { newMap[statement.quote(k)] = v } statement.cond = statement.cond.And(builder.Eq(newMap)) case builder.Cond: cond := query.(builder.Cond) statement.cond = statement.cond.And(cond) for _, v := range args { if vv, ok := v.(builder.Cond); ok { statement.cond = statement.cond.And(vv) } } default: statement.LastError = ErrConditionType } return statement } ``` 可以改写为 ```go // And add Where & and statement func (statement *Statement) And(query interface{}, args ...interface{}) *Statement { switch qr := query.(type) { case string: cond := builder.Expr(qr, args...) statement.cond = statement.cond.And(cond) case map[string]interface{}: cond := make(builder.Eq) for k, v := range qr { cond[statement.quote(k)] = v } statement.cond = statement.cond.And(cond) case builder.Cond: statement.cond = statement.cond.And(qr) for _, v := range args { if vv, ok := v.(builder.Cond); ok { statement.cond = statement.cond.And(vv) } } default: statement.LastError = ErrConditionType } return statement } ```
Owner

Please send a PR. Thanks in advance!

Please send a PR. Thanks in advance!
lunny referenced this issue from a commit 2021-06-12 09:54:37 +00:00
lunny added this to the 1.1.1 milestone 2021-06-12 09:54:39 +00:00
lunny added the
kind
refactor
label 2021-06-12 09:55:08 +00:00
lunny referenced this issue from a commit 2021-06-12 13:17:11 +00:00
lunny referenced this issue from a commit 2021-06-14 03:23:06 +00:00
lunny closed this issue 2021-06-14 03:23:06 +00:00
lunny closed this issue 2021-06-14 03:23:06 +00:00
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#1663
No description provided.