-
Notifications
You must be signed in to change notification settings - Fork 36
/
orderby_parser.go
56 lines (46 loc) · 1.19 KB
/
orderby_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package godata
import (
"strings"
)
const (
ASC = "asc"
DESC = "desc"
)
type OrderByItem struct {
Field *Token
Order string
}
func ParseOrderByString(orderby string) (*GoDataOrderByQuery, error) {
items := strings.Split(orderby, ",")
result := make([]*OrderByItem, 0)
for _, v := range items {
parts := strings.Split(v, " ")
field := &Token{Value: parts[0]}
var order string = ASC
if len(parts) > 1 {
if strings.ToLower(parts[1]) == ASC {
order = ASC
} else if strings.ToLower(parts[1]) == DESC {
order = DESC
} else {
return nil, BadRequestError("Could not parse orderby query.")
}
}
result = append(result, &OrderByItem{field, order})
}
return &GoDataOrderByQuery{result}, nil
}
func SemanticizeOrderByQuery(orderby *GoDataOrderByQuery, service *GoDataService, entity *GoDataEntityType) error {
if orderby == nil {
return nil
}
for _, item := range orderby.OrderByItems {
if prop, ok := service.PropertyLookup[entity][item.Field.Value]; ok {
item.Field.SemanticType = SemanticTypeProperty
item.Field.SemanticReference = prop
} else {
return BadRequestError("No property " + item.Field.Value + " for entity " + entity.Name)
}
}
return nil
}