parent
9946a74437
commit
ad9e72cf4d
@ -0,0 +1,3 @@
|
||||
module aios
|
||||
|
||||
go 1.22
|
||||
@ -0,0 +1,3 @@
|
||||
module bfrs
|
||||
|
||||
go 1.22
|
||||
@ -0,0 +1,41 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project active3
|
||||
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name field_builder.go
|
||||
// ✍️ Created by DEV
|
||||
// 📅 Modified 04/05/2024
|
||||
//
|
||||
// 🔒 Copyright 2024 activething.com
|
||||
//
|
||||
// 📜 LICENSE --------------------------------------------------------------
|
||||
// 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 blds
|
||||
|
||||
|
||||
type (
|
||||
|
||||
FieldBuilder interface {
|
||||
|
||||
BuildField (builder ObjectBuilder)
|
||||
}
|
||||
|
||||
|
||||
BuildFieldFnc func (builder ObjectBuilder)
|
||||
|
||||
)
|
||||
|
||||
func (f BuildFieldFnc) BuildField (builder ObjectBuilder) {
|
||||
f(builder) }
|
||||
@ -0,0 +1,33 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project active3
|
||||
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name flags.go
|
||||
// ✍️ Created by DEV
|
||||
// 📅 Modified 04/05/2024
|
||||
//
|
||||
// 🔒 Copyright 2024 activething.com
|
||||
//
|
||||
// 📜 LICENSE --------------------------------------------------------------
|
||||
// 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 blds
|
||||
|
||||
|
||||
type (
|
||||
|
||||
Flags uint32
|
||||
)
|
||||
|
||||
|
||||
@ -0,0 +1,193 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name active.go
|
||||
// Created by DEV
|
||||
// Modified 23/03/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/ertext"
|
||||
"errs/stack"
|
||||
"errs/wrap"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
CodePanicError string = "panic"
|
||||
CodeMultiError string = "multiple-errors"
|
||||
|
||||
|
||||
CodeParamError string = "param"
|
||||
CodeParamNilError string = "param-nil"
|
||||
CodeParamOutOfRangeError string = "param-out-of-range"
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
var (
|
||||
G = struct {
|
||||
|
||||
Encoder ErrorEncoder
|
||||
|
||||
ErrParam ErrCode
|
||||
ErrParamNil ErrCode
|
||||
ErrParamOutOfRange ErrCode
|
||||
|
||||
}{
|
||||
|
||||
ErrParam : ErrCode(CodeParamError),
|
||||
ErrParamNil : ErrCode(CodeParamNilError),
|
||||
ErrParamOutOfRange : ErrCode(CodeParamOutOfRangeError),
|
||||
|
||||
Encoder : ertext.DefaultEncoder(),
|
||||
}
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func ToError(err any) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
switch e := err.(type) {
|
||||
case error : return e
|
||||
case []error:
|
||||
if len(e) == 0 {
|
||||
return nil
|
||||
}
|
||||
return NewErrMulti(e...)
|
||||
default : return fmt.Errorf("%v",err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
func Encode(err error, encoder ErrorEncoder) string {
|
||||
if encoder != nil {
|
||||
return string(encoder.EncodeError(err))
|
||||
}
|
||||
return string(G.Encoder.EncodeError(err))
|
||||
}
|
||||
|
||||
|
||||
func ToString (err error) string {
|
||||
if err == nil {
|
||||
return ""
|
||||
}
|
||||
if st,is := err.(interface { String() string }); is {
|
||||
return st.String()
|
||||
}
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
|
||||
|
||||
func PanicRecoveredError (rec any) error {
|
||||
if rec == nil {
|
||||
return nil
|
||||
}
|
||||
return *NewErrPanic(ToError(rec))
|
||||
}
|
||||
|
||||
|
||||
func Unwrap(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if cs,is := err.(Unwrapper); is {
|
||||
|
||||
|
||||
return cs.Unwrap()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func UnwrapCause(err error) error {
|
||||
e := err
|
||||
for e != nil {
|
||||
cs,is := e.(Causer)
|
||||
if is {
|
||||
return cs.Cause()
|
||||
}
|
||||
e = Unwrap(e)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func UnwrapCauses (err error) []error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if al,is := err.(Causers); is {
|
||||
return al.Causes()
|
||||
}
|
||||
var l ErrorList
|
||||
e := err
|
||||
for e!= nil {
|
||||
l = append(l,e)
|
||||
e = Unwrap(e)
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
|
||||
func UnwrapList(err error) ErrorList {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if al,is := err.(UnwrapperList); is {
|
||||
return al.Unwrap()
|
||||
}
|
||||
var l ErrorList
|
||||
e := err
|
||||
for e!= nil {
|
||||
l = append(l,e)
|
||||
e = Unwrap(e)
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
|
||||
func UnwrapTrace(err error) stack.Trace {
|
||||
return stack.UnwrapTrace(err,false) }
|
||||
|
||||
|
||||
|
||||
func Wrap (err, cause error) error {
|
||||
if err == nil {
|
||||
return cause
|
||||
}
|
||||
if cause == nil {
|
||||
return err
|
||||
}
|
||||
return *wrap.WithCause(err, cause)
|
||||
}
|
||||
|
||||
@ -0,0 +1,41 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name causer.go
|
||||
// Created by DEV
|
||||
// Modified 20/03/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
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
Causer interface {
|
||||
|
||||
Cause () error
|
||||
}
|
||||
|
||||
CauseFnc func () error
|
||||
)
|
||||
|
||||
|
||||
func (f CauseFnc) Cause() error {
|
||||
return f() }
|
||||
|
||||
@ -0,0 +1,41 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name causer.go
|
||||
// Created by DEV
|
||||
// Modified 20/03/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
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
Causers interface {
|
||||
|
||||
Causes () []error
|
||||
}
|
||||
|
||||
CausesFnc func () []error
|
||||
)
|
||||
|
||||
|
||||
func (f CausesFnc) Causes() []error {
|
||||
return f() }
|
||||
|
||||
@ -0,0 +1,185 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File Name active.go
|
||||
// Created by DEV
|
||||
// Modified 20/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 erdata
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
const (
|
||||
|
||||
FieldNameError string = "error"
|
||||
FieldNameCode string = "code"
|
||||
FieldNameType string = "type"
|
||||
FieldNameTypes string = "types"
|
||||
FieldNameFields string = "fields"
|
||||
FieldNameCause string = "cause"
|
||||
FieldNameCauses string = "causes"
|
||||
FieldNameTrace string = "trace"
|
||||
)
|
||||
|
||||
const(
|
||||
|
||||
fieldNameMessage string = "message"
|
||||
fieldNameInfo string = "info"
|
||||
fieldNameDate string = "date"
|
||||
fieldNameTime string = "time"
|
||||
fieldNameParam string = "param"
|
||||
fieldNameSource string = "source"
|
||||
fieldNameUser string = "user"
|
||||
)
|
||||
|
||||
|
||||
const (
|
||||
|
||||
fmtNameDuplicated string = "%s-%d"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
g = struct {
|
||||
mux sync.RWMutex
|
||||
typeCollectors map[reflect.Type]ErrorCollector
|
||||
defaultCollectors FieldCollectors
|
||||
|
||||
}{
|
||||
|
||||
typeCollectors : make(map[reflect.Type]ErrorCollector),
|
||||
defaultCollectors: defaultCollectors(),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
func getTypeCollector (rtype reflect.Type) ErrorCollector {
|
||||
ec,_ := g.typeCollectors[rtype]
|
||||
return ec
|
||||
}
|
||||
|
||||
|
||||
func CollectErrorFields (collector Collector, err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
rt := reflect.TypeOf(err)
|
||||
if rt.Kind() == reflect.Pointer {
|
||||
rt = rt.Elem()
|
||||
}
|
||||
|
||||
if ec,is := err.(Collectable); is {
|
||||
ec.Collect(collector)
|
||||
return
|
||||
}
|
||||
|
||||
g.mux.RLock()
|
||||
eb,_ := g.typeCollectors[rt]
|
||||
g.mux.RUnlock()
|
||||
if eb != nil {
|
||||
eb.CollectError(collector,err)
|
||||
return
|
||||
}
|
||||
|
||||
var nc FieldCollectors
|
||||
for _,f := range g.defaultCollectors {
|
||||
if f.CollectField(collector,err) {
|
||||
nc = append(nc, f)
|
||||
}
|
||||
}
|
||||
|
||||
g.mux.Lock()
|
||||
g.typeCollectors[rt]=nc
|
||||
g.mux.Unlock()
|
||||
}
|
||||
|
||||
|
||||
|
||||
func defaultCollectors () FieldCollectors {
|
||||
return []FieldCollector{
|
||||
|
||||
CollectFieldFnc(func (b Collector, e error ) bool {
|
||||
if ms,is := e.(interface { Message() string }); is {
|
||||
b.AddFieldString(fieldNameMessage, ms.Message())
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}),
|
||||
|
||||
CollectFieldFnc(func (b Collector, e error ) bool {
|
||||
if in,is := e.(interface { Info () string }); is {
|
||||
b.AddFieldString(fieldNameInfo,in.Info())
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}),
|
||||
|
||||
CollectFieldFnc(func (b Collector, e error ) bool {
|
||||
if tm,is := e.(interface { Time() time.Time }); is {
|
||||
b.AddFieldTime(fieldNameTime, tm.Time())
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}),
|
||||
|
||||
CollectFieldFnc(func (b Collector, e error ) bool {
|
||||
if tm,is := e.(interface { Date() time.Time }); is {
|
||||
b.AddFieldDate(fieldNameDate, tm.Date())
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}),
|
||||
|
||||
CollectFieldFnc(func (b Collector, e error ) bool {
|
||||
if tm,is := e.(interface { Param() string }); is {
|
||||
b.AddFieldString(fieldNameParam, tm.Param())
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}),
|
||||
|
||||
CollectFieldFnc(func (b Collector, e error ) bool {
|
||||
if tm,is := e.(interface { User() string }); is {
|
||||
b.AddFieldString(fieldNameUser, tm.User())
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}),
|
||||
|
||||
CollectFieldFnc(func (b Collector, e error ) bool {
|
||||
if tm,is := e.(interface { Source() string }); is {
|
||||
b.AddFieldString(fieldNameSource, tm.Source())
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}),
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,31 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File Name collectable.go
|
||||
// Created by DEV
|
||||
// Modified 20/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 erdata
|
||||
|
||||
type (
|
||||
|
||||
Collectable interface {
|
||||
Collect (collector Collector)
|
||||
}
|
||||
)
|
||||
@ -0,0 +1,41 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File Name collector.go
|
||||
// Created by DEV
|
||||
// Modified 20/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 erdata
|
||||
|
||||
import "time"
|
||||
|
||||
type (
|
||||
|
||||
Collector interface {
|
||||
AddFieldAny (name string, value any )
|
||||
AddFieldInt (name string, value int )
|
||||
AddFieldBool (name string, value bool )
|
||||
AddFieldString(name string, value string )
|
||||
AddFieldFloat (name string, value float64)
|
||||
AddFieldTime (name string, value time.Time)
|
||||
AddFieldDate (name string, value time.Time)
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@ -0,0 +1,42 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File Name error_collectable.go
|
||||
// Created by DEV
|
||||
// Modified 20/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 erdata
|
||||
|
||||
type (
|
||||
|
||||
ErrorCollector interface {
|
||||
CollectError (collector Collector, err error)
|
||||
}
|
||||
|
||||
|
||||
CollectErrorFnc func(collector Collector, err error)
|
||||
)
|
||||
|
||||
|
||||
func (f CollectErrorFnc) CollectError (collector Collector, err error) {
|
||||
f(collector,err) }
|
||||
|
||||
|
||||
|
||||
func NopCollectError (_ Collector, _ error) {}
|
||||
@ -0,0 +1,137 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File Name error_data.go
|
||||
// Created by DEV
|
||||
// Modified 20/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 erdata
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"errs/stack"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
|
||||
ErrorData struct {
|
||||
Err error
|
||||
ErrStr string
|
||||
Types []reflect.Type
|
||||
TypesStr []string
|
||||
Fields Fields
|
||||
|
||||
Causes []ErrorData
|
||||
Trace stack.Trace
|
||||
|
||||
Invalid error
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
func NewErrorData(err error) *ErrorData {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return &ErrorData{ Err: err }
|
||||
}
|
||||
|
||||
|
||||
func (c *ErrorData) Collect (addFields,addCauses,addTrace bool) *ErrorData {
|
||||
c.collect(addFields,addCauses,addTrace)
|
||||
return c
|
||||
}
|
||||
|
||||
|
||||
func (c *ErrorData) collect (addFields,addCauses,addTrace bool) *ErrorData {
|
||||
d := false
|
||||
ne := c.Err
|
||||
|
||||
defer func(ec *ErrorData, dn *bool){
|
||||
if !(*dn) {
|
||||
if rc := recover(); rc != nil {
|
||||
if er,is := rc.(error); is {
|
||||
ec.Invalid = er
|
||||
} else {
|
||||
ec.Invalid = fmt.Errorf("%#v",rc)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}(c,&d)
|
||||
|
||||
c.ErrStr = c.Err.Error()
|
||||
|
||||
for ne != nil {
|
||||
rt := reflect.TypeOf(ne)
|
||||
c.Types = append(c.Types,rt)
|
||||
c.TypesStr = append(c.TypesStr,rt.String())
|
||||
|
||||
// Unwrap Fields
|
||||
if addFields {
|
||||
CollectErrorFields(&c.Fields,ne)
|
||||
}
|
||||
|
||||
// Unwrap Causes
|
||||
if addCauses {
|
||||
if cs, is := ne.(interface{ Cause() error }); is {
|
||||
if er := cs.Cause(); er != nil {
|
||||
c.Causes = append(c.Causes, *NewErrorData(er).Collect(addFields,addCauses,false))
|
||||
}
|
||||
} else {
|
||||
var ls []error
|
||||
if cs, is := ne.(interface{ Causes() []error }); is {
|
||||
ls = cs.Causes()
|
||||
} else if cs, is := ne.(interface{ Unwrap() []error }); is {
|
||||
ls = cs.Unwrap()
|
||||
}
|
||||
for _, er := range ls {
|
||||
if ed := NewErrorData(er); ed != nil {
|
||||
c.Causes = append(c.Causes, *ed.Collect(addFields,addCauses,false))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Unwrap Trace
|
||||
if addTrace {
|
||||
if tc, is := ne.(stack.Tracer); is {
|
||||
c.Trace = tc.StackTrace()
|
||||
addTrace = c.Trace == nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// next error
|
||||
ne = errors.Unwrap(ne)
|
||||
}
|
||||
d = true
|
||||
return c
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,41 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File Name field_collector.go
|
||||
// Created by DEV
|
||||
// Modified 20/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 erdata
|
||||
|
||||
type (
|
||||
|
||||
FieldCollector interface {
|
||||
|
||||
CollectField (collector Collector, err error) bool
|
||||
|
||||
}
|
||||
|
||||
|
||||
CollectFieldFnc func(collector Collector, err error) bool
|
||||
)
|
||||
|
||||
|
||||
func (f CollectFieldFnc) CollectField (collector Collector, err error) bool {
|
||||
return f(collector,err) }
|
||||
|
||||
@ -0,0 +1,51 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File Name field_collectors.go
|
||||
// Created by DEV
|
||||
// Modified 20/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 erdata
|
||||
|
||||
type (
|
||||
|
||||
FieldCollectors []FieldCollector
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func (c FieldCollectors) CollectError (collector Collector, err error) {
|
||||
for _,f := range c {
|
||||
f.CollectField(collector,err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (c FieldCollectors) CollectField (collector Collector, err error) bool {
|
||||
r := true
|
||||
for _,fc := range c {
|
||||
r = r && fc.CollectField(collector,err)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,36 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File Name field_head.go
|
||||
// Created by DEV
|
||||
// Modified 20/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 erdata
|
||||
|
||||
|
||||
type (
|
||||
|
||||
FieldHead struct {
|
||||
Name string
|
||||
Kind FieldKind
|
||||
Pos int
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@ -0,0 +1,45 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File Name field_kind.go
|
||||
// Created by DEV
|
||||
// Modified 20/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 erdata
|
||||
|
||||
|
||||
const (
|
||||
|
||||
FieldKindAny FieldKind = iota
|
||||
FieldKindString
|
||||
FieldKindInt
|
||||
FieldKindFloat
|
||||
FieldKindBool
|
||||
FieldKindDate
|
||||
FieldKindTime
|
||||
|
||||
)
|
||||
|
||||
|
||||
type (
|
||||
|
||||
FieldKind uint8
|
||||
)
|
||||
|
||||
|
||||
@ -0,0 +1,158 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File Name builder.go
|
||||
// Created by DEV
|
||||
// Modified 20/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 erdata
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
Fields struct {
|
||||
heads []*FieldHead
|
||||
|
||||
anys []any
|
||||
strings []string
|
||||
nums []int
|
||||
floats []float64
|
||||
dates []time.Time
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
func (f *Fields) Len() int {
|
||||
return len(f.heads) }
|
||||
|
||||
|
||||
func (f *Fields) exists(name string) bool {
|
||||
for _,h := range f.heads {
|
||||
if h.Name == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (f *Fields) AddFieldAny (name string, value any) {
|
||||
f.addField(name, FieldKindAny).anys = append(f.anys,value) }
|
||||
|
||||
func (f *Fields) AddFieldString(name string, value string) {
|
||||
f.addField(name, FieldKindString)
|
||||
f.strings = append(f.strings, value)
|
||||
}
|
||||
|
||||
func (f *Fields) AddFieldInt (name string, value int) {
|
||||
f.addField(name, FieldKindInt).nums = append(f.nums, value) }
|
||||
|
||||
func (f *Fields) AddFieldBool (name string, value bool) {
|
||||
bv := 0
|
||||
if value {
|
||||
bv = 1
|
||||
}
|
||||
f.addField(name, FieldKindInt).nums = append(f.nums, bv)
|
||||
}
|
||||
|
||||
func (f *Fields) AddFieldFloat (name string, value float64) {
|
||||
f.addField(name, FieldKindFloat).floats = append(f.floats, value) }
|
||||
|
||||
func (f *Fields) AddFieldTime (name string, value time.Time) {
|
||||
f.addField(name, FieldKindTime).dates = append(f.dates, value) }
|
||||
|
||||
func (f *Fields) AddFieldDate (name string, value time.Time) {
|
||||
f.addField(name, FieldKindDate).dates = append(f.dates, value) }
|
||||
|
||||
|
||||
|
||||
|
||||
func (f *Fields) addField (name string, kind FieldKind) *Fields {
|
||||
for i:=1; f.exists(name); i++ {
|
||||
name = fmt.Sprintf(fmtNameDuplicated,name,i)
|
||||
}
|
||||
var pos int
|
||||
switch kind {
|
||||
case FieldKindBool,
|
||||
FieldKindInt : pos = len(f.nums)
|
||||
case FieldKindString: pos = len(f.strings)
|
||||
case FieldKindDate,
|
||||
FieldKindTime : pos = len(f.dates)
|
||||
case FieldKindFloat : pos = len(f.floats)
|
||||
case FieldKindAny : pos = len(f.anys)
|
||||
}
|
||||
f.heads = append(f.heads, &FieldHead{
|
||||
Name: name,
|
||||
Kind: kind,
|
||||
Pos: pos,
|
||||
})
|
||||
return f
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (f *Fields) GetHeads () []*FieldHead {
|
||||
return f.heads }
|
||||
|
||||
|
||||
func (f *Fields) GetField (name string) (*FieldHead,bool) {
|
||||
for i,h := range f.heads {
|
||||
if h.Name == name {
|
||||
return f.heads[i],true
|
||||
}
|
||||
}
|
||||
return nil,false
|
||||
}
|
||||
|
||||
func (f *Fields) GetFieldValueAny (head *FieldHead) any {
|
||||
return f.anys[head.Pos] }
|
||||
|
||||
func (f *Fields) GetFieldValueString (head *FieldHead) string {
|
||||
return f.strings[head.Pos] }
|
||||
|
||||
func (f *Fields) GetFieldValueInt (head *FieldHead) int {
|
||||
return f.nums[head.Pos] }
|
||||
|
||||
func (f *Fields) GetFieldValueFloat (head *FieldHead) float64 {
|
||||
return f.floats[head.Pos] }
|
||||
|
||||
func (f *Fields) GetFieldValueBool (head *FieldHead) bool {
|
||||
return (f.nums[head.Pos]) > 0 }
|
||||
|
||||
func (f *Fields) GetFieldValueDate (head *FieldHead) time.Time {
|
||||
return f.dates[head.Pos] }
|
||||
|
||||
func (f *Fields) GetFieldValueTime (head *FieldHead) time.Time {
|
||||
return f.dates[head.Pos] }
|
||||
|
||||
|
||||
func (f *Fields) Sort () {
|
||||
sort.Slice(f.heads, func(i, j int) bool {
|
||||
return f.heads[i].Name < f.heads[j].Name
|
||||
})
|
||||
}
|
||||
@ -0,0 +1,43 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name active.go
|
||||
// Created by DEV
|
||||
// Modified 20/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 erjson
|
||||
|
||||
import "sync"
|
||||
|
||||
var (
|
||||
|
||||
g = struct {
|
||||
buildDefault sync.Once
|
||||
formatter *JSONEncoder
|
||||
}{}
|
||||
|
||||
)
|
||||
|
||||
|
||||
func DefaultEncoder() *JSONEncoder {
|
||||
g.buildDefault.Do(func() {
|
||||
g.formatter = NewJSONEncoder()
|
||||
})
|
||||
return g.formatter
|
||||
}
|
||||
@ -0,0 +1,390 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name text_builder.go
|
||||
// Created by DEV
|
||||
// Modified 20/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 erjson
|
||||
|
||||
import (
|
||||
"bfrs"
|
||||
"cdcs/cjson"
|
||||
"encoding/json"
|
||||
"errs/erdata"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
||||
type (
|
||||
|
||||
jsonBuilder struct {
|
||||
f *JSONEncoder
|
||||
b *bfrs.ABuffer
|
||||
encoder *json.Encoder
|
||||
sep string
|
||||
pretty bool
|
||||
invalid error
|
||||
}
|
||||
|
||||
)
|
||||
|
||||
|
||||
func newJsonBuilder (f *JSONEncoder) *jsonBuilder {
|
||||
sep := ":"
|
||||
if f.HasUseSpace() && f.HasShowPretty() {
|
||||
sep = ": "
|
||||
}
|
||||
return &jsonBuilder{
|
||||
f : f,
|
||||
b : bfrs.NewABuffer(),
|
||||
pretty : f.HasShowPretty(),
|
||||
sep : sep,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (b *jsonBuilder) format (err error) {
|
||||
cl := erdata.NewErrorData(err)
|
||||
cl.Collect(b.f.HasAddFields(),b.f.HasAddCauses(),b.f.HasAddTrace())
|
||||
|
||||
if cl.Invalid != nil {
|
||||
b.writeFormatError(cl.Invalid)
|
||||
return
|
||||
}
|
||||
|
||||
b.write(cl,false, 0)
|
||||
}
|
||||
|
||||
|
||||
func (b *jsonBuilder) write (coll *erdata.ErrorData, cont bool, level int) {
|
||||
if b.invalid != nil {
|
||||
return
|
||||
}
|
||||
|
||||
d := false
|
||||
defer func(dn *bool, bl *jsonBuilder){
|
||||
if !(*dn) {
|
||||
if rc := recover(); rc != nil {
|
||||
if er,is := rc.(error); is {
|
||||
bl.invalid = er
|
||||
} else {
|
||||
bl.invalid = fmt.Errorf("%#v",rc)
|
||||
}
|
||||
bl.writeFormatError(bl.invalid)
|
||||
}
|
||||
}
|
||||
}(&d,b)
|
||||
|
||||
|
||||
var (
|
||||
indLvl,
|
||||
indObj,
|
||||
ind2Obj string
|
||||
)
|
||||
|
||||
if b.pretty {
|
||||
if level > 0 {
|
||||
indLvl = strings.Repeat(b.f.layout.Indent, 1+level)
|
||||
indObj = strings.Repeat(b.f.layout.Indent, 2+level)
|
||||
ind2Obj = strings.Repeat(b.f.layout.Indent, 3+level)
|
||||
} else {
|
||||
indObj = b.f.layout.Indent
|
||||
ind2Obj = strings.Repeat(b.f.layout.Indent,2)
|
||||
}
|
||||
if !cont {
|
||||
b.b.WriteString(indLvl)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
b.b.WriteByte('{')
|
||||
if b.pretty {
|
||||
b.b.WriteByte('\n')
|
||||
}
|
||||
b.writeField(erdata.FieldNameError,coll.ErrStr, indObj,true,false, false)
|
||||
|
||||
|
||||
if b.f.HasAddTypes() {
|
||||
types := coll.Types
|
||||
|
||||
b.b.WriteByte(',')
|
||||
if b.pretty {
|
||||
b.b.WriteByte('\n')
|
||||
}
|
||||
|
||||
if len(types) == 1 {
|
||||
b.writeField(erdata.FieldNameType, types[0].String(), indObj, true, false,false)
|
||||
} else {
|
||||
|
||||
b.writeOpen(erdata.FieldNameTypes, indObj, '[', b.pretty)
|
||||
for i, t := range coll.TypesStr {
|
||||
if b.pretty {
|
||||
b.b.WriteString(ind2Obj)
|
||||
}
|
||||
b.b.WriteByte('"')
|
||||
b.b.WriteString(t)
|
||||
b.b.WriteByte('"')
|
||||
if i < len(coll.Types)-1 {
|
||||
b.b.WriteByte(',')
|
||||
}
|
||||
if b.pretty {
|
||||
b.b.WriteByte('\n')
|
||||
}
|
||||
}
|
||||
|
||||
b.writeClose(indObj, ']')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
heads := coll.Fields.GetHeads()
|
||||
if len(heads) > 0 {
|
||||
|
||||
b.b.WriteByte(',')
|
||||
if b.pretty {
|
||||
b.b.WriteByte('\n')
|
||||
}
|
||||
|
||||
coll.Fields.Sort()
|
||||
b.writeOpen(erdata.FieldNameFields, indObj, '{', b.pretty)
|
||||
var n = 0
|
||||
|
||||
for _, h := range heads {
|
||||
p := b.b.Len()
|
||||
|
||||
if n > 0 {
|
||||
b.b.WriteByte(',')
|
||||
if b.pretty {
|
||||
b.b.WriteByte('\n')
|
||||
}
|
||||
}
|
||||
|
||||
b.writeFieldName(h.Name,ind2Obj)
|
||||
|
||||
if !b.writeFieldValue(coll.Fields, h,level) {
|
||||
b.b.Truncate(p)
|
||||
continue
|
||||
}
|
||||
n++
|
||||
}
|
||||
if n > 0 && b.pretty {
|
||||
b.b.WriteByte('\n')
|
||||
}
|
||||
b.writeClose(indObj,'}')
|
||||
}
|
||||
|
||||
causes := coll.Causes
|
||||
if len (causes) > 0 {
|
||||
|
||||
b.b.WriteByte(',')
|
||||
if b.pretty {
|
||||
b.b.WriteByte('\n')
|
||||
}
|
||||
|
||||
if len(causes) == 1 {
|
||||
b.writeOpen(erdata.FieldNameCause, indObj, 0, false)
|
||||
b.write(&causes[0], true,level+1)
|
||||
} else if len(causes) > 1 {
|
||||
b.writeOpen(erdata.FieldNameCauses, indObj, '[', b.pretty)
|
||||
for i, ce := range causes {
|
||||
if i > 0 {
|
||||
b.b.WriteByte(',')
|
||||
if b.pretty {
|
||||
b.b.WriteByte('\n')
|
||||
}
|
||||
}
|
||||
b.write(&ce, false,level+1)
|
||||
}
|
||||
if b.pretty {
|
||||
b.b.WriteByte('\n')
|
||||
}
|
||||
b.writeClose(indObj, ']')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if coll.Trace != nil {
|
||||
p := b.b.Len()
|
||||
|
||||
b.b.WriteByte(',')
|
||||
if b.pretty {
|
||||
b.b.WriteByte('\n')
|
||||
}
|
||||
b.writeFieldName(erdata.FieldNameTrace,indObj)
|
||||
|
||||
if b.encoder == nil {
|
||||
b.encoder = json.NewEncoder(b.b)
|
||||
}
|
||||
if b.pretty {
|
||||
b.encoder.SetIndent(strings.Repeat(b.f.layout.Indent,level+1),b.f.layout.Indent)
|
||||
}
|
||||
|
||||
if er := b.encoder.Encode(coll.Trace.Frames()); er != nil {
|
||||
b.b.Truncate(p)
|
||||
}
|
||||
}
|
||||
|
||||
if b.pretty {
|
||||
b.b.WriteByte('\n')
|
||||
}
|
||||
b.writeClose(indLvl,'}')
|
||||
|
||||
d= true
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (b *jsonBuilder) writeOpen(name, indent string, object byte,nl bool) {
|
||||
b.writeFieldName(name,indent)
|
||||
if object != 0 {
|
||||
b.b.WriteByte(object)
|
||||
}
|
||||
if nl {
|
||||
b.b.WriteByte('\n')
|
||||
}
|
||||
}
|
||||
|
||||
func (b *jsonBuilder) writeClose(indent string, object byte) {
|
||||
if b.pretty {
|
||||
b.b.WriteString(indent)
|
||||
}
|
||||
b.b.WriteByte(object)
|
||||
}
|
||||
|
||||
func (b *jsonBuilder) writeFieldName (name, indent string) {
|
||||
if b.pretty {
|
||||
b.b.WriteString(indent)
|
||||
}
|
||||
cjson.WriteString(b.b,name,true)
|
||||
b.b.WriteString(b.sep)
|
||||
}
|
||||
|
||||
func (b *jsonBuilder) writeField (name, value, indent string, quote, coma, nl bool) {
|
||||
b.writeFieldName(name,indent)
|
||||
if quote {
|
||||
b.b.WriteByte('"')
|
||||
}
|
||||
b.b.WriteString(value)
|
||||
if quote {
|
||||
b.b.WriteByte('"')
|
||||
}
|
||||
if coma {
|
||||
b.b.WriteByte(',')
|
||||
}
|
||||
if b.pretty && nl {
|
||||
b.b.WriteByte('\n')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (b *jsonBuilder) writeFieldValue (fields erdata.Fields, field *erdata.FieldHead, level int) bool {
|
||||
switch field.Kind {
|
||||
case erdata.FieldKindFloat:
|
||||
n := fields.GetFieldValueFloat(field)
|
||||
if n == 0 && b.f.HasIgnoreDefaults() {
|
||||
|
||||
return false
|
||||
}
|
||||
cjson.WriteFloat(b.b,n,64,b.f.HasUseQuote())
|
||||
return true
|
||||
|
||||
case erdata.FieldKindInt:
|
||||
n := fields.GetFieldValueInt(field)
|
||||
if n == 0 && b.f.HasIgnoreDefaults() {
|
||||
return false
|
||||
}
|
||||
cjson.WriteInt(b.b,int64(n),b.f.HasUseQuote())
|
||||
return true
|
||||
|
||||
case erdata.FieldKindString:
|
||||
s := fields.GetFieldValueString(field)
|
||||
if len(s) == 0 && b.f.HasIgnoreDefaults() {
|
||||
return false
|
||||
}
|
||||
cjson.WriteString(b.b,s,b.f.HasEscapeHTML())
|
||||
return true
|
||||
|
||||
case erdata.FieldKindTime:
|
||||
d := fields.GetFieldValueDate(field)
|
||||
if d.IsZero() && b.f.HasIgnoreDefaults(){
|
||||
return false
|
||||
}
|
||||
cjson.WriteDate(b.b,d,b.f.layout.TimeLayout,true)
|
||||
return true
|
||||
|
||||
case erdata.FieldKindDate:
|
||||
d := fields.GetFieldValueDate(field)
|
||||
if d.IsZero() && b.f.HasIgnoreDefaults(){
|
||||
return false
|
||||
}
|
||||
cjson.WriteDate(b.b,d,b.f.layout.DateLayout,true)
|
||||
return true
|
||||
|
||||
case erdata.FieldKindBool:
|
||||
t := fields.GetFieldValueBool(field)
|
||||
if !t && b.f.HasIgnoreDefaults() {
|
||||
return false
|
||||
}
|
||||
cjson.WriteBool(b.b,t,b.f.HasUseQuote())
|
||||
return true
|
||||
|
||||
default:
|
||||
a := fields.GetFieldValueAny(field)
|
||||
if a == nil {
|
||||
if b.f.HasIgnoreDefaults() {
|
||||
return false
|
||||
}
|
||||
b.b.WOQ(true,'"',cjson.G.NullValue)
|
||||
return true
|
||||
}
|
||||
|
||||
if b.encoder == nil {
|
||||
b.encoder = json.NewEncoder(b.b)
|
||||
b.encoder.SetEscapeHTML(b.f.HasEscapeHTML())
|
||||
}
|
||||
|
||||
if b.pretty {
|
||||
b.encoder.SetIndent(strings.Repeat(b.f.layout.Indent,level+3),b.f.layout.Indent)
|
||||
}
|
||||
if er := b.encoder.Encode(a); er != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (b *jsonBuilder) writeFormatError (err error) {
|
||||
b.b.Reset()
|
||||
b.b.WriteByte('{')
|
||||
b.writeFieldName("json-formatter-error","")
|
||||
cjson.WriteString(b.b,err.Error(),b.f.HasEscapeHTML())
|
||||
b.b.WriteByte('}')
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (b *jsonBuilder) reset () {
|
||||
b.b.Reset()
|
||||
b.encoder = nil
|
||||
b.invalid = nil
|
||||
}
|
||||
@ -0,0 +1,75 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name json_formatter.go
|
||||
// Created by DEV
|
||||
// Modified 20/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 erjson
|
||||
|
||||
type (
|
||||
|
||||
JSONEncoder struct {
|
||||
Option
|
||||
layout Layout
|
||||
}
|
||||
|
||||
|
||||
JSONEncoderFnc func (f *JSONEncoder)
|
||||
)
|
||||
|
||||
|
||||
func NewJSONEncoder (options ...JSONEncoderFnc) *JSONEncoder {
|
||||
f := &JSONEncoder{
|
||||
Option: ShowPretty | UseSpace | AddTypes | AddFields | AddCauses | AddTrace | IgnoreDefaults,
|
||||
layout: *newLayout(),
|
||||
}
|
||||
for _,o := range options {
|
||||
o(f)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
|
||||
func (f *JSONEncoder) EncodeError(err error) []byte {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
b := newJsonBuilder(f)
|
||||
defer func(bl *jsonBuilder){
|
||||
bl.reset()
|
||||
}(b)
|
||||
|
||||
b.format(err)
|
||||
return b.b.Bytes()
|
||||
}
|
||||
|
||||
|
||||
|
||||
func WithLayout (config LayoutFnc) JSONEncoderFnc {
|
||||
return func(f *JSONEncoder) {
|
||||
config(&f.layout) } }
|
||||
|
||||
func WithOptions (set, unset Option) JSONEncoderFnc {
|
||||
return func(f *JSONEncoder) {
|
||||
f.Option = f.Option.Add(set)
|
||||
f.Option = f.Option.Del(unset)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name Layout.go
|
||||
// Created by DEV
|
||||
// Modified 13/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 erjson
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
Layout struct {
|
||||
Indent string
|
||||
TimeLayout string
|
||||
DateLayout string
|
||||
}
|
||||
|
||||
LayoutFnc func (layouts *Layout)
|
||||
)
|
||||
|
||||
|
||||
|
||||
func newLayout () *Layout {
|
||||
return &Layout{
|
||||
Indent: " ",
|
||||
DateLayout: time.RFC3339,
|
||||
TimeLayout: time.TimeOnly,
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,86 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name option.go
|
||||
// Created by DEV
|
||||
// Modified 21/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 erjson
|
||||
|
||||
|
||||
|
||||
|
||||
const (
|
||||
|
||||
AddTypes Option = 1 << iota
|
||||
AddFields
|
||||
AddCauses
|
||||
AddTrace
|
||||
ShowPretty
|
||||
UseSpace
|
||||
UseQuote
|
||||
IgnoreDefaults
|
||||
SortFields
|
||||
EscapeHTML
|
||||
)
|
||||
|
||||
|
||||
type (
|
||||
|
||||
Option uint16
|
||||
)
|
||||
|
||||
func (o Option) Add (opt Option) Option {
|
||||
return o | opt
|
||||
}
|
||||
|
||||
func (o Option) Del (opt Option) Option {
|
||||
return o &^ opt }
|
||||
|
||||
|
||||
func (o Option) HasAddTypes() bool {
|
||||
return o&AddTypes != 0 }
|
||||
|
||||
func (o Option) HasAddFields() bool {
|
||||
return o&AddFields != 0 }
|
||||
|
||||
func (o Option) HasAddCauses() bool {
|
||||
return o&AddCauses != 0 }
|
||||
|
||||
func (o Option) HasAddTrace() bool {
|
||||
return o&AddTrace != 0 }
|
||||
|
||||
func (o Option) HasShowPretty() bool {
|
||||
return o&ShowPretty != 0 }
|
||||
|
||||
func (o Option) HasUseSpace() bool {
|
||||
return o&UseSpace != 0 }
|
||||
|
||||
func (o Option) HasUseQuote() bool {
|
||||
return o&UseQuote != 0 }
|
||||
|
||||
func (o Option) HasSortFields() bool {
|
||||
return o&SortFields != 0 }
|
||||
|
||||
func (o Option) HasIgnoreDefaults() bool {
|
||||
return o&IgnoreDefaults != 0 }
|
||||
|
||||
func (o Option) HasEscapeHTML() bool {
|
||||
return o&EscapeHTML != 0 }
|
||||
|
||||
@ -0,0 +1,59 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name err_multi.go
|
||||
// Created by DEV
|
||||
// Modified 08/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
|
||||
|
||||
type (
|
||||
|
||||
ErrMulti struct {
|
||||
ErrorList
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
func NewErrMulti (err ...error) *ErrMulti {
|
||||
return &ErrMulti{ ErrorList : ErrorList(err).Clean() } }
|
||||
|
||||
func NewErrMultiIf (errors []error) error {
|
||||
if len(errors) == 0 {
|
||||
return nil
|
||||
}
|
||||
return &ErrMulti {errors }
|
||||
}
|
||||
|
||||
func (e ErrMulti) Code () string {
|
||||
return CodeMultiError }
|
||||
|
||||
|
||||
func (e ErrMulti) Error () string {
|
||||
return CodeMultiError }
|
||||
|
||||
|
||||
func (e ErrMulti) Unwrap() []error {
|
||||
return e.ErrorList }
|
||||
|
||||
func (e ErrMulti) Causes() []error {
|
||||
return e.ErrorList }
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,50 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name err_panic.go
|
||||
// Created by DEV
|
||||
// Modified 30/03/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
|
||||
|
||||
type (
|
||||
|
||||
ErrPanic struct {
|
||||
cause error
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
func NewErrPanic (cause error) *ErrPanic {
|
||||
return &ErrPanic{ cause: cause }
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (e ErrPanic) Error () string {
|
||||
return CodePanicError }
|
||||
|
||||
|
||||
|
||||
func (e ErrPanic) Unwrap () error {
|
||||
return nil }
|
||||
|
||||
func (e ErrPanic) Cause () error {
|
||||
return e.cause }
|
||||
|
||||
@ -0,0 +1,67 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name err_param.go
|
||||
// Created by DEV
|
||||
// Modified 04/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 (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
ErrParam struct {
|
||||
code string
|
||||
param string
|
||||
message string
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
func NewErrParam (param string, msgf string, params ...any) *ErrParam {
|
||||
return &ErrParam {
|
||||
code : CodeParamError,
|
||||
param : param,
|
||||
message: fmt.Sprintf(msgf,params...) }}
|
||||
|
||||
|
||||
func NewErrNilParam (param string, msgf string, params ...any) *ErrParam {
|
||||
return &ErrParam {
|
||||
code : CodeParamNilError,
|
||||
param : param,
|
||||
message : fmt.Sprintf(msgf,params...) }}
|
||||
|
||||
|
||||
|
||||
func (e ErrParam) Code () string {
|
||||
return e.code }
|
||||
|
||||
func (e ErrParam) Error() string {
|
||||
return e.code }
|
||||
|
||||
func (e ErrParam) Message () string {
|
||||
return e.message }
|
||||
|
||||
func (e ErrParam) Param () string {
|
||||
return e.param }
|
||||
|
||||
|
||||
@ -0,0 +1,39 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name ErrorEncoder.go
|
||||
// Created by DEV
|
||||
// Modified 15/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
|
||||
|
||||
|
||||
type (
|
||||
|
||||
ErrorEncoder interface {
|
||||
EncodeError(err error) []byte
|
||||
}
|
||||
|
||||
|
||||
EncodeErrorFnc func (err error) []byte
|
||||
)
|
||||
|
||||
|
||||
func (f EncodeErrorFnc) EncodeError(err error) []byte {
|
||||
return f(err) }
|
||||
@ -0,0 +1,45 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name error_handler.go
|
||||
// Created by DEV
|
||||
// Modified 19/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
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
ErrorHandler interface {
|
||||
|
||||
HandleError (err error)
|
||||
|
||||
}
|
||||
|
||||
|
||||
HandleErrorFnc func (err error)
|
||||
)
|
||||
|
||||
|
||||
func (f HandleErrorFnc) HandleError (err error) {
|
||||
f(err) }
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,112 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name error_list.go
|
||||
// Created by DEV
|
||||
// Modified 21/03/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
|
||||
|
||||
|
||||
type (
|
||||
|
||||
ErrorList []error
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
func (l ErrorList) Any () bool {
|
||||
for _,e := range l {
|
||||
if e != nil {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
func (l ErrorList) Len () int {
|
||||
return len(l) }
|
||||
|
||||
|
||||
func (l ErrorList) Count () int {
|
||||
c:=0
|
||||
for _,e := range l {
|
||||
if e != nil {
|
||||
c++
|
||||
}
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
|
||||
func (l ErrorList) First () error {
|
||||
for _,e := range l {
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l ErrorList) Last () error {
|
||||
for i := len(l) - 1; i >= 0; i-- {
|
||||
if l[i] != nil {
|
||||
return l[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l ErrorList) Append(err error) ErrorList {
|
||||
return append(l,err) }
|
||||
|
||||
func (l ErrorList) AppendNotNil (err error) ErrorList {
|
||||
if err != nil {
|
||||
return append(l,err)
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
|
||||
func (l ErrorList) Reset () ErrorList {
|
||||
return l[:0] }
|
||||
|
||||
|
||||
|
||||
// Clean removes any nil errors from the list.
|
||||
func (l ErrorList) Clean() ErrorList {
|
||||
return l.filter(func(e error) bool {
|
||||
return e != nil }) }
|
||||
|
||||
|
||||
func (l ErrorList) filter(filter func(error) bool) ErrorList {
|
||||
rs := make([]error, 0, len(l))
|
||||
for _, e := range l {
|
||||
if filter(e) {
|
||||
rs = append(rs, e)
|
||||
}
|
||||
}
|
||||
if len(rs) == 0 {
|
||||
return nil
|
||||
}
|
||||
return rs
|
||||
}
|
||||
@ -0,0 +1,154 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name active.go
|
||||
// Created by DEV
|
||||
// Modified 22/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 ertext
|
||||
|
||||
import (
|
||||
"ansi"
|
||||
"atypes/astrings"
|
||||
"errs/stack"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
|
||||
var (
|
||||
|
||||
g = struct {
|
||||
nullValue []byte
|
||||
buildDefault sync.Once
|
||||
formatter *TextEncoder
|
||||
}{
|
||||
nullValue: []byte{'n','u','l','l'},
|
||||
}
|
||||
|
||||
)
|
||||
|
||||
|
||||
func DefaultEncoder() *TextEncoder {
|
||||
g.buildDefault.Do(func() {
|
||||
g.formatter = NewTextEncoder(
|
||||
|
||||
WithNameTransformers(
|
||||
astrings.TransformFnc(func (s string) string {
|
||||
return astrings.ToKebab(s) }),
|
||||
astrings.TransformFnc(func (s string) string {
|
||||
return astrings.Pad(s,12,' ',true) }),
|
||||
),
|
||||
|
||||
WithSectionsFormat(func(c *Sections) {
|
||||
c.Main.Set(
|
||||
func(b *TextBuilder) {
|
||||
b.AddTextAnsi(ansi.Code(0).SetFgColor(ansi.ColorRed), "# ERROR ------------------------------------------------------\n\n")
|
||||
},
|
||||
func(b *TextBuilder) {
|
||||
b.AddTextAnsi(ansi.Code(0).SetFgColor(ansi.ColorRed), "\n----------------------------------------------------- ERROR #\n")
|
||||
})
|
||||
|
||||
c.Head.Set(func(b *TextBuilder){
|
||||
if b.Level() == 0 {
|
||||
b.SetAnsi(ansi.Code(0).SetFgColor(ansi.ColorRed))
|
||||
}
|
||||
}, nil)
|
||||
|
||||
c.Fields.Set(func(b *TextBuilder){
|
||||
if b.Level() == 0 {
|
||||
b.SetAnsi(ansi.Code(0).SetFgColor(ansi.ColorCyan))
|
||||
} else if b.level == 1 {
|
||||
b.SetAnsi(ansi.Code(0).SetFgColor(ansi.ColorBlue))
|
||||
} else if b.level == 2 {
|
||||
b.SetAnsi(ansi.Code(0).SetFgColor(ansi.ColorCyan))
|
||||
}
|
||||
},nil)
|
||||
|
||||
c.Causes.Set(func(b *TextBuilder) {
|
||||
b.SetAnsi(ansi.Code(0).SetFgColor(ansi.ColorCyan))
|
||||
b.AddTextAnsi(ansi.Code(0), "\n# PREVIOUS CAUSES -------------------------------------------\n\n")
|
||||
},nil)
|
||||
|
||||
c.Cause.Set(
|
||||
func(b *TextBuilder) {
|
||||
b.SetAnsi(ansi.Code(0).SetFgColor(ansi.ColorCyan))
|
||||
lin := strings.Repeat("-",b.Level())
|
||||
if b.Level() == 1 {
|
||||
b.SetAnsi(ansi.Code(0).SetFgColor(ansi.ColorMagenta))
|
||||
} else if b.level == 3 {
|
||||
b.SetAnsi(ansi.Code(0).SetFgColor(ansi.ColorGreen))
|
||||
}
|
||||
b.AddTextAnsi(0, fmt.Sprintf("%sLevel %d ----------------------------------------------------\n",lin,b.Level()))
|
||||
}, nil)
|
||||
|
||||
c.Trace.Set(func(b *TextBuilder) {
|
||||
b.SetAnsi(ansi.Code(0).SetFgColor(ansi.ColorHYellow).SetBgColor(ansi.ColorBlack))
|
||||
b.AddTextAnsi(b.ansiC.SetFgColor(ansi.ColorHBlue), "\n# STACK TRACE ------------------------------------------------\n\n")
|
||||
},nil)
|
||||
|
||||
}))
|
||||
})
|
||||
return g.formatter
|
||||
}
|
||||
|
||||
|
||||
|
||||
func defaultFrameBuilder (builder *TextBuilder, frame stack.Frame, num int) {
|
||||
|
||||
do := strings.Index(frame.Package,"runtime") == -1
|
||||
|
||||
|
||||
|
||||
var c1, c2, c3, c4 ansi.Code
|
||||
c1 = c1.SetFgColor(ansi.ColorBlue) //.SetBgColor(ansi.ColorHBlack)
|
||||
c2 = c2.SetFgColor(ansi.ColorMagenta)
|
||||
c3 = c3.SetFgColor(ansi.ColorWhite)
|
||||
c4 = c3.SetFgColor(ansi.ColorHWhite)
|
||||
|
||||
|
||||
builder.AddTextAnsi(c4,fmt.Sprintf("%d %s.%s \n",num,frame.Package, frame.FuncName))
|
||||
builder.AddTextAnsi(c2,fmt.Sprintf(" %.90s:%d\n", frame.File, frame.LineNumber))
|
||||
|
||||
if !do {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
f, e := frame.SourceLine()
|
||||
if e == nil {
|
||||
builder.AddTextAnsi(c3,fmt.Sprintf(" %s\n", f))
|
||||
} else {
|
||||
builder.AddTextAnsi(c3,fmt.Sprintf(" error %s\n", e.Error()))
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
func defaultFallback (b *TextBuilder, err, cause error) {
|
||||
b.AddTextAnsi(ansi.Code(0).SetFgColor(ansi.ColorHRed),"- FORMAT ERROR -----------------------------------------------\n")
|
||||
b.AddText(fmt.Sprintf("Cant format error type (%s)\n\n%#v \n\nCause : %s\n",reflect.TypeOf(err).String(),err,cause.Error()))
|
||||
b.AddTextAnsi(ansi.Code(0).SetFgColor(ansi.ColorHRed),"----------------------------------------------- FORMAT ERROR -\n")
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,31 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name fallback.go
|
||||
// Created by DEV
|
||||
// Modified 18/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 ertext
|
||||
|
||||
|
||||
type (
|
||||
|
||||
|
||||
ErrorFallbackFnc func (builder *TextBuilder, err, cause error)
|
||||
)
|
||||
@ -0,0 +1,61 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name Layout.go
|
||||
// Created by DEV
|
||||
// Modified 13/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 ertext
|
||||
|
||||
import (
|
||||
"ansi"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
Layout struct {
|
||||
AnsiC ansi.Code
|
||||
Indent string
|
||||
FieldSep string
|
||||
|
||||
TimeLayout string
|
||||
DateLayout string
|
||||
IntLayout string
|
||||
FloatLayout string
|
||||
BoolLayout string
|
||||
}
|
||||
|
||||
LayoutFnc func (layouts *Layout)
|
||||
)
|
||||
|
||||
|
||||
|
||||
func newLayouts () *Layout {
|
||||
return &Layout{
|
||||
Indent : " ",
|
||||
FieldSep : " : ",
|
||||
|
||||
TimeLayout : time.RFC822Z,
|
||||
DateLayout : time.RFC822Z,
|
||||
IntLayout : "%d",
|
||||
FloatLayout : "%-10s: %.2f",
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,79 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name show.go
|
||||
// Created by DEV
|
||||
// Modified 07/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 ertext
|
||||
|
||||
|
||||
const (
|
||||
|
||||
AddTypes Option = 1 << iota
|
||||
AddFields
|
||||
AddCauses
|
||||
AddTrace
|
||||
ShowAnsi
|
||||
UseIndent
|
||||
IgnoreEmpty
|
||||
|
||||
optionsDefault = AddTypes | ShowAnsi | UseIndent | AddFields | AddCauses | AddTrace | IgnoreEmpty
|
||||
)
|
||||
|
||||
|
||||
type (
|
||||
|
||||
|
||||
Option uint16
|
||||
)
|
||||
|
||||
func (o Option) Add (opt Option) Option {
|
||||
return o | opt
|
||||
}
|
||||
|
||||
func (o Option) Del (opt Option) Option {
|
||||
return o &^ opt }
|
||||
|
||||
|
||||
func (o Option) SetDefaults () Option {
|
||||
return optionsDefault }
|
||||
|
||||
|
||||
func (o Option) HasAddTypes() bool {
|
||||
return o&AddTypes !=0 }
|
||||
|
||||
|
||||
func (o Option) HasAddFields() bool {
|
||||
return o&AddFields !=0 }
|
||||
|
||||
func (o Option) HasAddCauses() bool {
|
||||
return o&AddCauses !=0 }
|
||||
|
||||
func (o Option) HasAddTrace() bool {
|
||||
return o&AddTrace !=0 }
|
||||
|
||||
func (o Option) HasIgnoreEmpty() bool {
|
||||
return o&IgnoreEmpty !=0 }
|
||||
|
||||
func (o Option) HasShowAnsi() bool {
|
||||
return o&ShowAnsi !=0 }
|
||||
|
||||
func (o Option) HasUseIndent() bool {
|
||||
return o&UseIndent !=0 }
|
||||
@ -0,0 +1,52 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name section.go
|
||||
// Created by DEV
|
||||
// Modified 16/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 ertext
|
||||
|
||||
type (
|
||||
|
||||
section struct {
|
||||
beginFn func(textBuilder *TextBuilder)
|
||||
endFn func(textBuilder *TextBuilder)
|
||||
}
|
||||
|
||||
|
||||
)
|
||||
|
||||
|
||||
func (s *section) Set(beginFn, endFn TextBuilderFnc) {
|
||||
s.beginFn,s.endFn = beginFn,endFn }
|
||||
|
||||
|
||||
func (s *section) begin(txtBuilder *TextBuilder) {
|
||||
if s.beginFn != nil {
|
||||
s.beginFn(txtBuilder)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *section) end(txtBuilder *TextBuilder) {
|
||||
if s.endFn != nil {
|
||||
s.endFn(txtBuilder)
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,44 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name sections.go
|
||||
// Created by DEV
|
||||
// Modified 16/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 ertext
|
||||
|
||||
|
||||
type (
|
||||
|
||||
Sections struct {
|
||||
Main section
|
||||
Head section
|
||||
Fields section
|
||||
Causes section
|
||||
Cause section
|
||||
Trace section
|
||||
}
|
||||
|
||||
|
||||
SectionsFnc func(secs *Sections)
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,287 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name text_builder.go
|
||||
// Created by DEV
|
||||
// Modified 22/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 ertext
|
||||
|
||||
import (
|
||||
"aios/ansi"
|
||||
"bfrs"
|
||||
"errs/erdata"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
TextBuilder struct {
|
||||
f *TextEncoder
|
||||
b *bfrs.ABuffer
|
||||
|
||||
useAux bool
|
||||
auxB *bfrs.ABuffer
|
||||
|
||||
level int
|
||||
ansiC ansi.Code
|
||||
invalid error
|
||||
}
|
||||
|
||||
|
||||
TextBuilderFnc func (b *TextBuilder)
|
||||
)
|
||||
|
||||
|
||||
func newTextBuilder(formatter *TextEncoder) *TextBuilder {
|
||||
b := &TextBuilder{
|
||||
f : formatter,
|
||||
b : bfrs.NewABuffer(),
|
||||
useAux: len(formatter.valueTrans) > 0,
|
||||
ansiC : formatter.layout.AnsiC,
|
||||
}
|
||||
if b.useAux {
|
||||
b.auxB = bfrs.NewABuffer()
|
||||
} else {
|
||||
b.auxB = b.b
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
func (b *TextBuilder) Level () int {
|
||||
return b.level }
|
||||
|
||||
func (b *TextBuilder) AddText (text string) {
|
||||
b.b.WriteString(text) }
|
||||
|
||||
func (b *TextBuilder) AddTextAnsi (ansiC ansi.Code, text string) {
|
||||
if b.f.HasShowAnsi() {
|
||||
if ansiC == 0 {
|
||||
ansiC = b.ansiC
|
||||
}
|
||||
b.b.WriteString(ansiC.Begin())
|
||||
defer b.b.WriteString(ansiC.End())
|
||||
}
|
||||
|
||||
b.b.WriteString(text)
|
||||
}
|
||||
|
||||
func (b *TextBuilder) SetAnsi(ansiC ansi.Code) {
|
||||
if ansiC != 0 {
|
||||
b.ansiC = ansiC
|
||||
} else {
|
||||
b.ansiC = b.f.layout.AnsiC
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (b *TextBuilder) format (err error) {
|
||||
|
||||
c := erdata.NewErrorData(err)
|
||||
if c.Collect(b.f.HasAddFields(), b.f.HasAddCauses(), b.f.HasAddTrace()); c.Invalid != nil {
|
||||
//b.writeFormatError(cl.Invalid)
|
||||
return
|
||||
}
|
||||
b.write(c, 0)
|
||||
}
|
||||
|
||||
func (b *TextBuilder) write (c *erdata.ErrorData, level int) {
|
||||
if b.invalid != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ind := strings.Repeat(b.f.layout.Indent,level)
|
||||
|
||||
d := false
|
||||
f := b.f
|
||||
b.level = level
|
||||
if level == 0 {
|
||||
b.f.sections.Main.begin(b)
|
||||
}
|
||||
|
||||
defer func(bl *TextBuilder, dn *bool,er error){
|
||||
if !(*dn) {
|
||||
if rc := recover(); rc != nil {
|
||||
if er,is := rc.(error); is {
|
||||
bl.invalid = er
|
||||
} else {
|
||||
bl.invalid = fmt.Errorf("%#v",rc)
|
||||
}
|
||||
bl.f.onFormatError(bl,er,bl.invalid)
|
||||
}
|
||||
}
|
||||
}(b,&d, c.Err)
|
||||
|
||||
f.sections.Head.begin(b)
|
||||
b.writeFieldNameValue(erdata.FieldNameError, c.ErrStr, ind)
|
||||
if f.HasAddTypes() {
|
||||
if len(c.TypesStr) == 1 {
|
||||
b.writeFieldNameValue(erdata.FieldNameType, c.TypesStr[0], ind)
|
||||
} else {
|
||||
b.writeFieldNameValue(erdata.FieldNameTypes, strings.Join(c.TypesStr, ","), ind)
|
||||
}
|
||||
}
|
||||
f.sections.Head.end(b)
|
||||
|
||||
if f.HasAddFields() && c.Fields.Len() > 0 {
|
||||
f.sections.Fields.begin(b)
|
||||
heads := c.Fields.GetHeads()
|
||||
for _,fl := range heads {
|
||||
b.writeField(c.Fields,fl,ind)
|
||||
}
|
||||
f.sections.Fields.end(b)
|
||||
}
|
||||
|
||||
if len(c.Causes) > 0 {
|
||||
if level == 0 {
|
||||
f.sections.Causes.begin(b)
|
||||
}
|
||||
b.level++
|
||||
for _,cs := range c.Causes {
|
||||
f.sections.Cause.begin(b)
|
||||
b.write(&cs,level+1)
|
||||
f.sections.Cause.end(b)
|
||||
}
|
||||
if level == 0 {
|
||||
f.sections.Causes.end(b)
|
||||
}
|
||||
}
|
||||
|
||||
if c.Trace != nil {
|
||||
f.sections.Trace.begin(b)
|
||||
for nu,fm := range c.Trace.Frames() {
|
||||
b.f.frameBuilder(b,fm,nu)
|
||||
}
|
||||
f.sections.Trace.end(b)
|
||||
}
|
||||
|
||||
if level == 0 {
|
||||
b.f.sections.Main.end(b)
|
||||
}
|
||||
d = true
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func (b *TextBuilder) reset () {
|
||||
b.b.Reset()
|
||||
if b.useAux {
|
||||
b.auxB.Reset()
|
||||
b.useAux = false
|
||||
}
|
||||
b.f = nil
|
||||
b.level = 0
|
||||
b.invalid = nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
func (b *TextBuilder) writeFieldBegin (name, indent string) {
|
||||
if b.ansiC != 0 {
|
||||
b.b.WriteString(b.ansiC.Begin())
|
||||
}
|
||||
b.b.WriteString(indent)
|
||||
b.b.WriteString(b.f.nameTrans.Transform(name))
|
||||
b.b.WriteString(b.f.layout.FieldSep)
|
||||
}
|
||||
|
||||
func (b *TextBuilder) writeFieldEnd () {
|
||||
b.b.WriteByte('\n')
|
||||
if b.ansiC != 0 {
|
||||
b.b.WriteString(b.ansiC.End())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (b *TextBuilder) writeFieldNameValue(name string, value string, indent string) {
|
||||
p:= b.b.Len()
|
||||
b.writeFieldBegin(name,indent)
|
||||
if b.f.HasIgnoreEmpty() && len(value) == 0 {
|
||||
b.b.Truncate(p)
|
||||
return
|
||||
}
|
||||
b.b.WriteString(b.f.valueTrans.Transform(value))
|
||||
b.writeFieldEnd()
|
||||
|
||||
}
|
||||
|
||||
func (b *TextBuilder) writeField (fields erdata.Fields, field *erdata.FieldHead, indent string) {
|
||||
p := b.b.Len()
|
||||
b.writeFieldBegin(field.Name,indent)
|
||||
if !b.writeFieldValue(fields,field) {
|
||||
b.b.Truncate(p)
|
||||
return
|
||||
}
|
||||
b.writeFieldEnd()
|
||||
}
|
||||
|
||||
|
||||
func (b *TextBuilder) writeFieldValue (fields erdata.Fields, field *erdata.FieldHead) bool {
|
||||
|
||||
switch field.Kind {
|
||||
case erdata.FieldKindFloat:
|
||||
b.auxB.WriteFloatStr(fields.GetFieldValueFloat(field),'f',-1)
|
||||
|
||||
case erdata.FieldKindInt:
|
||||
b.auxB.WriteIntStr(int64(fields.GetFieldValueInt(field)),10)
|
||||
|
||||
case erdata.FieldKindString:
|
||||
s := fields.GetFieldValueString(field)
|
||||
if b.f.HasIgnoreEmpty() && len(s) == 0 {
|
||||
return false
|
||||
}
|
||||
b.auxB.WriteString(fields.GetFieldValueString(field))
|
||||
|
||||
case erdata.FieldKindTime:
|
||||
b.auxB.WriteDateStr(fields.GetFieldValueDate(field), b.f.layout.TimeLayout)
|
||||
|
||||
case erdata.FieldKindDate:
|
||||
b.auxB.WriteDateStr(fields.GetFieldValueDate(field), b.f.layout.DateLayout)
|
||||
|
||||
case erdata.FieldKindBool:
|
||||
b.auxB.WriteBoolStr(fields.GetFieldValueBool(field))
|
||||
|
||||
default:
|
||||
a := fields.GetFieldValueAny(field)
|
||||
if a == nil {
|
||||
if b.f.HasIgnoreEmpty() {
|
||||
return false
|
||||
}
|
||||
b.auxB.WOQ(false,'"', g.nullValue)
|
||||
} else {
|
||||
s := fmt.Sprintf("%#v",a)
|
||||
if b.f.HasIgnoreEmpty() && len(s) == 0 {
|
||||
return false
|
||||
}
|
||||
b.auxB.WriteString(s)
|
||||
}
|
||||
}
|
||||
|
||||
if b.useAux {
|
||||
b.b.WriteString(b.f.valueTrans.Transform(string(b.auxB.Bytes())))
|
||||
b.auxB.Reset()
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
@ -0,0 +1,134 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name text_formatter.go
|
||||
// Created by DEV
|
||||
// Modified 22/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 ertext
|
||||
|
||||
import (
|
||||
"errs/stack"
|
||||
"typs/astrings"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
|
||||
TextEncoder struct {
|
||||
Option
|
||||
|
||||
layout Layout
|
||||
sections Sections
|
||||
|
||||
nameTrans astrings.Transformers
|
||||
valueTrans astrings.Transformers
|
||||
|
||||
onFormatError ErrorFallbackFnc
|
||||
onTraceError ErrorFallbackFnc
|
||||
onCauseError ErrorFallbackFnc
|
||||
|
||||
frameBuilder func (builder *TextBuilder, frame stack.Frame, num int)
|
||||
}
|
||||
|
||||
TextEncoderFnc func (formatter *TextEncoder)
|
||||
)
|
||||
|
||||
|
||||
func NewTextEncoder(options ...TextEncoderFnc) *TextEncoder {
|
||||
fm := &TextEncoder{
|
||||
Option : optionsDefault,
|
||||
layout : *newLayouts(),
|
||||
frameBuilder : defaultFrameBuilder,
|
||||
onFormatError : defaultFallback,
|
||||
onTraceError : defaultFallback,
|
||||
onCauseError : defaultFallback,
|
||||
}
|
||||
for _,o := range options {
|
||||
o(fm)
|
||||
}
|
||||
return fm
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
func (f *TextEncoder) EncodeError(err error) []byte {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
b := newTextBuilder(f)
|
||||
defer func(tb *TextBuilder){
|
||||
tb.reset()
|
||||
}(b)
|
||||
|
||||
b.format(err)
|
||||
return b.b.Bytes()
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func WithErrorFallbacks (format, cause, trace ErrorFallbackFnc) TextEncoderFnc {
|
||||
return func(f *TextEncoder) {
|
||||
if format == nil {
|
||||
f.onFormatError = format
|
||||
}
|
||||
if cause == nil {
|
||||
f.onCauseError = cause
|
||||
}
|
||||
if trace == nil {
|
||||
f.onTraceError = trace
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func WithOptions (set, unset Option) TextEncoderFnc {
|
||||
return func(f *TextEncoder) {
|
||||
f.Option = f.Option.Add(set)
|
||||
f.Option = f.Option.Del(unset) } }
|
||||
|
||||
|
||||
func WithNameTransformers(transformer ...astrings.Transformer) TextEncoderFnc {
|
||||
return func(f *TextEncoder) {
|
||||
f.nameTrans = append(f.nameTrans, transformer ...) } }
|
||||
|
||||
|
||||
func WithValueTransformers(transformer ...astrings.Transformer) TextEncoderFnc {
|
||||
return func(f *TextEncoder) {
|
||||
f.valueTrans = append(f.nameTrans, transformer ...) } }
|
||||
|
||||
|
||||
func WithSectionsFormat(config SectionsFnc) TextEncoderFnc {
|
||||
return func(f *TextEncoder) {
|
||||
config(&f.sections) } }
|
||||
|
||||
|
||||
func WithLayouts (config LayoutFnc) TextEncoderFnc {
|
||||
return func(f *TextEncoder) {
|
||||
config(&f.layout) } }
|
||||
|
||||
|
||||
func WithFrameBuilder (builderFn func (builder *TextBuilder, frame stack.Frame, num int)) TextEncoderFnc {
|
||||
return func(f *TextEncoder) {
|
||||
f.frameBuilder = builderFn } }
|
||||
@ -0,0 +1,80 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name active.go
|
||||
// Created by DEV
|
||||
// Modified 01/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 stack
|
||||
|
||||
import "errors"
|
||||
|
||||
const (
|
||||
|
||||
stackSkipFrames = 2
|
||||
stackMaxFrames = 32
|
||||
|
||||
stackUnknownInfo = "???"
|
||||
)
|
||||
|
||||
|
||||
func UnwrapTrace(err error, cause bool) Trace {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
e := err
|
||||
for e != nil {
|
||||
if st,is := e.(Tracer); is {
|
||||
return st.StackTrace()
|
||||
}
|
||||
e = errors.Unwrap(e)
|
||||
}
|
||||
if cause {
|
||||
e = err
|
||||
for e != nil {
|
||||
if cs, is := e.(interface{ Cause() error }); is {
|
||||
UnwrapTrace(cs.Cause(), cause)
|
||||
}
|
||||
|
||||
if cs, is := e.(interface{ Causes() []error}); is {
|
||||
if tc := unwrapTraceMulti(cs.Causes()); tc != nil {
|
||||
return tc
|
||||
}
|
||||
}
|
||||
if cs, is := e.(interface{ Unwrap() []error}); is {
|
||||
if tc := unwrapTraceMulti(cs.Unwrap()); tc != nil {
|
||||
return tc
|
||||
}
|
||||
}
|
||||
|
||||
e = errors.Unwrap(e)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func unwrapTraceMulti (ers []error) Trace {
|
||||
for _,e := range ers {
|
||||
if tc := UnwrapTrace(e,true); tc != nil {
|
||||
return tc
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -0,0 +1,105 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name stack_frame.go
|
||||
// Created by DEV
|
||||
// Modified 20/03/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 stack
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
Frame struct {
|
||||
LineNumber int // Number line
|
||||
File string // File FuncName
|
||||
FuncName string // Function name
|
||||
Package string // Package name
|
||||
Counter uintptr
|
||||
|
||||
files map[string][]byte
|
||||
}
|
||||
|
||||
)
|
||||
|
||||
|
||||
func NewFrame(counter uintptr) *Frame {
|
||||
if counter == 0 {
|
||||
return nil
|
||||
}
|
||||
sf := &Frame{
|
||||
Counter: counter,
|
||||
}
|
||||
fn := runtime.FuncForPC(counter)
|
||||
sf.Package, sf.FuncName = packageAndName(fn)
|
||||
sf.File, sf.LineNumber = fn.FileLine(counter-1)
|
||||
return sf
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (f Frame) SourceLine() (string, error) {
|
||||
if f.LineNumber <= 0 {
|
||||
return stackUnknownInfo, nil
|
||||
}
|
||||
|
||||
file, err := os.Open(f.File)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
currentLine := 1
|
||||
for scanner.Scan() {
|
||||
if currentLine == f.LineNumber {
|
||||
return string(bytes.Trim(scanner.Bytes(), " \t")), nil
|
||||
}
|
||||
currentLine++
|
||||
}
|
||||
_ = file.Close()
|
||||
if err := scanner.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return stackUnknownInfo, nil
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
func packageAndName (fn *runtime.Func) (string,string) {
|
||||
nm := fn.Name()
|
||||
pg := ""
|
||||
if ls := strings.LastIndex(nm,"/"); ls >= 0 {
|
||||
pg = nm[:ls] + "/"
|
||||
nm = nm[ls+1:]
|
||||
}
|
||||
if pd := strings.Index(nm,"."); pd >= 0 {
|
||||
pg += nm[:pd]
|
||||
nm = nm[pd+1:]
|
||||
}
|
||||
return pg,strings.Replace(nm,"·", ".",-1)
|
||||
}
|
||||
@ -0,0 +1,59 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name stack_trace.go
|
||||
// Created by DEV
|
||||
// Modified 23/03/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 stack
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
|
||||
Trace []uintptr
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
func MakeStackTrace(offset int) Trace {
|
||||
ls := make([]uintptr, stackMaxFrames)
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
}
|
||||
cn := runtime.Callers(offset+stackSkipFrames,ls)
|
||||
return ls[:cn]
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
func (s Trace) Frames() []Frame {
|
||||
f := make([]Frame,len(s))
|
||||
for i,p := range s {
|
||||
f[i]=*NewFrame(p)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,37 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name stack_tracer.go
|
||||
// Created by DEV
|
||||
// Modified 23/03/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 stack
|
||||
|
||||
type (
|
||||
|
||||
Tracer interface {
|
||||
|
||||
StackTrace () Trace
|
||||
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,37 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project active3
|
||||
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name active.go
|
||||
// ✍️ Created by DEV
|
||||
// 📅 Modified 04/05/2024
|
||||
//
|
||||
// 🔒 Copyright 2024 activething.com
|
||||
//
|
||||
// 📜 LICENSE --------------------------------------------------------------
|
||||
// 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 strg
|
||||
|
||||
|
||||
|
||||
func ErrorToString (err error) string {
|
||||
if err == nil {
|
||||
return ""
|
||||
}
|
||||
if st,is := err.(Stringer); is {
|
||||
return st.String()
|
||||
}
|
||||
return err.Error()
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project active3
|
||||
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name astringer.go
|
||||
// ✍️ Created by DEV
|
||||
// 📅 Modified 04/05/2024
|
||||
//
|
||||
// 🔒 Copyright 2024 activething.com
|
||||
//
|
||||
// 📜 LICENSE --------------------------------------------------------------
|
||||
// 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 strg
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
AErrorStringer struct {
|
||||
useLabels bool
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
func (s AStringer) AddString (label, str string, err error) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,38 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project active3
|
||||
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name error_stringer.go
|
||||
// ✍️ Created by DEV
|
||||
// 📅 Modified 04/05/2024
|
||||
//
|
||||
// 🔒 Copyright 2024 activething.com
|
||||
//
|
||||
// 📜 LICENSE --------------------------------------------------------------
|
||||
// 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 strg
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
ErrorStringer interface {
|
||||
|
||||
StringError (err error) string
|
||||
}
|
||||
|
||||
)
|
||||
|
||||
|
||||
@ -0,0 +1,43 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project active3
|
||||
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name stringer.go
|
||||
// ✍️ Created by DEV
|
||||
// 📅 Modified 04/05/2024
|
||||
//
|
||||
// 🔒 Copyright 2024 activething.com
|
||||
//
|
||||
// 📜 LICENSE --------------------------------------------------------------
|
||||
// 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 strg
|
||||
|
||||
|
||||
type (
|
||||
|
||||
Stringer interface {
|
||||
|
||||
String () string
|
||||
}
|
||||
|
||||
StringFnc func () string
|
||||
)
|
||||
|
||||
|
||||
|
||||
func (f StringFnc) String () string {
|
||||
return f() }
|
||||
|
||||
|
||||
@ -0,0 +1,48 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name unwraper.go
|
||||
// Created by DEV
|
||||
// Modified 21/03/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
|
||||
|
||||
type (
|
||||
|
||||
Unwrapper interface {
|
||||
Unwrap () error
|
||||
}
|
||||
|
||||
UnwrapperFnc func () error
|
||||
|
||||
|
||||
|
||||
)
|
||||
|
||||
|
||||
func (f UnwrapperFnc) Unwrap () error {
|
||||
return f() }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,45 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name unwrapper_list.go
|
||||
// Created by DEV
|
||||
// Modified 08/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
|
||||
|
||||
|
||||
type (
|
||||
|
||||
|
||||
UnwrapperList interface {
|
||||
Unwrap () []error
|
||||
}
|
||||
|
||||
UnwrapperListFnc func () []error
|
||||
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
func (f UnwrapperListFnc) Unwrap () []error {
|
||||
return f() }
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,120 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project active3
|
||||
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name active.go
|
||||
// ✍️ Created by DEV
|
||||
// 📅 Modified 04/05/2024
|
||||
//
|
||||
// 🔒 Copyright 2024 activething.com
|
||||
//
|
||||
// 📜 LICENSE --------------------------------------------------------------
|
||||
// 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 wrap
|
||||
|
||||
import "fmt"
|
||||
|
||||
const(
|
||||
|
||||
errNilWrapMsg string = "error trying to wrap % into a nil error"
|
||||
errNilCauseMsg string = "error trying to wrap % into a nil cause"
|
||||
)
|
||||
|
||||
const(
|
||||
|
||||
labelMessage string = "message"
|
||||
labelInfo string = "info"
|
||||
labelDate string = "date"
|
||||
labelTime string = "time"
|
||||
labelHelp string = "help"
|
||||
labelLink string = "link"
|
||||
labelCause string = "cause"
|
||||
labelTrace string = "trace"
|
||||
labelParam string = "param"
|
||||
labelSource string = "source"
|
||||
labelUser string = "user"
|
||||
)
|
||||
|
||||
|
||||
|
||||
var (
|
||||
|
||||
|
||||
G = struct {
|
||||
|
||||
ErrNilWrap ErrCode
|
||||
|
||||
InfoLink string
|
||||
InfoHelp string
|
||||
|
||||
}{
|
||||
ErrNilWrap : ErrCode("wrap-nil-error"),
|
||||
|
||||
InfoLink : "https://git.activething.com/go/active",
|
||||
InfoHelp : "https://doc.activething.com/go/active",
|
||||
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
||||
func ComposeWrappedString (str string, wrapped error) string {
|
||||
type s interface { String() string }
|
||||
if wrapped == nil {
|
||||
return fmt.Sprintf("%s\n",str)
|
||||
}
|
||||
if st,is := wrapped.(s); is {
|
||||
return fmt.Sprintf("%s\n%s",st.String(),str)
|
||||
}
|
||||
return fmt.Sprintf("%s\n%s",wrapped.Error(),str)
|
||||
}
|
||||
|
||||
|
||||
func TryWithCause(err error, cause error) error {
|
||||
if err == nil && cause == nil {
|
||||
return nil
|
||||
}
|
||||
if err == nil {
|
||||
return cause
|
||||
}
|
||||
if cause == nil {
|
||||
return err
|
||||
}
|
||||
return *WithCause(err,cause)
|
||||
}
|
||||
|
||||
|
||||
func TryWrapWithInfo (err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return *WithInfo(err)
|
||||
}
|
||||
|
||||
|
||||
func TryWrapWithTrace(err error, offset int) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return *WhitTrace(err,offset)
|
||||
}
|
||||
|
||||
|
||||
func TryWrapWithMessage (err error, message string) error {
|
||||
if err == nil { return nil }
|
||||
return *WithMessage(err,message)
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,61 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name err_cause.go
|
||||
// Created by DEV
|
||||
// Modified 30/03/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 wrap
|
||||
|
||||
import (
|
||||
"errs/strg"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
Cause struct {
|
||||
error
|
||||
cause error
|
||||
}
|
||||
)
|
||||
|
||||
func WithCause(err error, cause error) *Cause {
|
||||
if err == nil {
|
||||
panic(G.ErrNilWrap.WithMessageF(errNilWrapMsg, "Cause"))
|
||||
}
|
||||
if cause == nil {
|
||||
panic(G.ErrNilWrap.WithMessageF(errNilCauseMsg, "Cause"))
|
||||
}
|
||||
return &Cause{
|
||||
error: err,
|
||||
cause: cause,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (e Cause) Unwrap() error {
|
||||
return e.error }
|
||||
|
||||
func (e Cause) Cause () error {
|
||||
return e.cause }
|
||||
|
||||
|
||||
func (e Cause) String() string {
|
||||
return ComposeWrappedString(strg.ErrorToString(e.cause),e.error)
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project active3
|
||||
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name err_code.go
|
||||
// ✍️ Created by DEV
|
||||
// 📅 Modified 04/05/2024
|
||||
//
|
||||
// 🔒 Copyright 2024 activething.com
|
||||
//
|
||||
// 📜 LICENSE --------------------------------------------------------------
|
||||
// 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 wrap
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
ErrCode string
|
||||
)
|
||||
|
||||
|
||||
|
||||
func (e ErrCode) Error() string {
|
||||
return string(e) }
|
||||
|
||||
|
||||
func (e ErrCode) Code () string {
|
||||
return string(e) }
|
||||
|
||||
|
||||
|
||||
func (e ErrCode) WithMessage (msg string) *Message {
|
||||
return WithMessage(e,msg) }
|
||||
|
||||
func (e ErrCode) WithMessageF(msgf string, params ...any) *Message {
|
||||
return WithMessageF(e,msgf,params) }
|
||||
|
||||
|
||||
func (e ErrCode) String () string {
|
||||
return string(e)
|
||||
}
|
||||
@ -0,0 +1,65 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 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 wrap
|
||||
|
||||
import (
|
||||
"flds"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
|
||||
ErrFields struct {
|
||||
error
|
||||
*flds.FieldsObject
|
||||
}
|
||||
|
||||
|
||||
)
|
||||
|
||||
func WithFields (err error, options ...flds.FieldsObjectFnc) *ErrFields {
|
||||
if err == nil {
|
||||
panic(G.ErrNilWrap.WithMessageF(errNilWrapMsg, "Fields"))
|
||||
}
|
||||
fo,er := flds.NewFieldsObject(options...)
|
||||
if er != nil {
|
||||
panic(er)
|
||||
}
|
||||
return &ErrFields{
|
||||
error: err,
|
||||
FieldsObject: fo,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (e ErrFields) Unwrap() error {
|
||||
return e.error }
|
||||
|
||||
func (e ErrFields) UnwrapAll() []error {
|
||||
return nil }
|
||||
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,68 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name errMeta.go
|
||||
// Created by DEV
|
||||
// Modified 24/03/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 wrap
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
Info struct {
|
||||
error
|
||||
Message string
|
||||
Info string
|
||||
Desc string
|
||||
Help string
|
||||
Link string
|
||||
Date time.Time
|
||||
}
|
||||
|
||||
InfoFnc func (*Info)
|
||||
|
||||
)
|
||||
|
||||
|
||||
func WithInfo (err error) *Info {
|
||||
if err == nil {
|
||||
panic(G.ErrNilWrap.WithMessageF(errNilWrapMsg,"Info"))
|
||||
}
|
||||
return &Info{
|
||||
error: err,
|
||||
Link: G.InfoLink,
|
||||
Date: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (e Info) Unwrap () error {
|
||||
return e.error }
|
||||
|
||||
|
||||
func (e Info) String () string {
|
||||
return ComposeWrappedString(fmt.Sprintf("date : %s",e.Date),e.error)
|
||||
}
|
||||
|
||||
@ -0,0 +1,61 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name err_message.go
|
||||
// Created by DEV
|
||||
// Modified 27/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 wrap
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
Message struct {
|
||||
error
|
||||
message string
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
func WithMessage(err error, msg string) *Message {
|
||||
if err == nil {
|
||||
panic(G.ErrNilWrap.WithMessageF(errNilWrapMsg, "Message"))
|
||||
}
|
||||
return &Message{
|
||||
error: err,
|
||||
message: msg} }
|
||||
|
||||
func WithMessageF (err error, msgf string, params ...any) *Message {
|
||||
return WithMessage(err,fmt.Sprintf(msgf,params...)) }
|
||||
|
||||
|
||||
|
||||
func (e Message) Unwrap () error {
|
||||
return e.error }
|
||||
|
||||
func (e Message) Message () string {
|
||||
return e.message }
|
||||
|
||||
|
||||
|
||||
func (e Message) String () string {
|
||||
return ComposeWrappedString(e.message,e.error) }
|
||||
@ -0,0 +1,63 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name err_trace.go
|
||||
// Created by DEV
|
||||
// Modified 25/03/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 wrap
|
||||
|
||||
import (
|
||||
"errs/stack"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
Trace struct {
|
||||
error
|
||||
stack stack.Trace
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
func WhitTrace(err error, offset int) *Trace {
|
||||
if err == nil {
|
||||
panic(G.ErrNilWrap.WithMessageF(errNilWrapMsg, "Trace"))
|
||||
}
|
||||
var st stack.Trace
|
||||
tc,is := err.(stack.Tracer)
|
||||
if !is {
|
||||
st = stack.MakeStackTrace(offset)
|
||||
} else {
|
||||
st = tc.StackTrace()
|
||||
}
|
||||
return &Trace{
|
||||
error: err,
|
||||
stack: st,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (e Trace) Unwrap() error {
|
||||
return e.error }
|
||||
|
||||
|
||||
func (e Trace) StackTrace () stack.Trace {
|
||||
return e.stack }
|
||||
|
||||
@ -1,3 +0,0 @@
|
||||
module ansi
|
||||
|
||||
go 1.22
|
||||
@ -1,3 +0,0 @@
|
||||
module iosys
|
||||
|
||||
go 1.22
|
||||
@ -1,3 +0,0 @@
|
||||
module mems
|
||||
|
||||
go 1.22
|
||||
Loading…
Reference in new issue