incluidas las librerias de transformacion de strings

master
DEV 3 years ago
parent 32a497aef4
commit d11c5133d7

@ -45,8 +45,8 @@ func (m MiCustomErr) Unwrap () error {
func main() {
ercode := errs.ErrCode("error->err-code")
fmt.Print(errs.ToFormat(ercode,nil))
fmt.Print("\n")
//fmt.Print(errs.ToFormat(ercode,nil))
//fmt.Print("\n")
erpanic := errs.NewErrPanic("error->err-mensaje-panico")
fmt.Print(errs.ToFormat(erpanic,nil))

@ -33,17 +33,22 @@ import (
"sync"
)
const (
CodePanicError string = "error-panic"
CodeGroupError string = "group-error"
CodeParamNilError string = "param-nil"
CodeParamInvalidError string = "param-invalid"
CodeParamOutOfRangeError string = "param-out-of-range"
)
var (
G = struct {
mux sync.RWMutex
Formatter fmts.Formatter
Formatter fmts.Formatter
ErrGroup ErrCode
ErrPanic ErrCode
ErrParamNil ErrCode
ErrParamInvalid ErrCode
ErrParamOutOfRange ErrCode
@ -51,18 +56,15 @@ var (
stackTraceRType reflect.Type
}{
ErrGroup : ErrCode("group-error"),
ErrPanic : ErrCode("panic-error"),
ErrParamNil : ErrCode("param-nil"),
ErrParamNil : ErrCode("param-CodePanicError"),
ErrParamInvalid : ErrCode("param-invalid"),
ErrParamOutOfRange : ErrCode("param-out-of-range"),
stackTraceRType : reflect.TypeOf(stack.Trace(nil)),
Formatter : fmts.NewFmtText(func (f *fmts.FmtText) {
f.FixFieldLen = 13
f.StringOnCause = "Cause ------------------------\n"
f.StringOnError = "Error ------------------------\n"
},
fmts.FmtTextWithNameTransformer(
astrings.ToKebab,

@ -157,12 +157,12 @@ func ErrInfoWithInfo (info string, params ...any) ErrInfoFnc {
}
}
/*
func ErrInfoWithStackTrace (offset int) ErrInfoFnc {
return func (e *ErrInfo) {
e.error = *NewErrTrace(e.error,offset) }
}
*/
func ErrInfoWithMeta (name string, value string) ErrInfoFnc {

@ -28,22 +28,26 @@ import "errs/fmts"
type (
ErrPanic struct {
error
cause error
}
)
func NewErrPanic (err any) *ErrPanic {
if err == nil {
return nil
}
return &ErrPanic{ error: ToErr(err) }
return &ErrPanic{ cause: ToErr(err) }
}
func (e ErrPanic) Error () string {
return CodePanicError }
func (e ErrPanic) Unwrap () error {
return e.error }
return nil }
func (e ErrPanic) Cause () error {
return e.cause }
@ -51,10 +55,6 @@ func (e ErrPanic) WrapWithInfo (options ...ErrInfoFnc) error {
return WrapWithInfo(e,options...) }
func (e ErrPanic) WrapWithCause(cause error) error {
return WrapWithCause(e,cause) }
func (e ErrPanic) WrapWithTrace(offset int) error {
return WrapWithTrace(e,offset) }
@ -66,6 +66,6 @@ func (e ErrPanic) String () string {
func (e ErrPanic) BuildFormat (builder fmts.FormatBuilder) {
builder.AddCode (e.Error())
builder.AddCode (CodePanicError)
builder.AddType ("core.errs.ErrPanic")
}

@ -25,14 +25,14 @@ package errs
import (
"errs/fmts"
stack2 "errs/stack"
"errs/stack"
)
type (
ErrTrace struct {
error
stack stack2.Trace
stack stack.Trace
}
)
@ -41,10 +41,10 @@ func NewErrTrace (err error, offset int) *ErrTrace {
if err == nil {
return nil
}
var st stack2.Trace
tc,is := err.(stack2.Tracer)
var st stack.Trace
tc,is := err.(stack.Tracer)
if !is {
st = stack2.MakeStackTrace(offset)
st = stack.MakeStackTrace(offset)
} else {
st = tc.StackTrace()
}
@ -59,7 +59,7 @@ func (e ErrTrace) Unwrap() error {
return e.error }
func (e ErrTrace) StackTrace () stack2.Trace {
func (e ErrTrace) StackTrace () stack.Trace {
return e.stack }

@ -26,15 +26,17 @@ package fmts
var (
G = struct {
ErrBuilderCode string
ErrInvalidLevelCode string
CodeBuilderError string
CodeInvalidLevelError string
}{
ErrBuilderCode : "format-builder-error",
ErrInvalidLevelCode : "invalid-level",
CodeBuilderError: "format-builder-error",
CodeInvalidLevelError: "format-invalid-level",
}
)

@ -64,10 +64,7 @@ type (
FieldNameColor FmtTextColor
FieldValueColor FmtTextColor
StringOnError string
StringOnCause string
StringOnWrapped string
StringOnTrace string
Lines [4]*fmtTextSection
StringTransformers astrings.Transformers
ValueTransformers astrings.Transformers
@ -116,16 +113,16 @@ func (f *FmtText) Format (err error) []byte {
}
st := newFmtTextState(f)
defer st.Reset()
defer st.reset()
st.format(err)
if st.error != nil {
e := st.error
st.Reset()
st.reset()
st.format(e)
}
return st.Bytes()
return st.buffer.Bytes()
}

@ -2,9 +2,9 @@
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name format_head_builder.go
// File name fmt_lines.go
// Created by DEV
// Modified 02/04/2024
// Modified 03/04/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
@ -21,14 +21,15 @@
// limitations under the License.
// ------------------------------------------------------------------------
package fms
package fmts
type (
FormatHeadBuilder interface {
FormatStringBuilder
AddCode (code string)
AddType (rtyp string)
fmtTextSection struct {
startLines []string
endLines []string
color FmtTextColor
}
)

@ -52,7 +52,7 @@ func newFmtTextState(f *FmtText) *fmtTextState {
func (s *fmtTextState) Reset () {
func (s *fmtTextState) reset() {
s.buffer.Reset()
s.level = 0
s.error = nil
@ -60,44 +60,40 @@ func (s *fmtTextState) Reset () {
func (s *fmtTextState) Level () int {
return s.level }
func (s *fmtTextState) Error () error {
return s.error }
func (s *fmtTextState) Bytes () []byte {
return s.buffer.Bytes() }
func (s *fmtTextState) AddError (err error) {
if err == nil {
return
}
}
func (s *fmtTextState) AddCause (err error) {
func (s *fmtTextState) addCause(err error) {
if err == nil || !s.f.ShowCause() {
return
}
s.addLevel()
defer s.decLevel()
s.writeString(s.indent)
s.writeString(s.f.StringOnCause)
w := s.isWrapped
if w {
s.isWrapped = false
defer func(){
s.isWrapped = true
}()
}
s.format(err)
}
func (s *fmtTextState) AddTrace (trace stack.Trace) {
func (s *fmtTextState) addTrace(trace stack.Trace) {
if trace == nil || !s.f.ShowTrace() {
return
}
}
func (s *fmtTextState) AddWrapped(err error) {
func (s *fmtTextState) addWrapped(err error) {
if err == nil || !s.f.ShowWrapped() {
return
}
@ -182,12 +178,12 @@ func (s *fmtTextState) format (er error) {
if ne,is := re.(error); is {
ie = ne
} else {
ie = fmt.Errorf(G.ErrBuilderCode,"%v",re)
ie = fmt.Errorf(G.CodeBuilderError,"%v",re)
}
}
s.error = *NewErrFormat(G.ErrBuilderCode,ie)
s.error = *NewErrFormat(G.CodeBuilderError,ie)
} else {
s.error = *NewErrFormat(G.ErrBuilderCode,s.error)
s.error = *NewErrFormat(G.CodeBuilderError,s.error)
}
}
}(&dn, s)
@ -208,16 +204,17 @@ func (s *fmtTextState) format (er error) {
}
}
s.AddWrapped(errors.Unwrap(er))
s.addWrapped(errors.Unwrap(er))
if cs,is := er.(interface{ Cause() error }); is {
s.AddCause(cs.Cause())
s.addCause(cs.Cause())
}
if s.level == 0 {
s.AddTrace (stack.UnwrapTrace(er,true))
s.addTrace(stack.UnwrapTrace(er,true))
}
dn=true
}

@ -24,30 +24,14 @@
package fmts
import (
"errs/stack"
"time"
)
type (
FormatBuilder interface {
Reset ()
Level () int
Error () error
Bytes () []byte
AddError (err error)
AddCause (err error)
AddWrapped(err error)
AddCode (code string)
AddType (ter string )
AddTrace (trace stack.Trace)
AddString (str string)
AddStrings (str []string)
AddCode (code string)
AddType (ter string )
AddFieldString (name string, value string )
AddFieldInt (name string, value int )

@ -0,0 +1,53 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name active.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 text
const (
ErrorSection int = iota
FieldsSection
CauseSection
TraceSection
)
const (
stringsTrans int = iota
fieldNameTrans
fieldValueTrans
)
var (
G = struct {
}{
}
)

@ -0,0 +1,61 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name color.go
// Created by DEV
// Modified 27/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 text
import "fmt"
const (
ColorBlack Color = iota + 30
ColorRed
ColorGreen
ColorYellow
ColorBlue
ColorMagenta
ColorCyan
ColorWhite
)
type (
Color uint8
)
func (c Color) BeginColorString() string {
return fmt.Sprintf("\u001B[%dm",uint8(c))
}
func (_ Color) EndColorString () string {
return "\x1b[0m" }
func (c Color) Sprintf(fm string, params ...any) string {
return fmt.Sprintf("\x1b[%dm%s\x1b[0m", uint8(c),fmt.Sprintf(fm,params...)) }
func (c Color) IsValid() bool {
return c >= ColorBlack && c <= ColorWhite
}

@ -0,0 +1,171 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name format_text.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 text
import (
"atypes/astrings"
"errs/fmts"
"time"
)
const (
FormatShowWrapped uint8 = 1 << iota
FormatShowType
FormatShowColors
FormatShowIndent
FormatShowDetails
FormatShowCause
FormatShowTrace
FormatShowFieldEmpty
)
type (
FmtText struct {
Show uint8
Indent string
FieldSep string
FixFieldLen int
MaxStringLen int
TimeLayout string
DateLayout string
IntLayout string
FloatLayout string
Color Color
CodeColor Color
FieldNameColor Color
FieldValueColor Color
sections sections
transformers transformers
builders *fmts.TypeFormatBuilders
extractors *fmts.FieldExtractors
}
FmtTextFnc func (fm *FmtText)
)
func NewFmtText(options ...FmtTextFnc) *FmtText {
f := &FmtText{
Show : FormatShowCause | FormatShowWrapped | FormatShowDetails | FormatShowDetails,
Indent : " ",
FieldSep : " : ",
TimeLayout : time.RFC822Z,
DateLayout : time.RFC822Z,
IntLayout : "%d",
FloatLayout: "%-10s: %.2f",
builders : fmts.NewTypeBuilders(),
extractors : DefaultsExtractors(),
}
for _,o := range options {
o(f)
}
if f.FixFieldLen > 0 {
f.transformers.fieldName = append(f.transformers.fieldName,func(s string)string {
return astrings.Pad(s,f.FixFieldLen,' ',true)
})
}
return f
}
func (f *FmtText) Format (err error) []byte {
if err == nil {
return nil
}
st := newFmtTextState(f)
defer st.reset()
st.format(err)
if st.error != nil {
e := st.error
st.reset()
st.format(e)
}
return st.buffer.Bytes()
}
func (f *FmtText) ShowWrapped () bool {
return f.Show&FormatShowWrapped !=0 }
func (f *FmtText) ShowType () bool {
return f.Show&FormatShowType !=0 }
func (f *FmtText) ShowColors () bool {
return f.Show&FormatShowColors !=0 }
func (f *FmtText) ShowIndent () bool {
return f.Show&FormatShowIndent !=0 }
func (f *FmtText) ShowDetails() bool {
return f.Show&FormatShowDetails !=0 }
func (f *FmtText) ShowCause () bool {
return f.Show&FormatShowCause !=0 }
func (f *FmtText) ShowTrace () bool {
return f.Show&FormatShowTrace !=0 }
func (f *FmtText) ShowFieldEmpty () bool {
return f.Show&FormatShowFieldEmpty !=0 }
func FmtTextWithNameLenTransformer(len int) FmtTextFnc {
return func(f *FmtText) {
f.transformers.fieldName = append(f.transformers.fieldName,func(s string)string {
return astrings.Pad(s,len,' ',true)
})
}
}
func FmtTextWithNameTransformer (transformer ...astrings.Transform) FmtTextFnc {
return func(f *FmtText) {
f.transformers.fieldName = append(f.transformers.fieldName,transformer...)
}
}

@ -2,9 +2,9 @@
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name format_string_builder.go
// File name fmt_lines.go
// Created by DEV
// Modified 02/04/2024
// Modified 03/04/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
@ -21,15 +21,15 @@
// limitations under the License.
// ------------------------------------------------------------------------
package fms
package text
type (
FormatStringBuilder interface {
AddLine (line string)
AddLineF (line string, params ...any)
AddLines (line ...string)
section struct {
startLines []string
endLines []string
color Color
}
)

@ -2,9 +2,9 @@
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name format_builder.go
// File name sections.go
// Created by DEV
// Modified 02/04/2024
// Modified 04/04/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
@ -21,20 +21,20 @@
// limitations under the License.
// ------------------------------------------------------------------------
package fms
package text
type (
FormatBuilder interface {
FormatStringBuilder
BuildHead (headbuilder FormatHeadBuilder)
BuildBody (bbuilder FormatBodyBuilder)
sections struct {
err *section
fields *section
cause *section
trace *section
}
)

@ -0,0 +1,247 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name format_text_builder.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 text
import (
"bytes"
"errors"
"errs/stack"
"fmt"
"reflect"
"strings"
"time"
)
type (
fmtTextState struct {
f *FmtText
buffer bytes.Buffer
level int
indent string
error error
isWrapped bool
}
)
func newFmtTextState(f *FmtText) *fmtTextState {
return &fmtTextState{ f : f } }
func (s *fmtTextState) reset() {
s.buffer.Reset()
s.level = 0
s.error = nil
}
func (s *fmtTextState) AddError (err error) {
if err == nil {
return
}
}
func (s *fmtTextState) addCause(err error) {
if err == nil || !s.f.ShowCause() {
return
}
s.addLevel()
defer s.decLevel()
s.writeString(s.indent)
s.writeString(s.f.StringOnCause)
w := s.isWrapped
if w {
s.isWrapped = false
defer func(){
s.isWrapped = true
}()
}
s.format(err)
}
func (s *fmtTextState) addTrace(trace stack.Trace) {
if trace == nil || !s.f.ShowTrace() {
return
}
}
func (s *fmtTextState) addWrapped(err error) {
if err == nil || !s.f.ShowWrapped() {
return
}
if !s.isWrapped {
s.isWrapped = true
defer func(st *fmtTextState) { s.isWrapped = false }(s)
}
s.format(err)
}
func (s *fmtTextState) AddCode (code string) {
if s.isWrapped {
return
}
s.writeField("error", s.f.ValueTransformers.Apply(code))
}
func (s *fmtTextState) AddType (ter string ) {
if s.isWrapped {
return
}
s.writeField("error-type",ter)
}
func (s *fmtTextState) AddString (str string) {
s.writeString(s.f.StringTransformers.Apply(str))
}
func (s *fmtTextState) AddStrings(str []string) {
for _,st := range str {
s.writeString(s.f.StringTransformers.Apply(st))
}
}
func (s *fmtTextState) AddFieldString (name string, value string ) {
value = s.f.ValueTransformers.Apply(value)
if len(value) == 0 && !s.f.ShowFieldEmpty() {
return
}
s.writeField(name,value) }
func (s *fmtTextState) AddFieldInt (name string, value int ) {
s.writeField(name,fmt.Sprintf(s.f.IntLayout,value)) }
func (s *fmtTextState) AddFieldFloat (name string, value float64 ) {
s.writeField(name,fmt.Sprintf(s.f.FloatLayout,value)) }
func (s *fmtTextState) AddFieldTime (name string, value time.Time) {
s.writeField(name,value.Format(s.f.TimeLayout)) }
func (s *fmtTextState) AddFieldDate (name string, value time.Time) {
s.writeField(name,value.Format(s.f.DateLayout)) }
func (s *fmtTextState) writeField (name string, value string) {
name = s.f.NameTransformers.Apply (name)
s.writeString(fmt.Sprintf("%s%s%s%s\n",s.indent,name,s.f.FieldSep,value))
}
func (s *fmtTextState) writeString(str string) {
s.buffer.WriteString(str)
}
func (s *fmtTextState) format (er error) {
dn := false
defer func(d *bool, s *fmtTextState){
if !(*d) {
if s.error == nil {
var ie error
if re := recover(); re != nil {
if ne,is := re.(error); is {
ie = ne
} else {
ie = fmt.Errorf(G.CodeBuilderError,"%v",re)
}
}
s.error = *NewErrFormat(G.CodeBuilderError,ie)
} else {
s.error = *NewErrFormat(G.CodeBuilderError,s.error)
}
}
}(&dn, s)
if fm,is := er.(Formattable); is {
fm.BuildFormat(s)
} else {
rt := reflect.TypeOf(er)
if rt.Kind() == reflect.Pointer {
rt = rt.Elem()
}
if fb := s.f.builders.TypeFormatBuilder(rt); fb != nil {
fb.BuildFormatError(s,er)
} else {
s.f.builders.Set(rt,s.f.extractors.BuildAndCreate(s,er))
}
}
s.addWrapped(errors.Unwrap(er))
if cs,is := er.(interface{ Cause() error }); is {
s.addCause(cs.Cause())
}
if s.level == 0 {
s.addTrace(stack.UnwrapTrace(er,true))
}
dn=true
}
func (s *fmtTextState) decLevel() {
s.level--
if s.level <= 0 {
s.indent = ""
return
}
if s.level == 1 {
s.indent = s.f.Indent
return
}
s.indent = strings.Repeat(s.f.Indent,s.level)
}
func (s *fmtTextState) addLevel() {
s.level++
if s.level == 1 {
s.indent = s.f.Indent
return
}
s.indent = strings.Repeat(s.f.Indent,s.level)
}

@ -2,9 +2,9 @@
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name format_body_builder.go
// File name transformers.go
// Created by DEV
// Modified 02/04/2024
// Modified 04/04/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
@ -21,20 +21,17 @@
// limitations under the License.
// ------------------------------------------------------------------------
package fms
package text
import "time"
import "atypes/astrings"
type (
FormatBodyBuilder interface {
FormatStringBuilder
AddFieldString (name string, value string )
AddFieldInt (name string, value int )
AddFieldFloat (name string, value float64)
AddFieldTime (name string, value time.Time)
AddFieldDate (name string, value time.Time)
transformers struct {
strings astrings.Transformers
fieldName astrings.Transformers
fieldValue astrings.Transformers
}
)
)
Loading…
Cancel
Save

Powered by TurnKey Linux.