|
|
|
|
@ -0,0 +1,246 @@
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
// ℹ️ Project active3
|
|
|
|
|
// 🔗 Active Thing (activething.com) git.activething.com/go
|
|
|
|
|
//
|
|
|
|
|
// File name factory.go
|
|
|
|
|
// ✍️ Created by DEV
|
|
|
|
|
// 📅 Modified 01/05/2024
|
|
|
|
|
//
|
|
|
|
|
// 🔒 COPYRIGHT Copyright 2024 activething.com
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
package acache
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"reflect"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Create es un método que crea y devuelve un nuevo campo con el nombre, valor y tipo especificados.
|
|
|
|
|
// Este método es utilizado para crear campos de diferentes tipos según el valor del tipo Kind proporcionado.
|
|
|
|
|
// Si el tipo Kind es KindAny, el campo se crea utilizando el valor y su tipo se infiere automáticamente.
|
|
|
|
|
//
|
|
|
|
|
// Parámetros:
|
|
|
|
|
// - name: El nombre del campo que se va a crear.
|
|
|
|
|
// - valueFn: El valor del campo que se va a asignar.
|
|
|
|
|
// - valuerKind: El tipo de campo que se va a crear, representado por un valor de la enumeración Kind.
|
|
|
|
|
// Devuelve:
|
|
|
|
|
// - Field: El campo creado con el nombre, valor y tipo especificados.
|
|
|
|
|
// Pánico:
|
|
|
|
|
// - Si se proporciona un tipo de Kind inválido, el método entra en un estado de pánico y devuelve un error ErrInvalidKind.
|
|
|
|
|
func Create (name string, value any, kind Kind) Field {
|
|
|
|
|
if kind == 0 {
|
|
|
|
|
return CreateFromValue(name,value)
|
|
|
|
|
}
|
|
|
|
|
switch kind {
|
|
|
|
|
case KindAny : return NewFieldAny (name,value)
|
|
|
|
|
case KindBool : return NewFieldBool (name,value.(bool))
|
|
|
|
|
case KindBinary : return NewFieldBinary(name,value.([]byte))
|
|
|
|
|
case KindString : return NewFieldString(name,value.(string))
|
|
|
|
|
case KindInt : return NewFieldInt (name,value.(int))
|
|
|
|
|
case KindUint : return NewFieldUint (name,value.(uint))
|
|
|
|
|
case KindFloat : return NewFieldFloat (name,value.(float64))
|
|
|
|
|
case KindTime : return NewFieldTime (name,value.(time.Time))
|
|
|
|
|
case KindDuration:return NewFieldDuration(name,value.(time.Duration))
|
|
|
|
|
case KindError : return NewFieldError (name,value.(error))
|
|
|
|
|
case KindReflect : return NewFieldReflect(name,value.(reflect.Value))
|
|
|
|
|
|
|
|
|
|
//valuerKind Fields
|
|
|
|
|
case KindDefer,
|
|
|
|
|
KindDeferAny : return NewDFieldAny (name,value.(func()any))
|
|
|
|
|
case KindDeferBool : return NewDFieldBool (name,value.(func()bool))
|
|
|
|
|
case KindDeferString : return NewDFieldString(name,value.(func()string))
|
|
|
|
|
case KindDeferBinary : return NewDFieldBinary(name,value.(func()[]byte))
|
|
|
|
|
case KindDeferInt : return NewDFieldInt (name,value.(func()int))
|
|
|
|
|
case KindDeferUint : return NewDFieldUint (name,value.(func()uint))
|
|
|
|
|
case KindDeferFloat : return NewDFieldFloat (name,value.(func()float64))
|
|
|
|
|
case KindDeferTime : return NewDFieldTime (name,value.(func()time.Time))
|
|
|
|
|
case KindDeferDuration:return NewDFieldDuration (name,value.(func()time.Duration))
|
|
|
|
|
case KindDeferError : return NewDFieldError (name,value.(func()error))
|
|
|
|
|
case KindDeferReflect :return NewDFieldReflect(name,value.(func()reflect.Value))
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
panic(G.ErrInvalidKind)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// CreateFromValue es un método que crea y devuelve un nuevo campo basado en el valor proporcionado.
|
|
|
|
|
// Examina el tipo del valor y crea un campo correspondiente, asignando el nombre especificado.
|
|
|
|
|
// Si el tipo del valor no coincide con ninguno de los tipos predefinidos, se crea un campo genérico utilizando el tipo KindAny.
|
|
|
|
|
//
|
|
|
|
|
// Parámetros:
|
|
|
|
|
// - name: El nombre del campo que se va a crear.
|
|
|
|
|
// - valueFn: El valor del campo que se va a asignar.
|
|
|
|
|
// Devuelve:
|
|
|
|
|
// - Field: El campo creado con el nombre y valor especificados.
|
|
|
|
|
func CreateFromValue(name string, value any) Field {
|
|
|
|
|
switch t := value.(type) {
|
|
|
|
|
|
|
|
|
|
case bool : return NewFieldBool (name,t)
|
|
|
|
|
case []byte : return NewFieldBinary (name,t)
|
|
|
|
|
case string : return NewFieldString (name,t)
|
|
|
|
|
|
|
|
|
|
case int : return NewFieldInt (name,t)
|
|
|
|
|
case int8 : return NewFieldInt (name,int(t))
|
|
|
|
|
case int16 : return NewFieldInt (name,int(t))
|
|
|
|
|
case int32 : return NewFieldInt (name,int(t))
|
|
|
|
|
case int64 : return NewFieldInt (name,int(t))
|
|
|
|
|
|
|
|
|
|
case uint : return NewFieldUint (name,t)
|
|
|
|
|
case uint8 : return NewFieldUint (name,uint(t))
|
|
|
|
|
case uint16 : return NewFieldUint (name,uint(t))
|
|
|
|
|
case uint32 : return NewFieldUint (name,uint(t))
|
|
|
|
|
case uint64 : return NewFieldUint (name,uint(t))
|
|
|
|
|
case uintptr: return NewFieldUint (name,uint(t))
|
|
|
|
|
|
|
|
|
|
case float64: return NewFieldFloat(name,t)
|
|
|
|
|
case float32: return NewFieldFloat(name,float64(t))
|
|
|
|
|
|
|
|
|
|
case error : return NewFieldError(name,t)
|
|
|
|
|
case time.Time:
|
|
|
|
|
return NewFieldTime (name,t)
|
|
|
|
|
case time.Duration:
|
|
|
|
|
return NewFieldDuration (name,t)
|
|
|
|
|
case reflect.Value :
|
|
|
|
|
return NewFieldReflect(name,t)
|
|
|
|
|
|
|
|
|
|
case func() any : return NewDFieldAny (name,t)
|
|
|
|
|
case func() bool : return NewDFieldBool(name,t)
|
|
|
|
|
|
|
|
|
|
case func() []byte : return NewDFieldBinary(name,t)
|
|
|
|
|
case func() string : return NewDFieldString(name,t)
|
|
|
|
|
|
|
|
|
|
case func() int : return NewDFieldInt (name,t)
|
|
|
|
|
case func() int8 : return NewDFieldInt (name,func() int { return int(t()) })
|
|
|
|
|
case func() int16 : return NewDFieldInt (name,func() int { return int(t()) })
|
|
|
|
|
case func() int32 : return NewDFieldInt (name,func() int { return int(t()) })
|
|
|
|
|
case func() int64 : return NewDFieldInt (name,func() int { return int(t()) })
|
|
|
|
|
|
|
|
|
|
case func() uint : return NewDFieldUint(name,t)
|
|
|
|
|
case func() uint8 : return NewDFieldUint(name,func() uint { return uint(t()) })
|
|
|
|
|
case func() uint16 : return NewDFieldUint(name,func() uint { return uint(t()) })
|
|
|
|
|
case func() uint32 : return NewDFieldUint(name,func() uint { return uint(t()) })
|
|
|
|
|
case func() uint64 : return NewDFieldUint(name,func() uint { return uint(t()) })
|
|
|
|
|
case func() uintptr: return NewDFieldUint(name,func() uint { return uint(t()) })
|
|
|
|
|
|
|
|
|
|
case func() float64: return NewDFieldFloat(name,t)
|
|
|
|
|
case func() float32: return NewDFieldFloat(name,func() float64 { return float64(t()) })
|
|
|
|
|
|
|
|
|
|
case func() error : return NewDFieldError(name,t)
|
|
|
|
|
|
|
|
|
|
case func() time.Time :
|
|
|
|
|
return NewDFieldTime(name,t)
|
|
|
|
|
case func() time.Duration :
|
|
|
|
|
return NewDFieldDuration(name,t)
|
|
|
|
|
case func() reflect.Value :
|
|
|
|
|
return NewDFieldReflect(name,t)
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
if value != nil {
|
|
|
|
|
rv := reflect.ValueOf(value)
|
|
|
|
|
if rv.Kind() == reflect.Func {
|
|
|
|
|
tp := rv.Type()
|
|
|
|
|
if tp.NumIn() == 0 && tp.NumOut() == 1 {
|
|
|
|
|
return NewDFieldAny(name, func() any {
|
|
|
|
|
return rv.Call(nil)[0].Interface()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NewFieldAny(name,value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TypeToKind es una función que determina el tipo de Kind correspondiente al valor proporcionado.
|
|
|
|
|
// Examina el tipo del valor y devuelve un tipo Kind adecuado que representa el tipo del valor.
|
|
|
|
|
// Si el valor es una función, el tipo Kind resultante tendrá la bandera KindDefer para indicar que es una función deferida.
|
|
|
|
|
//
|
|
|
|
|
// Parámetros:
|
|
|
|
|
// - valueFn: El valor del cual se determinará el tipo Kind.
|
|
|
|
|
// Devuelve:
|
|
|
|
|
// - Kind: El tipo Kind correspondiente al valor proporcionado.
|
|
|
|
|
func TypeToKind(value any) Kind {
|
|
|
|
|
switch value.(type) {
|
|
|
|
|
case bool:
|
|
|
|
|
return KindAny
|
|
|
|
|
case []byte :
|
|
|
|
|
return KindBinary
|
|
|
|
|
case string :
|
|
|
|
|
return KindString
|
|
|
|
|
case int,
|
|
|
|
|
int8,
|
|
|
|
|
int16,
|
|
|
|
|
int32,
|
|
|
|
|
int64 :
|
|
|
|
|
return KindInt
|
|
|
|
|
case uint,
|
|
|
|
|
uint8,
|
|
|
|
|
uint16,
|
|
|
|
|
uint32,
|
|
|
|
|
uint64,
|
|
|
|
|
uintptr : return KindUint
|
|
|
|
|
case float64,
|
|
|
|
|
float32:
|
|
|
|
|
return KindFloat
|
|
|
|
|
case error :
|
|
|
|
|
return KindError
|
|
|
|
|
case time.Time:
|
|
|
|
|
return KindTime
|
|
|
|
|
case reflect.Value:
|
|
|
|
|
return KindReflect
|
|
|
|
|
|
|
|
|
|
case func() any :
|
|
|
|
|
return KindAny|KindDefer
|
|
|
|
|
case func() bool :
|
|
|
|
|
return KindBool|KindDefer
|
|
|
|
|
case func() []byte :
|
|
|
|
|
return KindBinary|KindDefer
|
|
|
|
|
case func() string :
|
|
|
|
|
return KindString|KindDefer
|
|
|
|
|
case func() int,
|
|
|
|
|
func() int8,
|
|
|
|
|
func() int16,
|
|
|
|
|
func() int32,
|
|
|
|
|
func() int64 :
|
|
|
|
|
return KindInt|KindDefer
|
|
|
|
|
case func() uint,
|
|
|
|
|
func() uint8,
|
|
|
|
|
func() uint16,
|
|
|
|
|
func() uint32,
|
|
|
|
|
func() uint64,
|
|
|
|
|
func() uintptr :
|
|
|
|
|
return KindInt|KindDefer
|
|
|
|
|
case func() float64,
|
|
|
|
|
func() float32 :
|
|
|
|
|
return KindFloat|KindDefer
|
|
|
|
|
case func() error :
|
|
|
|
|
return KindError|KindDefer
|
|
|
|
|
case func() time.Time:
|
|
|
|
|
return KindTime|KindDefer
|
|
|
|
|
case func() reflect.Value:
|
|
|
|
|
return KindReflect|KindDefer
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
rt := reflect.TypeOf(value)
|
|
|
|
|
if rt.Kind() == reflect.Func && rt.NumIn() == 0 && rt.NumOut() == 1 {
|
|
|
|
|
return KindAny|KindDefer
|
|
|
|
|
}
|
|
|
|
|
return KindAny
|
|
|
|
|
}
|
|
|
|
|
}
|