Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order func accept interface{} type but it not order and not error when the value is not an expected type. so why not change it or add a new func. #7303

Open
yyle88 opened this issue Dec 5, 2024 · 0 comments
Assignees
Labels
type:feature_request feature request

Comments

@yyle88
Copy link

yyle88 commented Dec 5, 2024

Describe the feature

The order func is this:

func (db *DB) Order(value interface{}) (tx *DB) {
	tx = db.getInstance()

	switch v := value.(type) {
	case clause.OrderBy:
		tx.Statement.AddClause(v)
	case clause.OrderByColumn:
		tx.Statement.AddClause(clause.OrderBy{
			Columns: []clause.OrderByColumn{v},
		})
	case string:
		if v != "" {
			tx.Statement.AddClause(clause.OrderBy{
				Columns: []clause.OrderByColumn{{
					Column: clause.Column{Name: v, Raw: true},
				}},
			})
		}
	}
	return
}

The input is an interface while the switch has no default branch.

So why don't give a func OrderBy() and func OrderByColumn()

Motivation

Some times we use a string-builder to build the order stmt.

While when we pass the custom string-builder type to the Order func. it not return error and not do the order.

For example.

type A struct {
    orderByString string
}

func (a *A) AppendOrder(orderString string){
    if a.orderByString != ""{
        a.orderByString += ","
    }
    a.orderByString += orderString
}

var a = &A{}
a.AppendOrder("name asc")
a.AppendOrder("age desc")

db.Where(...).Order(a).Find(...).Error

same with the logic:

db.Where(...).Find(...).Error

which means no order.

Related Issues

@github-actions github-actions bot added the type:feature_request feature request label Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature_request feature request
Projects
None yet
Development

No branches or pull requests

2 participants