parent
4338ed6787
commit
11b8bf37cd
@ -0,0 +1,139 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name active.go
|
||||
// Created by DEV
|
||||
// Modified 09/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 fmtr
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sync"
|
||||
)
|
||||
|
||||
|
||||
const (
|
||||
|
||||
CodeBuilderError string = "build-format"
|
||||
CodeBuilderLevelError string = "builder-invalid-level"
|
||||
|
||||
fmtErrorFormatUnwrap string = "cant unwrap error type %s"
|
||||
fmtErrorTypeFormat string = "cant build format for error type %s"
|
||||
|
||||
|
||||
)
|
||||
|
||||
const (
|
||||
FieldNameError string = "error"
|
||||
FieldNameCode string = "code"
|
||||
FieldNameType string = "type"
|
||||
FieldNameMessage string = "message"
|
||||
FieldNameInfo string = "info"
|
||||
FieldNameDate string = "date"
|
||||
FieldNameTime string = "time"
|
||||
FieldNameParam string = "param"
|
||||
FieldNameSource string = "source"
|
||||
FieldNameUser string = "user"
|
||||
|
||||
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
g = struct {
|
||||
mux sync.RWMutex
|
||||
|
||||
builders map[reflect.Type]ErrorBuilder
|
||||
collectors FieldCollectors
|
||||
}{
|
||||
|
||||
builders : make(map[reflect.Type]ErrorBuilder),
|
||||
collectors : defaultCollectors(),
|
||||
}
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
func GetErrorTypeBuilder(rtype reflect.Type) ErrorBuilder {
|
||||
g.mux.RLock()
|
||||
b,_ := g.builders[rtype]
|
||||
g.mux.RUnlock()
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
func SetErrorTypeBuilder(rtype reflect.Type, errBuilder ErrorBuilder) {
|
||||
if rtype == nil {
|
||||
return
|
||||
}
|
||||
g.mux.Lock()
|
||||
if errBuilder == nil {
|
||||
delete(g.builders,rtype)
|
||||
} else {
|
||||
g.builders[rtype] = errBuilder
|
||||
}
|
||||
g.mux.Unlock()
|
||||
}
|
||||
|
||||
|
||||
func MakeErrorTypeBuilder(builder Builder, err error) ErrorBuilder {
|
||||
var eb FieldCollectors
|
||||
|
||||
g.mux.RLock()
|
||||
defer g.mux.RUnlock()
|
||||
|
||||
builder.Build(err.Error(),reflect.TypeOf(err).String(), func(fb FieldBuilder){
|
||||
for _,c := range g.collectors {
|
||||
if c.CollectField(fb,err) {
|
||||
eb = append(eb,c)
|
||||
}
|
||||
}
|
||||
})
|
||||
if len(eb) == 0 {
|
||||
return BuildErrorFnc(BasicBuildError)
|
||||
}
|
||||
return eb
|
||||
}
|
||||
|
||||
|
||||
|
||||
func AddCollector (collector FieldCollector) {
|
||||
if collector == nil {
|
||||
return
|
||||
}
|
||||
g.mux.Lock()
|
||||
g.collectors = append(g.collectors,collector)
|
||||
g.mux.Unlock()
|
||||
}
|
||||
|
||||
|
||||
func defaultCollectors () FieldCollectors {
|
||||
return FieldCollectors{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,43 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name builder.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 fmtr
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
|
||||
Builder interface {
|
||||
|
||||
Build(errCode, errType string, fbuilder BuildFieldFnc)
|
||||
|
||||
}
|
||||
|
||||
|
||||
BuildFnc func(errCode, errType string, fbuilder BuildFieldFnc)
|
||||
)
|
||||
|
||||
|
||||
func (f BuildFnc) Build(errCode, errType string, fbuilder BuildFieldFnc) {
|
||||
f(errCode,errType,fbuilder) }
|
||||
@ -0,0 +1,41 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name collector.go
|
||||
// Created by DEV
|
||||
// Modified 09/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 fmtr
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
FieldCollector interface {
|
||||
CollectField (builder FieldBuilder, err error) bool
|
||||
}
|
||||
|
||||
CollectFieldFnc func (builder FieldBuilder, err error) bool
|
||||
|
||||
)
|
||||
|
||||
func (f CollectFieldFnc) CollectField(builder FieldBuilder, err error) bool {
|
||||
return f(builder,err) }
|
||||
|
||||
|
||||
@ -0,0 +1,66 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name field_collectors.go
|
||||
// Created by DEV
|
||||
// Modified 09/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 fmtr
|
||||
|
||||
import "reflect"
|
||||
|
||||
type (
|
||||
|
||||
FieldCollectors []FieldCollector
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
func (f FieldCollectors) BuildError(builder Builder, err error) {
|
||||
builder.Build(err.Error(),reflect.TypeOf(err).String(), func(fb FieldBuilder) {
|
||||
f.CollectField(fb,err)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
func (f FieldCollectors) MakeErrorBuilder (builder Builder, err error) ErrorBuilder {
|
||||
var eb FieldCollectors
|
||||
builder.Build(err.Error(),reflect.TypeOf(err).String(),func(fb FieldBuilder){
|
||||
for _,c := range f {
|
||||
if c.CollectField(fb,err) {
|
||||
eb = append(eb,c)
|
||||
}
|
||||
}
|
||||
})
|
||||
if len(eb) == 0 {
|
||||
return BuildErrorFnc(BasicBuildError)
|
||||
}
|
||||
return eb
|
||||
}
|
||||
|
||||
|
||||
func (f FieldCollectors) CollectField (builder FieldBuilder, err error) bool {
|
||||
r := true
|
||||
for _,fc := range f {
|
||||
r = r && fc.CollectField(builder,err)
|
||||
}
|
||||
return r
|
||||
}
|
||||
@ -0,0 +1,98 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name err_format.go
|
||||
// Created by DEV
|
||||
// Modified 10/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 fmtr
|
||||
|
||||
import (
|
||||
"errs/stack"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
ErrBuilder struct {
|
||||
code string
|
||||
message string
|
||||
cause error
|
||||
trace stack.Trace
|
||||
}
|
||||
)
|
||||
|
||||
func NewErrBuilder(code string, cause error, message string, addTrace bool) *ErrBuilder {
|
||||
var tc stack.Trace
|
||||
if addTrace {
|
||||
tc = stack.MakeStackTrace(3)
|
||||
}
|
||||
return &ErrBuilder{
|
||||
code : code,
|
||||
cause : cause,
|
||||
message: message,
|
||||
trace : tc,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func NewErrBuilderFormat (cause error, rtype reflect.Type, addTrace bool) *ErrBuilder {
|
||||
return NewErrBuilder(CodeBuilderError,cause,fmt.Sprintf(fmtErrorTypeFormat,rtype.String()),addTrace) }
|
||||
|
||||
|
||||
func NewErrBuilderUnwrap (cause error, rtype reflect.Type, addTrace bool) *ErrBuilder {
|
||||
return NewErrBuilder(CodeBuilderError,cause,fmt.Sprintf(fmtErrorFormatUnwrap,rtype.String()),addTrace) }
|
||||
|
||||
|
||||
func NewErrBuildLevel() *ErrBuilder {
|
||||
return &ErrBuilder{ code : CodeBuilderLevelError} }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func (e ErrBuilder) Code () string {
|
||||
return e.code }
|
||||
|
||||
func (e ErrBuilder) Error() string {
|
||||
return e.code }
|
||||
|
||||
|
||||
func (e ErrBuilder) Cause() error {
|
||||
return e.cause }
|
||||
|
||||
func (e ErrBuilder) Message() string {
|
||||
return e.message }
|
||||
|
||||
|
||||
func (e ErrBuilder) StackTrace () stack.Trace {
|
||||
return e.trace }
|
||||
|
||||
|
||||
func (e ErrBuilder) BuildFormat (builder Builder) {
|
||||
builder.Build(e.Error(),"core.errs.efmtr.ErrBuilder",func (fb FieldBuilder){
|
||||
if len(e.message) > 0 {
|
||||
fb.AddString(FieldNameMessage,e.message)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -0,0 +1,47 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name error_format_builder.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 fmtr
|
||||
|
||||
import "reflect"
|
||||
|
||||
type (
|
||||
|
||||
ErrorBuilder interface {
|
||||
BuildError(builder Builder, err error)
|
||||
}
|
||||
|
||||
|
||||
BuildErrorFnc func (builder Builder, err error)
|
||||
|
||||
)
|
||||
|
||||
|
||||
func (f BuildErrorFnc) BuildError(builder Builder, err error) {
|
||||
f(builder,err) }
|
||||
|
||||
|
||||
|
||||
func BasicBuildError (builder Builder, err error) {
|
||||
builder.Build(err.Error(),reflect.TypeOf(err).String(),nil) }
|
||||
|
||||
@ -0,0 +1,39 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name ErrorFormatter.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 fmtr
|
||||
|
||||
|
||||
type (
|
||||
|
||||
ErrorFormatter interface {
|
||||
FormatError(err error) []byte
|
||||
}
|
||||
|
||||
|
||||
FormatErrorFnc func (err error) []byte
|
||||
)
|
||||
|
||||
|
||||
func (f FormatErrorFnc) FormatError(err error) []byte {
|
||||
return f(err) }
|
||||
@ -0,0 +1,71 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name builder.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 fmtr
|
||||
|
||||
import "time"
|
||||
|
||||
type (
|
||||
|
||||
FieldBuilder interface {
|
||||
AddAny (name string, value any )
|
||||
AddInt (name string, value int )
|
||||
AddBool (name string, value bool )
|
||||
AddString(name string, value string )
|
||||
AddFloat (name string, value float64)
|
||||
AddTime (name string, value time.Time)
|
||||
AddDate (name string, value time.Time)
|
||||
}
|
||||
|
||||
BuildFieldFnc func (builder FieldBuilder)
|
||||
)
|
||||
|
||||
|
||||
|
||||
func AddAny(name string, value any) BuildFieldFnc {
|
||||
return func(b FieldBuilder) {
|
||||
b.AddAny(name,value) }}
|
||||
|
||||
func AddString(name, value string) BuildFieldFnc {
|
||||
return func(b FieldBuilder) {
|
||||
b.AddString(name,value) }}
|
||||
|
||||
func AddInt (name string, value int) BuildFieldFnc {
|
||||
return func(b FieldBuilder) {
|
||||
b.AddInt(name,value) }}
|
||||
|
||||
func AddFloat(name string, value float64) BuildFieldFnc {
|
||||
return func(b FieldBuilder) {
|
||||
b.AddFloat(name,value) }}
|
||||
|
||||
func AddBool (name string, value bool) BuildFieldFnc {
|
||||
return func(b FieldBuilder) {
|
||||
b.AddBool(name,value) }}
|
||||
|
||||
func AddTime (name string, value time.Time) BuildFieldFnc {
|
||||
return func(b FieldBuilder) {
|
||||
b.AddTime(name,value) }}
|
||||
|
||||
func AddDate (name string, value time.Time) BuildFieldFnc {
|
||||
return func(b FieldBuilder) {
|
||||
b.AddDate(name,value) }}
|
||||
@ -0,0 +1,36 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name format_buildable.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 fmtr
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
Formattable interface {
|
||||
|
||||
Format(builder Builder)
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@ -0,0 +1,26 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name active.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 ftext
|
||||
|
||||
|
||||
@ -0,0 +1,67 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name formtter.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 ftext
|
||||
|
||||
import "atypes/astrings"
|
||||
|
||||
type (
|
||||
|
||||
|
||||
Formatter struct {
|
||||
option
|
||||
|
||||
lays layouts
|
||||
secs Sections
|
||||
|
||||
nameTrans ,
|
||||
valueTrans,
|
||||
stringTrans astrings.Transformers
|
||||
|
||||
}
|
||||
|
||||
FormatterFnc func (formatter *Formatter)
|
||||
)
|
||||
|
||||
|
||||
func NewFormatter (options ...FormatterFnc) *Formatter {
|
||||
fm := &Formatter {
|
||||
option: optionsDefault,
|
||||
lays: *newLayouts(),
|
||||
}
|
||||
return fm
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
func (f *Formatter) FormatError (err error) []byte {
|
||||
|
||||
s := newState(f)
|
||||
if fe := s.format(err); fe != nil {
|
||||
|
||||
}
|
||||
|
||||
return s.b.Bytes()
|
||||
}
|
||||
|
||||
@ -0,0 +1,55 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name layouts.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 ftext
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
layouts struct {
|
||||
sep string
|
||||
indent string
|
||||
timeLayout string
|
||||
dateLayout string
|
||||
intLayout string
|
||||
floatLayout string
|
||||
boolLayout string
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
||||
func newLayouts () *layouts {
|
||||
return &layouts{
|
||||
indent : " ",
|
||||
sep : " : ",
|
||||
timeLayout : time.RFC822Z,
|
||||
dateLayout : time.RFC822Z,
|
||||
intLayout : "%d",
|
||||
floatLayout : "%-10s: %.2f",
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,76 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 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 ftext
|
||||
|
||||
|
||||
const (
|
||||
|
||||
ShowWrapped option = 1 << iota
|
||||
ShowType
|
||||
ShowAnsi
|
||||
ShowIndent
|
||||
ShowFields
|
||||
ShowCause
|
||||
ShowTrace
|
||||
NotShowEmpty
|
||||
|
||||
optionsDefault = ShowWrapped | ShowType | ShowAnsi | ShowIndent | ShowFields | ShowCause | ShowTrace | NotShowEmpty
|
||||
)
|
||||
|
||||
|
||||
type (
|
||||
|
||||
|
||||
option uint8
|
||||
)
|
||||
|
||||
|
||||
|
||||
func (s option) SetDefaults () option {
|
||||
return optionsDefault
|
||||
}
|
||||
|
||||
func (s option) ShowWrapped () bool {
|
||||
return s&ShowWrapped !=0 }
|
||||
|
||||
func (s option) ShowType () bool {
|
||||
return s&ShowType !=0 }
|
||||
|
||||
func (s option) ShowAnsi () bool {
|
||||
return s&ShowAnsi !=0 }
|
||||
|
||||
func (s option) ShowIndent () bool {
|
||||
return s&ShowIndent !=0 }
|
||||
|
||||
func (s option) ShowFields() bool {
|
||||
return s&ShowFields !=0 }
|
||||
|
||||
func (s option) ShowCause () bool {
|
||||
return s&ShowCause !=0 }
|
||||
|
||||
func (s option) ShowTrace () bool {
|
||||
return s&ShowTrace !=0 }
|
||||
|
||||
func (s option) NotShowEmpty() bool {
|
||||
return s&NotShowEmpty !=0 }
|
||||
@ -0,0 +1,45 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 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 ftext
|
||||
|
||||
import "ansi"
|
||||
|
||||
type (
|
||||
|
||||
Section struct {
|
||||
Ansi ansi.Code
|
||||
}
|
||||
|
||||
|
||||
)
|
||||
|
||||
|
||||
func (s Section) Begin () {
|
||||
|
||||
}
|
||||
|
||||
func (s Section) End () {
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,38 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 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 ftext
|
||||
|
||||
|
||||
type (
|
||||
|
||||
Sections struct {
|
||||
Main Section
|
||||
Head Section
|
||||
Fields Section
|
||||
Cause Section
|
||||
Trace Section
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@ -0,0 +1,279 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name formatter_stae.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 ftext
|
||||
|
||||
import (
|
||||
"ansi"
|
||||
"bytes"
|
||||
"errors"
|
||||
"errs/fmtr"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
state struct {
|
||||
f *Formatter
|
||||
b bytes.Buffer
|
||||
|
||||
section *Section
|
||||
level int
|
||||
ansiC ansi.Code
|
||||
unwrap bool
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
func newState(formatter *Formatter) *state {
|
||||
return &state{
|
||||
f : formatter,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (s *state) format (err error) (fe error) {
|
||||
|
||||
if fe = s.wError(err,0); fe != nil {
|
||||
return fe
|
||||
}
|
||||
|
||||
if fe = s.wCause(err,0); fe != nil {
|
||||
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (s *state) wError (err error, level int) (fe error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if s.level != level && !s.unwrap {
|
||||
ol := s.level
|
||||
s.level = level
|
||||
defer func(){ s.level = ol }()
|
||||
}
|
||||
|
||||
if eb,is := err.(fmtr.Formattable); is {
|
||||
eb.Format(s)
|
||||
} else {
|
||||
rt := reflect.TypeOf(err)
|
||||
if rt.Kind() == reflect.Pointer {
|
||||
rt = rt.Elem()
|
||||
}
|
||||
eb := fmtr.GetErrorTypeBuilder(rt)
|
||||
if eb != nil {
|
||||
eb.BuildError(s,err)
|
||||
return
|
||||
}
|
||||
|
||||
eb = fmtr.MakeErrorTypeBuilder(s,err)
|
||||
fmtr.SetErrorTypeBuilder(rt,eb)
|
||||
}
|
||||
|
||||
if !s.unwrap {
|
||||
s.unwrap = true
|
||||
}
|
||||
if fe = s.wError(errors.Unwrap(err), level); fe != nil {
|
||||
|
||||
return fe
|
||||
}
|
||||
|
||||
s.unwrap = false
|
||||
|
||||
return fe
|
||||
}
|
||||
|
||||
func (s *state) wCause(err error, level int ) (fe error){
|
||||
nx := err
|
||||
for nx != nil {
|
||||
|
||||
if cs, is := nx.(interface{ Cause() error }); is {
|
||||
if ce := cs.Cause(); ce != nil {
|
||||
s.b.WriteString("-Prev Cause --------------------\n")
|
||||
if fe := s.wError(ce, level+1); fe != nil {
|
||||
// llamamos al fallback del formateador
|
||||
}
|
||||
nx = errors.Unwrap(ce)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
var lc []error
|
||||
if cs, is := nx.(interface{ Causes() []error }); is {
|
||||
lc = cs.Causes()
|
||||
} else if me, ok := nx.(interface{ Unwrap() []error }); ok {
|
||||
lc = me.Unwrap()
|
||||
}
|
||||
if len(lc) > 0 {
|
||||
for i, e := range lc {
|
||||
s.b.WriteString(fmt.Sprintf("-%d---------------\n",i))
|
||||
if fe = s.wError(e,level+1); fe != nil {
|
||||
// llamamos al fallback del formateado
|
||||
}
|
||||
if fe = s.wCause(e,level+1); fe != nil {
|
||||
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
nx = errors.Unwrap(nx)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func getCauses (err error) []error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if cs,is := err.(interface { Cause() error }); is {
|
||||
ce := cs.Cause()
|
||||
if ce == nil {
|
||||
return nil
|
||||
}
|
||||
return []error{ce}
|
||||
}
|
||||
|
||||
if cs,is := err.(interface { Causes() []error }); is {
|
||||
return cs.Causes()
|
||||
}
|
||||
if cs,is := err.(interface{ Unwrap() []error }); is {
|
||||
return cs.Unwrap()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
func (s *state) applySection (sec *Section, begin bool) {
|
||||
if begin {
|
||||
s.section = sec
|
||||
s.section.Begin()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
func (s *state) indent () string {
|
||||
if s.level == 0 {
|
||||
return ""
|
||||
}
|
||||
return strings.Repeat(s.f.lays.indent,s.level)
|
||||
}
|
||||
|
||||
|
||||
func (s *state) Build(errCode, errType string, fbuilder fmtr.BuildFieldFnc) {
|
||||
if !s.unwrap {
|
||||
s.applySection(&s.f.secs.Head,true)
|
||||
s.writeField(fmtr.FieldNameError,errCode)
|
||||
if s.f.ShowType() {
|
||||
s.writeField(fmtr.FieldNameType, errType)
|
||||
}
|
||||
s.applySection(&s.f.secs.Head,false)
|
||||
}
|
||||
if fbuilder != nil {
|
||||
s.applySection(&s.f.secs.Fields,true)
|
||||
fbuilder(s)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (s *state) AddAny (name string, value any ) {
|
||||
if value == nil && s.f.NotShowEmpty() {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (s *state) AddInt (name string, value int ) {
|
||||
s.writeField(name,fmt.Sprintf(s.f.lays.intLayout,value))}
|
||||
|
||||
func (s *state) AddBool (name string, value bool ) {
|
||||
s.writeField(name,fmt.Sprintf(s.f.lays.boolLayout)) }
|
||||
|
||||
func (s *state) AddString(name string, value string ) {
|
||||
if len(value) == 0 && s.f.NotShowEmpty() {
|
||||
return
|
||||
}
|
||||
s.writeField(name,value) }
|
||||
|
||||
func (s *state) AddFloat (name string, value float64) {
|
||||
s.writeField(name,fmt.Sprintf(s.f.lays.floatLayout,value)) }
|
||||
|
||||
func (s *state) AddTime (name string, value time.Time) {
|
||||
if value.IsZero() && s.f.NotShowEmpty() {
|
||||
return
|
||||
}
|
||||
s.writeField(name,value.Format(s.f.lays.timeLayout)) }
|
||||
|
||||
func (s *state) AddDate (name string, value time.Time) {
|
||||
if value.IsZero() && s.f.NotShowEmpty() {
|
||||
return
|
||||
}
|
||||
s.writeField(name,value.Format(s.f.lays.dateLayout)) }
|
||||
|
||||
|
||||
|
||||
func (s *state) writeField (name string, value string) {
|
||||
|
||||
n := s.f.nameTrans.Apply(name)
|
||||
v := s.f.valueTrans.Apply(value)
|
||||
|
||||
s.b.WriteString(s.indent())
|
||||
if s.ansiC == 0 {
|
||||
s.b.WriteString(n)
|
||||
s.b.WriteString(s.f.lays.sep)
|
||||
s.b.WriteString(v)
|
||||
s.b.WriteByte('\n')
|
||||
return
|
||||
}
|
||||
|
||||
s.b.WriteString(s.ansiC.String())
|
||||
s.b.WriteString(n)
|
||||
s.b.WriteString(s.f.lays.sep)
|
||||
s.b.WriteString(v)
|
||||
s.b.WriteByte('\n')
|
||||
s.b.WriteString(s.ansiC.End())
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in new issue