2024-09-01 16:17:48 -04:00
|
|
|
package orm
|
|
|
|
|
2024-09-02 19:32:39 -04:00
|
|
|
import (
|
|
|
|
"fmt"
|
2024-09-04 19:43:12 -04:00
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
2024-09-02 19:32:39 -04:00
|
|
|
"reflect"
|
2024-09-05 13:57:35 -04:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2024-09-02 19:32:39 -04:00
|
|
|
"strings"
|
|
|
|
)
|
2024-09-01 16:17:48 -04:00
|
|
|
|
|
|
|
func panik(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func nameOf(i interface{}) string {
|
|
|
|
v := reflect.ValueOf(i)
|
|
|
|
var n string
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.Slice, reflect.Map:
|
|
|
|
if v.Type().Elem().Kind() == reflect.Pointer {
|
|
|
|
n = v.Type().Elem().Elem().Name()
|
|
|
|
}
|
|
|
|
case reflect.Pointer:
|
|
|
|
n = nameOf(reflect.Indirect(v).Interface())
|
|
|
|
default:
|
|
|
|
n = v.Type().Name()
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func valueOf(i interface{}) reflect.Value {
|
|
|
|
v := reflect.ValueOf(i)
|
2024-09-03 00:14:12 -04:00
|
|
|
if v.Type().Kind() == reflect.Pointer {
|
2024-09-01 16:17:48 -04:00
|
|
|
v = valueOf(reflect.Indirect(v).Interface())
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func coerceInt(input reflect.Value, dst reflect.Value) interface{} {
|
|
|
|
if input.Type().Kind() == reflect.Pointer {
|
|
|
|
input = input.Elem()
|
|
|
|
}
|
|
|
|
if dst.Type().Kind() == reflect.Pointer {
|
|
|
|
dst = dst.Elem()
|
|
|
|
}
|
|
|
|
if input.Type().ConvertibleTo(dst.Type()) {
|
|
|
|
return input.Convert(dst.Type()).Interface()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-09-02 19:32:39 -04:00
|
|
|
|
2024-09-05 13:57:35 -04:00
|
|
|
var arrRegex, _ = regexp.Compile(`\[(?P<index>\d+)]$`)
|
|
|
|
|
2024-09-12 17:15:04 -04:00
|
|
|
func getNested(field string, aValue reflect.Value) (*reflect.Type, *reflect.Value, error) {
|
2024-09-02 19:32:39 -04:00
|
|
|
if strings.HasPrefix(field, ".") || strings.HasSuffix(field, ".") {
|
2024-09-12 17:15:04 -04:00
|
|
|
return nil, nil, fmt.Errorf(errFmtMalformedField, field)
|
2024-09-02 19:32:39 -04:00
|
|
|
}
|
2024-09-12 17:50:31 -04:00
|
|
|
value := aValue
|
|
|
|
if value.Kind() == reflect.Pointer {
|
|
|
|
value = value.Elem()
|
|
|
|
}
|
|
|
|
aft := value.Type()
|
2024-09-02 19:32:39 -04:00
|
|
|
dots := strings.Split(field, ".")
|
2024-09-14 15:34:49 -04:00
|
|
|
if value.Kind() != reflect.Struct {
|
2024-09-12 17:15:04 -04:00
|
|
|
if value.Kind() == reflect.Slice {
|
|
|
|
st := reflect.MakeSlice(value.Type().Elem(), 0, 0)
|
|
|
|
for i := 0; i < value.Len(); i++ {
|
|
|
|
cur := value.Index(i)
|
|
|
|
if len(dots) > 1 {
|
|
|
|
_, cv, _ := getNested(strings.Join(dots[1:], "."), cur.FieldByName(dots[0]))
|
|
|
|
reflect.Append(st, *cv)
|
|
|
|
//return getNested(, "."), fv)
|
|
|
|
} else {
|
|
|
|
reflect.Append(st, cur)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
typ := st.Type().Elem()
|
|
|
|
return &typ, &st, nil
|
|
|
|
}
|
|
|
|
if len(dots) > 1 {
|
|
|
|
return nil, nil, ErrNotSliceOrStruct
|
|
|
|
} else {
|
|
|
|
return &aft, &value, nil
|
|
|
|
}
|
2024-09-02 19:32:39 -04:00
|
|
|
}
|
|
|
|
ref := value
|
|
|
|
if ref.Kind() == reflect.Pointer {
|
|
|
|
ref = ref.Elem()
|
|
|
|
}
|
2024-09-12 17:15:04 -04:00
|
|
|
var fv = ref.FieldByName(arrRegex.ReplaceAllString(dots[0], ""))
|
2024-09-05 13:57:35 -04:00
|
|
|
if arrRegex.FindString(dots[0]) != "" && fv.Kind() == reflect.Slice {
|
|
|
|
matches := arrRegex.FindStringSubmatch(dots[0])
|
|
|
|
ridx, _ := strconv.Atoi(matches[0])
|
2024-09-14 15:34:49 -04:00
|
|
|
idx := ridx
|
2024-09-05 13:57:35 -04:00
|
|
|
fv = fv.Index(idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
ft, _ := ref.Type().FieldByName(arrRegex.ReplaceAllString(dots[0], ""))
|
2024-09-02 19:32:39 -04:00
|
|
|
if len(dots) > 1 {
|
|
|
|
return getNested(strings.Join(dots[1:], "."), fv)
|
|
|
|
} else {
|
2024-09-12 17:15:04 -04:00
|
|
|
return &ft.Type, &fv, nil
|
2024-09-02 19:32:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
func makeSettable(rval reflect.Value, value interface{}) reflect.Value {
|
|
|
|
if !rval.CanSet() {
|
|
|
|
nv := reflect.New(rval.Type())
|
|
|
|
nv.Elem().Set(reflect.ValueOf(value))
|
|
|
|
return nv
|
|
|
|
}
|
|
|
|
return rval
|
|
|
|
}
|
2024-09-04 19:43:12 -04:00
|
|
|
|
|
|
|
func incrementInterface(t interface{}) interface{} {
|
|
|
|
switch pt := t.(type) {
|
|
|
|
case uint:
|
|
|
|
t = pt + 1
|
|
|
|
case uint32:
|
|
|
|
t = pt + 1
|
|
|
|
case uint64:
|
|
|
|
t = pt + 1
|
|
|
|
case int:
|
|
|
|
t = pt + 1
|
|
|
|
case int32:
|
|
|
|
t = pt + 1
|
|
|
|
case int64:
|
|
|
|
t = pt + 1
|
|
|
|
case string:
|
|
|
|
t = NextStringID()
|
|
|
|
case primitive.ObjectID:
|
|
|
|
t = primitive.NewObjectID()
|
|
|
|
default:
|
2024-09-14 15:20:17 -04:00
|
|
|
panic(ErrUnsupportedID)
|
2024-09-04 19:43:12 -04:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func pull(s reflect.Value, idx int, typ reflect.Type) reflect.Value {
|
|
|
|
retI := reflect.New(reflect.SliceOf(typ))
|
|
|
|
for i := 0; i < s.Len(); i++ {
|
|
|
|
if i == idx {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
retI.Elem().Set(reflect.Append(retI.Elem(), s.Index(i)))
|
|
|
|
}
|
|
|
|
return retI.Elem()
|
|
|
|
}
|
2024-09-05 13:57:35 -04:00
|
|
|
|
|
|
|
func checkStruct(ref reflect.Value) error {
|
|
|
|
if ref.Kind() == reflect.Slice {
|
2024-09-12 17:22:42 -04:00
|
|
|
return ErrAppendMultipleDocuments
|
2024-09-05 13:57:35 -04:00
|
|
|
}
|
|
|
|
if ref.Kind() != reflect.Struct {
|
2024-09-12 17:22:42 -04:00
|
|
|
return ErrNotAStruct
|
2024-09-05 13:57:35 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkSlice(ref reflect.Value) error {
|
|
|
|
if ref.Kind() != reflect.Slice {
|
2024-09-12 17:22:42 -04:00
|
|
|
return ErrNotASlice
|
2024-09-05 13:57:35 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-09-14 02:09:38 -04:00
|
|
|
|
|
|
|
func convertSlice[In, Out any](in []In) []Out {
|
|
|
|
out := make([]Out, 0)
|
|
|
|
for _, i := range in {
|
|
|
|
ii, ok := any(i).(Out)
|
|
|
|
if ok {
|
|
|
|
out = append(out, ii)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func normalizeSliceToDocumentSlice(in any) *DocumentSlice {
|
|
|
|
ret := make(DocumentSlice, 0)
|
|
|
|
val := reflect.ValueOf(in)
|
|
|
|
if val.Kind() == reflect.Pointer {
|
|
|
|
val = val.Elem()
|
|
|
|
}
|
|
|
|
if val.Kind() == reflect.Slice {
|
|
|
|
for i := 0; i < val.Len(); i++ {
|
|
|
|
if idoc, ok := val.Index(i).Interface().(IDocument); ok {
|
|
|
|
ret = append(ret, idoc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &ret
|
|
|
|
}
|