Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Frontend should send the parameters and operator like this schema to the backend
"limit": 50,
"search": "abcd",
"sort": [
{ "key": "title", "order": "desc" },
{ "key": "created_at", "order": "asc" }
{ "name": "title", "order": "desc" },
{ "name": "created_at", "order": "asc" }
]
}
```
Expand Down
8 changes: 4 additions & 4 deletions rql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Filter struct {
}

type Sort struct {
Key string `json:"key"`
Name string `json:"name"`
Order string `json:"order"`
}

Expand Down Expand Up @@ -185,12 +185,12 @@ func isValidOperator(filterItem Filter) bool {

func validateSortKey(q *Query, val reflect.Value) error {
for _, item := range q.Sort {
filterIdx := searchKeyInsideStruct(item.Key, val)
filterIdx := searchKeyInsideStruct(item.Name, val)
if filterIdx < 0 {
return fmt.Errorf("'%s' is not a valid sort key", item.Key)
return fmt.Errorf("'%s' is not a valid sort key", item.Name)
}
if !slices.Contains(validSortOrder, item.Order) {
return fmt.Errorf("'%s' is not a valid sort key", item.Key)
return fmt.Errorf("'%s' is not a valid sort key", item.Name)
}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions rql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestValidateQuery(t *testing.T) {
{Name: "CreatedAt", Operator: "eq", Value: "2021-09-15T15:53:00Z"},
},
Sort: []Sort{
{Key: "ID", Order: "asc"},
{Name: "ID", Order: "asc"},
},
},
checkStruct: TestStruct{},
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestValidateQuery(t *testing.T) {
name: "Invalid sort key",
query: Query{
Sort: []Sort{
{Key: "NonExistentKey", Order: "asc"},
{Name: "NonExistentKey", Order: "asc"},
},
},
checkStruct: TestStruct{},
Expand Down