parent
0d0e220350
commit
8e20678ed0
@ -0,0 +1,150 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name fmt.go
|
||||
// Created by DEV
|
||||
// Modified 12/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 err_text_fmt
|
||||
|
||||
import (
|
||||
"ansi"
|
||||
"atypes/astrings"
|
||||
"errs/efmt"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
TextFormatter struct {
|
||||
options efmt.BuilderOption
|
||||
|
||||
vLayouts layouts
|
||||
sections sections
|
||||
|
||||
nameTrans ,
|
||||
valueTrans,
|
||||
stringTrans astrings.Transformers
|
||||
|
||||
fbOnError ,
|
||||
fbOnUnwrap,
|
||||
fbOnCause efmt.BuilderErrorFallback
|
||||
|
||||
frameBuilder efmt.FrameBuilder
|
||||
}
|
||||
|
||||
TextFormatterFnc func(f *TextFormatter)
|
||||
)
|
||||
|
||||
|
||||
|
||||
func NewTextFormatter(options ...TextFormatterFnc) *TextFormatter {
|
||||
fm := &TextFormatter{
|
||||
options : efmt.BuilderOption(0).SetDefaults(),
|
||||
|
||||
vLayouts : *newLayouts(),
|
||||
sections : sections{},
|
||||
|
||||
|
||||
fbOnError : efmt.MakeErrorFallback(fbBuildError),
|
||||
fbOnUnwrap: efmt.MakeErrorFallback(fbUnwrapError),
|
||||
fbOnCause : efmt.MakeErrorFallback(fbUnwrapError),
|
||||
|
||||
frameBuilder: efmt.BuildFrameFnc(defaultFrameBuilder),
|
||||
}
|
||||
for _,o := range options {
|
||||
o(fm)
|
||||
}
|
||||
return fm
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (f *TextFormatter) Format (err error) []byte {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
st := &state{
|
||||
f : f,
|
||||
}
|
||||
|
||||
fe := st.writeError(err,f.options.ShowWrapped(),f.options.ShowCause(),f.options.ShowTrace(),0)
|
||||
if fe == nil {
|
||||
return st.b.Bytes()
|
||||
}
|
||||
st.reset()
|
||||
if fe = st.writeError(fe,false,true,false,0); fe == nil{
|
||||
return st.b.Bytes()
|
||||
}
|
||||
|
||||
return []byte(f.fbOnError(err))
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func WithLayout (name, layout string) TextFormatterFnc {
|
||||
return func(f *TextFormatter) {
|
||||
switch name {
|
||||
case "time":
|
||||
f.vLayouts.timeLayout = layout
|
||||
case "date":
|
||||
f.vLayouts.dateLayout = layout
|
||||
case "int":
|
||||
f.vLayouts.intLayout = layout
|
||||
case "float":
|
||||
f.vLayouts.floatLayout = layout
|
||||
case "bool":
|
||||
f.vLayouts.boolLayout = layout
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func WithShowOptions (option efmt.BuilderOption,enabled bool) TextFormatterFnc {
|
||||
return func(f *TextFormatter) {
|
||||
if enabled {
|
||||
f.options |= option
|
||||
return
|
||||
}
|
||||
f.options &^=option
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func WithSectionFormat(sec string, before, after efmt.BuildFnc, ansiCode ansi.Code) TextFormatterFnc {
|
||||
return func(f *TextFormatter) {
|
||||
var s *sectionFormat
|
||||
switch sec {
|
||||
case "err", "" : s = &f.sections.err
|
||||
case "head" : s = &f.sections.head
|
||||
case "fields" : s = &f.sections.fields
|
||||
case "cause" : s = &f.sections.cause
|
||||
case "trace" : s = &f.sections.trace
|
||||
default:
|
||||
return
|
||||
}
|
||||
s.before = before
|
||||
s.after = after
|
||||
s.ansi = ansiCode
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,42 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name error_formatter.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 efmt
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
ErrorFormatter interface {
|
||||
|
||||
Format(err error) []byte
|
||||
}
|
||||
|
||||
|
||||
FormatErrorFnc func (err error) []byte
|
||||
)
|
||||
|
||||
|
||||
func (f FormatErrorFnc) Format(err error) []byte {
|
||||
return f(err) }
|
||||
|
||||
@ -1,51 +0,0 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name field_format.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 fmx
|
||||
|
||||
import (
|
||||
"ansi"
|
||||
"atypes/astrings"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
fieldFormat struct {
|
||||
NameAnsi ansi.Code
|
||||
ValueAnsi ansi.Code
|
||||
NameTrans astrings.Transformers
|
||||
ValueTrans astrings.Transformers
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
func (f fieldFormat) clone() fieldFormat {
|
||||
return fieldFormat{
|
||||
NameAnsi: f.NameAnsi,
|
||||
ValueAnsi: f.ValueAnsi,
|
||||
NameTrans: append(astrings.Transformers(nil),f.NameTrans...),
|
||||
ValueTrans: append(astrings.Transformers(nil),f.ValueTrans...),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,97 +0,0 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name fmt.go
|
||||
// Created by DEV
|
||||
// Modified 12/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 fmx
|
||||
|
||||
import (
|
||||
"atypes/astrings"
|
||||
"errs/err_fmt"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
TextFmt struct {
|
||||
options err_fmt.BuilderOption
|
||||
|
||||
vLayouts layouts
|
||||
sections sections
|
||||
defField fieldFormat
|
||||
fields map[string]*fieldFormat
|
||||
|
||||
stringTrans astrings.Transformers
|
||||
|
||||
fbOnError ,
|
||||
fbOnUnwrap,
|
||||
fbOnCause err_fmt.BuilderErrorFallback
|
||||
|
||||
frameBuilder err_fmt.FrameBuilder
|
||||
}
|
||||
|
||||
TextFmtFnc func(f *TextFmt)
|
||||
)
|
||||
|
||||
|
||||
|
||||
func NewTextFmt (options ...TextFmtFnc) *TextFmt {
|
||||
fm := &TextFmt{
|
||||
options : err_fmt.BuilderOption(0).SetDefaults(),
|
||||
|
||||
vLayouts : *newLayouts(),
|
||||
sections : sections{},
|
||||
|
||||
|
||||
fbOnError : err_fmt.MakeErrorFallback(fbBuildError),
|
||||
fbOnUnwrap: err_fmt.MakeErrorFallback(fbUnwrapError),
|
||||
fbOnCause : err_fmt.MakeErrorFallback(fbUnwrapError),
|
||||
|
||||
frameBuilder: err_fmt.BuildFrameFnc(defaultFrameBuilder),
|
||||
}
|
||||
for _,o := range options {
|
||||
o(fm)
|
||||
}
|
||||
return fm
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (f *TextFmt) Format (err error) []byte {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
st := &state{
|
||||
f : f,
|
||||
}
|
||||
|
||||
fe := st.writeError(err,f.options.ShowWrapped(),f.options.ShowCause(),f.options.ShowTrace(),0)
|
||||
if fe == nil {
|
||||
return st.b.Bytes()
|
||||
}
|
||||
st.reset()
|
||||
if fe = st.writeError(fe,false,true,false,0); fe == nil{
|
||||
return st.b.Bytes()
|
||||
}
|
||||
|
||||
return []byte(f.fbOnError(err))
|
||||
}
|
||||
|
||||
|
||||
@ -1,87 +0,0 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name fmt_ops.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 fmx
|
||||
|
||||
func WithLayout (name, layout string) TextFmtFnc {
|
||||
return func(f *TextFmt) {
|
||||
switch name {
|
||||
case "time":
|
||||
f.vLayouts.timeLayout = layout
|
||||
case "date":
|
||||
f.vLayouts.dateLayout = layout
|
||||
case "int":
|
||||
f.vLayouts.intLayout = layout
|
||||
case "float":
|
||||
f.vLayouts.floatLayout = layout
|
||||
case "bool":
|
||||
f.vLayouts.boolLayout = layout
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
func WithShowOptions (option erfmt.BuilderOption,enabled bool) FmtTextFnc {
|
||||
return func(f *ErrTextFormatter) {
|
||||
if enabled {
|
||||
f.options |= option
|
||||
return
|
||||
}
|
||||
f.options &^=option
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func WithSectionFormat(id int, config func (s *SectionFormat)) FmtTextFnc {
|
||||
if id < DefaultSection && id > TraceSection {
|
||||
return func(_ *ErrTextFormatter){}
|
||||
}
|
||||
return func(f *ErrTextFormatter) {
|
||||
config(&f.secs[id])
|
||||
}
|
||||
}
|
||||
|
||||
func WithFieldFormat(field string, config func(format *FieldFormat)) FmtTextFnc {
|
||||
return func(f *ErrTextFormatter) {
|
||||
if field == "" || field == "*" {
|
||||
config(&f.defFieldFormat)
|
||||
return
|
||||
}
|
||||
if f.fieldsFormat == nil {
|
||||
f.fieldsFormat = make(map[string]FieldFormat)
|
||||
}
|
||||
fm := FieldFormat{}
|
||||
config(&fm)
|
||||
if strings.Index(field,",") == -1 {
|
||||
f.fieldsFormat[field]=fm
|
||||
return
|
||||
}
|
||||
for _,fn := range strings.SplitAfter(field,",") {
|
||||
f.fieldsFormat[fn]=fm
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
@ -1,91 +0,0 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name state_builder.go
|
||||
// Created by DEV
|
||||
// Modified 12/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 fmx
|
||||
|
||||
import (
|
||||
"errs/err_fmt"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (s *state) AddText(str string) {}
|
||||
|
||||
func (s *state) AddHead(errStr, errType string) {
|
||||
if s.curSec > 0 {
|
||||
return
|
||||
}
|
||||
s.f.sections.head.doBefore(s)
|
||||
s.curSec = 1
|
||||
s.ansi = s.f.sections.head.ansi
|
||||
s.writeField(err_fmt.FieldNameError,errStr)
|
||||
s.writeField(err_fmt.FieldNameType ,errType)
|
||||
s.f.sections.head.doAfter (s)
|
||||
}
|
||||
|
||||
|
||||
func (s *state) AddFieldString (name string, value string ) {
|
||||
if s.curSec != 1 {
|
||||
return
|
||||
}
|
||||
s.writeField(name,value)
|
||||
}
|
||||
|
||||
func (s *state) AddFieldInt (name string, value int ) {
|
||||
if s.curSec != 1 {
|
||||
return
|
||||
}
|
||||
s.writeField(name,fmt.Sprintf(s.f.vLayouts.intLayout,value))
|
||||
}
|
||||
|
||||
func (s *state) AddFieldFloat (name string, value float64 ) {
|
||||
if s.curSec != 1 {
|
||||
return
|
||||
}
|
||||
s.writeField(name,fmt.Sprintf(s.f.vLayouts.floatLayout,value))
|
||||
}
|
||||
|
||||
func (s *state) AddFieldBool (name string, value bool ) {
|
||||
if s.curSec != 1 {
|
||||
return
|
||||
}
|
||||
s.writeField(name,fmt.Sprintf(s.f.vLayouts.boolLayout,value))
|
||||
}
|
||||
|
||||
func (s *state) AddFieldTime (name string, value time.Time) {
|
||||
if s.curSec != 1 {
|
||||
return
|
||||
}
|
||||
s.writeField(name,value.Format(s.f.vLayouts.timeLayout))
|
||||
}
|
||||
|
||||
func (s *state) AddFieldDate (name string, value time.Time) {
|
||||
if s.curSec != 1 {
|
||||
return
|
||||
}
|
||||
s.writeField(name,value.Format(s.f.vLayouts.dateLayout))
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in new issue