func ReadVariable(f *dbox.Filter, in toolkit.M) *dbox.Filter { f.Field = strings.ToLower(f.Field) if (f.Op == "$and" || f.Op == "$or") && strings.Contains(reflect.TypeOf(f.Value).String(), "dbox.Filter") { fs := f.Value.([]*dbox.Filter) for i, ff := range fs { bf := ReadVariable(ff, in) fs[i] = bf } f.Value = fs } else { if reflect.TypeOf(f.Value).Kind() == reflect.Slice { fSlice := f.Value.([]interface{}) // nilai fSlice : [@name1 @name2] for i := 0; i < len(fSlice); i++ { // nilai fSlice [i] : @name1 if len(cast.ToString(f.Value)) > 0 && string(cast.ToString(fSlice[i])[0]) == "@" { fSlice[i] = in.Get(cast.ToString(fSlice[i]), "") } } f.Value = fSlice } else if len(cast.ToString(f.Value)) > 0 && string(cast.ToString(f.Value)[0]) == "@" { f.Value = in.Get(cast.ToString(f.Value), "") } } return f }
func ReadVariable(f *dbox.Filter, in toolkit.M) *dbox.Filter { f.Field = strings.ToLower(f.Field) if (f.Op == "$and" || f.Op == "$or") && strings.Contains(reflect.TypeOf(f.Value).String(), "dbox.Filter") { fs := f.Value.([]*dbox.Filter) /* nilai fs : [0xc082059590 0xc0820595c0]*/ for i, ff := range fs { /* nilai ff[0] : &{umur $gt @age} && ff[1] : &{name $eq @nama}*/ bf := ReadVariable(ff, in) /* nilai bf[0] : &{umur $gt 25} && bf[1] : &{name $eq Kane}*/ fs[i] = bf } f.Value = fs return f } else { if reflect.TypeOf(f.Value).Kind() == reflect.Slice { if strings.Contains(reflect.TypeOf(f.Value).String(), "interface") { fSlice := f.Value.([]interface{}) /*nilai fSlice : [@name1 @name2]*/ for i := 0; i < len(fSlice); i++ { /* nilai fSlice [i] : @name1*/ if string(cast.ToString(fSlice[i])[0]) == "@" { for key, val := range in { if cast.ToString(fSlice[i]) == key { fSlice[i] = val } } } } f.Value = fSlice } else if strings.Contains(reflect.TypeOf(f.Value).String(), "string") { fSlice := f.Value.([]string) for i := 0; i < len(fSlice); i++ { if string(fSlice[i][0]) == "@" { for key, val := range in { if fSlice[i] == key { fSlice[i] = val.(string) } } } } f.Value = fSlice } return f } else { if string(cast.ToString(f.Value)[0]) == "@" { for key, val := range in { if cast.ToString(f.Value) == key { f.Value = val } } } return f } } return f }
func (fb *FilterBuilder) CheckFilter(f *dbox.Filter, p M) *dbox.Filter { if f.Op == "$or" || f.Op == "$and" { fs := f.Value.([]*dbox.Filter) for i, ff := range fs { bf := fb.CheckFilter(ff, p) fs[i] = bf } return f } else if f.Op == "$contains" { // Println(f.Value) for i, v := range f.Value.([]string) { if p != nil { f.Value.([]string)[i] = p.Get(v).(string) } else { f.Value.([]string)[i] = v } } return f } else { if !IsSlice(f.Value) { if strings.ToLower(Kind(f.Value).String()) == "string" { foundSubstring := strings.Index(f.Value.(string), "@") if foundSubstring != 0 { return f } if strings.Contains(f.Value.(string), "@") { f.Value = p.Get(f.Value.(string)) return f } } } else { for i, v := range f.Value.([]interface{}) { foundSubstring := strings.Index(v.(string), "@") if foundSubstring != 0 { return f } switch Kind(v) { case reflect.String: stringValue := p.Get(v.(string)) f.Value.([]interface{})[i] = stringValue case reflect.Int: stringValue := ToInt(p.Get(v.(string)), ".") f.Value.([]interface{})[i] = stringValue case reflect.Bool: f.Value.([]interface{})[i] = p.Get(v.(string)).(bool) } } return f } } return f }
func ReadVariable(f *dbox.Filter, in toolkit.M) *dbox.Filter { if (f.Op == "$and" || f.Op == "$or") && strings.Contains(reflect.TypeOf(f.Value).String(), "dbox.Filter") { fs := f.Value.([]*dbox.Filter) // nilai fs : [0xc082059590 0xc0820595c0] for i, ff := range fs { // nilai ff[0] : &{umur $gt @age} && ff[1] : &{name $eq @nama} bf := ReadVariable(ff, in) // nilai bf[0] : &{umur $gt 25} && bf[1] : &{name $eq Kane} fs[i] = bf } f.Value = fs return f } else { if reflect.TypeOf(f.Value).Kind() == reflect.Slice { fSlice := f.Value.([]interface{}) // nilai fSlice : [@name1 @name2] for i := 0; i < len(fSlice); i++ { // nilai fSlice [i] : @name1 if string(cast.ToString(fSlice[i])[0]) == "@" { for key, val := range in { if strings.Replace(cast.ToString(fSlice[i]), "@", "", 1) == key { fSlice[i] = val } } } } f.Value = fSlice return f } else { if string(cast.ToString(f.Value)[0]) == "@" { for key, val := range in { if strings.Replace(cast.ToString(f.Value), "@", "", 1) == key { f.Value = val } } } return f } } return f }
func (fb *FilterBuilder) BuildFilter(f *dbox.Filter) (interface{}, error) { fm := M{} f.Field = strings.ToLower(f.Field) if f.Op == dbox.FilterOpEqual { fm.Set(f.Field, f.Value) } else if f.Op == dbox.FilterOpNoEqual { fm.Set(f.Field, M{}.Set("$ne", f.Value)) } else if f.Op == dbox.FilterOpContains { fm.Set(f.Field, M{}. Set("$regex", fmt.Sprintf(".*%s.*", f.Value)). Set("$options", "i")) } else if f.Op == dbox.FilterOpStartWith { fm.Set(f.Field, M{}. Set("$regex", fmt.Sprintf("^%s.*$", f.Value)). Set("$options", "i")) } else if f.Op == dbox.FilterOpEndWith { fm.Set(f.Field, M{}. Set("$regex", fmt.Sprintf("^.*%s$", f.Value)). Set("$options", "i")) } else if f.Op == dbox.FilterOpIn { fm.Set(f.Field, M{}.Set("$in", f.Value)) } else if f.Op == dbox.FilterOpNin { fm.Set(f.Field, M{}.Set("$nin", f.Value)) } else if f.Op == dbox.FilterOpGt { fm.Set(f.Field, M{}.Set("$gt", f.Value)) } else if f.Op == dbox.FilterOpGte { fm.Set(f.Field, M{}.Set("$gte", f.Value)) } else if f.Op == dbox.FilterOpLt { fm.Set(f.Field, M{}.Set("$lt", f.Value)) } else if f.Op == dbox.FilterOpLte { fm.Set(f.Field, M{}.Set("$lte", f.Value)) } else if f.Op == dbox.FilterOpOr || f.Op == dbox.FilterOpAnd { bfs := []interface{}{} fs := f.Value.([]*dbox.Filter) for _, ff := range fs { bf, eb := fb.BuildFilter(ff) if eb == nil { bfs = append(bfs, bf) } } fm.Set(f.Op, bfs) } else { return nil, fmt.Errorf("Filter Op %s is not defined", f.Op) } return fm, nil }