Skip to content

Commit edecc8f

Browse files
refactor: change key name (#75)
1 parent 95b0b2c commit edecc8f

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

rql/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ Frontend should send the parameters and operator like this schema to the backend
2626
"limit": 50,
2727
"search": "abcd",
2828
"sort": [
29-
{ "key": "title", "order": "desc" },
30-
{ "key": "created_at", "order": "asc" }
29+
{ "name": "title", "order": "desc" },
30+
{ "name": "created_at", "order": "asc" }
3131
]
3232
}
3333
```

rql/parser.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type Filter struct {
4040
}
4141

4242
type Sort struct {
43-
Key string `json:"key"`
43+
Name string `json:"name"`
4444
Order string `json:"order"`
4545
}
4646

@@ -185,12 +185,12 @@ func isValidOperator(filterItem Filter) bool {
185185

186186
func validateSortKey(q *Query, val reflect.Value) error {
187187
for _, item := range q.Sort {
188-
filterIdx := searchKeyInsideStruct(item.Key, val)
188+
filterIdx := searchKeyInsideStruct(item.Name, val)
189189
if filterIdx < 0 {
190-
return fmt.Errorf("'%s' is not a valid sort key", item.Key)
190+
return fmt.Errorf("'%s' is not a valid sort key", item.Name)
191191
}
192192
if !slices.Contains(validSortOrder, item.Order) {
193-
return fmt.Errorf("'%s' is not a valid sort key", item.Key)
193+
return fmt.Errorf("'%s' is not a valid sort key", item.Name)
194194
}
195195
}
196196
return nil

rql/parser_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestValidateQuery(t *testing.T) {
2929
{Name: "CreatedAt", Operator: "eq", Value: "2021-09-15T15:53:00Z"},
3030
},
3131
Sort: []Sort{
32-
{Key: "ID", Order: "asc"},
32+
{Name: "ID", Order: "asc"},
3333
},
3434
},
3535
checkStruct: TestStruct{},
@@ -69,7 +69,7 @@ func TestValidateQuery(t *testing.T) {
6969
name: "Invalid sort key",
7070
query: Query{
7171
Sort: []Sort{
72-
{Key: "NonExistentKey", Order: "asc"},
72+
{Name: "NonExistentKey", Order: "asc"},
7373
},
7474
},
7575
checkStruct: TestStruct{},

0 commit comments

Comments
 (0)