src-d/go-kallax

Ordering from variable

nikkon226 opened this issue · 4 comments

I want to be able to implement ordering based on user input from GET request params.

Easy enough to use a switch statement with different cases, but here is the problem I ran into: I want to order based upon information in a JSON field. Currently, there is no way to do this because of private functions in the interface.

The way I went about solving this is to use a custom type for column sorting and implement the interface for ColumnOrder. I cannot implement the interface because it requires kallax.isColumnOrder() which I cannot make a function in my code like this.

Please change this function to a public function so that we could implement our own sort order names.

** Ordering based on JSON fields are simple ORDER column->>'jsonKey' ASC for example.

You need to embed the ColumnOrder interface in your own struct, and then define ToSql(Schema) string as usual:

type jsonOrder struct {
    kallax.ColumnOrder
    // any other stuff in case you need it
}

func (jso *jsonOrder) ToSql(Schema) string {
    // do even more stuff
}

Please note that I haven't tested this, but technically it should work.

Embedding ColumnOrder like that is possible.
I have used the same method to achieve random ordering: #221

Closing issue based on the above (no problems, just noobish).