Mike Schwörer
ae43cbb623
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Failing after 55s
216 lines
5.1 KiB
Go
216 lines
5.1 KiB
Go
package wmo
|
|
|
|
import (
|
|
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
"gogs.mikescher.com/BlackForestBytes/goext/reflectext"
|
|
"reflect"
|
|
"strings"
|
|
)
|
|
|
|
func (c *Coll[TData]) EnsureInitializedReflection(v TData) {
|
|
|
|
if !c.isInterfaceDataType {
|
|
return // only dynamically load dataTypeMap on interface TData
|
|
}
|
|
|
|
rval := reflect.ValueOf(v)
|
|
for rval.Type().Kind() == reflect.Pointer {
|
|
rval = rval.Elem()
|
|
}
|
|
|
|
if _, ok := c.implDataTypeMap[rval.Type()]; ok {
|
|
return // already loaded
|
|
}
|
|
|
|
m := make(map[string]fullTypeRef)
|
|
|
|
c.initFields("", rval, m, make([]int, 0))
|
|
|
|
c.implDataTypeMap[rval.Type()] = m
|
|
}
|
|
|
|
func (c *Coll[TData]) init() {
|
|
|
|
example := *new(TData)
|
|
|
|
datatype := reflect.TypeOf(&example).Elem()
|
|
|
|
if datatype.Kind() == reflect.Interface {
|
|
|
|
c.isInterfaceDataType = true
|
|
|
|
c.dataTypeMap = make(map[string]fullTypeRef)
|
|
c.implDataTypeMap = make(map[reflect.Type]map[string]fullTypeRef)
|
|
} else {
|
|
|
|
c.isInterfaceDataType = false
|
|
|
|
c.dataTypeMap = make(map[string]fullTypeRef)
|
|
c.implDataTypeMap = make(map[reflect.Type]map[string]fullTypeRef)
|
|
|
|
v := reflect.ValueOf(example)
|
|
c.initFields("", v, c.dataTypeMap, make([]int, 0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (c *Coll[TData]) initFields(prefix string, rval reflect.Value, m map[string]fullTypeRef, idxarr []int) {
|
|
|
|
rtyp := rval.Type()
|
|
|
|
for i := 0; i < rtyp.NumField(); i++ {
|
|
|
|
rsfield := rtyp.Field(i)
|
|
rvfield := rval.Field(i)
|
|
|
|
if !rsfield.IsExported() {
|
|
continue
|
|
}
|
|
|
|
bsontags := make([]string, 0)
|
|
bsonkey, found := rsfield.Tag.Lookup("bson")
|
|
if !found {
|
|
continue
|
|
}
|
|
if strings.Contains(bsonkey, ",") {
|
|
bsontags = strings.Split(bsonkey[strings.Index(bsonkey, ",")+1:], ",")
|
|
bsonkey = bsonkey[:strings.Index(bsonkey, ",")]
|
|
}
|
|
if bsonkey == "-" {
|
|
continue
|
|
}
|
|
|
|
if bsonkey == "" {
|
|
bsonkey = rsfield.Name
|
|
}
|
|
|
|
fullKey := prefix + bsonkey
|
|
|
|
newIdxArr := langext.ArrCopy(idxarr)
|
|
newIdxArr = append(newIdxArr, i)
|
|
|
|
if langext.InArray("inline", bsontags) && rvfield.Kind() == reflect.Struct {
|
|
|
|
// pass-through field
|
|
c.initFields(prefix, rvfield, m, newIdxArr)
|
|
|
|
} else {
|
|
|
|
if rvfield.Type().Kind() == reflect.Pointer {
|
|
|
|
m[fullKey] = fullTypeRef{
|
|
IsPointer: true,
|
|
RealType: rvfield.Type(),
|
|
Kind: rvfield.Type().Elem().Kind(),
|
|
Type: rvfield.Type().Elem(),
|
|
UnderlyingType: reflectext.Underlying(rvfield.Type().Elem()),
|
|
Name: rsfield.Name,
|
|
Index: newIdxArr,
|
|
}
|
|
|
|
} else {
|
|
|
|
m[fullKey] = fullTypeRef{
|
|
IsPointer: false,
|
|
RealType: rvfield.Type(),
|
|
Kind: rvfield.Type().Kind(),
|
|
Type: rvfield.Type(),
|
|
UnderlyingType: reflectext.Underlying(rvfield.Type()),
|
|
Name: rsfield.Name,
|
|
Index: newIdxArr,
|
|
}
|
|
|
|
}
|
|
|
|
if rvfield.Kind() == reflect.Struct {
|
|
c.initFields(fullKey+".", rvfield, m, newIdxArr)
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (c *Coll[TData]) getTokenValueAsMongoType(value string, fieldName string) (any, error) {
|
|
|
|
fref, err := c.getFieldType(fieldName)
|
|
if err != nil {
|
|
return nil, exerr.Wrap(err, "failed to get-field-type").Str("fieldName", fieldName).Build()
|
|
}
|
|
|
|
pss := reflectext.PrimitiveStringSerializer{}
|
|
|
|
return pss.ValueFromString(value, fref.RealType)
|
|
|
|
}
|
|
|
|
func (c *Coll[TData]) getFieldValueAsTokenString(entity TData, fieldName string) (string, error) {
|
|
|
|
realValue, err := c.getFieldValue(entity, fieldName)
|
|
if err != nil {
|
|
return "", exerr.Wrap(err, "failed to get-field-value").Str("fieldName", fieldName).Build()
|
|
}
|
|
|
|
pss := reflectext.PrimitiveStringSerializer{}
|
|
|
|
return pss.ValueToString(realValue)
|
|
|
|
}
|
|
|
|
func (c *Coll[TData]) getFieldType(fieldName string) (fullTypeRef, error) {
|
|
if c.isInterfaceDataType {
|
|
|
|
for _, m := range c.implDataTypeMap {
|
|
if r, ok := m[fieldName]; ok {
|
|
return r, nil
|
|
}
|
|
}
|
|
|
|
return fullTypeRef{}, exerr.New(exerr.TypeMongoReflection, "unknown field: '"+fieldName+"' (in any impl)").Str("fieldName", fieldName).Build()
|
|
|
|
} else {
|
|
|
|
if r, ok := c.dataTypeMap[fieldName]; ok {
|
|
return r, nil
|
|
} else {
|
|
return fullTypeRef{}, exerr.New(exerr.TypeMongoReflection, "unknown field: '"+fieldName+"'").Str("fieldName", fieldName).Build()
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (c *Coll[TData]) getFieldValue(data TData, fieldName string) (any, error) {
|
|
if c.isInterfaceDataType {
|
|
|
|
rval := reflect.ValueOf(data)
|
|
for rval.Type().Kind() == reflect.Pointer {
|
|
rval = rval.Elem()
|
|
}
|
|
|
|
if m, ok := c.implDataTypeMap[rval.Type()]; ok {
|
|
if fref, ok := m[fieldName]; ok {
|
|
rval := reflect.ValueOf(data)
|
|
return rval.FieldByIndex(fref.Index).Interface(), nil
|
|
} else {
|
|
return nil, exerr.New(exerr.TypeMongoReflection, "unknown bson field '"+fieldName+"' in type '"+rval.Type().String()+"'").Str("fieldName", fieldName).Type("rval", rval).Build()
|
|
}
|
|
} else {
|
|
return nil, exerr.New(exerr.TypeMongoReflection, "unknown TData type: '"+rval.Type().String()+"'").Type("rval", rval).Build()
|
|
}
|
|
|
|
} else {
|
|
|
|
if fref, ok := c.dataTypeMap[fieldName]; ok {
|
|
rval := reflect.ValueOf(data)
|
|
return rval.FieldByIndex(fref.Index).Interface(), nil
|
|
} else {
|
|
return nil, exerr.New(exerr.TypeMongoReflection, "unknown bson field '"+fieldName+"'").Str("fieldName", fieldName).Build()
|
|
}
|
|
|
|
}
|
|
}
|