Skip to content

Commit 2113506

Browse files
author
崔竞宁
committed
feat: init function
1 parent e313b42 commit 2113506

File tree

9 files changed

+1976
-0
lines changed

9 files changed

+1976
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
17+
.DS_Store

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,30 @@
11
# go-querystring-parser
22
A golang querystring parser
3+
4+
## Usage
5+
6+
```go
7+
import (
8+
querystring "github.com/bytedance/go-querystring-parser"
9+
)
10+
```
11+
12+
Then use:
13+
14+
```go
15+
query := "message: test\\ value AND datetime: [\"2020-01-01T00:00:00\" TO \"2020-12-31T00:00:00\"]"
16+
ast, err := querystring.Parse(query)
17+
if err != nil {
18+
// error handling
19+
}
20+
21+
// do something with ast
22+
```
23+
24+
## For Developers
25+
26+
After edit querystring.y, gen code via run:
27+
28+
```shell
29+
go generate
30+
```

condition.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package querystring
2+
3+
import (
4+
"strings"
5+
"time"
6+
)
7+
8+
// Condition .
9+
type Condition interface {
10+
}
11+
12+
// AndCondition .
13+
type AndCondition struct {
14+
Left Condition
15+
Right Condition
16+
}
17+
18+
// NewAndCondition .
19+
func NewAndCondition(left, right Condition) *AndCondition {
20+
return &AndCondition{Left: left, Right: right}
21+
}
22+
23+
// OrCondition .
24+
type OrCondition struct {
25+
Left Condition
26+
Right Condition
27+
}
28+
29+
// NewOrCondition .
30+
func NewOrCondition(left, right Condition) *OrCondition {
31+
return &OrCondition{Left: left, Right: right}
32+
}
33+
34+
// NotCondition .
35+
type NotCondition struct {
36+
Condition Condition
37+
}
38+
39+
// NewNotCondition .
40+
func NewNotCondition(q Condition) *NotCondition {
41+
return &NotCondition{Condition: q}
42+
}
43+
44+
// FieldableCondition .
45+
type FieldableCondition interface {
46+
SetField(field string)
47+
}
48+
49+
// MatchCondition .
50+
type MatchCondition struct {
51+
Field string
52+
Value string
53+
}
54+
55+
// NewMatchCondition .
56+
func NewMatchCondition(s string) *MatchCondition {
57+
return &MatchCondition{
58+
Value: s,
59+
}
60+
}
61+
62+
// SetField .
63+
func (q *MatchCondition) SetField(field string) {
64+
q.Field = field
65+
}
66+
67+
// RegexpCondition .
68+
type RegexpCondition struct {
69+
Field string
70+
Value string
71+
}
72+
73+
// NewRegexpCondition .
74+
func NewRegexpCondition(s string) *RegexpCondition {
75+
return &RegexpCondition{
76+
Value: s,
77+
}
78+
}
79+
80+
// SetField .
81+
func (q *RegexpCondition) SetField(field string) {
82+
q.Field = field
83+
}
84+
85+
// WildcardCondition .
86+
type WildcardCondition struct {
87+
Field string
88+
Value string
89+
}
90+
91+
// NewWildcardCondition .
92+
func NewWildcardCondition(s string) *WildcardCondition {
93+
return &WildcardCondition{
94+
Value: s,
95+
}
96+
}
97+
98+
// SetField .
99+
func (q *WildcardCondition) SetField(field string) {
100+
q.Field = field
101+
}
102+
103+
// NumberRangeCondition .
104+
type NumberRangeCondition struct {
105+
Field string
106+
Start *string
107+
End *string
108+
IncludeStart bool
109+
IncludeEnd bool
110+
}
111+
112+
// NewNumberRangeCondition .
113+
func NewNumberRangeCondition(start, end *string, includeStart, includeEnd bool) *NumberRangeCondition {
114+
return &NumberRangeCondition{
115+
Start: start,
116+
End: end,
117+
IncludeStart: includeStart,
118+
IncludeEnd: includeEnd,
119+
}
120+
}
121+
122+
// SetField .
123+
func (q *NumberRangeCondition) SetField(field string) {
124+
q.Field = field
125+
}
126+
127+
// TimeRangeCondition .
128+
type TimeRangeCondition struct {
129+
Field string
130+
Start *string
131+
End *string
132+
IncludeStart bool
133+
IncludeEnd bool
134+
}
135+
136+
// NewTimeRangeCondition .
137+
func NewTimeRangeCondition(start, end *string, includeStart, includeEnd bool) *TimeRangeCondition {
138+
return &TimeRangeCondition{
139+
Start: start,
140+
End: end,
141+
IncludeStart: includeStart,
142+
IncludeEnd: includeEnd,
143+
}
144+
}
145+
146+
// SetField .
147+
func (q *TimeRangeCondition) SetField(field string) {
148+
q.Field = field
149+
}
150+
151+
func queryTimeFromString(t string) (time.Time, error) {
152+
return time.Parse(time.RFC3339, t)
153+
}
154+
155+
func newStringCondition(str string) FieldableCondition {
156+
if strings.HasPrefix(str, "/") && strings.HasSuffix(str, "/") {
157+
return NewRegexpCondition(str[1 : len(str)-1])
158+
}
159+
160+
if strings.ContainsAny(str, "*?") {
161+
return NewWildcardCondition(str)
162+
}
163+
164+
return NewMatchCondition(str)
165+
}

0 commit comments

Comments
 (0)