-
Notifications
You must be signed in to change notification settings - Fork 36
/
select_parser.go
63 lines (50 loc) · 1.5 KB
/
select_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
57
58
59
60
61
62
63
package godata
import (
"errors"
"strings"
)
type SelectItem struct {
Segments []*Token
}
func ParseSelectString(sel string) (*GoDataSelectQuery, error) {
items := strings.Split(sel, ",")
result := []*SelectItem{}
for _, item := range items {
segments := []*Token{}
for _, val := range strings.Split(item, "/") {
segments = append(segments, &Token{Value: val})
}
result = append(result, &SelectItem{segments})
}
return &GoDataSelectQuery{result}, nil
}
func SemanticizeSelectQuery(sel *GoDataSelectQuery, service *GoDataService, entity *GoDataEntityType) error {
if sel == nil {
return nil
}
newItems := []*SelectItem{}
// replace wildcards with every property of the entity
for _, item := range sel.SelectItems {
// TODO: allow multiple path segments
if len(item.Segments) > 1 {
return NotImplementedError("Multiple path segments in select clauses are not yet supported.")
}
if item.Segments[0].Value == "*" {
for _, prop := range service.PropertyLookup[entity] {
newItems = append(newItems, &SelectItem{[]*Token{&Token{Value: prop.Name}}})
}
} else {
newItems = append(newItems, item)
}
}
sel.SelectItems = newItems
for _, item := range sel.SelectItems {
if prop, ok := service.PropertyLookup[entity][item.Segments[0].Value]; ok {
item.Segments[0].SemanticType = SemanticTypeProperty
item.Segments[0].SemanticReference = prop
} else {
return errors.New("Entity " + entity.Name + " has no property " + item.Segments[0].Value)
}
}
return nil
}