v0.0.147
This commit is contained in:
@@ -1,12 +1,9 @@
|
||||
package wmo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/reflectext"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (c *Coll[TData]) init() {
|
||||
@@ -48,9 +45,10 @@ func (c *Coll[TData]) initFields(prefix string, rval reflect.Value, idxarr []int
|
||||
|
||||
c.dataTypeMap[fullKey] = fullTypeRef[TData]{
|
||||
IsPointer: true,
|
||||
RealType: rvfield.Type(),
|
||||
Kind: rvfield.Type().Elem().Kind(),
|
||||
Type: rvfield.Type().Elem(),
|
||||
UnderlyingType: langext.Underlying(rvfield.Type().Elem()),
|
||||
UnderlyingType: reflectext.Underlying(rvfield.Type().Elem()),
|
||||
Name: rsfield.Name,
|
||||
Index: newIdxArr,
|
||||
}
|
||||
@@ -59,9 +57,10 @@ func (c *Coll[TData]) initFields(prefix string, rval reflect.Value, idxarr []int
|
||||
|
||||
c.dataTypeMap[fullKey] = fullTypeRef[TData]{
|
||||
IsPointer: false,
|
||||
RealType: rvfield.Type(),
|
||||
Kind: rvfield.Type().Kind(),
|
||||
Type: rvfield.Type(),
|
||||
UnderlyingType: langext.Underlying(rvfield.Type()),
|
||||
UnderlyingType: reflectext.Underlying(rvfield.Type()),
|
||||
Name: rsfield.Name,
|
||||
Index: newIdxArr,
|
||||
}
|
||||
@@ -77,210 +76,23 @@ func (c *Coll[TData]) initFields(prefix string, rval reflect.Value, idxarr []int
|
||||
}
|
||||
|
||||
func (c *Coll[TData]) getTokenValueAsMongoType(value string, fieldName string) (any, error) {
|
||||
|
||||
fref := c.dataTypeMap[fieldName]
|
||||
|
||||
if fref.IsPointer && value == "" {
|
||||
pointertype := reflect.New(fref.Type).Type()
|
||||
nilvalue := reflect.Zero(pointertype)
|
||||
outvalue := nilvalue.Interface()
|
||||
return outvalue, nil
|
||||
}
|
||||
pss := reflectext.PrimitiveStringSerializer{}
|
||||
|
||||
pointerize := func(v any) any {
|
||||
if !fref.IsPointer {
|
||||
return v
|
||||
}
|
||||
return pss.ValueFromString(value, fref.RealType)
|
||||
|
||||
rval1 := reflect.ValueOf(v)
|
||||
rval2 := rval1.Convert(fref.Type)
|
||||
rval3 := reflect.New(fref.Type)
|
||||
rval3.Elem().Set(rval2)
|
||||
return rval3.Interface()
|
||||
}
|
||||
|
||||
if fref.UnderlyingType == reflect.TypeOf("") {
|
||||
|
||||
rt, ok := langext.TryCastType(value, fref.Type)
|
||||
if !ok {
|
||||
return nil, errors.New(fmt.Sprintf("failed to parse field '%s' from string to %s", fieldName, fref.Type.String()))
|
||||
}
|
||||
|
||||
return pointerize(rt), nil
|
||||
}
|
||||
|
||||
if fref.UnderlyingType == reflect.TypeOf(time.Time{}) {
|
||||
|
||||
t, err := time.Parse(time.RFC3339Nano, value)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("failed to parse field '%s' as time.Time ('%s')", fieldName, value))
|
||||
}
|
||||
|
||||
rt, ok := langext.TryCastType(t, fref.Type)
|
||||
if !ok {
|
||||
return nil, errors.New(fmt.Sprintf("failed to parse field '%s' from time.Time to %s", fieldName, fref.Type.String()))
|
||||
}
|
||||
|
||||
return pointerize(rt), nil
|
||||
}
|
||||
|
||||
if fref.UnderlyingType == reflect.TypeOf(int(0)) {
|
||||
|
||||
t, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("failed to parse field '%s' as int64 ('%s')", fieldName, value))
|
||||
}
|
||||
|
||||
rt, ok := langext.TryCastType(int(t), fref.Type)
|
||||
if !ok {
|
||||
return nil, errors.New(fmt.Sprintf("failed to parse field '%s' from int to %s", fieldName, fref.Type.String()))
|
||||
}
|
||||
|
||||
return pointerize(rt), nil
|
||||
}
|
||||
|
||||
if fref.UnderlyingType == reflect.TypeOf(int32(0)) {
|
||||
|
||||
t, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("failed to parse field '%s' as int32 ('%s')", fieldName, value))
|
||||
}
|
||||
|
||||
rt, ok := langext.TryCastType(int32(t), fref.Type)
|
||||
if !ok {
|
||||
return nil, errors.New(fmt.Sprintf("failed to parse field '%s' from int32 to %s", fieldName, fref.Type.String()))
|
||||
}
|
||||
|
||||
return pointerize(rt), nil
|
||||
}
|
||||
|
||||
if fref.UnderlyingType == reflect.TypeOf(int64(0)) {
|
||||
|
||||
t, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("failed to parse field '%s' as int64 ('%s')", fieldName, value))
|
||||
}
|
||||
|
||||
rt, ok := langext.TryCastType(int64(t), fref.Type)
|
||||
if !ok {
|
||||
return nil, errors.New(fmt.Sprintf("failed to parse field '%s' from int64 to %s", fieldName, fref.Type.String()))
|
||||
}
|
||||
|
||||
return pointerize(rt), nil
|
||||
}
|
||||
|
||||
if fref.UnderlyingType == reflect.TypeOf(float32(0)) {
|
||||
|
||||
t, err := strconv.ParseFloat(value, 64)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("failed to parse field '%s' as float32 ('%s')", fieldName, value))
|
||||
}
|
||||
|
||||
rt, ok := langext.TryCastType(float32(t), fref.Type)
|
||||
if !ok {
|
||||
return nil, errors.New(fmt.Sprintf("failed to parse field '%s' from float32 to %s", fieldName, fref.Type.String()))
|
||||
}
|
||||
|
||||
return pointerize(rt), nil
|
||||
}
|
||||
|
||||
if fref.UnderlyingType == reflect.TypeOf(float64(0)) {
|
||||
|
||||
t, err := strconv.ParseFloat(value, 64)
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprintf("failed to parse field '%s' as float64 ('%s')", fieldName, value))
|
||||
}
|
||||
|
||||
rt, ok := langext.TryCastType(float64(t), fref.Type)
|
||||
if !ok {
|
||||
return nil, errors.New(fmt.Sprintf("failed to parse field '%s' from float64 to %s", fieldName, fref.Type.String()))
|
||||
}
|
||||
|
||||
return pointerize(rt), nil
|
||||
}
|
||||
|
||||
return nil, errors.New(fmt.Sprintf("failed to parse field '%s' of type %s (%s)", fieldName, fref.Type.String(), fref.UnderlyingType.String()))
|
||||
}
|
||||
|
||||
func (c *Coll[TData]) getFieldValueAsTokenString(entity TData, fieldName string) (string, error) {
|
||||
fref := c.dataTypeMap[fieldName]
|
||||
|
||||
realValue := c.getFieldValue(entity, fieldName)
|
||||
|
||||
if langext.IsNil(realValue) {
|
||||
return "", nil
|
||||
}
|
||||
pss := reflectext.PrimitiveStringSerializer{}
|
||||
|
||||
reflValue := reflect.ValueOf(realValue)
|
||||
if reflValue.Kind() == reflect.Pointer {
|
||||
reflValue = reflValue.Elem()
|
||||
realValue = reflValue.Interface()
|
||||
}
|
||||
return pss.ValueToString(realValue)
|
||||
|
||||
if fref.UnderlyingType == reflect.TypeOf("") {
|
||||
|
||||
rt, ok := langext.TryCastType(realValue, reflect.TypeOf(""))
|
||||
if !ok {
|
||||
return "", errors.New(fmt.Sprintf("failed to cast field '%s' from %s to string", fieldName, fref.Type.String()))
|
||||
}
|
||||
|
||||
return rt.(string), nil
|
||||
}
|
||||
|
||||
if fref.UnderlyingType == reflect.TypeOf(time.Time{}) {
|
||||
rt, ok := langext.TryCastType(realValue, reflect.TypeOf(time.Time{}))
|
||||
if !ok {
|
||||
return "", errors.New(fmt.Sprintf("failed to cast field '%s' from %s to time.Time", fieldName, fref.Type.String()))
|
||||
}
|
||||
|
||||
return rt.(time.Time).Format(time.RFC3339Nano), nil
|
||||
}
|
||||
|
||||
if fref.UnderlyingType == reflect.TypeOf(int(0)) {
|
||||
rt, ok := langext.TryCastType(realValue, reflect.TypeOf(int(0)))
|
||||
if !ok {
|
||||
return "", errors.New(fmt.Sprintf("failed to cast field '%s' from %s to int", fieldName, fref.Type.String()))
|
||||
}
|
||||
|
||||
return strconv.Itoa(rt.(int)), nil
|
||||
}
|
||||
|
||||
if fref.UnderlyingType == reflect.TypeOf(int32(0)) {
|
||||
rt, ok := langext.TryCastType(realValue, reflect.TypeOf(int32(0)))
|
||||
if !ok {
|
||||
return "", errors.New(fmt.Sprintf("failed to cast field '%s' from %s to int32", fieldName, fref.Type.String()))
|
||||
}
|
||||
|
||||
return strconv.FormatInt(int64(rt.(int32)), 10), nil
|
||||
}
|
||||
|
||||
if fref.UnderlyingType == reflect.TypeOf(int64(0)) {
|
||||
rt, ok := langext.TryCastType(realValue, reflect.TypeOf(int64(0)))
|
||||
if !ok {
|
||||
return "", errors.New(fmt.Sprintf("failed to cast field '%s' from %s to int64", fieldName, fref.Type.String()))
|
||||
}
|
||||
|
||||
return strconv.FormatInt(rt.(int64), 10), nil
|
||||
}
|
||||
|
||||
if fref.UnderlyingType == reflect.TypeOf(float32(0)) {
|
||||
rt, ok := langext.TryCastType(realValue, reflect.TypeOf(float32(0)))
|
||||
if !ok {
|
||||
return "", errors.New(fmt.Sprintf("failed to cast field '%s' from %s to float32", fieldName, fref.Type.String()))
|
||||
}
|
||||
|
||||
return strconv.FormatFloat(float64(rt.(float32)), 'f', -1, 32), nil
|
||||
}
|
||||
|
||||
if fref.UnderlyingType == reflect.TypeOf(float64(0)) {
|
||||
rt, ok := langext.TryCastType(realValue, reflect.TypeOf(float64(0)))
|
||||
if !ok {
|
||||
return "", errors.New(fmt.Sprintf("failed to cast field '%s' from %s to float64", fieldName, fref.Type.String()))
|
||||
}
|
||||
|
||||
return strconv.FormatFloat(rt.(float64), 'f', -1, 64), nil
|
||||
}
|
||||
|
||||
return "", errors.New(fmt.Sprintf("failed to parse field '%s' of type %s (%s)", fieldName, fref.Type.String(), fref.UnderlyingType.String()))
|
||||
}
|
||||
|
||||
func (c *Coll[TData]) getFieldType(fieldName string) fullTypeRef[TData] {
|
||||
|
Reference in New Issue
Block a user