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