diff --git a/src/app/main.go b/src/app/main.go index 8a66748..1970d06 100644 --- a/src/app/main.go +++ b/src/app/main.go @@ -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()) } diff --git a/src/core/cflss/a.go b/src/core/cflss/a.go index fbb60c0..394becc 100644 --- a/src/core/cflss/a.go +++ b/src/core/cflss/a.go @@ -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", + } +) \ No newline at end of file diff --git a/src/core/cflss/cvalue.go b/src/core/cflss/cvalue.go index b00a1b8..253b42d 100644 --- a/src/core/cflss/cvalue.go +++ b/src/core/cflss/cvalue.go @@ -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 } \ No newline at end of file diff --git a/src/core/cflss/cvalue_state.go b/src/core/cflss/cvalue_state.go index 1d53a2d..143260f 100644 --- a/src/core/cflss/cvalue_state.go +++ b/src/core/cflss/cvalue_state.go @@ -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<