-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsort_parser.go
More file actions
124 lines (98 loc) · 2.79 KB
/
Copy pathsort_parser.go
File metadata and controls
124 lines (98 loc) · 2.79 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package qfv
import (
"fmt"
"strings"
)
type QFVSortError struct {
Field string
Message string
}
func (e *QFVSortError) Error() string {
if e.Field != "" {
return fmt.Sprintf("error on field '%s': %s", e.Field, e.Message)
}
return fmt.Sprintf("error: %s", e.Message)
}
// SortDirection represents the sorting direction in sort expressions
type SortDirection string
const (
SortAsc SortDirection = "ASC"
SortDesc SortDirection = "DESC"
)
func (sd SortDirection) String() string {
return string(sd)
}
// SortFieldNode represents a single field in the sort expression
type SortFieldNode struct {
Field string
Direction SortDirection
}
func (n SortFieldNode) Type() NodeType {
return NodeTypeSortField
}
// SortNode represents the sort part of the query
type SortNode struct {
Fields []SortFieldNode
}
func (n SortNode) Type() NodeType {
return NodeTypeSort
}
// SortParser parses the query parameter for sorting
type SortParser struct {
allowedFields map[string]any // any because don't allocate memory for struct{}
}
// NewSortParser creates a new parser with the allowed fields for sorting
func NewSortParser(allowedFields []string) *SortParser {
sortFields := make(map[string]any, len(allowedFields))
for _, f := range allowedFields {
sortFields[f] = struct{}{}
}
return &SortParser{
allowedFields: sortFields,
}
}
// Parse parses the sort parameter
func (p *SortParser) Parse(input string) (SortNode, error) {
if input == "" {
return SortNode{}, &QFVSortError{Message: "empty input expression"}
}
parts := strings.Split(input, ",")
fields := make([]SortFieldNode, 0, len(parts))
for _, part := range parts {
part = strings.TrimSpace(part)
if part == "" {
return SortNode{}, &QFVSortError{Field: part, Message: "empty sort expression"}
}
sortParts := strings.Fields(part)
if len(sortParts) == 0 {
return SortNode{}, &QFVSortError{Field: part, Message: "invalid sort expression"}
}
if len(sortParts) > 2 {
return SortNode{}, &QFVSortError{Field: part, Message: "too many sort expressions"}
}
fieldName := sortParts[0]
if _, exists := p.allowedFields[fieldName]; !exists {
return SortNode{}, &QFVSortError{Field: fieldName, Message: "field not allowed for sorting"}
}
direction := SortAsc
if len(sortParts) == 1 {
return SortNode{}, &QFVSortError{Field: fieldName, Message: "missing sort direction after field"}
}
if len(sortParts) > 1 {
dirStr := strings.ToUpper(sortParts[1])
switch dirStr {
case SortDesc.String():
direction = SortDesc
case SortAsc.String():
direction = SortAsc
default:
return SortNode{}, &QFVSortError{Field: fieldName, Message: "invalid sort direction"}
}
}
fields = append(fields, SortFieldNode{
Field: fieldName,
Direction: direction,
})
}
return SortNode{Fields: fields}, nil
}