-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_value.go
More file actions
121 lines (108 loc) · 2.38 KB
/
Copy pathquery_value.go
File metadata and controls
121 lines (108 loc) · 2.38 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
package songo
import (
"strconv"
"strings"
)
type SongoQueryValue struct {
Operator string
Value interface{}
}
func (s *SongoQueryValue) Split(value string) bool {
if strings.HasPrefix(value, "$") {
value = value[1:]
}
vs := strings.Split(value, "$")
if len(vs) <= 1 {
return false
}
s.Operator = "$" + strings.TrimSpace(vs[0])
if !VerifyQueryOperator(s.Operator) {
return false
}
if len(vs) == 2 {
s.Value = strings.TrimSpace(vs[1])
return true
}
var ss SongoQueryValue
s.Value = &ss
return ss.Split(strings.Join(vs[1:], "$"))
}
func (s *SongoQueryValue) Next() (qv *SongoQueryValue, ok bool) {
if s.Value != nil {
qv, ok = s.Value.(*SongoQueryValue)
}
if !ok {
qv = s
}
return
}
func (s *SongoQueryValue) HasNext() (ok bool) {
_, ok = s.Next()
return
}
func (s SongoQueryValue) GetValue() interface{} {
if str, ok := s.Value.(string); ok {
return StringToValue(str)
} else if v, ok := s.Value.(*SongoQueryValue); ok {
return v.GetValue()
}
return s.Value
}
func (s SongoQueryValue) GetQuery() (operators []string, value interface{}) {
qv := &s
for ok := true; ok; {
operators = append(operators, qv.Operator)
qv, ok = qv.Next()
}
value = qv.GetValue()
return
}
func (s *SongoQueryValue) ValueString() string {
if s.Value != nil {
if qv, ok := s.Next(); ok {
return qv.String()
}
return s.Value.(string)
}
return ""
}
func (s *SongoQueryValue) ValueStrings() (values []string) {
values = append(values, s.Operator)
if qv, ok := s.Next(); ok {
values = append(values, qv.ValueStrings()...)
} else {
values = append(values, s.ValueString())
}
return
}
func (s *SongoQueryValue) String() string {
if s.Value != nil {
if qv, ok := s.Next(); ok {
return s.Operator + qv.String()
}
return s.Operator + s.Value.(string)
}
return ""
}
func StringToValue(str string) interface{} {
if v, err := strconv.ParseInt(str, 10, 64); err == nil {
return v
} else if v, err := strconv.ParseFloat(str, 64); err == nil {
return v
} else if v, err := strconv.ParseUint(str, 10, 64); err == nil {
return v
} else if v, err := strconv.ParseBool(str); err == nil {
return v
} else if strings.Contains(str, ",") {
return strings.Split(str, ",")
}
return str
}
func SplitQueryValue(value string) (v SongoQueryValue, ok bool) {
ok = v.Split(value)
return
}
func VerifyQueryValue(value string) bool {
_, ok := SplitQueryValue(value)
return ok
}