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

Closed
opened 2 years ago by azhai · 1 comments
azhai commented 2 years ago

以这段代码 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 6 months ago
lunny added this to the 1.1.1 milestone 6 months ago
lunny added the
kind/refactor
label 6 months ago
lunny referenced this issue from a commit 6 months ago
lunny referenced this issue from a commit 6 months ago
lunny closed this issue 6 months ago
lunny closed this issue 6 months ago
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date

No due date set.

Dependencies

This issue currently doesn't have any dependencies.

Loading…
There is no content yet.