You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
2.7 KiB
122 lines
2.7 KiB
// ------------------------------------------------------------------------
|
|
// 🚀 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 (
|
|
|
|
Stringify struct {
|
|
LabelSep string
|
|
LineLabelFmt string
|
|
LineErrorFmt string
|
|
Transformers strs.Transformers
|
|
}
|
|
|
|
ErrorStringifyFnc func (stringify *Stringify)
|
|
)
|
|
|
|
|
|
|
|
func NewStringify (options ...ErrorStringifyFnc) *Stringify {
|
|
s := &Stringify{
|
|
LabelSep : labelSep,
|
|
LineLabelFmt : "%s" + labelSep + "%s\n",
|
|
LineErrorFmt : labelError + labelSep + "%s\n",
|
|
}
|
|
for _,o := range options {
|
|
o(s)
|
|
}
|
|
return s
|
|
}
|
|
|
|
|
|
func (s *Stringify) 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.Transformers.Transform(nl)
|
|
break
|
|
}
|
|
}
|
|
ln = append(ln, s.Transformers.Transform(nl))
|
|
}
|
|
|
|
if len(ln[0]) == 0 {
|
|
ln[0]=s.Transformers.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 *Stringify) FormatLineError(strErr string) string {
|
|
return fmt.Sprintf(s.LineErrorFmt,strErr) }
|
|
|
|
func (s *Stringify) FormatLine (label, text string) string {
|
|
return fmt.Sprintf(s.LineLabelFmt,label,text) }
|
|
|
|
|
|
func (s *Stringify) 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()
|
|
}
|
|
|
|
|
|
|
|
|
|
|