dal/pkg/builder/convert_find.go
Anton Nesterov d644ef077e
[ref] move to builder pkg
Signed-off-by: Anton Nesterov <anton@demiurg.io>
2024-08-09 17:00:52 +02:00

38 lines
834 B
Go

package builder
import (
"fmt"
"strings"
filters "l12.xyz/dal/filters"
)
func CovertFind(ctx Context, find Find) string {
return covert_find(ctx, find, "")
}
func covert_find(ctx Context, find Find, join string) string {
if join == "" {
join = " AND "
}
expressions := []string{}
for key, value := range find {
if strings.Contains(key, "$and") {
v := covert_find(ctx, value.(Find), "")
expressions = append(expressions, fmt.Sprintf("(%s)", v))
continue
}
if strings.Contains(key, "$or") {
v := covert_find(ctx, value.(Find), " OR ")
expressions = append(expressions, fmt.Sprintf("(%s)", v))
continue
}
context := ctx.New(CtxOpts{
"FieldName": key,
})
values, _ := filters.Convert(context, value)
expressions = append(expressions, values)
}
return strings.Join(expressions, join)
}