From c11ba8fdb41fe5bfcbe8af76f58fcc0e18aa5cfa Mon Sep 17 00:00:00 2001 From: dev Date: Sat, 14 Jun 2025 00:14:36 +0200 Subject: [PATCH] cdts creado, refinando ctype y cvalue --- .idea/dictionaries/project.xml | 8 + src/base/cdts/a.go | 8 +- src/base/cdts/cstate.go | 12 +- src/base/cdts/ctype_infos.go | 22 +-- src/base/cdts/cvalue.go | 241 +++++++++++++++++++++++++++---- src/corx/cdts/a.go | 99 +++++++++++++ src/corx/cdts/cbtype.go | 109 ++++++++++++++ src/corx/cdts/cbtype_info.go | 214 +++++++++++++++++++++++++++ src/corx/cdts/cctype.go | 47 ++++++ src/corx/cdts/cstate.go | 61 ++++++++ src/corx/cdts/cvalue.go | 257 +++++++++++++++++++++++++++++++++ src/corx/cdts/cvtype.go | 71 +++++++++ 12 files changed, 1105 insertions(+), 44 deletions(-) create mode 100644 src/corx/cdts/a.go create mode 100644 src/corx/cdts/cbtype.go create mode 100644 src/corx/cdts/cbtype_info.go create mode 100644 src/corx/cdts/cctype.go create mode 100644 src/corx/cdts/cstate.go create mode 100644 src/corx/cdts/cvalue.go create mode 100644 src/corx/cdts/cvtype.go diff --git a/.idea/dictionaries/project.xml b/.idea/dictionaries/project.xml index 971c02b..d11ac1a 100644 --- a/.idea/dictionaries/project.xml +++ b/.idea/dictionaries/project.xml @@ -1,11 +1,19 @@ + btype + clist + cmplx + cptr ctype cval cvalue + hashalg + ifcs + ktype ntype tname + uintprt vstate vtype diff --git a/src/base/cdts/a.go b/src/base/cdts/a.go index 90ceba4..2b6ce90 100644 --- a/src/base/cdts/a.go +++ b/src/base/cdts/a.go @@ -52,8 +52,12 @@ type ( ints interface { int | int8 | int16 | int32 | int64 } - uints interface { uint | uint8 | uint16 | uint32 | uint64 } - numsEnt interface { ints | uints } + uints interface { uint | uint8 | uint16 | uint32 | uint64 | uintptr } + floats interface { float32 | float64 } + + intValues interface { ints | uints | floats | Duration } + anyValues interface { complex64 | complex128 | Date | CValue} + slcValues interface { Bytes | Stack } ) diff --git a/src/base/cdts/cstate.go b/src/base/cdts/cstate.go index b33bcca..ecd1c99 100644 --- a/src/base/cdts/cstate.go +++ b/src/base/cdts/cstate.go @@ -36,10 +36,10 @@ const ( CStateNil // Indica que el valor es nulo CStateZero // Indica que el valor tiene valor 0 CStateReflected // Indica que el valor guardado es un value.reflect - CStateShort // Indica que el valor es la versión corta del valor, por ejemplo: los dates - CStateSensitive // Indica que el valor es delicado, por ejemplo no mostrar o no serializar - CStateEncoded // Indica que el valor está codificado - CStateError // Indica que el valor no ha podido ser establecido por un error que esta representado en el + CStateVersion // Indica que el valor es la versión corta del valor, por ejemplo: los dates + CStateSensitive // Indica que el valor es delicado, por ejemplo no mostrar o no serializar + CStateEncoded // Indica que el valor está codificado + CStateError // Indica que el valor no ha podido ser establecido por un error que esta representado en el // valor CStateNone CState = 0 @@ -55,7 +55,7 @@ func (s CState) HasInvalid () bool { return s&CStateInvalid != 0 } func (s CState) HasNil () bool { return s&CStateNil != 0 } func (s CState) HasZero () bool { return s&CStateZero != 0 } func (s CState) HasReflected() bool { return s&CStateReflected != 0 } -func (s CState) HasShort () bool { return s&CStateShort != 0 } +func (s CState) HasVersion () bool { return s&CStateVersion != 0 } func (s CState) HasSensitive() bool { return s&CStateSensitive != 0 } func (s CState) HasEncoded () bool { return s&CStateEncoded != 0 } func (s CState) HasError () bool { return s&CStateError != 0 } @@ -66,7 +66,7 @@ func (s CState) setOption (options CState) CState { return s|(options&CStateOps) func (s CState) String() string { - states := [...]string{"invalid","nil","zero","reflected","short","sensitive","encoded","error"} + states := [...]string{"invalid","nil","zero","reflected","version","sensitive","encoded","error"} c := make([]string,0,4) for i:=0; i<7; i++ { if s& (1< math.MaxInt64 { + return CValue{vType: CTypeUint64, vState: CStateVersion, anyVal: value } + } return CValue{ vType: CTypeUint64, intVal: int64(value) } } func CreateCValueUintptr (value uintptr) CValue { if value == 0 { return CValue{ vType: CTypeUintptr, vState: CStateZero } } + if uint64(value) <= math.MaxInt64 { + return CValue{ vType: CTypeUintptr, anyVal: value } + } return CValue{ vType: CTypeUintptr, intVal: int64(value) } } @@ -155,7 +161,11 @@ func CreateCValueFloat64 (value float64) CValue { if value == 0.0 { // Comprobación directa para el valor cero return CValue{vType: CTypeFloat64, vState: CStateZero} } - return CValue{vType: CTypeFloat64, intVal: int64(math.Float64bits(value))} + b := math.Float64bits(value) + if b > math.MaxInt64 { + return CValue{vType: CTypeFloat64, vState: CStateVersion, anyVal: value} + } + return CValue{vType: CTypeFloat64, intVal: int64(b) } } func CreateCValueComplex64 (value complex64) CValue { @@ -185,7 +195,7 @@ func CreateCValueDate (value Date) CValue { return CValue{vType: CTypeDate, vState: CStateZero} } if value.After(g.minTime) && value.Before(g.maxTime) { - return CValue{vType: CTypeDate, vState: CStateShort,intVal: value.UnixNano(), anyVal: value.Location()} + return CValue{vType: CTypeDate, vState: CStateVersion,intVal: value.UnixNano(), anyVal: value.Location()} } return CValue{vType: CTypeDate, anyVal: value } } @@ -282,7 +292,6 @@ func (v CValue) Value() (any,bool) { // dependiendo donde se ha almacenado // si es valido o no, si esta reflectado // si es un valor lazy, etc - if v.vState.HasInvalid() { return nil, false } @@ -290,15 +299,51 @@ func (v CValue) Value() (any,bool) { return nil, true } if v.vState.HasReflected() { + if v.vType.IsLazy() { + fn,ok := v.anyVal.(func()any) + if !ok { + return nil,false + } + return fn(),true + } return v.anyVal.(reflect.Value), true } - // todo: resto probalidades - return v.anyVal, true + if v.vType.IsList() || v.vType.IsMap() { + return v.anyVal,true + } + + switch v.vType.BaseType() { + case CTypeBool : return v.Bool() + case CTypeString : return v.String() + case CTypeInt : return v.Int() + case CTypeInt8 : return v.Int8() + case CTypeInt16 : return v.Int16() + case CTypeInt32 : return v.Int32() + case CTypeInt64 : return v.Int64() + case CTypeUint : return v.Uint() + case CTypeUint8 : return v.Uint8() + case CTypeUint16 : return v.Uint16() + case CTypeUint32 : return v.Uint32() + case CTypeUint64 : return v.Uint64() + case CTypeFloat32 : return v.Float32() + case CTypeFloat64 : return v.Float64() + case CTypeDate : return v.Date() + case CTypeDuration : return v.Duration() + case CTypeCValue : return v.CValue() + case CTypeComplex64 : return v.Complex64() + case CTypeComplex128 : return v.Complex128() + case CTypeBytes : return v.Bytes() + case CTypeStack : return v.Stack() + case CTypeError : return v.Error() + case CTypeContext : return v.Context() + default: + return v.anyVal,true + } } -func (v CValue) AsBool () (bool,bool) { +func (v CValue) Bool () (bool,bool) { if v.vType != CTypeBool { return false, false } @@ -308,44 +353,190 @@ func (v CValue) AsBool () (bool,bool) { if v.vType.IsLazy() { return v.anyVal.(func()bool)(), true } + // deberiamos poder devolver + // directamente true return v.intVal > 0,true } -func asNumEnt[T numsEnt] (ctype CType, cvalue CValue) (T, bool) { - if cvalue.vType != ctype { - return T(0), false + + + +func (v CValue) Int () (int ,bool) { return fromIntVal[int ](CTypeInt , &v) } + +func (v CValue) Int8 () (int8 ,bool) { return fromIntVal[int8](CTypeInt8 , &v) } + +func (v CValue) Int16() (int16,bool) { return fromIntVal[int16](CTypeInt16, &v) } + +func (v CValue) Int32() (int32,bool) { return fromIntVal[int32](CTypeInt32, &v) } + +func (v CValue) Int64() (int64,bool) { return fromIntVal[int64](CTypeInt64, &v) } + + +func (v CValue) Uint () (uint ,bool) { return fromIntVal[uint ](CTypeUint ,&v) } + +func (v CValue) Uint8 () (uint8 ,bool) { return fromIntVal[uint8](CTypeUint8 ,&v) } + +func (v CValue) Uint16() (uint16,bool) { return fromIntVal[uint16](CTypeUint16,&v) } + +func (v CValue) Uint32() (uint32,bool) { return fromIntVal[uint32](CTypeUint32,&v) } + +func (v CValue) Uint64() (uint64,bool) { return fromIntVal[uint64](CTypeUint64,&v) } + +func (v CValue) Uintprt()(uintptr,bool){ return fromIntVal[uintptr](CTypeUintptr,&v) } + + +func (v CValue) Float32()(float32,bool){ return fromIntVal[float32](CTypeFloat32, &v) } + +func (v CValue) Float64()(float64,bool){ return fromIntVal[float64](CTypeFloat64, &v) } + + +func (v CValue) Complex64 ()(complex64 ,bool){ return fromAnyVal[complex64 ](CTypeComplex64, &v) } + +func (v CValue) Complex128()(complex128,bool){ return fromAnyVal[complex128](CTypeComplex128, &v) } + + +func (v CValue) Duration()(Duration,bool){ return fromIntVal[Duration](CTypeDuration, &v) } + +func (v CValue) Date ()(Date,bool){ return fromAnyVal[Date](CTypeDate, &v) } + + + +func (v CValue) String () (string,bool) { + if v.vType != CTypeString { + return "", false } - if cvalue.vState.HasNilOrZero() { - return T(0), true + if v.vState.HasNilOrZero() { + return "",true } - if cvalue.vType.IsLazy() { - return cvalue.anyVal.(func()T)(),true + if v.vType.IsLazy() { + fn,ok := v.anyVal.(func()string) + if !ok { + return "",false + } + return fn(),true } - return T(cvalue.intVal), true + return v.strVal,true } -func (v CValue) AsInt () (int ,bool) { return asNumEnt[int ](CTypeInt,v) } +func (v CValue) Bytes ()(Bytes,bool){ return fromSlcVal[[]uint8](CTypeBytes, &v)} -func (v CValue) AsInt8 () (int8 ,bool) { return asNumEnt[int8](CTypeInt8,v) } +func (v CValue) Stack ()(Stack,bool){ return fromSlcVal[Stack](CTypeStack, &v) } -func (v CValue) AsInt16() (int16,bool) { return asNumEnt[int16](CTypeInt16, v) } -func (v CValue) AsInt32() (int32,bool) { return asNumEnt[int32](CTypeInt32, v) } +func (v CValue) CValue ()(CValue,bool){ return fromAnyVal[CValue](CTypeCValue, &v) } -func (v CValue) AsInt64() (int64,bool) { return asNumEnt[int64](CTypeInt64, v) } + +func (v CValue) Error ()(error,bool){ + if v.vType != CTypeError { + return nil, false + } + if v.vState.HasNilOrZero() { + return nil, true + } + if v.vType.IsLazy() { + fn, ok := v.anyVal.(func() error) + if !ok { + return nil, false + } + return fn(), true + } + val, ok := v.anyVal.(error) + if !ok { + return nil, false + } + return val, true +} -func (v CValue) AsUint () (uint ,bool) { return asNumEnt[uint ](CTypeUint,v) } +func (v CValue) Context () (Context,bool) { + if v.vType != CTypeContext { + return nil, false + } + if v.vState.HasNilOrZero() { + return nil, true + } + if v.vType.IsLazy() { + fn, ok := v.anyVal.(func() Context) + if !ok { + return nil, false + } + return fn(), true + } + val, ok := v.anyVal.(Context) + if !ok { + return nil, false + } + return val, true +} + + + + +func fromIntVal[T intValues] (ctype CType, cvalue *CValue) (T, bool) { + if cvalue.vType != ctype { + return T(0), false + } + if cvalue.vState.HasNilOrZero() { + return T(0), true + } + if cvalue.vType.IsLazy() { + fn,ok := cvalue.anyVal.(func()T) + if !ok { + return T(0),false + } + return fn(),true + } + if cvalue.vState.HasVersion() { + return T(cvalue.anyVal), true + } + if cvalue.vType&maskCTypeNums == 0 { + if cvalue.vType.IsFloat32() { + return T(math.Float32frombits(uint32(cvalue.intVal))), true + } + if cvalue.vType.IsFloat64() { + return T(math.Float64frombits(uint64(cvalue.intVal))), true + } + } + return T(cvalue.intVal), true +} -func (v CValue) AsUint8 () (uint8 ,bool) { return asNumEnt[uint8](CTypeUint8,v) } -func (v CValue) AsUint16() (uint16,bool) { return asNumEnt[uint16](CTypeUint16, v) } +func fromAnyVal[T anyValues ] (ctype CType, cvalue *CValue) (T, bool) { + if cvalue.vType != ctype { + return *(new(T)), false + } + if cvalue.vState.HasNilOrZero() { + return *(new(T)), true + } + if cvalue.vType.IsLazy() { + fn,ok := cvalue.anyVal.(func()T) + if !ok { + return *(new(T)),false + } + return fn(),true + } + return T(cvalue.anyVal), true +} -func (v CValue) AsUint32() (uint32,bool) { return asNumEnt[uint32](CTypeUint32, v) } -func (v CValue) AsUint64() (uint64,bool) { return asNumEnt[uint64](CTypeUint64, v) } +func fromSlcVal[T slcValues ] (ctype CType, cvalue *CValue) (T, bool) { + if cvalue.vType != ctype { + return nil, false + } + if cvalue.vState.HasNil() { + return nil,true + } + if cvalue.vType.IsLazy() { + fn,ok := cvalue.anyVal.(func()T) + if !ok { + return nil,false + } + return fn(),true + } + return T(cvalue.anyVal), true +} diff --git a/src/corx/cdts/a.go b/src/corx/cdts/a.go new file mode 100644 index 0000000..37f240b --- /dev/null +++ b/src/corx/cdts/a.go @@ -0,0 +1,99 @@ +// ============================================================================= +// GO·AT Framework +// Copyright (c) 2025 ActiveThing (https://activething.com) +// Author: Juan V. Navarro juanvnl@activething.com +// ============================================================================= +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +// ============================================================================= +// Date Create 12/06/2025 +// ============================================================================= + +package cdts + +import ( + "context" + "crypto" + "fmt" + "math" + "math/big" + "reflect" + "time" +) + +type ( + + Bytes = []uint8 + Stack = []struct{ Line string } + UUID = [16]byte + + Reflect = reflect.Value + Date = time.Time + Duration= time.Duration + + Stringer= fmt.Stringer + + BigInt = big.Int + HashAlg = crypto.Hash + + Packable= interface { P () } + MapPackable = interface { Map () } + ListPackable = interface { List () } + ObjPackable = interface { Object () } + + Context = context.Context + + + ints interface { int | int8 | int16 | int32 | int64 } + uints interface { uint | uint8 | uint16 | uint32 | uint64 | uintptr | Duration | HashAlg } + cmplx interface { complex64 | complex128 } + lists interface { Bytes | Stack } +) + + + +var ( + + + g = struct { + bigZero *BigInt + uuidZero UUID + maxCBType CBType + maxCCType CCType + + minTime, + maxTime time.Time + }{ + uuidZero : UUID{}, + bigZero : big.NewInt(0), + maxCCType: ccMaxType, + maxCBType: cbMaxType, + + minTime: time.Unix(0, math.MinInt64), + maxTime: time.Unix(0, math.MaxInt64), + } + + E = struct { + + InvalidReflectValue string + }{ + InvalidReflectValue: "::invalid_reflect_value", + } +) \ No newline at end of file diff --git a/src/corx/cdts/cbtype.go b/src/corx/cdts/cbtype.go new file mode 100644 index 0000000..18a44d9 --- /dev/null +++ b/src/corx/cdts/cbtype.go @@ -0,0 +1,109 @@ +// ============================================================================= +// GO·AT Framework +// Copyright (c) 2025 ActiveThing (https://activething.com) +// Author: Juan V. Navarro juanvnl@activething.com +// ============================================================================= +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +// ============================================================================= +// Date Create 12/06/2025 +// ============================================================================= + +package cdts + + +const ( + + CBTypeInvalid CBType = iota + + CBTypeAny + + CBTypeBool + + CBTypeInt + CBTypeInt8 + CBTypeInt16 + CBTypeInt32 + CBTypeInt64 + + CBTypeUint + CBTypeUint8 + CBTypeUint16 + CBTypeUint32 + CBTypeUint64 + CBTypeUintptr + CBTypeDuration + + CBTypeFloat32 + CBTypeFloat64 + CBTypeComplex64 + CBTypeComplex128 + + + CBTypeBigInt + CBTypeHashAlg + + CBTypeString + CBTypeUUID + CBTypeBytes + CBTypeStack + + CBTypeDate + CBTypeCValue + CBTypeReflect + + + CBTypeListPackable + CBTypeMapPackable + CBTypeObjPackable + CBTypePackable + + + CBTypeContext + CBTypeError + CBTypeStringer + + CBTypeNil + + cbMaxType + +) + + +type CBType uint8 + +func (c CBType) Check() CBType { + if c >= g.maxCBType { + return CBTypeInvalid + } + return c +} + +func (c CBType) IsValid() bool { return c > CBTypeInvalid && c < g.maxCBType } + + + + + + + + + + diff --git a/src/corx/cdts/cbtype_info.go b/src/corx/cdts/cbtype_info.go new file mode 100644 index 0000000..aa710e8 --- /dev/null +++ b/src/corx/cdts/cbtype_info.go @@ -0,0 +1,214 @@ +// ============================================================================= +// GO·AT Framework +// Copyright (c) 2025 ActiveThing (https://activething.com) +// Author: Juan V. Navarro juanvnl@activething.com +// ============================================================================= +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +// ============================================================================= +// Date Create 13/06/2025 +// ============================================================================= + +package cdts + + +type ( + + cBTypeInfo struct { + valType CBType + name string + cByVal, + cByPtr, + cByLazy, + cByList func(any) CValue + } +) + + + +func newCVTypeInfo (cbtype CBType, name string, opts ...func(info *cBTypeInfo)) *cBTypeInfo { + vi := &cBTypeInfo{ + valType: cbtype, + name : name, + } + for _, opt := range opts { + opt(vi) + } + return vi +} + + + +func optInvalidCreators() func(*cBTypeInfo) { + return func (i *cBTypeInfo) { + i.cByVal = createCInvalid + i.cByPtr = createCInvalid + i.cByLazy = createCInvalid + i.cByList = createCInvalid + } +} + +func optSameCreators (cf func(any) CValue) func(info *cBTypeInfo) { + return func(i *cBTypeInfo) { + i.cByVal = cf + i.cByPtr = cf + i.cByLazy = cf + i.cByList = cf + } +} + +func optWithCreators(cvalue, cptr, clazy, clist func(any) CValue) func (*cBTypeInfo) { + return func ( i *cBTypeInfo) { + i.cByVal = cvalue + i.cByPtr = cptr + i.cByLazy = clazy + i.cByList = clist + } +} + + +func optAnyCreators [T any](byValueFn func(v T) CValue) func (*cBTypeInfo) { + var cf func(any) CValue + if byValueFn != nil { + cf = func(v any) CValue { + return byValueFn(v.(T)) + } + } else { + cf = CreateCAny + } + return func (i *cBTypeInfo) { + i.cByVal = cf + i.cByPtr = CreateCAny + i.cByLazy = CreateCAny + i.cByList = CreateCAny + } +} + + + +func optAnyPCreators [T any](byPtrFnc func(v *T) CValue) func (*cBTypeInfo) { + return func(i *cBTypeInfo) { + cf := func(v any) CValue { return byPtrFnc(v.(*T)) } + i.cByPtr = cf + i.cByList= buildCreatorPByList[T](i.valType) + i.cByLazy= buildCreatorPByLazy[T](i.valType) + } +} + + + + +func optNormalCreators [T any](byValueFn func(v T) CValue) func(*cBTypeInfo) { + return func(i *cBTypeInfo) { + vf := func(v any) CValue { return byValueFn(v.(T))} + i.cByVal = vf + i.cByPtr = buildCreatorByPtr [T](i.valType,byValueFn) + i.cByList = buildCreatorByList[T](i.valType) + i.cByLazy = buildCreatorByLazy[T](i.valType) + } +} + + + + + +func buildCreatorByPtr [T any](cbType CBType, byValueFn func(v T) CValue) func(any) CValue { + return func(v any) CValue { + vf := byValueFn + vl,ok := v.(*T) + if !ok { + //todo error + } + if vl == nil { + return CValue{CVType: CVType{ v: cbType}, CState: CStateNil} + } + return vf(*vl) + } +} + +func buildCreatorByList[T any](btype CBType) func(any) CValue { + return func(v any) CValue { + vl,ok := v.([]T) + if !ok { + //todo error + } + vt := CreateCVTypeList(btype) + ln := len(vl) + vs := CStateZero + if ln == 0 { + if vl == nil { + return CValue{ CVType : vt , CState: CStateNil } + } + vs = CStateZero + } + return CValue{ CVType : vt , CState: vs, anyVal: vl, intVal: int64(ln) } + } +} + + +func buildCreatorPByList[T any](btype CBType) func(any) CValue { + return func(v any) CValue { + vl,ok := v.([]*T) + if !ok { + //todo error + } + vt := CreateCVTypeList(btype) + ln := len(vl) + vs := CStateZero + if ln == 0 { + if vl == nil { + return CValue{ CVType : vt , CState: CStateNil } + } + vs = CStateZero + } + return CValue{ CVType : vt , CState: vs, anyVal: vl, intVal: int64(ln) } + } +} + + + +func buildCreatorByLazy[T any](btype CBType) func(any) CValue { + return func(v any) CValue { + vl,ok := v.(func()T) + if !ok { + //todo error + } + vt := CreateCVTypeLazy(btype) + if vl == nil { + return CValue{ CVType: vt, CState: CStateNil } + } + return CValue{ CVType: vt, anyVal: vl } + } +} + + +func buildCreatorPByLazy[T any](btype CBType) func(any) CValue { + return func(v any) CValue { + vl,ok := v.(func()*T) + if !ok { + //todo error + } + vt := CreateCVTypeLazy(btype) + if vl == nil { + return CValue{ CVType: vt, CState: CStateNil } + } + return CValue{ CVType: vt, anyVal: vl } + } +} \ No newline at end of file diff --git a/src/corx/cdts/cctype.go b/src/corx/cdts/cctype.go new file mode 100644 index 0000000..ef8451a --- /dev/null +++ b/src/corx/cdts/cctype.go @@ -0,0 +1,47 @@ +// ============================================================================= +// GO·AT Framework +// Copyright (c) 2025 ActiveThing (https://activething.com) +// Author: Juan V. Navarro juanvnl@activething.com +// ============================================================================= +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +// ============================================================================= +// Date Create 12/06/2025 +// ============================================================================= + +package cdts + + +const ( + + CCTypeNone CCType = iota + CCTypeLazy + CCTypeList + CCTypeMap + + ccMaxType +) + + +type CCType uint8 + + +func (c CCType) IsValid () bool { return c > CCTypeNone && c < g.maxCCType } + diff --git a/src/corx/cdts/cstate.go b/src/corx/cdts/cstate.go new file mode 100644 index 0000000..381dcd3 --- /dev/null +++ b/src/corx/cdts/cstate.go @@ -0,0 +1,61 @@ +// ============================================================================= +// GO·AT Framework +// Copyright (c) 2025 ActiveThing (https://activething.com) +// Author: Juan V. Navarro juanvnl@activething.com +// ============================================================================= +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +// ============================================================================= +// Date Create 12/06/2025 +// ============================================================================= + +package cdts + + +const ( + + CStateInvalid CState = 1 << iota + CStateError + CStateNil + CStateZero + CStateVersion + CStateReflected + CStateSensitive + CStateEncoded + + CStateNone CState = 0 + + cStateOps= CStateSensitive | CStateEncoded +) + +type CState uint8 + + +func (s CState) IsStateInvalid () bool { return s&CStateInvalid != 0 } +func (s CState) IsStateError () bool { return s&CStateError != 0 } +func (s CState) IsStateNil () bool { return s&CStateNil != 0 } +func (s CState) IsStateZero () bool { return s&CStateZero != 0 } +func (s CState) IsStateNilOrZero() bool { return s&(CStateZero |CStateNil) != 0 } +func (s CState) IsStateVersion () bool { return s&CStateVersion != 0 } +func (s CState) IsStateReflected() bool { return s&CStateReflected != 0 } +func (s CState) IsStateSensitive() bool { return s&CStateSensitive != 0 } +func (s CState) IsStateEncoded () bool { return s&CStateEncoded != 0 } + + diff --git a/src/corx/cdts/cvalue.go b/src/corx/cdts/cvalue.go new file mode 100644 index 0000000..8e46662 --- /dev/null +++ b/src/corx/cdts/cvalue.go @@ -0,0 +1,257 @@ +// ============================================================================= +// GO·AT Framework +// Copyright (c) 2025 ActiveThing (https://activething.com) +// Author: Juan V. Navarro juanvnl@activething.com +// ============================================================================= +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +// ============================================================================= +// Date Create 12/06/2025 +// ============================================================================= + +package cdts + +import ( + "math" + "reflect" +) + +type ( + + + CValue struct { + _ [0]func() + + CVType + CState + + intVal int64 + strVal string + anyVal any + } +) + +func CreateCAny (v any) CValue { + if v == nil { + return CValue{ CVType: CVType{v: CBTypeAny }, CState : CStateNil } + } + return CValue{ CVType: CVType{v: CBTypeAny }, anyVal: v } +} + +func CreateCBool (v bool) CValue { + if !v { + return CValue{ CVType: CVType{ v: CBTypeBool }, CState : CStateZero} + } + return CValue{ CVType: CVType{ v: CBTypeBool }, intVal: 1 } +} + +func CreateCInt (v int ) CValue { return createCInts [int ](v,CBTypeInt )} +func CreateCInt8 (v int8 ) CValue { return createCInts [int8 ](v,CBTypeInt8 )} +func CreateCInt16 (v int16 ) CValue { return createCInts [int16 ](v,CBTypeInt16 )} +func CreateCInt32 (v int32 ) CValue { return createCInts [int32 ](v,CBTypeInt32 )} +func CreateCInt64 (v int64 ) CValue { return createCInts [int64 ](v,CBTypeInt64 )} + +func CreateCUint (v uint ) CValue { return createCUints[uint ](v,CBTypeUint )} +func CreateCUint8 (v uint8 ) CValue { return createCUints[uint8 ](v,CBTypeUint8 )} +func CreateCUint16(v uint16) CValue { return createCUints[uint16](v,CBTypeUint16)} +func CreateCUint32(v uint32) CValue { return createCUints[uint32](v,CBTypeUint32)} +func CreateCUint64(v uint64) CValue { return createCUints[uint64](v,CBTypeUint64)} + +func CreateCUintptr (v uintptr ) CValue { return createCUints[uintptr ](v,CBTypeUintptr )} +func CreateCDuration(v Duration) CValue { return createCUints[Duration](v,CBTypeDuration)} + +func CreateCFloat32 (v float32) CValue { + if v == 0 { + return CValue{ CVType: CVType{ v: CBTypeFloat32}, CState: CStateZero } + } + return CValue{ CVType: CVType{ v: CBTypeFloat32}, intVal: int64(math.Float32bits(v)) } +} +func CreateCFloat64 (v float64) CValue { + if v == 0 { + return CValue{ CVType: CVType{ v: CBTypeFloat64}, CState: CStateZero } + } + b := math.Float64bits(v) + if b > math.MaxInt64 { + return CValue{ CVType: CVType{ v: CBTypeFloat64 }, CState : CStateVersion, anyVal: v } + } + return CValue{ CVType: CVType{ v: CBTypeFloat64}, intVal: int64(b) } +} + +func CreateCComplex64 (v complex64 ) CValue { return createCCmplx[complex64 ](v,CBTypeComplex64 ) } +func CreateCComplex128(v complex128) CValue { return createCCmplx[complex128](v,CBTypeComplex128) } + + +func CreateCHashAlg(v HashAlg) CValue { return createCUints[HashAlg](v,CBTypeHashAlg ) } + +func CreateCBigInt (v *BigInt) CValue { + if v.Cmp(g.bigZero) == 0 { + return CValue{ CVType: CVType{ v: CBTypeBigInt }, CState : CStateZero } + } + return CValue{ CVType: CVType{ v: CBTypeBigInt }, anyVal: v } +} + + +func CreateCString (v string) CValue { + if v == "" { + return CValue{ CVType: CVType{ v: CBTypeString }, CState : CStateZero } + } + return CValue{ CVType: CVType{ v: CBTypeString }, strVal: v } +} + +func CreateCBytes (v Bytes) CValue { return createCList[Bytes](v,CBTypeBytes) } + +func CreateCStack (v Stack) CValue { return createCList[Stack](v,CBTypeStack) } + +func CreateCUUID (v UUID) CValue { + if v == g.uuidZero { + return CValue{ CVType: CVType{ v: CBTypeUUID }, CState: CStateZero } + } + return CValue{ CVType: CVType{ v: CBTypeUUID }, anyVal: v } +} + + + +func CreateCDate (v Date) CValue { + if v.IsZero() { // Comprobación directa para el valor cero + return CValue{ CVType: CVType{ v: CBTypeDate }, CState: CStateZero} + } + if v.After(g.minTime) && v.Before(g.maxTime) { + return CValue{ CVType: CVType{ v : CBTypeDate }, CState: CStateVersion, intVal: v.UnixNano(), anyVal: v.Location()} + } + return CValue{ CVType: CVType{ v : CBTypeDate }, anyVal: v } +} + +func CreateCCValue (v CValue) CValue { return CValue{ CVType: CVType{ v: CBTypeCValue }, anyVal: v } } + +func CreateCReflect(v reflect.Value) CValue { + if !v.IsValid() { + return CValue{ CVType: CVType{ v: CBTypeReflect}, CState: CStateInvalid } + } + return CValue{ CVType: CVType{ v: CBTypeReflect}, anyVal: v } +} + + +func CreateCListPackable (v ListPackable ) CValue { return createCIfcs[ListPackable ](v,CBTypeListPackable )} +func CreateCMapPackable (v MapPackable ) CValue { return createCIfcs[MapPackable ](v,CBTypeMapPackable )} +func CreateCObjectPackable(v ObjPackable) CValue { return createCIfcs[ObjPackable](v, CBTypeObjPackable)} +func CreateCPackable (v Packable ) CValue { return createCIfcs[Packable ](v,CBTypePackable )} + +func CreateCContext (v Context) CValue { return createCIfcs[Context ](v,CBTypeContext) } + +func CreateCError (v error ) CValue { return createCIfcs[error ](v,CBTypeError ) } + +func CreateCStringer (v Stringer) CValue { return createCIfcs[Stringer](v,CBTypeStringer ) } + + +func CreateCNil (_ any) CValue { return CValue{ CVType: CVType{ v: CBTypeNil }, CState: CStateNil } } + + +func createCInvalid (_ any) CValue { + return CValue{ CVType: CVType{ v: CBTypeInvalid}, CState : CStateInvalid } +} + +func createCInvalidError (value any, state CState, err string) CValue { + s := CStateError|CStateInvalid + if value == nil { + s|=CStateNil + } + return CValue{ CVType: CVType{ v : CBTypeInvalid }, CState: s,strVal: err, anyVal: value } +} + + +func (v CValue) WithState (state CState) CValue { + v.CState|= state&cStateOps + return v +} + + + + + + + + + +func createCInts [T ints ] (v T, t CBType) CValue { + if v == 0 { + return CValue{ CVType: CVType{ v: t }, CState : CStateZero } + } + return CValue{ CVType: CVType{ v: t }, intVal: int64(v) } +} + +func createCUints[T uints] (v T, t CBType) CValue { + if v == 0 { + return CValue{ CVType: CVType{ v: t }, CState : CStateZero } + } + if t == CBTypeUint64 || t == CBTypeUintptr && v > math.MaxInt64 { + return CValue{ CVType: CVType{ v: t }, CState : CStateVersion, anyVal: v } + } + return CValue{ CVType: CVType{ v: t }, intVal: int64(v) } +} + +func createCCmplx[T cmplx] (v T, t CBType) CValue { + if v == 0 { + return CValue{ CVType: CVType{ v: t }, CState : CStateZero } + } + return CValue{ CVType: CVType{ v: t }, anyVal: v } +} + +func createCList [T lists] (v T, t CBType) CValue { + if v == nil { + return CValue{ CVType: CVType{ v: t }, CState : CStateNil } + } + ln := len(v) + if ln == 0 { + return CValue{ CVType: CVType{ v: t }, CState : CStateZero, anyVal: v } + } + return CValue{ CVType: CVType{ v: t }, anyVal: v , intVal: int64(ln)} +} + +func createCIfcs[T any] (v T, t CBType) CValue { + if v == nil { + return CValue{ CVType: CVType{ v: t }, CState : CStateNil } + } + return CValue{ CVType: CVType{ v: t }, anyVal: v } +} + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/corx/cdts/cvtype.go b/src/corx/cdts/cvtype.go new file mode 100644 index 0000000..323544a --- /dev/null +++ b/src/corx/cdts/cvtype.go @@ -0,0 +1,71 @@ +// ============================================================================= +// GO·AT Framework +// Copyright (c) 2025 ActiveThing (https://activething.com) +// Author: Juan V. Navarro juanvnl@activething.com +// ============================================================================= +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +// ============================================================================= +// Date Create 12/06/2025 +// ============================================================================= + +package cdts + + +type ( + + CVType struct { + v CBType // Define el tipo del valor + k CBType // Indica el tipo de la key en caso de mapas + c CCType // Indica el tipo de valor compuesto que es list o mapas + } +) + + +func CreateCVType(vtype CBType) CVType { + return CVType{v: vtype.Check() } } + +func CreateCVTypeLazy(vtype CBType) CVType { + return CVType{ v: vtype.Check() , c: CCTypeLazy} } + +func CreateCVTypeList(vtype CBType) CVType { + return CVType{ v: vtype.Check() , c: CCTypeList} } + +func CreateCVTypeMap(vtype, ktype CBType) CVType { + return CVType{ v: vtype.Check(), k: ktype.Check(), c: CCTypeMap } } + + + +func (i CVType) Type () (CBType, CCType) { return i.v, i.c } + +func (i CVType) VType () CBType { return i.v } + +func (i CVType) KType () CBType { + // Si no es un tipo mapa, devolvemos + // un tipo no valido + if i.c != CCTypeMap { + return CBTypeInvalid + } + return i.k +} + +func (i CVType) CType () CCType { return i.c } + +