parent
cea56aba14
commit
6aec02d432
@ -0,0 +1,82 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name err_fields.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 errs
|
||||
|
||||
import (
|
||||
"errs/erdata"
|
||||
"vals/flds"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
|
||||
ErrFields struct {
|
||||
error
|
||||
flds.AFields
|
||||
}
|
||||
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
func (e ErrFields) Unwrap() error {
|
||||
return e.error }
|
||||
|
||||
func (e ErrFields) UnwrapAll() []error {
|
||||
return nil }
|
||||
|
||||
|
||||
|
||||
|
||||
func (e ErrFields) WrapWithInfo (options ...ErrInfoFnc) error {
|
||||
return WrapWithInfo(e,options...) }
|
||||
|
||||
|
||||
func (e ErrFields) WrapWithCause(cause error) error {
|
||||
return WrapWithCause(e,cause) }
|
||||
|
||||
|
||||
func (e ErrFields) WrapWithTrace(offset int) error {
|
||||
return WrapWithTrace(e,offset) }
|
||||
|
||||
func (e ErrFields) WrapWithFieldString (name, value string) error {
|
||||
return WrapWithFieldString(e,name,value) }
|
||||
|
||||
func (e ErrFields) WrapWithMessage (msg string) error {
|
||||
return WrapWithMessage(e,msg) }
|
||||
|
||||
|
||||
|
||||
func (e ErrFields) String () string {
|
||||
return string(G.Encoder.EncodeError(e)) }
|
||||
|
||||
|
||||
func (e ErrFields) Collect (collector erdata.Collector) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,136 +0,0 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 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 _deprecated
|
||||
|
||||
import (
|
||||
"time"
|
||||
"vals/flds"
|
||||
)
|
||||
|
||||
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 () flds.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 flds.KindAny: return f.fs.anys[p]
|
||||
case flds.KindError: return f.fs.anys[p].(error)
|
||||
case flds.KindBool: return f.fs.nums[p] != 0
|
||||
case flds.KindSigned: return f.fs.nums[p]
|
||||
case flds.KindUnsigned: return uint(f.fs.nums[p])
|
||||
case flds.KindFloat: return f.fs.floats [p]
|
||||
case flds.KindString: return f.fs.strings [p]
|
||||
case flds.KindDate: return f.fs.dates [p]
|
||||
case flds.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 != flds.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 != flds.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 != flds.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 != flds.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 != flds.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 != flds.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 != flds.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 != flds.KindBinary {
|
||||
return nil,false
|
||||
}
|
||||
return f.fs.binaries[f.fs.heads[f.hp].pos],true
|
||||
}
|
||||
|
||||
|
||||
@ -1,215 +0,0 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 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 _deprecated
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
"vals/flds"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
AFields struct {
|
||||
heads []head
|
||||
kinds flds.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 flds.g.pool.Get().init(options) }
|
||||
|
||||
|
||||
func ReleaseAFields (fields *AFields) {
|
||||
if fields == nil {
|
||||
return
|
||||
}
|
||||
flds.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() flds.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, flds.KindAny,len(f.anys)).anys = append(f.anys, value)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (f *AFields) AddError(name string, value error) {
|
||||
f.addField(name, flds.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, flds.KindBool,len(f.nums)).nums = append(f.nums,v)
|
||||
}
|
||||
|
||||
func (f *AFields) AddSigned (name string, value int) {
|
||||
f.addField(name, flds.KindSigned,len(f.nums)).nums = append(f.nums,value) }
|
||||
|
||||
func (f *AFields) AddUnsigned(name string, value uint) {
|
||||
f.addField(name, flds.KindUnsigned,len(f.nums)).nums = append(f.nums,int(value)) }
|
||||
|
||||
func (f *AFields) AddFloat(name string, value float64) {
|
||||
f.addField(name, flds.KindFloat,len(f.floats)).floats = append(f.floats,value) }
|
||||
|
||||
func (f *AFields) AddDate (name string, value time.Time) {
|
||||
f.addField(name, flds.KindDate,len(f.dates)).dates = append(f.dates,value) }
|
||||
|
||||
func (f *AFields) AddTime (name string, value time.Time) {
|
||||
f.addField(name, flds.KindTime,len(f.dates)).dates = append(f.dates,value) }
|
||||
|
||||
func (f *AFields) AddString(name string, value string) {
|
||||
f.addField(name, flds.KindString,len(f.strings)).strings = append(f.strings,value) }
|
||||
|
||||
func (f *AFields) AddBinary(name string, value []byte) {
|
||||
f.addField(name, flds.KindBinary,len(f.binaries)).binaries = append(f.binaries,value) }
|
||||
|
||||
|
||||
|
||||
func (f *AFields) addField (name string, kind flds.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
|
||||
}
|
||||
Loading…
Reference in new issue