incorporado la clase AFields para su uso generealizado para las clases que necesiten incorporar informacion estructurada de forma dinamica como por ejemplo el caso de los errores asi como el de los logs.
parent
fb7e0cab27
commit
4d42ed5798
@ -0,0 +1,46 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name active.go
|
||||
// Created by DEV
|
||||
// Modified 30/04/2024
|
||||
//
|
||||
// Copyright 2024 activething.com
|
||||
// ------------------------------------------------------------------------
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
package flds
|
||||
|
||||
import "syncs/pools"
|
||||
|
||||
var (
|
||||
|
||||
G = struct {
|
||||
|
||||
|
||||
|
||||
} {
|
||||
|
||||
}
|
||||
|
||||
g = struct {
|
||||
|
||||
pool pools.GPool[AFields]
|
||||
|
||||
} {
|
||||
pool : *pools.NewGPool[AFields](pools.WithNew(newAFields)),
|
||||
}
|
||||
|
||||
)
|
||||
@ -0,0 +1,133 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name afield.go
|
||||
// Created by DEV
|
||||
// Modified 30/04/2024
|
||||
//
|
||||
// Copyright 2024 activething.com
|
||||
// ------------------------------------------------------------------------
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
package flds
|
||||
|
||||
import "time"
|
||||
|
||||
type (
|
||||
|
||||
AField struct {
|
||||
fs *AFields
|
||||
hp int
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
||||
func (f AField) IsValid() bool {
|
||||
return f.fs.heads[f.hp].pos != -1 }
|
||||
|
||||
func (f AField) Name () string {
|
||||
return f.fs.heads[f.hp].name
|
||||
}
|
||||
|
||||
func (f AField) Kind () Kind {
|
||||
return f.fs.heads[f.hp].kind
|
||||
}
|
||||
|
||||
func (f AField) Value() any {
|
||||
if f.fs.heads[f.hp].pos == -1 {
|
||||
return nil
|
||||
}
|
||||
p := f.fs.heads[f.hp].pos
|
||||
switch f.fs.heads[f.hp].kind {
|
||||
case KindAny : return f.fs.anys[p]
|
||||
case KindError : return f.fs.anys[p].(error)
|
||||
case KindBool : return f.fs.nums[p] != 0
|
||||
case KindSigned : return f.fs.nums[p]
|
||||
case KindUnsigned : return uint(f.fs.nums[p])
|
||||
case KindFloat : return f.fs.floats [p]
|
||||
case KindString : return f.fs.strings [p]
|
||||
case KindDate : return f.fs.dates [p]
|
||||
case KindBinary : return f.fs.binaries[p]
|
||||
default:
|
||||
// Handle unknown field type
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (f AField) Any () (any,bool) {
|
||||
return f.Value(), f.hp != -1 }
|
||||
|
||||
|
||||
func (f AField) Bool () (bool,bool) {
|
||||
if f.hp != -1 || f.fs.heads[f.hp].kind != KindBool {
|
||||
return false,false
|
||||
}
|
||||
return f.fs.nums[f.fs.heads[f.hp].pos] > 0,true
|
||||
}
|
||||
|
||||
func (f AField) String () (string,bool) {
|
||||
if f.hp != -1 || f.fs.heads[f.hp].kind != KindString {
|
||||
return "",false
|
||||
}
|
||||
return f.fs.strings[f.fs.heads[f.hp].pos],true
|
||||
}
|
||||
|
||||
func (f AField) Signed () (int,bool) {
|
||||
if f.hp != -1 || f.fs.heads[f.hp].kind != KindSigned {
|
||||
return 0,false
|
||||
}
|
||||
return f.fs.nums[f.fs.heads[f.hp].pos],true
|
||||
}
|
||||
|
||||
func (f AField) Unsigned () (uint,bool) {
|
||||
if f.hp != -1 || f.fs.heads[f.hp].kind != KindUnsigned {
|
||||
return 0,false
|
||||
}
|
||||
return uint(f.fs.nums[f.fs.heads[f.hp].pos]),true
|
||||
}
|
||||
|
||||
|
||||
func (f AField) Date () (time.Time,bool) {
|
||||
if f.hp != -1 || f.fs.heads[f.hp].kind != KindDate {
|
||||
var z time.Time
|
||||
return z,false
|
||||
}
|
||||
return f.fs.dates[f.fs.heads[f.hp].pos],true
|
||||
}
|
||||
|
||||
func (f AField) Time () (time.Time,bool) {
|
||||
if f.hp != -1 || f.fs.heads[f.hp].kind != KindTime {
|
||||
var z time.Time
|
||||
return z,false
|
||||
}
|
||||
return f.fs.dates[f.fs.heads[f.hp].pos],true
|
||||
}
|
||||
|
||||
func (f AField) Error () (error,bool) {
|
||||
if f.hp != -1 || f.fs.heads[f.hp].kind != KindError {
|
||||
return nil,false
|
||||
}
|
||||
return f.fs.anys[f.fs.heads[f.hp].pos].(error),true
|
||||
}
|
||||
|
||||
func (f AField) Binary () ([]byte,bool) {
|
||||
if f.hp != -1 || f.fs.heads[f.hp].kind != KindBinary {
|
||||
return nil,false
|
||||
}
|
||||
return f.fs.binaries[f.fs.heads[f.hp].pos],true
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,214 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name afields.go
|
||||
// Created by DEV
|
||||
// Modified 30/04/2024
|
||||
//
|
||||
// Copyright 2024 activething.com
|
||||
// ------------------------------------------------------------------------
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
package flds
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
AFields struct {
|
||||
heads []head
|
||||
kinds Kind
|
||||
|
||||
anys []any
|
||||
nums []int
|
||||
floats []float64
|
||||
strings []string
|
||||
dates []time.Time
|
||||
binaries [][]byte
|
||||
}
|
||||
|
||||
AFieldsFnc func(afields *AFields)
|
||||
|
||||
AFieldsMapFnc func(field AField) bool
|
||||
)
|
||||
|
||||
|
||||
func AcquireAFields (options ...AFieldsFnc) *AFields {
|
||||
return g.pool.Get().init(options) }
|
||||
|
||||
|
||||
func ReleaseAFields (fields *AFields) {
|
||||
if fields == nil {
|
||||
return
|
||||
}
|
||||
g.pool.Put(fields.Reset())
|
||||
}
|
||||
|
||||
|
||||
func newAFields () *AFields {
|
||||
return &AFields{} }
|
||||
|
||||
func NewAFields (options ...AFieldsFnc) *AFields {
|
||||
return newAFields().init(options) }
|
||||
|
||||
|
||||
func (f *AFields) Len () int {
|
||||
return len(f.heads) }
|
||||
|
||||
|
||||
func (f *AFields) Has (name string) bool {
|
||||
return f.has(name) != -1 }
|
||||
|
||||
|
||||
|
||||
func (f *AFields) init (options []AFieldsFnc) *AFields {
|
||||
for _,o := range options {
|
||||
o(f)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func (f *AFields) Reset() *AFields {
|
||||
f.heads = f.heads[:0]
|
||||
f.kinds = 0
|
||||
|
||||
f.anys = f.anys [:0]
|
||||
f.nums = f.nums [:0]
|
||||
f.floats = f.floats [:0]
|
||||
f.strings = f.strings [:0]
|
||||
f.dates = f.dates [:0]
|
||||
f.binaries= f.binaries[:0]
|
||||
|
||||
return f
|
||||
}
|
||||
|
||||
|
||||
func (f *AFields) Kinds() Kind {
|
||||
return f.kinds }
|
||||
|
||||
|
||||
func (f *AFields) GetField (name string) AField {
|
||||
return AField {
|
||||
fs: f,
|
||||
hp: f.has(name),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (f *AFields) Map (mapFn AFieldsMapFnc) {
|
||||
fl := AField{ fs : f }
|
||||
for i := 0; i<len(f.heads); i++ {
|
||||
fl.hp = i
|
||||
if !mapFn(fl) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (f *AFields) Sort () {
|
||||
sort.Slice(f.heads, func(i, j int) bool {
|
||||
return f.heads[i].name < f.heads[j].name
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
func (f *AFields) AddAny (name string, value any) {
|
||||
switch t := value.(type) {
|
||||
case string : f.AddString(name,t)
|
||||
case bool : f.AddBool (name,t)
|
||||
case int : f.AddSigned(name,t)
|
||||
case int8 : f.AddSigned(name,int(t))
|
||||
case int16 : f.AddSigned(name,int(t))
|
||||
case int32 : f.AddSigned(name,int(t))
|
||||
case int64 : f.AddSigned(name,int(t))
|
||||
case uint : f.AddUnsigned(name,t)
|
||||
case uint8 : f.AddUnsigned(name,uint(t))
|
||||
case uint16 : f.AddUnsigned(name,uint(t))
|
||||
case uint32 : f.AddUnsigned(name,uint(t))
|
||||
case uint64 : f.AddUnsigned(name,uint(t))
|
||||
case float32 : f.AddFloat(name,float64(t))
|
||||
case float64 : f.AddFloat(name,t)
|
||||
case []byte : f.AddBinary(name,t)
|
||||
case time.Time: f.AddDate(name,t)
|
||||
default:
|
||||
f.addField(name,KindAny,len(f.anys)).anys = append(f.anys, value)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (f *AFields) AddError(name string, value error) {
|
||||
f.addField(name,KindError,len(f.anys)).anys = append(f.anys,value) }
|
||||
|
||||
func (f *AFields) AddBool (name string, value bool) {
|
||||
v := 0
|
||||
if value {
|
||||
v=1
|
||||
}
|
||||
f.addField(name,KindBool,len(f.nums)).nums = append(f.nums,v)
|
||||
}
|
||||
|
||||
func (f *AFields) AddSigned (name string, value int) {
|
||||
f.addField(name,KindSigned,len(f.nums)).nums = append(f.nums,value) }
|
||||
|
||||
func (f *AFields) AddUnsigned(name string, value uint) {
|
||||
f.addField(name,KindUnsigned,len(f.nums)).nums = append(f.nums,int(value)) }
|
||||
|
||||
func (f *AFields) AddFloat(name string, value float64) {
|
||||
f.addField(name,KindFloat,len(f.floats)).floats = append(f.floats,value) }
|
||||
|
||||
func (f *AFields) AddDate (name string, value time.Time) {
|
||||
f.addField(name,KindDate,len(f.dates)).dates = append(f.dates,value) }
|
||||
|
||||
func (f *AFields) AddTime (name string, value time.Time) {
|
||||
f.addField(name,KindTime,len(f.dates)).dates = append(f.dates,value) }
|
||||
|
||||
func (f *AFields) AddString(name string, value string) {
|
||||
f.addField(name,KindString,len(f.strings)).strings = append(f.strings,value) }
|
||||
|
||||
func (f *AFields) AddBinary(name string, value []byte) {
|
||||
f.addField(name,KindBinary,len(f.binaries)).binaries = append(f.binaries,value) }
|
||||
|
||||
|
||||
|
||||
func (f *AFields) addField (name string, kind Kind, pos int) *AFields {
|
||||
for i:=1; f.has(name) == -1; i++ {
|
||||
name = fmt.Sprintf("fmtNameDuplicated",name,i)
|
||||
}
|
||||
f.kinds |= kind
|
||||
f.heads = append(f.heads, head{
|
||||
name: name,
|
||||
kind: kind,
|
||||
pos : pos,
|
||||
})
|
||||
return f
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (f *AFields) has (name string) int {
|
||||
for i,h := range f.heads {
|
||||
if h.name == name {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name heads.go
|
||||
// Created by DEV
|
||||
// Modified 30/04/2024
|
||||
//
|
||||
// Copyright 2024 activething.com
|
||||
// ------------------------------------------------------------------------
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
package flds
|
||||
|
||||
|
||||
type (
|
||||
|
||||
head struct {
|
||||
name string
|
||||
kind Kind
|
||||
pos int
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@ -0,0 +1,90 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name kind.go
|
||||
// Created by DEV
|
||||
// Modified 30/04/2024
|
||||
//
|
||||
// Copyright 2024 activething.com
|
||||
// ------------------------------------------------------------------------
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
package flds
|
||||
|
||||
const(
|
||||
|
||||
KindAny Kind = 1 << iota
|
||||
KindBool
|
||||
KindSigned
|
||||
KindUnsigned
|
||||
KindFloat
|
||||
KindString
|
||||
KindDate
|
||||
KindTime
|
||||
KindBinary
|
||||
KindError
|
||||
)
|
||||
|
||||
|
||||
type (
|
||||
|
||||
Kind uint8
|
||||
|
||||
)
|
||||
|
||||
func (k Kind) IsAny() bool {
|
||||
return k&KindAny != 0 }
|
||||
|
||||
func (k Kind) IsBool() bool {
|
||||
return k&KindBool != 0 }
|
||||
|
||||
func (k Kind) IsSigned() bool {
|
||||
return k&KindSigned != 0 }
|
||||
|
||||
func (k Kind) IsUnsigned() bool {
|
||||
return k&KindUnsigned != 0 }
|
||||
|
||||
func (k Kind) IsFloat() bool {
|
||||
return k&KindFloat != 0 }
|
||||
|
||||
func (k Kind) IsString() bool {
|
||||
return k&KindString != 0 }
|
||||
|
||||
func (k Kind) IsDate() bool {
|
||||
return k&KindDate != 0 }
|
||||
|
||||
func (k Kind) IsTime() bool {
|
||||
return k&KindTime != 0 }
|
||||
|
||||
func (k Kind) IsBinary() bool {
|
||||
return k&KindBinary != 0 }
|
||||
|
||||
func (k Kind) IsError() bool {
|
||||
return k&KindError != 0 }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in new issue