|
| 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