casi terminacion de cvalue

master
dev 1 year ago
parent a7523466a3
commit dc80c6878b

@ -29,23 +29,38 @@
package main
import (
"core/cfls"
"core/cflss"
"fmt"
)
func main() {
t := cfls.CValType(0)
/*
var m map[string]int = map[string]int{ "ss" : 22, "ds" : 434}
for i:= 0; i <= 32; i++ {
fmt.Printf("index %d: valor (%d) %s == %s\n",t.Index(),t,t.String(),cfls.G.CValTypeNames[i])
t = 1 << i
}
c := cflss.G.TypeInfos.CreateCValue(m)
fmt.Printf("%v+ --> %s \n",c,c.Type().String())
t = cfls.CValType(1 << 31)
fmt.Printf("NOmre del tipo %d = %s \n",t.Index(),t.String())
g := cflss.G.TypeInfos.CreateCValue(func() int { return 1 } )
fmt.Printf("%v+ --> %s \n",g,g.Type().String())
var f func() int
g = cflss.G.TypeInfos.CreateCValue(f)
fmt.Printf("%v+ --> %s %s \n",g,g.Type().String(),g.State().String())
type st struct { na string }
var a *st
e := cflss.G.TypeInfos.CreateCValue(a)
fmt.Printf("%v+ --> %s %s \n",e,e.Type().String(),e.State().String())
*/
var an any
an = 0
e := cflss.G.TypeInfos.CreateCValue(an)
fmt.Printf("%v+ --> tipo : %s estado :%s \n",e,e.Type().String(),e.State().String())
}

@ -27,3 +27,30 @@
// =============================================================================
package cflss
import (
"math"
"time"
)
var (
G = struct {
minTime,
maxTime time.Time
TypeInfos *CValueTypeInfos
}{
minTime: time.Unix(0, math.MinInt64),
maxTime: time.Unix(0, math.MaxInt64),
TypeInfos: newCValueTypeInfos(),
}
E = struct {
InvalidCValueForType string
}{
InvalidCValueForType : "::invalid_value_for_type",
}
)

