cdts creado, refinando ctype y cvalue

master
dev 1 year ago
parent 775d64b147
commit c11ba8fdb4

@ -1,11 +1,19 @@
<component name="ProjectDictionaryState">
<dictionary name="project">
<words>
<w>btype</w>
<w>clist</w>
<w>cmplx</w>
<w>cptr</w>
<w>ctype</w>
<w>cval</w>
<w>cvalue</w>
<w>hashalg</w>
<w>ifcs</w>
<w>ktype</w>
<w>ntype</w>
<w>tname</w>
<w>uintprt</w>
<w>vstate</w>
<w>vtype</w>
</words>

@ -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 }
)

@ -36,7 +36,7 @@ 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
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
@ -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<<i) != 0 {

@ -175,8 +175,8 @@ func (i *cTypeInfos) createCValue (value any) CValue {
case []float64 : f = i.types[GetPos(CTypeFloat64 )].creators[createByList]
case []complex64: f = i.types[GetPos(CTypeComplex64 )].creators[createByList]
case []complex128:f = i.types[GetPos(CTypeComplex128)].creators[createByList]
case []Duration : f = i.types[GetPos(CTypeDate )].creators[createByList]
case []Date : f = i.types[GetPos(CTypeDuration )].creators[createByList]
case []Duration : f = i.types[GetPos(CTypeDuration )].creators[createByList]
case []Date : f = i.types[GetPos(CTypeDate )].creators[createByList]
case []string : f = i.types[GetPos(CTypeString )].creators[createByList]
case []Bytes : f = i.types[GetPos(CTypeBytes )].creators[createByList]
case []Stack : f = i.types[GetPos(CTypeStack )].creators[createByList]
@ -204,8 +204,8 @@ func (i *cTypeInfos) createCValue (value any) CValue {
case *float64 : f = i.types[GetPos(CTypeFloat64 )].creators[createByPtr]
case *complex64 : f = i.types[GetPos(CTypeComplex64 )].creators[createByPtr]
case *complex128: f = i.types[GetPos(CTypeComplex128)].creators[createByPtr]
case *Duration : f = i.types[GetPos(CTypeDate )].creators[createByPtr]
case *Date : f = i.types[GetPos(CTypeDuration )].creators[createByPtr]
case *Duration : f = i.types[GetPos(CTypeDuration )].creators[createByPtr]
case *Date : f = i.types[GetPos(CTypeDate )].creators[createByPtr]
case *string : f = i.types[GetPos(CTypeString )].creators[createByPtr]
case *Bytes : f = i.types[GetPos(CTypeBytes )].creators[createByPtr]
case *Stack : f = i.types[GetPos(CTypeStack )].creators[createByPtr]
@ -232,8 +232,8 @@ func (i *cTypeInfos) createCValue (value any) CValue {
case func()float64 : f = i.types[GetPos(CTypeFloat64 )].creators[createByLazy]
case func()complex64: f = i.types[GetPos(CTypeComplex64 )].creators[createByLazy]
case func()complex128:f = i.types[GetPos(CTypeComplex128)].creators[createByLazy]
case func()Duration : f = i.types[GetPos(CTypeDate )].creators[createByLazy]
case func()Date : f = i.types[GetPos(CTypeDuration )].creators[createByLazy]
case func()Duration : f = i.types[GetPos(CTypeDuration )].creators[createByLazy]
case func()Date : f = i.types[GetPos(CTypeDate )].creators[createByLazy]
case func()string : f = i.types[GetPos(CTypeString )].creators[createByLazy]
case func()Bytes : f = i.types[GetPos(CTypeBytes )].creators[createByLazy]
case func()Stack : f = i.types[GetPos(CTypeStack )].creators[createByLazy]
@ -425,14 +425,14 @@ func createCValueRAny (tname string) func(reflect.Value) CValue {
func GetPos(t CType) int {
if t == 0 {
// Si 't' es CType(0) (que corresponde a CTypeInvalid),
// Si 't' es CBType(0) (que corresponde a CTypeInvalid),
// se le asigna el índice 0. Esto es consistente con 'cTypeInfos[0]'
// albergando la información para el tipo inválido.
return 0
}
// Calcula la posición del bit '1' más a la derecha en la representación binaria de 't'.
//
// Para los CType definidos como potencias de 2 (ej. 1 << iota, donde iota es el índice del bit),
// Para los CBType definidos como potencias de 2 (ej. 1 << iota, donde iota es el índice del bit),
// bits.TrailingZeros32(uint32(t)) devuelve precisamente ese índice del bit (0-based).
//
// Al sumar '+1' al resultado, convertimos este índice de bit (0-based)
@ -441,9 +441,9 @@ func GetPos(t CType) int {
// Esto es crucial para indexar el array 'cTypeInfos [33]CTypeInfo',
// ya que:
// - CTypeInvalid (0) mapea a 'cTypeInfos[0]'.
// - El CType correspondiente al bit 0 (valor 1) mapea a 'cTypeInfos[1]'.
// - El CType correspondiente al bit 1 (valor 2) mapea a 'cTypeInfos[2]'.
// - ... y así sucesivamente, hasta el CType para el bit 31 (valor 1 << 31)
// - El CBType correspondiente al bit 0 (valor 1) mapea a 'cTypeInfos[1]'.
// - El CBType correspondiente al bit 1 (valor 2) mapea a 'cTypeInfos[2]'.
// - ... y así sucesivamente, hasta el CBType para el bit 31 (valor 1 << 31)
// que mapearía al índice 32, el último índice válido en el array de 33 posiciones.
//
// si es un tipo compuesto debemos aplicar la mascara

@ -101,7 +101,7 @@ func CreateCValueInt32 (value int32) CValue {
return CValue{vType: CTypeInt32, intVal: int64(value)}
}
func CreateCValueInt64 (value int64 ) CValue {
func CreateCValueInt64 (value int64) CValue {
if value == 0 { return CValue{ vType: CTypeInt64, vState: CStateZero } }
return CValue{ vType: CTypeInt64, intVal: value }
}
@ -132,12 +132,18 @@ func CreateCValueUint32 (value uint32) CValue {
func CreateCValueUint64 (value uint64) CValue {
if value == 0 { return CValue{ vType: CTypeUint64, vState: CStateZero } }
if value > 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
}
func (v CValue) AsBool () (bool,bool) {
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) 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 T(cvalue.intVal), true
return fn(),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) Stack ()(Stack,bool){ return fromSlcVal[Stack](CTypeStack, &v) }
func (v CValue) CValue ()(CValue,bool){ return fromAnyVal[CValue](CTypeCValue, &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) AsInt8 () (int8 ,bool) { return asNumEnt[int8](CTypeInt8,v) }
func (v CValue) AsInt16() (int16,bool) { return asNumEnt[int16](CTypeInt16, 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 (v CValue) AsInt32() (int32,bool) { return asNumEnt[int32](CTypeInt32, v) }
func (v CValue) AsInt64() (int64,bool) { return asNumEnt[int64](CTypeInt64, v) }
func (v CValue) AsUint () (uint ,bool) { return asNumEnt[uint ](CTypeUint,v) }
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
}

@ -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",
}
)

@ -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 }

@ -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 }
}
}

@ -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 }

@ -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 }

@ -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 }
}

@ -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 }
Loading…
Cancel
Save

Powered by TurnKey Linux.