corregido el formateador de error para que no escriba la seccion Causes en caso de que no tenga ninguna

master
DEV 2 years ago
parent 3d4b322144
commit 4b14eff493

@ -94,13 +94,19 @@ func main() {
errs.ErrInfoWithInfo("Mi mensaje")) errs.ErrInfoWithInfo("Mi mensaje"))
mu := errs.NewErrMulti(
errs.ErrCode("UnError"),
errs.ErrCode("UnOtro"),
me,
)
//printCollection(*collr.NewErrorCollection(me).Collect(true,true,true)) //printCollection(*collr.NewErrorCollection(me).Collect(true,true,true))
jso := fjson.NewJSONFormatter() jso := fjson.NewJSONFormatter()
fmt.Print(string(jso.FormatError(me))) fmt.Print(string(jso.FormatError(mu)))
} }

@ -24,6 +24,8 @@
package fjson package fjson
var ( var (
G = struct { G = struct {
@ -31,8 +33,16 @@ var (
}{} }{}
g = struct { g = struct {
null []byte
}{} zero []byte
vfalse []byte
vtrue []byte
}{
null : []byte("null"),
zero : []byte{'0'},
vfalse: []byte{'f','a','l','s','e'},
vtrue: []byte{'t','r','u','e'},
}
) )

@ -25,8 +25,8 @@ package fjson
import ( import (
"bins/bfers" "bins/bfers"
"encoding/json"
"errs/fmtr/collr" "errs/fmtr/collr"
"fmt"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -64,15 +64,15 @@ func newJsonBuilder (f *JSONFormatter) *jsonBuilder {
func (b *jsonBuilder) format (err error) { func (b *jsonBuilder) format (err error) {
cl := collr.NewErrorCollection(err)
b.write(*collr.NewErrorCollection(err).Collect(true,true,true),"",false) cl.Collect(b.f.AddFields(),b.f.AddCauses(),b.f.AddTrace())
b.write(cl,"",false)
} }
func (b *jsonBuilder) write (coll collr.ErrorCollection,indent string, cont bool) { func (b *jsonBuilder) write (coll *collr.ErrorCollection,indent string, cont bool) {
var in string in := ""
if b.pretty { if b.pretty {
in = indent + strings.Repeat(b.indent, 1) in = indent + strings.Repeat(b.indent, 1)
if !cont { if !cont {
@ -80,115 +80,129 @@ func (b *jsonBuilder) write (coll collr.ErrorCollection,indent string, cont bool
} }
} }
b.b.WriteByte('{') b.b.WriteByte('{')
if b.pretty { if b.pretty {
b.b.WriteByte('\n') b.b.WriteByte('\n')
} }
b.writeField("error",coll.ErrStr,in,true,true) b.writeField("error",coll.ErrStr,in,true,false, false)
types := coll.Types if b.f.AddTypes() {
if len(types) == 1 { types := coll.Types
b.writeField("type",types[0].String(),in,true,false)
} else { b.b.WriteByte(',')
b.writeObject("types", in, '[', b.pretty)
for i, t := range coll.Types {
if b.pretty {
b.b.WriteString(in)
b.b.WriteString(b.indent)
}
b.b.WriteByte('"')
b.b.WriteString(t.String())
b.b.WriteByte('"')
if i < len(coll.Types)-1 {
b.b.WriteByte(',')
}
if b.pretty {
b.b.WriteByte('\n')
}
}
if b.pretty { if b.pretty {
b.b.WriteString(in)
b.b.Write2Byte(']', ',')
b.b.WriteByte('\n') b.b.WriteByte('\n')
}
if len(types) == 1 {
b.writeField("type", types[0].String(), in, true, false,false)
} else { } else {
b.b.Write2Byte(']', ',') b.writeOpen("types", in, '[', b.pretty)
for i, t := range coll.Types {
if b.pretty {
b.b.WriteString(in)
b.b.WriteString(b.indent)
}
b.b.WriteByte('"')
b.b.WriteString(t.String())
b.b.WriteByte('"')
if i < len(coll.Types)-1 {
b.b.WriteByte(',')
}
if b.pretty {
b.b.WriteByte('\n')
}
}
b.writeClose(in, ']')
} }
} }
heads := coll.Fields.GetHeads() heads := coll.Fields.GetHeads()
if len(heads) > 0 { if len(heads) > 0 {
b.writeObject("fields", in, '{', b.pretty)
for i, h := range coll.Fields.GetHeads() {
if b.pretty {
b.b.WriteString(in)
b.b.WriteString(b.indent)
}
b.b.WriteByte('"') b.b.WriteByte(',')
b.b.WriteString(h.Name) if b.pretty {
b.b.WriteByte('"') b.b.WriteByte('\n')
b.b.WriteString(b.sep) }
b.writeFieldValue(coll.Fields, h)
if i < len(heads)-1 { coll.Fields.Sort()
b.writeOpen("fields", in, '{', b.pretty)
var n = 0
var fi string
if b.pretty {
fi = in + b.indent
}
for _, h := range heads {
p := b.b.Len()
if n > 0 {
b.b.WriteByte(',') b.b.WriteByte(',')
if b.pretty {
b.b.WriteByte('\n')
}
} }
if b.pretty {
b.b.WriteByte('\n') b.writeFieldName(h.Name,fi)
if !b.writeFieldValue(coll.Fields, h) {
b.b.Truncate(p)
continue
} }
n++
} }
if b.pretty { if n > 0 && b.pretty {
b.b.WriteString(in)
b.b.Write2Byte('}', ',')
b.b.WriteByte('\n') b.b.WriteByte('\n')
} else {
b.b.Write2Byte('}', ',')
} }
b.writeClose(in,'}')
} }
causes := coll.Causes causes := coll.Causes
if len(causes) == 1 { if len (causes) > 0 {
b.writeObject("cause",in,0,false)
b.write(causes[0],strings.Repeat(b.indent,1),true) b.b.WriteByte(',')
} else if len(causes) > 1 {
b.writeObject("causes", in, '[', b.pretty)
var ci string
if b.pretty { if b.pretty {
ci = strings.Repeat(b.indent,2) b.b.WriteByte('\n')
} }
for _,ce := range causes {
b.write(ce,ci,false) if len(causes) == 1 {
b.writeOpen("cause", in, 0, false)
b.write(&causes[0], strings.Repeat(b.indent, 1), true)
} else if len(causes) > 1 {
b.writeOpen("causes", in, '[', b.pretty)
var ci string
if b.pretty {
ci = strings.Repeat(b.indent, 2)
}
for i, ce := range causes {
if i > 0 {
b.b.WriteByte(',')
if b.pretty {
b.b.WriteByte('\n')
}
}
b.write(&ce, ci, false)
}
if b.pretty {
b.b.WriteByte('\n')
}
b.writeClose(in, ']')
} }
b.b.WriteString(in)
b.b.WriteByte(']')
b.b.WriteByte('\n')
} }
b.writeEndObject(indent,'}')
if b.pretty { if b.pretty {
b.b.WriteString(indent)
b.b.WriteByte('}')
b.b.WriteByte('\n') b.b.WriteByte('\n')
} else {
b.b.WriteByte('}')
} }
b.writeClose(indent,'}')
} }
func (b *jsonBuilder) writeObject(name, indent string, object byte,nl bool) { func (b *jsonBuilder) writeOpen(name, indent string, object byte,nl bool) {
if b.pretty { b.writeFieldName(name,indent)
b.b.WriteString(indent)
}
b.b.WriteByte('"')
b.b.WriteString(name)
b.b.WriteByte('"')
b.b.WriteString(b.sep)
if object != 0 { if object != 0 {
b.b.WriteByte(object) b.b.WriteByte(object)
} }
@ -197,23 +211,29 @@ func (b *jsonBuilder) writeObject(name, indent string, object byte,nl bool) {
} }
} }
func (b *jsonBuilder) writeEndObject (indent string, object byte) {
func (b *jsonBuilder) writeClose(indent string, object byte) {
if b.pretty { if b.pretty {
b.b.WriteString(indent) b.b.WriteString(indent)
b.b.WriteByte(object) b.b.WriteByte(object)
b.b.WriteByte('\n')
} else { } else {
b.b.WriteByte(object) b.b.WriteByte(object)
} }
} }
func (b *jsonBuilder) writeFieldName (name, indent string) {
func (b *jsonBuilder) writeField (name, value, indent string, quote, coma bool) { if b.pretty {
b.b.WriteString(indent) b.b.WriteString(indent)
}
b.b.WriteByte('"') b.b.WriteByte('"')
b.b.WriteString(name) b.b.WriteString(name)
b.b.WriteByte('"') b.b.WriteByte('"')
b.b.WriteString(b.sep) b.b.WriteString(b.sep)
}
func (b *jsonBuilder) writeField (name, value, indent string, quote, coma, nl bool) {
b.writeFieldName(name,indent)
if quote { if quote {
b.b.WriteByte('"') b.b.WriteByte('"')
} }
@ -224,34 +244,93 @@ func (b *jsonBuilder) writeField (name, value, indent string, quote, coma bool)
if coma { if coma {
b.b.WriteByte(',') b.b.WriteByte(',')
} }
if b.pretty { if b.pretty && nl {
b.b.WriteByte('\n') b.b.WriteByte('\n')
} }
} }
func (b *jsonBuilder) writeFieldValue (fields collr.Fields, field *collr.FieldHead) { func (b *jsonBuilder) writeFieldValue (fields collr.Fields, field *collr.FieldHead) bool {
var v []byte var v []byte
q := true q := true
switch field.Kind { switch field.Kind {
case collr.FieldKindFloat : v = strconv.AppendFloat(v,fields.GetFieldValueFloat(field),'E',-1,64) case collr.FieldKindFloat :
n := fields.GetFieldValueFloat(field)
if n == 0 {
if b.f.IgnoreDefaults() {
return false
}
v = g.zero
} else {
v = strconv.AppendFloat(v, n, 'E', -1, 64)
}
case collr.FieldKindInt : case collr.FieldKindInt :
v = strconv.AppendInt(v,int64(fields.GetFieldValueInt(field)),10) n := fields.GetFieldValueInt(field)
q = false if n == 0 {
case collr.FieldKindString: v = []byte(fields.GetFieldValueString(field)) if b.f.IgnoreDefaults() {
case collr.FieldKindTime : v = fields.GetFieldValueDate(field).AppendFormat(v,time.RFC822Z) return false
case collr.FieldKindDate : v = fields.GetFieldValueDate(field).AppendFormat(v,time.RFC822Z) }
v = g.zero
} else {
v = strconv.AppendInt(v, int64(n), 10)
}
q = b.f.UseQuote()
case collr.FieldKindString:
s := fields.GetFieldValueString(field)
if len(s) == 0 && b.f.IgnoreDefaults() {
return false
}
v = []byte(s)
case collr.FieldKindTime :
d := fields.GetFieldValueDate(field)
if d.IsZero() && b.f.IgnoreDefaults(){
return false
}
v = d.AppendFormat(v,time.RFC822Z)
case collr.FieldKindDate :
d := fields.GetFieldValueDate(field)
if d.IsZero() && b.f.IgnoreDefaults() {
return false
}
v = d.AppendFormat(v,time.RFC822Z)
case collr.FieldKindBool : case collr.FieldKindBool :
v = strconv.AppendBool(v,fields.GetFieldValueBool(field)) t := fields.GetFieldValueBool(field)
q = false if !t {
if b.f.IgnoreDefaults() {
return false
}
v = g.vfalse
} else {
v = g.vtrue
}
q = b.f.UseQuote()
default: default:
v = []byte(fmt.Sprintf("%v",fields.GetFieldValueAny(field))) a := fields.GetFieldValueAny(field)
if a == nil {
if b.f.IgnoreDefaults() {
return false
}
v = g.null
} else {
var e error
if v,e = json.Marshal(a); e != nil {
return false
}
}
} }
if q { if q {
b.b.WriteByte('"') b.b.WriteByte('"')
} }
b.b.Write(v) _,_ = b.b.Write(v)
if q { if q {
b.b.WriteByte('"') b.b.WriteByte('"')
} }
return true
} }

@ -38,7 +38,7 @@ type (
func NewJSONFormatter (options ...JSONFormatterFnc) *JSONFormatter { func NewJSONFormatter (options ...JSONFormatterFnc) *JSONFormatter {
f := &JSONFormatter{ f := &JSONFormatter{
Option: ShowPretty |UseSpace, Option: ShowPretty |UseSpace | AddTypes |AddFields | AddCauses | AddTrace | IgnoreDefaults,
indent: " ", indent: " ",
} }
for _,o := range options { for _,o := range options {

@ -28,20 +28,47 @@ package fjson
const ( const (
ShowPretty Option = 1 + iota AddTypes Option = 1 << iota
AddFields
AddCauses
AddTrace
ShowPretty
UseSpace UseSpace
UseNumQuoted UseQuote
IgnoreDefaults
SortFields
) )
type ( type (
Option uint8 Option uint16
) )
func (o Option) AddTypes () bool {
return o&AddTypes != 0 }
func (o Option) AddFields () bool {
return o&AddFields != 0 }
func (o Option) AddCauses () bool {
return o&AddCauses != 0 }
func (o Option) AddTrace () bool {
return o&AddTrace != 0 }
func (o Option) ShowPretty () bool { func (o Option) ShowPretty () bool {
return o&ShowPretty != 0 } return o&ShowPretty != 0 }
func (o Option) UseSpace () bool { func (o Option) UseSpace () bool {
return o&UseSpace != 0 } return o&UseSpace != 0 }
func (o Option) UseQuote () bool {
return o&UseQuote != 0 }
func (o Option) SortFields () bool {
return o&SortFields != 0 }
func (o Option) IgnoreDefaults() bool {
return o&IgnoreDefaults != 0 }

Loading…
Cancel
Save

Powered by TurnKey Linux.