-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
198 lines (174 loc) · 5.26 KB
/
filter.go
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package admin
import (
"fmt"
"reflect"
"strings"
"github.com/ecletus/core"
"github.com/ecletus/core/resource"
"github.com/go-aorm/aorm"
)
type FieldFilter struct {
FieldName string
Struct, OriginalStruct *aorm.StructField
Virtual *aorm.VirtualField
InlineQueryName string
TermFormatterFunc func(term interface{}) interface{}
QueryFieldFormatterFunc func(f *FieldFilter, queryField string) string
Applyer func(arg interface{}) (query string, argx interface{})
Handler func(db *aorm.DB, filterArgument *FilterArgument) *aorm.DB
}
func (f *FieldFilter) With(fn func(f *FieldFilter)) *FieldFilter {
fn(f)
return f
}
func (f *FieldFilter) QueryField() string {
if f.QueryFieldFormatterFunc != nil {
return f.QueryFieldFormatterFunc(f, f.InlineQueryName)
}
return f.InlineQueryName
}
func (f *FieldFilter) FormatTerm(term interface{}) interface{} {
if f.TermFormatterFunc != nil {
return f.TermFormatterFunc(term)
}
if s, ok := term.(string); ok {
if reflect.PtrTo(f.Struct.Struct.Type).Implements(reflect.TypeOf((*aorm.StringParser)(nil)).Elem()) {
v := reflect.New(f.Struct.Struct.Type)
_ = v.Interface().(aorm.StringParser).ParseString(s)
term = v.Elem().Interface()
}
}
return term
}
func NewFieldFilter(res *Resource, fieldName string) (f *FieldFilter) {
f = &FieldFilter{FieldName: fieldName}
if fpq := res.ModelStruct.FieldPathQueryOf(fieldName); fpq != nil {
f.Struct = fpq.Struct
f.Virtual = fpq.Virtual
f.InlineQueryName = fpq.Query()
f.OriginalStruct = res.ModelStruct.FieldsByName[fieldName]
if f.OriginalStruct != nil && f.OriginalStruct.Relationship != nil {
switch f.OriginalStruct.Relationship.Kind {
case aorm.BELONGS_TO:
f.TermFormatterFunc = func(term interface{}) interface{} {
var (
err error
s = term.(string)
)
if s == "" {
id := f.OriginalStruct.Model.GetID(reflect.New(f.OriginalStruct.Model.Type).Interface())
return id
}
if term, err = f.OriginalStruct.Model.ParseIDString(s); err != nil {
panic(fmt.Errorf("filter of %s#%q: convert term %q to ID: %s", res.UID, s, fieldName, err.Error()))
}
return term
}
f.Applyer = func(arg interface{}) (query string, argx interface{}) {
return "", aorm.FilterOfID(arg.(aorm.ID), f.InlineQueryName).Fpq()
}
}
} else if f.OriginalStruct == nil && f.Struct.IsPrimaryKey {
f.TermFormatterFunc = func(term interface{}) interface{} {
term, _ = f.Struct.BaseModel.ParseIDString(term.(string))
return term
}
f.Applyer = func(arg interface{}) (query string, argx interface{}) {
return "", aorm.FilterOfID(arg.(aorm.ID), f.InlineQueryName).Fpq()
}
}
return f
}
return nil
}
// Filter filter definiation
type Filter struct {
Scheme *Scheme
ID uintptr
Name string
Label string
LabelDisabled bool
Type string
DefaultOperation string
Operations []string // eq, cont, gt, gteq, lt, lteq
NotChooseOperation bool
Resource *Resource
Handler func(db *aorm.DB, arg *FilterArgument) *aorm.DB
HandleEmpty bool
Config FilterConfigInterface
Available func(context *Context) bool
Visible func(context *Context) bool
Hidden bool
AllowBlank bool
advanced bool
DefaultLabel string
FieldLabel bool
LabelPairFunc func(ctx *core.Context) (key, defaul string)
FieldName string
Field *FieldFilter
Valuer func(arg *FilterArgument) (value interface{}, err error)
index uint32
}
func (this *Filter) Index() uint32 {
return this.index
}
func (this *Filter) With(fn func(f *Filter)) *Filter {
fn(this)
return this
}
func (this *Filter) IsAdvanced() bool {
return this.advanced
}
func (this *Filter) Advanced() *Filter {
this.advanced = true
return this
}
func (this *Filter) IsVisible(context *Context) bool {
if this.Hidden {
return false
}
if this.Visible != nil {
return this.Visible(context)
}
return true
}
func (this *Filter) GetLabelC(ctx *core.Context) string {
if key, defaul := this.GetLabelPair(ctx); key != "" {
return ctx.Ts(key, defaul)
} else {
return defaul
}
}
func (this *Filter) GetLabelPair(ctx *core.Context) (string, string) {
if this.LabelPairFunc != nil {
return this.LabelPairFunc(ctx)
}
return fmt.Sprintf("%v.filter.%v", this.Scheme.Resource.I18nPrefix, this.Name), this.Label
}
// FilterConfigInterface filter config interface
type FilterConfigInterface interface {
ConfigureQORAdminFilter(*Filter)
}
// FilterArgument filter argument that used in handler
type FilterArgument struct {
Default bool
Scheme *Scheme
Filter *Filter
Value *resource.MetaValues
Resource *Resource
Context *core.Context
GoValue interface{}
}
func (this *FilterArgument) QueryValues() (res []struct{ Name, Value string }) {
for _, v := range this.Value.Values {
v.EachQueryVal([]string{""}, func(prefix []string, name string, value interface{}) {
res = append(res, struct{ Name, Value string }{Name: fmt.Sprintf("filter[%s]%s", this.Filter.Name, strings.Join(append(prefix, name), ".")), Value: fmt.Sprint(value)})
})
}
return
}
// SavedFilter saved filter settings
type SavedFilter struct {
Name string
URL string
}