@ -28,7 +28,10 @@
package cflss
import "reflect"
import (
"math"
"reflect"
)
type CValue struct {
_ [0]func()
@ -42,139 +45,324 @@ type CValue struct {
func CreateCValue(value any) CValue {
return G.TypeInfos.CreateCValue(value)
}
func createCValueAny (value any ) CValue {
if value == nil { return CValue{ vType: CValueTypeAny, vState: cValueStateNil|cValueStateZero } }
return CValue{ vType: CValueTypeAny, anyVal: value }
}
func createCValueAny (value any ) CValue { return CValue{ } }
func CreateCValueBool (value bool ) CValue { return CValue{ } }
func CreateCValueInt (value int ) CValue { return CValue{ } }
func CreateCValueInt8 (value int8 ) CValue { return CValue{ } }
func CreateCValueInt16 (value int16 ) CValue { return CValue{ } }
func CreateCValueInt32 (value int32 ) CValue { return CValue{ } }
func CreateCValueInt64 (value int64 ) CValue { return CValue{ } }
func CreateCValueUint (value uint ) CValue { return CValue{ } }
func CreateCValueUint8 (value uint8 ) CValue { return CValue{ } }
func CreateCValueUint16 (value uint16 ) CValue { return CValue{ } }
func CreateCValueUint32 (value uint32 ) CValue { return CValue{ } }
func CreateCValueUint64 (value uint64 ) CValue { return CValue{ } }
func CreateCValueUintptr (value uintptr ) CValue { return CValue{ } }
func CreateCValueFloat32 (value float32 ) CValue { return CValue{ } }
func CreateCValueFloat64 (value float64 ) CValue { return CValue{ } }
func CreateCValueComplex64 (value complex64 ) CValue { return CValue{ } }
func CreateCValueComplex128 (value complex128) CValue { return CValue{ } }
func CreateCValueDuration (value Duration ) CValue { return CValue{ } }
func CreateCValueDate (value Date ) CValue { return CValue{ } }
func CreateCValueString (value string ) CValue { return CValue{ } }
func CreateCValueBytes (value []uint8 ) CValue { return CValue{ } }
func CreateCValueStack (value Stack ) CValue { return CValue{ } }
func CreateCValueBaleable (value Baleable ) CValue { return CValue{ } }
func CreateCValueError (value error ) CValue { return CValue{ } }
func CreateCValueNil (value any ) CValue { return CValue{ } }
func CreateCValue (value CValue ) CValue { return CValue{ } }
func CreateCValueCField (value CField ) CValue { return CValue{ } }
func CreateCValueObject (value any ) CValue { return CValue{ } }
func CreateCValueBool (value bool ) CValue {
if !value { return CValue{ vType: CValueTypeInt, vState: cValueStateZero } }
return CValue{ vType: CValueTypeInt, vState: cValueStateZero, intVal: 1}
}
func CreateCValueDefer (value any) CValue { return CValue{ } }
func CreateCValueList (value any) CValue { return CValue{ } }
func CreateCValueMap (value any) CValue { return CValue{ } }
// Int's
func createCValueInvalid (value any) CValue { return CValue{ } }
func CreateCValueInt (value int ) CValue {
if value == 0 { return CValue{ vType: CValueTypeInt, vState: cValueStateZero } }
return CValue{ vType: CValueTypeInt, intVal: int64(value) }
}
func CreateCValueInt8 (value int8 ) CValue {
if value == 0 { return CValue{ vType: CValueTypeInt8, vState: cValueStateZero } }
return CValue{ vType: CValueTypeInt8, intVal: int64(value) }
}
func createCValueListByReflected (value any, vtype CValueType, ntype string) CValue {
func CreateCValueInt16 (value int16 ) CValue {
if value == 0 { return CValue{ vType: CValueTypeInt16, vState: cValueStateZero } }
return CValue{ vType: CValueTypeInt16, intVal: int64(value) }
}
rv := reflect.ValueOf(value)
if !rv.IsValid() {
// Si el valor de entrada es inválido (ej. un reflect.Value{} vacío),
// esto indica un problema. No podemos crear un CVal válido para una lista.
return CValue{
vType : CValueTypeInvalid,
vState: cValueStateReflected|cValueStateError,
strVal: "todo/error_invalid_value",
}
func CreateCValueInt32 (value int32 ) CValue {
if value == 0 { return CValue{ vType: CValueTypeInt32, vState: cValueStateZero } }
return CValue{ vType: CValueTypeInt32, intVal: int64(value) }
}
func CreateCValueInt64 (value int64 ) CValue {
if value == 0 { return CValue{ vType: CValueTypeInt64, vState: cValueStateZero } }
return CValue{ vType: CValueTypeInt64, intVal: value }
}
// Uint's
func CreateCValueUint (value uint ) CValue {
if value == 0 { return CValue{ vType: CValueTypeUint, vState: cValueStateZero } }
return CValue{ vType: CValueTypeUint, intVal: int64(value) }
}
func CreateCValueUint8 (value uint8 ) CValue {
if value == 0 { return CValue{ vType: CValueTypeUint8, vState: cValueStateZero } }
return CValue{ vType: CValueTypeUint8, intVal: int64(value) }
}
func CreateCValueUint16 (value uint16 ) CValue {
if value == 0 { return CValue{ vType: CValueTypeUint16, vState: cValueStateZero } }
return CValue{ vType: CValueTypeUint16, intVal: int64(value) }
}
func CreateCValueUint32 (value uint32 ) CValue {
if value == 0 { return CValue{ vType: CValueTypeUint32, vState: cValueStateZero } }
return CValue{ vType: CValueTypeUint32, intVal: int64(value) }
}
func CreateCValueUint64 (value uint64 ) CValue {
if value == 0 { return CValue{ vType: CValueTypeUint64, vState: cValueStateZero } }
return CValue{ vType: CValueTypeUint64, intVal: int64(value) }
}
func CreateCValueUintptr (value uintptr ) CValue {
if value == 0 { return CValue{ vType: CValueTypeUintptr, vState: cValueStateZero } }
return CValue{ vType: CValueTypeUintptr, intVal: int64(value) }
}
// Floats
func CreateCValueFloat32 (value float32 ) CValue {
if value == 0.0 { // Comprobación directa para el valor cero
return CValue{vType: CValueTypeFloat32, vState: cValueStateZero}
}
// Estas comprobaciones son redundantes ya que por logica antes de la llamada
// a esta funcion deben de haberse realizado
//
/*
if rv.Kind() != reflect.Slice && rv.Kind() != reflect.Array {
// El valor no es un slice ni un array. Esto es un error de uso para esta función.
return CValue {
vType : CValueTypeInvalid,
vState: cValueStateReflected|cValueStateError,
strVal: "todo/error_invalid_value",
}
return CValue{vType: CValueTypeFloat32, intVal: int64(math.Float32bits(value))}
}
func CreateCValueFloat64 (value float64 ) CValue {
if value == 0.0 { // Comprobación directa para el valor cero
return CValue{vType: CValueTypeFloat64, vState: cValueStateZero}
}
*/
vs := cValueStateReflected
if rv.Len() == 0 {
if rv.IsNil() {
vs|=cValueStateNil
} else {
vs|= cValueStateZero
}
return CValue{vType: CValueTypeFloat64, intVal: int64(math.Float64bits(value))}
}
func CreateCValueComplex64 (value complex64 ) CValue {
if value == 0 { // Comprobación directa para el valor cero
return CValue{vType: CValueTypeComplex64, vState: cValueStateZero }
}
return CValue{vType: vtype, vState: vs, anyVal: value, strVal: ntype }
return CValue{vType: CValueTypeComplex64, anyVal: value}
}
func createCValueMapByReflected (value any, vtype CValueType, ntype string) CValue {
rv := reflect.ValueOf(value)
if !rv.IsValid() {
// Si el valor de entrada es inválido (ej. un reflect.Value{} vacío),
// esto indica un problema. No podemos crear un CVal válido para un mapa
return CValue{
vType : CValueTypeInvalid,
vState: cValueStateReflected|cValueStateError,
strVal: "todo/error_invalid_value",
func CreateCValueComplex128 (value complex128) CValue {
if value == 0 { // Comprobación directa para el valor cero
return CValue{vType: CValueTypeComplex128, vState: cValueStateZero }
}
return CValue{vType: CValueTypeComplex128, anyVal: value}
}
func CreateCValueDuration (value Duration ) CValue {
if value == 0 {
return CValue{vType: CValueTypeDuration, vState: cValueStateZero}
}
return CValue{vType: CValueTypeDuration, intVal: int64(value)}
}
func CreateCValueDate (value Date ) CValue {
if value.IsZero() { // Comprobación directa para el valor cero
return CValue{vType: CValueTypeDate, vState: cValueStateZero}
}
if value.After(G.minTime) && value.Before(G.maxTime) {
return CValue{vType: CValueTypeDate, vState: cValueStateShort,intVal: value.UnixNano(), anyVal: value.Location()}
}
return CValue{vType: CValueTypeDate, anyVal: value }
}
func CreateCValueString (value string ) CValue {
if value == "" { // Comprobación directa para el valor cero
return CValue{vType: CValueTypeString, vState: cValueStateZero}
}
return CValue{vType: CValueTypeString, strVal: value}
}
func CreateCValueBytes (value []uint8 ) CValue {
l := len(value)
if l == 0 {
if value == nil {
return CValue{vType: CValueTypeBytes, vState: cValueStateNil}
} else {
return CValue{vType: CValueTypeBytes, vState: cValueStateZero,anyVal: value}
}
}
vs := cValueStateReflected
if rv.Len() == 0 {
if rv.IsNil() {
vs|=cValueStateNil
return CValue{vType: CValueTypeBytes, anyVal: value, intVal: int64(l)}
}
func CreateCValueStack (value Stack ) CValue {
l := len(value)
if l == 0 {
if value == nil {
return CValue{vType: CValueTypeStack, vState: cValueStateNil}
} else {
vs|= cValueStateZero
return CValue{vType: CValueTypeStack, vState: cValueStateZero,anyVal: value}
}
}
return CValue{vType: CValueTypeStack, anyVal: value, intVal: int64(l)}
}
func CreateCValueBaleable (value Baleable ) CValue {
if value == nil {
return CValue{vType: CValueTypeBaleable, vState: cValueStateNil}
}
return CValue{ vType: CValueTypeBaleable, anyVal: value }
}
func CreateCValueError (value error ) CValue {
if value == nil {
return CValue{vType: CValueTypeError, vState: cValueStateNil}
}
return CValue{ vType: CValueTypeError, anyVal: value }
}
func CreateCValueNil (value any ) CValue { return CValue{ vType: CValueTypeNil } }
func createCValueCValue (value CValue ) CValue { return CValue{vType: CValueTypeCValue, anyVal: value } }
func createCValueCField (value CField ) CValue { return CValue{vType: CValueTypeCField, anyVal: value} }
func createCValueObject (value any ) CValue {
return createCValueReflected(value,reflect.ValueOf(value),CValueTypeObject, true) }
func CreateCValueList (value any) CValue {
return createCValueReflected (value,reflect.ValueOf(value),CValueTypeList , true)
}
func CreateCValueDefer (value any) CValue {
return createCValueReflected (value,reflect.ValueOf(value),CValueTypeDefer ,true)
}
func CreateCValueMap (value any) CValue {
return createCValueReflected (value,reflect.ValueOf(value),CValueTypeMap , true)
}
func createCValueInvalid (value any) CValue {
return CValue{ vType: CValueTypeInvalid, anyVal: value}
}
func createCValueWithInvalidError (value any, vtype CValueType, vstate CValueState, er string) CValue {
return CValue{
vType: vtype,vState: vs, anyVal: value, strVal: ntype,
vType : CValueTypeInvalid,
vState: cValueStateInvalid|cValueStateError|vstate,
intVal: int64(vtype),
anyVal: value,
strVal: er,
}
}
func createCValueDeferByReflected (value any, vtype CValueType, ntype string) CValue {
rv := reflect.ValueOf(value)
func createCValueReflected (value any, rv reflect.Value, vtype CValueType, check bool) CValue {
if !rv.IsValid() {
// Si el valor de entrada es inválido (ej. un reflect.Value{} vacío),
// esto indica un problema. No podemos crear un CVal válido para una funcion.
return CValue{
vType : CValueTypeInvalid,
vState: cValueStateReflected|cValueStateError,
strVal: "todo/error_invalid_value",
}
return buildCValueInvalidWithError("TODO/error invalid reflect value")(value)
}
rt := rv.Type()
vs := cValueStateReflected
if value == nil || !rv.IsValid() {
return CValue{
vType : vtype,
vState: vs|cValueStateNil,
strVal: ntype,
tn := rt.String()
switch rt.Kind() {
case reflect.Slice,
reflect.Array:
if check {
if !vtype.IsList() {
return createCValueWithInvalidError(value,vtype,vs,E.InvalidCValueForType)
}
ke := reflectKindToCValueType(rt.Elem().Kind(), true)
if ke == CValueTypeInvalid {
return createCValueWithInvalidError(value,vtype,vs,E.InvalidCValueForType)
}
vtype |= ke
}
ln := rv.Len()
if ln == 0 {
if rv.IsNil() {
vs|=cValueStateNil
} else {
vs|= cValueStateZero
}
}
return CValue{ vType: vtype,vState: vs, anyVal: value, strVal: tn, intVal: int64(ln) }
case reflect.Map :
if check {
if !vtype.IsMap() {
return createCValueWithInvalidError(value,vtype,vs|cValueStateInvalid,E.InvalidCValueForType)
}
kt := reflectKindToCValueType(rt.Key().Kind(), true)
if kt == CValueTypeInvalid {
return createCValueWithInvalidError(value,vtype,vs|cValueStateInvalid,E.InvalidCValueForType)
}
et := reflectKindToCValueType(rt.Elem().Kind(), true)
if et == CValueTypeInvalid {
return createCValueWithInvalidError(value,vtype,vs|cValueStateInvalid,E.InvalidCValueForType)
}
vtype = buildMapType(kt, et)
}
ln := rv.Len()
if ln == 0 {
if rv.IsNil() {
vs|=cValueStateNil
} else {
vs|= cValueStateZero
}
}
return CValue{ vType: vtype,vState: vs, anyVal: value, strVal: tn, intVal: int64(ln) }
case reflect.Func :
if check {
if !vtype.IsDefer() {
return createCValueWithInvalidError(value,vtype,vs,E.InvalidCValueForType)
}
if rt.NumIn() != 0 || rt.NumOut() == 1 {
return createCValueWithInvalidError(value,vtype,vs,E.InvalidCValueForType)
}
vtype = CValueTypeDefer|CValueTypeAny
}
if rv.IsNil() {
vs|=cValueStateNil
}
return CValue{ vType: vtype,vState: vs, strVal: tn,
anyVal: func() any {
return (rv.Call(nil))[0].Interface()
},
}
case reflect.Struct :
if check {
if !vtype.IsTypeObject() {
return createCValueWithInvalidError(value,vtype,vs,E.InvalidCValueForType)
}
}
return CValue{vType: vtype,vState: vs, anyVal: value, strVal: tn}
case reflect.Ptr:
if rv.IsNil() {
vs|=cValueStateNil
}
}
return CValue{
vType : vtype,
vState: vs,
strVal: ntype,
anyVal: func() any {
return (rv.Call(nil))[0].Interface()
},
}
return CValue{ vType: vtype,vState: vs, anyVal: value, strVal: tn }
}
func (v CValue) Type () CValueType { return v.vType }
func (v CValue) State() CValueState { return v.vState }

@ -28,9 +28,7 @@
package cflss
import "strings"
const (
@ -64,3 +62,19 @@ func (s CValueState) HasValueError () bool { return s&cValueStateError != 0 }
func (s CValueState) setOption (options CValueState) CValueState { return s|(options&cValueStateOps) }
func (s CValueState) String() string {
states := [...]string{"invalid","nil","zero","reflected","short","sensitive","encoded","error"}
c := make([]string,0,4)
for i:=0; i<7; i++ {
if s& (1<<i) != 0 {
c=append(c,states[i])
}
}
if len(c) == 0 {
return ""
}
return strings.Join(c,":")
}

@ -159,6 +159,11 @@ func (t CValueType) IsList () bool { return t&CValueTypeList != 0 }
func (t CValueType) IsMap () bool { return t&CValueTypeMap != 0 }
func (t CValueType) String () string {
return G.TypeInfos.getName(t)
}
func buildMapType(keyType, valType CValueType) CValueType {
if keyType == CValueTypeInvalid || valType == CValueTypeInvalid {
return CValueTypeInvalid

@ -95,7 +95,7 @@ func newCValueTypeInfos () *CValueTypeInfos {
},
creators: [maxCValueTypes+1][cValueNumCreators]func(any)CValue{
/* 0: */ createGValCreators[any ](CValueTypeInvalid,createCValueInvalid),
/* 1: */ createGValCreators[any ](CValueTypeAny, createCValueAny),
/* 1: */ createGValCreators[any ](CValueTypeAny, createCValueAny),
/* 2: */ createGValCreators[bool ](CValueTypeBool, CreateCValueBool),
/* 3: */ createGValCreators[int ](CValueTypeInt, CreateCValueInt),
/* 4: */ createGValCreators[int8 ](CValueTypeInt8, CreateCValueInt8),
@ -132,9 +132,9 @@ func newCValueTypeInfos () *CValueTypeInfos {
/* 23:*/ createGValCreators[Baleable ](CValueTypeBaleable, CreateCValueBaleable),
/* 24:*/ createGValCreators[error ](CValueTypeError, CreateCValueError),
/* 25:*/ createGValCreators[any ](CValueTypeNil, CreateCValueNil),
/* 26:*/ createGValCreators[CValue ](CValueTypeCValue, CreateCValue),
/* 27:*/ createGValCreators[CField ](CValueTypeCField, CreateCValueCField),
/* 28:*/ createGValCreators[any ](CValueTypeObject, CreateCValueObject),
/* 26:*/ createGValCreators[CValue ](CValueTypeCValue, createCValueCValue),
/* 27:*/ createGValCreators[CField ](CValueTypeCField, createCValueCField),
/* 28:*/ createGValCreators[any ](CValueTypeObject, createCValueObject),
/* 29:*/ createGValCreators[any ](CValueTypeDefer, CreateCValueDefer),
/* 30:*/ createGValCreators[any ](CValueTypeList, CreateCValueList),
/* 31:*/ createGValCreators[any ](CValueTypeMap, CreateCValueMap),
@ -168,7 +168,7 @@ func (_ *CValueTypeInfos) getIndex(CValueType CValueType) int {
}
func (i *CValueTypeInfos) createCValue (value any) CValue {
func (i *CValueTypeInfos) CreateCValue(value any) CValue {
var f func(any) CValue
switch value.(type) {
@ -312,67 +312,49 @@ func (i *CValueTypeInfos) createByReflection(value any) CValue {
i.mux.RUnlock()
vt := CValueTypeAny
vs := cValueStateReflected
st := rt.String()
f = func(v any) CValue { return CValue{ vType: vt, vState: vs, strVal: st, anyVal: value } }
switch rt.Kind() {
// Los tipos soportados deben de haber sido capturados
// por el switch de tipos
case reflect.Struct : // createCValueAny
vt = CValueTypeObject
f = func (v any) CValue {
return CValue{
vType : vt,
vState: vs,
strVal: st,
anyVal: v,
}
}
case reflect.Func :
if rt.NumIn() == 0 && rt.NumOut() == 1 {
vt = CValueTypeDefer|CValueTypeAny
f = func(v any) CValue {
return createCValueDeferByReflected(v,vt,st)
}
}
case reflect.Map :
var kt,et CValueType
if kt = reflectKindToCValueType(rt.Key().Kind(),true); kt == CValueTypeInvalid {
f = createCValueInvalid
} else if et = reflectKindToCValueType(rt.Elem().Kind(),true); et == CValueTypeInvalid {
f = createCValueInvalid
} else {
vt = buildMapType(kt,et)
f = func (v any) CValue {
return createCValueMapByReflected(v,vt,st)
if kt = reflectKindToCValueType(rt.Key().Kind(),true); kt != CValueTypeInvalid {
if et = reflectKindToCValueType(rt.Elem().Kind(),true); et != CValueTypeInvalid {
vt = buildMapType(kt,et)
}
}
case reflect.Slice,
reflect.Array :
ke := reflectKindToCValueType(rt.Elem().Kind(),true)
if ke == CValueTypeInvalid {
f = createCValueInvalid
}
f = func (v any) CValue {
return createCValueListByReflected(v, vt, st)
if ke != CValueTypeInvalid {
vt = ke|CValueTypeList
}
case reflect.Interface:
vt = CValueTypeAny
}
f = func(v any) CValue {
return createCValueReflected(v,reflect.ValueOf(v), vt,false)
}
i.mux.Lock()
i.cache[rv.Type()] = f
i.mux.Unlock()
return f(value)
return createCValueReflected(value,rv,vt,false)
}
func createGValCreators [T any] (valType CValueType, createFnc func(value T) CValue) [cValueNumCreators]func(any)CValue {
return [cValueNumCreators]func(any)CValue {
func(v any) CValue { return createFnc(v.(T)) },
@ -382,6 +364,7 @@ func createGValCreators [T any] (valType CValueType, createFnc func(value T) CVa
}
}
func createGValTypeList [T any](vType CValueType, value []T) CValue {
if value == nil {
return CValue{vType: vType, vState: cValueStateNil}
@ -414,11 +397,13 @@ func buildCValueInvalidWithError (err string) func(v any) CValue {
}
func reflectKindToCValueType (kind reflect.Kind, base bool) CValueType {
switch kind {
case reflect.Invalid: return CValueTypeInvalid
case reflect.Bool: return CValueTypeBool
case reflect.Int: return CValueTypeInt
case reflect.Invalid: return CValueTypeInvalid
case reflect.Bool: return CValueTypeBool
case reflect.Int: return CValueTypeInt
case reflect.Int8: return CValueTypeInt8
case reflect.Int16: return CValueTypeInt16
case reflect.Int32: return CValueTypeInt32

Loading…
Cancel
Save

Powered by TurnKey Linux.