Skip to content

Commit 0e0ee9b

Browse files
authored
Merge pull request #7 from xbsoftware/dev-alternative-type
[dev] alternative type
2 parents 7583d62 + dc8300d commit 0e0ee9b

File tree

3 files changed

+158
-62
lines changed

3 files changed

+158
-62
lines changed

condition.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

sql.go

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,32 @@ type DBDriver interface {
2121
type Filter struct {
2222
Glue string `json:"glue"`
2323
Field string `json:"field"`
24-
Condition Condition `json:"condition"`
24+
Type string `json:"type"`
25+
Predicate string `json:"predicate"`
26+
Filter string `json:"filter"`
27+
Value interface{} `json:"value"`
2528
Includes []interface{} `json:"includes"`
26-
Kids []Filter `json:"rules"`
29+
Rules []Filter `json:"rules"`
30+
}
31+
32+
func (f *Filter) getValues() []interface{} {
33+
valueMap, ok := f.Value.(map[string]interface{})
34+
if !ok {
35+
return []interface{}{f.Value}
36+
}
37+
38+
return []interface{}{valueMap["start"], valueMap["end"]}
2739
}
2840

2941
type CustomOperation func(string, string, []interface{}) (string, []interface{}, error)
42+
type CustomPredicate func(string, string) (string, error)
3043

3144
type CheckFunction = func(string) bool
3245
type SQLConfig struct {
3346
WhitelistFunc CheckFunction
3447
Whitelist map[string]bool
3548
Operations map[string]CustomOperation
49+
Predicates map[string]CustomPredicate
3650
}
3751

3852
func FromJSON(text []byte) (Filter, error) {
@@ -62,7 +76,7 @@ func GetSQL(data Filter, config *SQLConfig, dbArr ...DBDriver) (string, []interf
6276
db = MySQL{}
6377
}
6478

65-
if data.Kids == nil {
79+
if data.Rules == nil {
6680
if data.Field == "" {
6781
return "", make([]interface{}, 0), nil
6882
}
@@ -77,8 +91,21 @@ func GetSQL(data Filter, config *SQLConfig, dbArr ...DBDriver) (string, []interf
7791
return inSQL(name, data.Includes, db)
7892
}
7993

80-
values := data.Condition.getValues()
81-
switch data.Condition.Rule {
94+
values := data.getValues()
95+
96+
var err error
97+
if config != nil && config.Predicates != nil {
98+
if pr, prOk := config.Predicates[data.Predicate]; prOk {
99+
name, err = pr(name, data.Predicate)
100+
if err != nil {
101+
return "", NoValues, err
102+
}
103+
} else {
104+
return "", NoValues, fmt.Errorf("unknown predicate: %s", data.Predicate)
105+
}
106+
}
107+
108+
switch data.Filter {
82109
case "":
83110
return "", NoValues, nil
84111
case "equal":
@@ -132,19 +159,18 @@ func GetSQL(data Filter, config *SQLConfig, dbArr ...DBDriver) (string, []interf
132159
}
133160

134161
if config != nil && config.Operations != nil {
135-
op, opOk := config.Operations[data.Condition.Rule]
136-
if opOk {
137-
return op(name, data.Condition.Rule, data.Condition.getValues())
162+
if op, opOk := config.Operations[data.Filter]; opOk {
163+
return op(name, data.Filter, values)
138164
}
139165
}
140166

141-
return "", NoValues, fmt.Errorf("unknown operation: %s", data.Condition.Rule)
167+
return "", NoValues, fmt.Errorf("unknown operation: %s", data.Filter)
142168
}
143169

144-
out := make([]string, 0, len(data.Kids))
170+
out := make([]string, 0, len(data.Rules))
145171
values := make([]interface{}, 0)
146172

147-
for _, r := range data.Kids {
173+
for _, r := range data.Rules {
148174
subSql, subValues, err := GetSQL(r, config, db)
149175
if err != nil {
150176
return "", nil, err
@@ -161,7 +187,7 @@ func GetSQL(data Filter, config *SQLConfig, dbArr ...DBDriver) (string, []interf
161187
}
162188

163189
outStr := strings.Join(out, glue)
164-
if len(data.Kids) > 1 {
190+
if len(data.Rules) > 1 {
165191
outStr = "( " + outStr + " )"
166192
}
167193

0 commit comments

Comments
 (0)