2024-08-07 19:16:40 +00:00
|
|
|
package filters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-08-12 18:21:34 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2024-08-07 19:16:40 +00:00
|
|
|
)
|
|
|
|
|
2024-08-08 17:26:29 +00:00
|
|
|
func FromJson[T IFilter](data interface{}) *T {
|
2024-08-07 19:16:40 +00:00
|
|
|
var t T
|
|
|
|
str, ok := data.(string)
|
|
|
|
if ok {
|
|
|
|
err := json.Unmarshal([]byte(str), &t)
|
|
|
|
if err != nil {
|
2024-08-08 17:26:29 +00:00
|
|
|
return &t
|
2024-08-07 19:16:40 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-08 17:26:29 +00:00
|
|
|
m, ok := data.(Filter)
|
2024-08-13 00:29:27 +00:00
|
|
|
if !ok {
|
|
|
|
m, ok = data.(map[string]interface{})
|
|
|
|
}
|
2024-08-07 19:16:40 +00:00
|
|
|
if ok {
|
|
|
|
s, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
2024-08-08 17:26:29 +00:00
|
|
|
return &t
|
2024-08-07 19:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
e := json.Unmarshal(s, &t)
|
|
|
|
if e != nil {
|
2024-08-08 17:26:29 +00:00
|
|
|
return &t
|
2024-08-07 19:16:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return &t
|
|
|
|
}
|
2024-08-12 18:21:34 +00:00
|
|
|
|
|
|
|
func ValueOrPlaceholder(value interface{}) interface{} {
|
|
|
|
if value == nil {
|
|
|
|
return "NULL"
|
|
|
|
}
|
|
|
|
val, ok := value.(string)
|
|
|
|
if !ok {
|
|
|
|
return "?"
|
|
|
|
}
|
|
|
|
if strings.Contains(val, ".") {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
return "?"
|
|
|
|
}
|
|
|
|
|
|
|
|
func FmtCompare(operator string, a interface{}, b interface{}) (string, Values) {
|
|
|
|
if ValueOrPlaceholder(b) == "?" {
|
|
|
|
return fmt.Sprintf("%s %s ?", a, operator), Values{b}
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s %s %s", a, operator, ValueOrPlaceholder(b)), nil
|
|
|
|
}
|