master
parent
7a621e5c17
commit
4e932c6e9e
@ -0,0 +1,81 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project active3
|
||||
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name labels.go
|
||||
// ✍️ Created by DEV
|
||||
// 📅 Modified 06/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 erstfy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"typs/strs"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
Labels struct {
|
||||
labelSep string
|
||||
lineLabelFmt string
|
||||
labelFmt string
|
||||
|
||||
labelTrans strs.Transformers
|
||||
labels map[string]string
|
||||
}
|
||||
)
|
||||
|
||||
func newLabels() *Labels {
|
||||
lb := &Labels {
|
||||
labels : map[string]string{
|
||||
"message":"message",
|
||||
"info" : "info",
|
||||
"date" : "date",
|
||||
"desc" : "desc",
|
||||
"time" : "time",
|
||||
"help" : "help",
|
||||
"link" : "link",
|
||||
"cause" : "cause",
|
||||
"trace:": "trace,",
|
||||
"param:": "param,",
|
||||
"source": "source",
|
||||
"user" : "user",
|
||||
},
|
||||
labelSep: labelSep,
|
||||
labelFmt: "\t%-12s" + labelSep,
|
||||
}
|
||||
lb.lineLabelFmt = lb.labelFmt + "%s\n"
|
||||
return lb
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (l *Labels) GetLabel (label string) string {
|
||||
ll := strings.ToLower(label)
|
||||
if lb,hs := l.labels[ll]; hs {
|
||||
return lb
|
||||
}
|
||||
l.labels[ll]=label
|
||||
return label
|
||||
}
|
||||
|
||||
|
||||
func (l *Labels) FormatLabel (label string) string {
|
||||
return fmt.Sprintf(l.labelFmt,l.labelTrans.Transform(l.GetLabel(label))) }
|
||||
@ -0,0 +1,131 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project active3
|
||||
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name stringify.go
|
||||
// ✍️ Created by DEV
|
||||
// 📅 Modified 06/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 erstfy
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"typs/strs"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
Stringify struct {
|
||||
labels Labels
|
||||
useLabels bool
|
||||
lineErrorFmt string
|
||||
lineTextFmt string
|
||||
trans strs.Transformers
|
||||
}
|
||||
|
||||
StringifyFnc func (stringify *Stringify)
|
||||
)
|
||||
|
||||
|
||||
|
||||
func NewStringify(options ...StringifyFnc) *Stringify {
|
||||
s := &Stringify{
|
||||
labels : *newLabels(),
|
||||
useLabels: true,
|
||||
lineTextFmt : "\t%s\n",
|
||||
lineErrorFmt: "ERROR %s\n",
|
||||
}
|
||||
for _,o := range options {
|
||||
o(s)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
|
||||
func (s *Stringify) ErrorToString(err error, lines ...string) string {
|
||||
ls := append([]string { "" },lines...)
|
||||
var nl string
|
||||
ne := err
|
||||
for ne != nil {
|
||||
if st,is := ne.(stringer); is {
|
||||
ls = append(ls,st.String())
|
||||
}
|
||||
if uw,is := ne.(unwrapper); is {
|
||||
ne = uw.Unwrap()
|
||||
} else {
|
||||
ne = nil
|
||||
if strings.Index(nl,labelError) != -1 {
|
||||
ls[0]= nl
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(ls[0]) == 0 {
|
||||
ls[0]=s.ErrorLine(err.Error())
|
||||
}
|
||||
if len(ls) == 1 {
|
||||
return ls[0]
|
||||
}
|
||||
b := bytes.Buffer{}
|
||||
for _,l := range ls {
|
||||
b.WriteString(l)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
|
||||
func (s *Stringify) ErrorLine(strErr string) string {
|
||||
if len(s.lineErrorFmt) == 0 {
|
||||
if s.useLabels {
|
||||
return s.LabelLine("error", strErr)
|
||||
}
|
||||
return s.TextLine(strErr)
|
||||
}
|
||||
return fmt.Sprintf(s.lineErrorFmt,s.trans.Transform(strErr))
|
||||
}
|
||||
|
||||
|
||||
func (s *Stringify) TextLine (text string) string {
|
||||
if len(s.lineTextFmt) == 0 {
|
||||
return s.trans.Transform(text) + "\n"
|
||||
}
|
||||
return fmt.Sprintf(s.lineTextFmt,s.trans.Transform(text))
|
||||
}
|
||||
|
||||
|
||||
func (s *Stringify) LabelLine(label, text string) string {
|
||||
if !s.useLabels || len(label) == 0 {
|
||||
return s.TextLine(text)
|
||||
}
|
||||
return fmt.Sprintf("%s%s\n",s.labels.FormatLabel(label),s.trans.Transform(text))
|
||||
}
|
||||
|
||||
|
||||
func (s *Stringify) FormatLineAppender (label string, appendFn func(d []byte) []byte) []byte {
|
||||
if !s.useLabels || len(label) == 0 {
|
||||
return appendFn(nil)
|
||||
}
|
||||
return append(appendFn([]byte(s.labels.FormatLabel(label))),'\n')
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -1,160 +0,0 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project active3
|
||||
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name stringify.go
|
||||
// ✍️ Created by DEV
|
||||
// 📅 Modified 06/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 erstr
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"typs/strs"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
StringifyError struct {
|
||||
labelSep string
|
||||
lineLabelFmt string
|
||||
lineErrorFmt string
|
||||
trans strs.Transformers
|
||||
}
|
||||
|
||||
StringifyErrorFnc func (stringify *StringifyError)
|
||||
)
|
||||
|
||||
|
||||
|
||||
func NewStringifyError (options ...StringifyErrorFnc) *StringifyError {
|
||||
s := &StringifyError{
|
||||
labelSep: labelSep,
|
||||
lineLabelFmt: "%s" + labelSep + "%s\n",
|
||||
lineErrorFmt: labelError + labelSep + "%s\n",
|
||||
}
|
||||
for _,o := range options {
|
||||
o(s)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
|
||||
func (s *StringifyError) Stringify (text string, err error) string {
|
||||
ln := []string { "", text }
|
||||
var nl string
|
||||
ne := err
|
||||
for ne != nil {
|
||||
if st,is := ne.(stringer); is {
|
||||
nl = st.String()
|
||||
}
|
||||
if uw,is := ne.(unwrapper); is {
|
||||
ne = uw.Unwrap()
|
||||
} else {
|
||||
ne = nil
|
||||
if strings.Index(nl,labelError) != -1 {
|
||||
ln[0]= s.trans.Transform(nl)
|
||||
break
|
||||
}
|
||||
}
|
||||
ln = append(ln, s.trans.Transform(nl))
|
||||
}
|
||||
|
||||
if len(ln[0]) == 0 {
|
||||
ln[0]=s.trans.Transform(err.Error())
|
||||
}
|
||||
if len(ln) == 0 {
|
||||
return ln[0]
|
||||
}
|
||||
b := bytes.Buffer{}
|
||||
for _,l := range ln {
|
||||
b.WriteString(l)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (s *StringifyError) FormatLineError(strErr string) string {
|
||||
return fmt.Sprintf(s.lineErrorFmt,strErr) }
|
||||
|
||||
func (s *StringifyError) FormatLine (label, text string) string {
|
||||
return fmt.Sprintf(s.lineLabelFmt,label,text) }
|
||||
|
||||
func (s *StringifyError) removeLabel (text string) string {
|
||||
if strings.Index(text,s.labelSep) == -1 {
|
||||
return text
|
||||
}
|
||||
l := strings.Split(text,"\n")
|
||||
b := bytes.Buffer{}
|
||||
for _,t := range l {
|
||||
if p := strings.Index(t,s.labelSep); p != -1 {
|
||||
b.WriteString(t[p+len(s.labelSep):])
|
||||
b.WriteByte('\n')
|
||||
} else {
|
||||
b.WriteString(t)
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func WithFormats (fmtLabelLn, fmtErrorLn, sep string) StringifyErrorFnc {
|
||||
return func (s *StringifyError) {
|
||||
s.labelSep = sep
|
||||
s.lineLabelFmt = fmtLabelLn
|
||||
s.lineErrorFmt = fmtErrorLn
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func WithTransformers (add bool, transformers ...any) StringifyErrorFnc {
|
||||
return func (s *StringifyError) {
|
||||
if !add {
|
||||
s.trans = s.trans[:0]
|
||||
}
|
||||
for _, tf := range transformers {
|
||||
switch t:=tf.(type) {
|
||||
case string:
|
||||
if t == LabelRemover {
|
||||
s.trans = append(s.trans,strs.TransformFnc(s.removeLabel))
|
||||
} else {
|
||||
tr, hs := strs.G.Transformers.Get(t)
|
||||
if !hs {
|
||||
panic(strs.G.ErrTransformerNotFound)
|
||||
}
|
||||
s.trans = append(s.trans, tr)
|
||||
}
|
||||
case strs.Transformer:
|
||||
if t != nil {
|
||||
s.trans = append(s.trans,t)
|
||||
}
|
||||
case func(string)string:
|
||||
if t != nil {
|
||||
s.trans = append(s.trans,strs.TransformFnc(t))
|
||||
}
|
||||
default:
|
||||
panic(strs.G.ErrTransformerNotFound)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,36 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project active3
|
||||
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name string_appender.go
|
||||
// ✍️ Created by DEV
|
||||
// 📅 Modified 06/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 flds
|
||||
|
||||
|
||||
type (
|
||||
|
||||
StringAppender interface {
|
||||
|
||||
AppendAsString (dest []byte) []byte
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@ -0,0 +1,43 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project active3
|
||||
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name stringer.go
|
||||
// ✍️ Created by DEV
|
||||
// 📅 Modified 06/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 strs
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
Stringer interface {
|
||||
|
||||
String() string
|
||||
}
|
||||
|
||||
StringFnc func () string
|
||||
)
|
||||
|
||||
|
||||
func (f StringFnc) String() string {
|
||||
return f() }
|
||||
|
||||
|
||||
Loading…
Reference in new issue