|
| 1 | +package filter |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + "unicode" |
| 9 | + |
| 10 | + "github.com/pquerna/ffjson/ffjson" |
| 11 | + "gopkg.in/mgo.v2/bson" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + id = "id" |
| 16 | + _id = "_id" |
| 17 | + desc = "desc" |
| 18 | + //ID ... |
| 19 | + ID = "ID" |
| 20 | +) |
| 21 | + |
| 22 | +//Filter describe fiter query |
| 23 | +type Filter struct { |
| 24 | + skip int |
| 25 | + limit int |
| 26 | + query bson.M |
| 27 | + sort []string |
| 28 | + search string |
| 29 | +} |
| 30 | + |
| 31 | +//BeforeFilter ... |
| 32 | +type BeforeFilter struct { |
| 33 | + Skip interface{} |
| 34 | + Limit interface{} |
| 35 | + Where map[string]interface{} |
| 36 | + Sort map[string]string |
| 37 | + Search string |
| 38 | +} |
| 39 | + |
| 40 | +func (bf *BeforeFilter) getSort() []string { |
| 41 | + var sort []string |
| 42 | + sort = make([]string, 0) |
| 43 | + for key, value := range bf.Sort { |
| 44 | + if desc == strings.ToLower(value) { |
| 45 | + key = "-" + key |
| 46 | + } |
| 47 | + sort = append(sort, key) |
| 48 | + } |
| 49 | + return sort |
| 50 | +} |
| 51 | + |
| 52 | +func (bf *BeforeFilter) getLimit() int { |
| 53 | + return convertParam(bf.Limit) |
| 54 | +} |
| 55 | + |
| 56 | +func (bf *BeforeFilter) getSkip() int { |
| 57 | + return convertParam(bf.Skip) |
| 58 | +} |
| 59 | + |
| 60 | +//convertParam to convert skip/limit to int |
| 61 | +//by default return 0 |
| 62 | +func convertParam(param interface{}) int { |
| 63 | + var result int |
| 64 | + switch param.(type) { |
| 65 | + default: |
| 66 | + return int(0) |
| 67 | + case float64: |
| 68 | + result = int(param.(float64)) |
| 69 | + case string: |
| 70 | + t, err := strconv.Atoi(param.(string)) |
| 71 | + if err != nil { |
| 72 | + result = int(0) |
| 73 | + } |
| 74 | + result = t |
| 75 | + } |
| 76 | + if result < 0 { |
| 77 | + return int(0) |
| 78 | + } |
| 79 | + return result |
| 80 | +} |
| 81 | + |
| 82 | +//GetSkip ... |
| 83 | +func (f *Filter) GetSkip() int { |
| 84 | + return f.skip |
| 85 | +} |
| 86 | + |
| 87 | +//GetLimit ... |
| 88 | +func (f *Filter) GetLimit() int { |
| 89 | + return f.limit |
| 90 | +} |
| 91 | + |
| 92 | +//GetSearch ... |
| 93 | +func (f *Filter) GetSearch() string { |
| 94 | + return f.search |
| 95 | +} |
| 96 | + |
| 97 | +//GetSort ... |
| 98 | +func (f *Filter) GetSort() []string { |
| 99 | + return f.sort |
| 100 | +} |
| 101 | + |
| 102 | +//GetQuery ... |
| 103 | +func (f *Filter) GetQuery() bson.M { |
| 104 | + return f.query |
| 105 | +} |
| 106 | + |
| 107 | +//AddQuery ... |
| 108 | +func (f *Filter) AddQuery(q bson.M) { |
| 109 | + if f.query == nil { |
| 110 | + f.query = q |
| 111 | + } else { |
| 112 | + f.query["$and"] = append(f.query["$and"].([]bson.M), q) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +//GetFilterData ... |
| 117 | +func GetFilterData(filter string, t interface{}) Filter { |
| 118 | + var f Filter |
| 119 | + if filter != "" { |
| 120 | + var tmp BeforeFilter |
| 121 | + ffjson.Unmarshal([]byte(filter), &tmp) |
| 122 | + f.limit = tmp.getLimit() |
| 123 | + f.skip = tmp.getSkip() |
| 124 | + f.sort = tmp.getSort() |
| 125 | + f.query = tmp.parseWhere(t) |
| 126 | + } |
| 127 | + return f |
| 128 | +} |
| 129 | + |
| 130 | +func (bf *BeforeFilter) parseWhere(t interface{}) bson.M { |
| 131 | + var q bson.M |
| 132 | + for key, values := range bf.Where { |
| 133 | + if key == "and" || key == "or" { |
| 134 | + key = "$" + key |
| 135 | + q = parseAnd(key, values.([]interface{}), q, t) |
| 136 | + } else { |
| 137 | + q = parse(key, values, q, t) |
| 138 | + } |
| 139 | + } |
| 140 | + return q |
| 141 | +} |
| 142 | + |
| 143 | +func parse(key string, values interface{}, q bson.M, t interface{}) bson.M { |
| 144 | + if key == id { |
| 145 | + key = _id |
| 146 | + } |
| 147 | + val := reflect.ValueOf(t) |
| 148 | + var fieldType string |
| 149 | + if val.FieldByName(convertName(key)).IsValid() == true { |
| 150 | + fieldType = val.FieldByName(convertName(key)).Type().String() |
| 151 | + if fieldType == "*time.Time" || fieldType == "time.Time" { |
| 152 | + if q == nil { |
| 153 | + q = bson.M{"$and": []bson.M{}} |
| 154 | + } else { |
| 155 | + if q["$and"] == nil { |
| 156 | + q["$and"] = []bson.M{} |
| 157 | + } |
| 158 | + } |
| 159 | + q = parseFilterDate("$and", key, values, q) |
| 160 | + } |
| 161 | + if fieldType == "bson.ObjectId" { |
| 162 | + q = parseID(key, values, q) |
| 163 | + } |
| 164 | + } |
| 165 | + return q |
| 166 | +} |
| 167 | + |
| 168 | +func addKeyToQuery(key string, q bson.M) bson.M { |
| 169 | + if q == nil { |
| 170 | + q = bson.M{key: []bson.M{}} |
| 171 | + } else { |
| 172 | + q[key] = []bson.M{} |
| 173 | + } |
| 174 | + return q |
| 175 | +} |
| 176 | + |
| 177 | +func parseID(key string, values interface{}, q bson.M) bson.M { |
| 178 | + q = addKeyToQuery(key, q) |
| 179 | + for field, data := range values.(map[string]interface{}) { |
| 180 | + ids := convertStringIDtoObjectID(data) |
| 181 | + if len(ids) > 0 { |
| 182 | + if field == "in" || field == "nin" || field == "all" { |
| 183 | + q[key] = bson.M{"$" + field: ids} |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + return q |
| 188 | +} |
| 189 | + |
| 190 | +func parseAndIDData(key string, field string, data interface{}, q bson.M) bson.M { |
| 191 | + if field == id { |
| 192 | + field = _id |
| 193 | + } |
| 194 | + slice := data.(map[string]interface{}) |
| 195 | + for k, value := range slice { |
| 196 | + IDS := convertStringIDtoObjectID(value) |
| 197 | + if len(IDS) > 0 { |
| 198 | + if k == "in" || k == "nin" || k == "all" { |
| 199 | + q[key] = append(q[key].([]bson.M), bson.M{field: bson.M{"$" + k: IDS}}) |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + return q |
| 204 | +} |
| 205 | + |
| 206 | +func convertStringIDtoObjectID(data interface{}) []bson.ObjectId { |
| 207 | + var ids []bson.ObjectId |
| 208 | + ids = make([]bson.ObjectId, 0) |
| 209 | + switch data.(type) { |
| 210 | + case []interface{}: |
| 211 | + ids = prepareIDSArray(data.([]interface{})) |
| 212 | + case string: |
| 213 | + ids = prepareInIds(data.(string)) |
| 214 | + } |
| 215 | + return ids |
| 216 | +} |
| 217 | + |
| 218 | +func prepareIDSArray(values []interface{}) []bson.ObjectId { |
| 219 | + var ids []bson.ObjectId |
| 220 | + ids = make([]bson.ObjectId, 0) |
| 221 | + for _, id := range values { |
| 222 | + ids = append(ids, bson.ObjectIdHex(id.(string))) |
| 223 | + } |
| 224 | + return ids |
| 225 | +} |
| 226 | + |
| 227 | +func parseAnd(key string, values []interface{}, q bson.M, t interface{}) bson.M { |
| 228 | + val := reflect.ValueOf(t) |
| 229 | + if q == nil { |
| 230 | + q = bson.M{key: []bson.M{}} |
| 231 | + } else { |
| 232 | + q[key] = []bson.M{} |
| 233 | + } |
| 234 | + for _, value := range values { |
| 235 | + for field, data := range value.(map[string]interface{}) { |
| 236 | + fieldName := convertName(field) |
| 237 | + var fieldType string |
| 238 | + if val.FieldByName(fieldName).IsValid() == true { |
| 239 | + fieldType = val.FieldByName(fieldName).Type().String() |
| 240 | + if fieldType == "*time.Time" || fieldType == "time.Time" { |
| 241 | + q = parseFilterDate(key, field, data, q) |
| 242 | + } |
| 243 | + if fieldType == "bson.ObjectId" { |
| 244 | + q = parseAndIDData(key, field, data, q) |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | + return q |
| 250 | +} |
| 251 | + |
| 252 | +func convertName(name string) string { |
| 253 | + if name == id || name == _id { |
| 254 | + return ID |
| 255 | + } |
| 256 | + newName := []rune(name) |
| 257 | + newName[0] = unicode.ToUpper(newName[0]) |
| 258 | + return string(newName) |
| 259 | +} |
| 260 | + |
| 261 | +func parseFilterDate(key string, field string, data interface{}, q bson.M) bson.M { |
| 262 | + for k, v := range data.(map[string]interface{}) { |
| 263 | + if k == "lte" || k == "lt" || k == "gte" || k == "gt" { |
| 264 | + t, err := time.Parse(time.RFC3339, v.(string)) |
| 265 | + if err == nil { |
| 266 | + q[key] = append(q[key].([]bson.M), bson.M{field: bson.M{"$" + k: t}}) |
| 267 | + } |
| 268 | + } |
| 269 | + } |
| 270 | + return q |
| 271 | +} |
| 272 | + |
| 273 | +func prepareInIds(sID string) []bson.ObjectId { |
| 274 | + var ids []bson.ObjectId |
| 275 | + ids = make([]bson.ObjectId, 0) |
| 276 | + IDs := strings.Split(sID, ",") |
| 277 | + for _, id := range IDs { |
| 278 | + ids = append(ids, bson.ObjectIdHex(id)) |
| 279 | + } |
| 280 | + return ids |
| 281 | +} |
0 commit comments