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 acb1ffc10b
commit 3d4b322144

@ -3,6 +3,8 @@ package main
import (
"bins/bfers"
"errs"
"errs/fmtr/collr"
"errs/fmtr/fjson"
"errs/stack"
"fmt"
)
@ -63,18 +65,42 @@ func (c MiCustomErr) Unwrap () error {
}
func printCollection (ec collr.ErrorCollection) {
if ec.Invalid != nil {
fmt.Printf("INvalid %s\n", ec.Invalid.Error())
}
ec.Fields.Sort()
fmt.Printf("Error: %s\n",ec.ErrStr)
fmt.Printf("Types: %d\n",len(ec.Types))
for _,t := range ec.Types {
fmt.Printf("\tWrap Type : %s\n",t.String())
}
fmt.Printf("Fields: \n")
for _,h := range ec.Fields.GetHeads() {
fmt.Printf("\t%s : %v\n",h.Name)
}
fmt.Printf("Causes: \n")
for _,c := range ec.Causes {
printCollection(c)
}
}
func main() {
me := bfers.G.ErrTooLarge
me := errs.WrapWithInfo(bfers.G.ErrTooLarge.WrapWithCause(MiCustomErr{}),
errs.ErrInfoWithInfo("Mi mensaje"))
//printCollection(*collr.NewErrorCollection(me).Collect(true,true,true))
fmt.Print(errs.ToFormat(me,nil))
jso := fjson.NewJSONFormatter()
fmt.Print(string(jso.FormatError(me)))
}

@ -1,3 +0,0 @@
module atypes
go 1.22

@ -24,7 +24,6 @@
package bits
import (
"atypes"
"encoding/binary"
"math/bits"
)

@ -56,9 +56,7 @@ var (
}
g = struct {
bufferPool *Pool
}{
bufferPool: NewPool(),
}

@ -23,11 +23,12 @@
package bfers
import "errs"
import (
"errs"
)
type (
Err struct { errs.ErrCode }
)

@ -26,6 +26,7 @@ package errs
import (
"errs/fmtr"
"errs/fmtr/collr"
)
type (
@ -75,3 +76,8 @@ func (e ErrCode) String () string {
func (e ErrCode) BuildFormat(builder fmtr.Builder) {
builder.Build(e,nil) }
func (e ErrCode) Collect (collector collr.Collector) {
collector.AddFieldString(collr.FieldNameCode,string(e))
}

@ -25,6 +25,7 @@ package errs
import (
"errs/fmtr"
"errs/fmtr/collr"
"fmt"
"time"
)
@ -117,6 +118,18 @@ func (e ErrInfo) BuildFormat(builder fmtr.Builder) {
}
func (e ErrInfo) Collect (collector collr.Collector) {
collector.AddFieldString(collr.FieldNameMessage,e.message)
collector.AddFieldString(collr.FieldNameInfo,e.info)
collector.AddFieldDate (collr.FieldNameDate,e.date)
if len(e.metas) > 0 {
for n,v := range e.metas {
collector.AddFieldString(n,v)
}
}
}

@ -2,7 +2,7 @@
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name builder.go
// File name text_builder.go
// Created by DEV
// Modified 15/04/2024
//

@ -0,0 +1,172 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File Name active.go
// Created by DEV
// Modified 20/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 collr
import (
"reflect"
"sync"
"time"
)
const (
FieldNameError string = "error"
FieldNameCode string = "code"
FieldNameType string = "type"
FieldNameMessage string = "message"
FieldNameInfo string = "info"
FieldNameDate string = "date"
FieldNameTime string = "time"
FieldNameParam string = "param"
FieldNameSource string = "source"
FieldNameUser string = "user"
)
var (
g = struct {
mux sync.RWMutex
typeCollectors map[reflect.Type]ErrorCollector
defaultCollectors FieldCollectors
}{
typeCollectors: make(map[reflect.Type]ErrorCollector),
}
)
func getTypeCollector (rtype reflect.Type) ErrorCollector {
ec,_ := g.typeCollectors[rtype]
return ec
}
func CollectErrorFields (collector Collector, err error) {
if err == nil {
return
}
rt := reflect.TypeOf(err)
if rt.Kind() == reflect.Pointer {
rt = rt.Elem()
}
if ec,is := err.(Collectable); is {
ec.Collect(collector)
return
}
g.mux.RLock()
eb,_ := g.typeCollectors[rt]
g.mux.RUnlock()
if eb != nil {
eb.CollectError(collector,err)
return
}
var nc FieldCollectors
for _,f := range g.defaultCollectors {
if f.CollectField(collector,err) {
nc = append(nc, f)
}
}
g.mux.Lock()
g.typeCollectors[rt]=nc
g.mux.Unlock()
}
func defaultCollectors () FieldCollectors {
return []FieldCollector {
CollectFieldFnc(func (b Collector, e error ) bool {
if ms,is := e.(interface { Message() string }); is {
b.AddFieldString(FieldNameMessage, ms.Message())
return true
}
return false
}),
CollectFieldFnc(func (b Collector, e error ) bool {
if in,is := e.(interface { Info () string }); is {
b.AddFieldString(FieldNameInfo,in.Info())
return true
}
return false
}),
CollectFieldFnc(func (b Collector, e error ) bool {
if tm,is := e.(interface { Time() time.Time }); is {
b.AddFieldTime(FieldNameTime, tm.Time())
return true
}
return false
}),
CollectFieldFnc(func (b Collector, e error ) bool {
if tm,is := e.(interface { Date() time.Time }); is {
b.AddFieldDate(FieldNameDate, tm.Date())
return true
}
return false
}),
CollectFieldFnc(func (b Collector, e error ) bool {
if tm,is := e.(interface { Param() string }); is {
b.AddFieldString(FieldNameParam, tm.Param())
return true
}
return false
}),
CollectFieldFnc(func (b Collector, e error ) bool {
if tm,is := e.(interface { User() string }); is {
b.AddFieldString(FieldNameUser, tm.User())
return true
}
return false
}),
CollectFieldFnc(func (b Collector, e error ) bool {
if tm,is := e.(interface { Source() string }); is {
b.AddFieldString(FieldNameSource, tm.Source())
return true
}
return false
}),
}
}

@ -0,0 +1,33 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File Name collectable.go
// Created by DEV
// Modified 20/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 collr
type (
Collectable interface {
Collect (collector Collector)
}
)

@ -0,0 +1,41 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File Name collector.go
// Created by DEV
// Modified 20/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 collr
import "time"
type (
Collector interface {
AddFieldAny (name string, value any )
AddFieldInt (name string, value int )
AddFieldBool (name string, value bool )
AddFieldString(name string, value string )
AddFieldFloat (name string, value float64)
AddFieldTime (name string, value time.Time)
AddFieldDate (name string, value time.Time)
}
)

@ -0,0 +1,133 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File Name error_data.go
// Created by DEV
// Modified 20/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 collr
import (
"errors"
"errs/stack"
"fmt"
"reflect"
)
type (
ErrorCollection struct {
Err error
ErrStr string
Types []reflect.Type
Fields Fields
Causes []ErrorCollection
Trace stack.Trace
Invalid error
}
)
func NewErrorCollection(err error) *ErrorCollection {
if err == nil {
return nil
}
return &ErrorCollection{ Err: err }
}
func (c *ErrorCollection) Collect (addFields,addCauses,addTrace bool) *ErrorCollection {
c.collect(addFields,addCauses,addTrace)
return c
}
func (c *ErrorCollection) collect (addFields,addCauses,addTrace bool) *ErrorCollection {
d := false
ne := c.Err
defer func(ec *ErrorCollection, dn *bool){
if !(*dn) {
if rc := recover(); rc != nil {
if er,is := rc.(error); is {
ec.Invalid = er
} else {
ec.Invalid = fmt.Errorf("%#v",rc)
}
}
}
}(c,&d)
c.ErrStr = c.Err.Error()
for ne != nil {
c.Types = append(c.Types,reflect.TypeOf(ne))
// Unwrap Fields
if addFields {
CollectErrorFields(&c.Fields,ne)
}
// Unwrap Causes
if addCauses {
if cs, is := ne.(interface{ Cause() error }); is {
if er := cs.Cause(); er != nil {
c.Causes = append(c.Causes, *NewErrorCollection(er).Collect(addFields,addCauses,false))
}
} else {
var ls []error
if cs, is := ne.(interface{ Causes() []error }); is {
ls = cs.Causes()
} else if cs, is := ne.(interface{ Unwrap() []error }); is {
ls = cs.Unwrap()
}
for _, er := range ls {
if ed := NewErrorCollection(er); ed != nil {
c.Causes = append(c.Causes, *ed.Collect(addFields,addCauses,false))
}
}
}
}
// Unwrap Trace
if addTrace {
if tc, is := ne.(stack.Tracer); is {
c.Trace = tc.StackTrace()
addTrace = c.Trace == nil
}
}
// next error
ne = errors.Unwrap(ne)
}
d = true
return c
}

@ -0,0 +1,43 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File Name error_collectable.go
// Created by DEV
// Modified 20/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 collr
type (
ErrorCollector interface {
CollectError (collector Collector, err error)
}
CollectErrorFnc func(collector Collector, err error)
)
func (f CollectErrorFnc) CollectError (collector Collector, err error) {
f(collector,err) }
func NopCollectError (_ Collector, _ error) {}

@ -0,0 +1,43 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File Name field_collector.go
// Created by DEV
// Modified 20/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 collr
type (
FieldCollector interface {
CollectField (collector Collector, err error) bool
}
CollectFieldFnc func(collector Collector, err error) bool
)
func (f CollectFieldFnc) CollectField (collector Collector, err error) bool {
return f(collector,err) }

@ -0,0 +1,53 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File Name field_collectors.go
// Created by DEV
// Modified 20/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 collr
type (
FieldCollectors []FieldCollector
)
func (c FieldCollectors) CollectError (collector Collector, err error) {
for _,f := range c {
f.CollectField(collector,err)
}
}
func (c FieldCollectors) CollectField (collector Collector, err error) bool {
r := true
for _,fc := range c {
r = r && fc.CollectField(collector,err)
}
return r
}

@ -0,0 +1,36 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File Name field_head.go
// Created by DEV
// Modified 20/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 collr
type (
FieldHead struct {
Name string
Kind FieldKind
Pos int
}
)

@ -0,0 +1,45 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File Name field_kind.go
// Created by DEV
// Modified 20/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 collr
const (
FieldKindAny FieldKind = iota
FieldKindString
FieldKindInt
FieldKindFloat
FieldKindBool
FieldKindDate
FieldKindTime
)
type (
FieldKind uint8
)

@ -0,0 +1,140 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File Name builder.go
// Created by DEV
// Modified 20/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 collr
import (
"sort"
"time"
)
type (
Fields struct {
heads []*FieldHead
anys []any
strings []string
ints []int
floats []float64
dates []time.Time
}
)
func (f *Fields) AddFieldAny (name string, value any) {
f.addField(name,FieldKindAny).anys = append(f.anys,value) }
func (f *Fields) AddFieldString(name string, value string) {
f.addField(name,FieldKindString)
f.strings = append(f.strings, value)
}
func (f *Fields) AddFieldInt (name string, value int) {
f.addField(name,FieldKindInt).ints = append(f.ints, value) }
func (f *Fields) AddFieldBool (name string, value bool) {
bv := 0
if value {
bv = 1
}
f.addField(name,FieldKindInt).ints = append(f.ints, bv)
}
func (f *Fields) AddFieldFloat (name string, value float64) {
f.addField(name,FieldKindFloat).floats = append(f.floats, value) }
func (f *Fields) AddFieldTime (name string, value time.Time) {
f.addField(name,FieldKindTime).dates = append(f.dates, value) }
func (f *Fields) AddFieldDate (name string, value time.Time) {
f.addField(name,FieldKindDate).dates = append(f.dates, value) }
func (f *Fields) addField (name string, kind FieldKind) *Fields {
var pos int
switch kind {
case FieldKindBool,
FieldKindInt : pos = len(f.ints)
case FieldKindString: pos = len(f.strings)
case FieldKindDate,
FieldKindTime : pos = len(f.dates)
case FieldKindFloat : pos = len(f.floats)
case FieldKindAny : pos = len(f.anys)
}
f.heads = append(f.heads, &FieldHead{
Name: name,
Kind: kind,
Pos: pos,
})
return f
}
func (f *Fields) GetHeads () []*FieldHead {
return f.heads }
func (f *Fields) GetField (name string) (*FieldHead,bool) {
for i,h := range f.heads {
if h.Name == name {
return f.heads[i],true
}
}
return nil,false
}
func (f *Fields) GetFieldValueAny (head *FieldHead) any {
return f.anys[head.Pos] }
func (f *Fields) GetFieldValueString (head *FieldHead) string {
return f.strings[head.Pos] }
func (f *Fields) GetFieldValueInt (head *FieldHead) int {
return f.ints[head.Pos] }
func (f *Fields) GetFieldValueFloat (head *FieldHead) float64 {
return f.floats[head.Pos] }
func (f *Fields) GetFieldValueBool (head *FieldHead) bool {
return (f.ints[head.Pos]) > 0 }
func (f *Fields) GetFieldValueDate (head *FieldHead) time.Time {
return f.dates[head.Pos] }
func (f *Fields) GetFieldValueTime (head *FieldHead) time.Time {
return f.dates[head.Pos] }
func (f *Fields) Sort () {
sort.Slice(f.heads, func(i, j int) bool {
return f.heads[i].Name < f.heads[j].Name
})
}

@ -2,7 +2,7 @@
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name builder.go
// File name text_builder.go
// Created by DEV
// Modified 15/04/2024
//

@ -0,0 +1,38 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name active.go
// Created by DEV
// Modified 20/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 fjson
var (
G = struct {
}{}
g = struct {
}{}
)

@ -0,0 +1,257 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name text_builder.go
// Created by DEV
// Modified 20/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 fjson
import (
"bins/bfers"
"errs/fmtr/collr"
"fmt"
"strconv"
"strings"
"time"
)
type (
jsonBuilder struct {
f *JSONFormatter
b bfers.Buffer
indent string
sep string
pretty bool
}
)
func newJsonBuilder (f *JSONFormatter) *jsonBuilder {
sep := ":"
if f.UseSpace() {
sep = ": "
}
return &jsonBuilder{
f : f,
b : *bfers.NewBuffer(),
indent : f.indent,
pretty : f.ShowPretty(),
sep : sep,
}
}
func (b *jsonBuilder) format (err error) {
b.write(*collr.NewErrorCollection(err).Collect(true,true,true),"",false)
}
func (b *jsonBuilder) write (coll collr.ErrorCollection,indent string, cont bool) {
var in string
if b.pretty {
in = indent + strings.Repeat(b.indent, 1)
if !cont {
b.b.WriteString(indent)
}
}
b.b.WriteByte('{')
if b.pretty {
b.b.WriteByte('\n')
}
b.writeField("error",coll.ErrStr,in,true,true)
types := coll.Types
if len(types) == 1 {
b.writeField("type",types[0].String(),in,true,false)
} else {
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 {
b.b.WriteString(in)
b.b.Write2Byte(']', ',')
b.b.WriteByte('\n')
} else {
b.b.Write2Byte(']', ',')
}
}
heads := coll.Fields.GetHeads()
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.WriteString(h.Name)
b.b.WriteByte('"')
b.b.WriteString(b.sep)
b.writeFieldValue(coll.Fields, h)
if i < len(heads)-1 {
b.b.WriteByte(',')
}
if b.pretty {
b.b.WriteByte('\n')
}
}
if b.pretty {
b.b.WriteString(in)
b.b.Write2Byte('}', ',')
b.b.WriteByte('\n')
} else {
b.b.Write2Byte('}', ',')
}
}
causes := coll.Causes
if len(causes) == 1 {
b.writeObject("cause",in,0,false)
b.write(causes[0],strings.Repeat(b.indent,1),true)
} else if len(causes) > 1 {
b.writeObject("causes", in, '[', b.pretty)
var ci string
if b.pretty {
ci = strings.Repeat(b.indent,2)
}
for _,ce := range causes {
b.write(ce,ci,false)
}
b.b.WriteString(in)
b.b.WriteByte(']')
b.b.WriteByte('\n')
}
b.writeEndObject(indent,'}')
if b.pretty {
b.b.WriteString(indent)
b.b.WriteByte('}')
b.b.WriteByte('\n')
} else {
b.b.WriteByte('}')
}
}
func (b *jsonBuilder) writeObject(name, indent string, object byte,nl bool) {
if b.pretty {
b.b.WriteString(indent)
}
b.b.WriteByte('"')
b.b.WriteString(name)
b.b.WriteByte('"')
b.b.WriteString(b.sep)
if object != 0 {
b.b.WriteByte(object)
}
if nl {
b.b.WriteByte('\n')
}
}
func (b *jsonBuilder) writeEndObject (indent string, object byte) {
if b.pretty {
b.b.WriteString(indent)
b.b.WriteByte(object)
b.b.WriteByte('\n')
} else {
b.b.WriteByte(object)
}
}
func (b *jsonBuilder) writeField (name, value, indent string, quote, coma bool) {
b.b.WriteString(indent)
b.b.WriteByte('"')
b.b.WriteString(name)
b.b.WriteByte('"')
b.b.WriteString(b.sep)
if quote {
b.b.WriteByte('"')
}
b.b.WriteString(value)
if quote {
b.b.WriteByte('"')
}
if coma {
b.b.WriteByte(',')
}
if b.pretty {
b.b.WriteByte('\n')
}
}
func (b *jsonBuilder) writeFieldValue (fields collr.Fields, field *collr.FieldHead) {
var v []byte
q := true
switch field.Kind {
case collr.FieldKindFloat : v = strconv.AppendFloat(v,fields.GetFieldValueFloat(field),'E',-1,64)
case collr.FieldKindInt :
v = strconv.AppendInt(v,int64(fields.GetFieldValueInt(field)),10)
q = false
case collr.FieldKindString: v = []byte(fields.GetFieldValueString(field))
case collr.FieldKindTime : v = fields.GetFieldValueDate(field).AppendFormat(v,time.RFC822Z)
case collr.FieldKindDate : v = fields.GetFieldValueDate(field).AppendFormat(v,time.RFC822Z)
case collr.FieldKindBool :
v = strconv.AppendBool(v,fields.GetFieldValueBool(field))
q = false
default:
v = []byte(fmt.Sprintf("%v",fields.GetFieldValueAny(field)))
}
if q {
b.b.WriteByte('"')
}
b.b.Write(v)
if q {
b.b.WriteByte('"')
}
}

@ -0,0 +1,60 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name json_formatter.go
// Created by DEV
// Modified 20/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 fjson
type (
JSONFormatter struct {
Option
indent string
}
JSONFormatterFnc func (f *JSONFormatter)
)
func NewJSONFormatter (options ...JSONFormatterFnc) *JSONFormatter {
f := &JSONFormatter{
Option: ShowPretty |UseSpace,
indent: " ",
}
for _,o := range options {
o(f)
}
return f
}
func (f *JSONFormatter) FormatError (err error) []byte {
b := newJsonBuilder (f)
defer func(bl *jsonBuilder){
bl.b.Reset()
}(b)
b.format(err)
return b.b.Bytes()
}

@ -0,0 +1,47 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name option.go
// Created by DEV
// Modified 21/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 fjson
const (
ShowPretty Option = 1 + iota
UseSpace
UseNumQuoted
)
type (
Option uint8
)
func (o Option) ShowPretty () bool {
return o&ShowPretty != 0 }
func (o Option) UseSpace () bool {
return o&UseSpace != 0 }

@ -25,12 +25,12 @@ package ftext
import (
"ansi"
"atypes/astrings"
"errs/stack"
"fmt"
"reflect"
"strings"
"sync"
"tbase/astrings"
)
var (
@ -38,9 +38,7 @@ var (
g = struct {
buildDefault sync.Once
formatter *TextFormatter
}{
}
}{}
)

@ -24,8 +24,8 @@
package ftext
import (
"atypes/astrings"
"errs/stack"
"tbase/astrings"
)
type (

@ -1,80 +0,0 @@
// ------------------------------------------------------------------------
// Project atila
// Active Thing (activething.com) git.activething.com/go
//
// File name builder_list.go
// Created by DEV
// Modified 06/02/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 pjson
import (
"packs"
"time"
)
func (p *Packer) ItemAny (value any, flags packs.Flags) { p.addItem(func(){ p.PackAny(value,flags) }) }
func (p *Packer) ItemRaw (value []byte, flags packs.Flags) { p.addItem(func() { p.PackRaw(value,flags) }) }
func (p *Packer) ItemBinary (value []byte, flags packs.Flags) { p.addItem(func (){ p.PackBinary(value,flags) }) }
func (p *Packer) ItemString (value string, flags packs.Flags) { p.addItem(func (){ p.PackString(value,flags) }) }
func (p *Packer) ItemBool (value bool, flags packs.Flags) { p.addItem(func (){ p.PackBool(value,flags) }) }
func (p *Packer) ItemInt8 (value int8 , flags packs.Flags) { p.addItem(func (){ packInt(p.state,int64(value),flags) }) }
func (p *Packer) ItemInt16 (value int16, flags packs.Flags) { p.addItem(func (){ packInt(p.state,int64(value),flags) }) }
func (p *Packer) ItemInt32 (value int32, flags packs.Flags) { p.addItem(func (){ packInt(p.state,int64(value),flags) }) }
func (p *Packer) ItemInt64 (value int64, flags packs.Flags) { p.addItem(func (){ packInt(p.state,value,flags) }) }
func (p *Packer) ItemUInt8 (value uint8 , flags packs.Flags){ p.addItem(func (){ packUInt(p.state,uint64(value),flags) }) }
func (p *Packer) ItemUInt16 (value uint16, flags packs.Flags){ p.addItem(func (){ packUInt(p.state,uint64(value),flags) }) }
func (p *Packer) ItemUInt32 (value uint32, flags packs.Flags){ p.addItem(func (){ packUInt(p.state,uint64(value),flags) }) }
func (p *Packer) ItemUInt64 (value uint64, flags packs.Flags){ p.addItem(func (){ packUInt(p.state,uint64(value),flags) }) }
func (p *Packer) ItemFloat32 (value float32, flags packs.Flags){ p.addItem(func (){ packFloatString(p.state,float64(value),flags,32)})}
func (p *Packer) ItemFloat64 (value float64, flags packs.Flags){ p.addItem(func (){ packFloatString(p.state,value,flags,64) })}
func (p *Packer) ItemDate (value time.Time, flags packs.Flags) { p.addItem(func(){ packDate(p.state,value,flags); p.state.Inc() })}
func (p *Packer) ItemList (builder func(packs.ListPackBuilder, packs.Flags), flags packs.Flags) {
p.addItem(func(){ p.PackList(builder,flags) }) }
func (p *Packer) ItemMap (builder func(packs.MapPackBuilder, packs.Flags), flags packs.Flags) {
p.addItem(func(){ p.PackMap(builder,flags) }) }
func (p *Packer) ItemObject(builder func(packs.ObjectPackBuilder, packs.Flags), flags packs.Flags) {
p.addItem(func(){ p.PackObject(builder,flags) }) }
func (p *Packer) addItem (caller func()) {
if p.state.Count() > 0 {
p.state.WriteByte(',')
}
caller()
if p.state != nil {
p.state.Inc()
}
}

@ -1,115 +0,0 @@
// ------------------------------------------------------------------------
// Project atila
// Active Thing (activething.com) git.activething.com/go
//
// File name builderMap.go
// Created by DEV
// Modified 06/02/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 pjson
import (
"packs"
"reflect"
"time"
)
func (p *Packer) KeyValueAny (key any, value any, flags packs.Flags) {
p.addKeyValue(key, func(){ p.PackAny(value,flags)}) }
func (p *Packer) KeyValueRaw (key any,value []byte, flags packs.Flags) {
p.addKeyValue(key, func(){ p.PackRaw(value,flags)}) }
func (p *Packer) KeyValueBinary (key any, value []byte, flags packs.Flags) {
p.addKeyValue(key, func(){ p.PackBinary(value,flags)}) }
func (p *Packer) KeyValueString (key any, value string, flags packs.Flags) {
p.addKeyValue(key, func(){ p.PackString(value,flags)}) }
func (p *Packer) KeyValueBool (key any, value bool, flags packs.Flags) {
p.addKeyValue(key, func(){ p.PackBool(value,flags)}) }
func (p *Packer) KeyValueInt8 (key any, value int8 , flags packs.Flags) { p.addKeyValue(key, func(){ p.PackInt64(int64(value),flags)}) }
func (p *Packer) KeyValueInt16 (key any, value int16, flags packs.Flags) { p.addKeyValue(key, func(){ p.PackInt64(int64(value),flags)}) }
func (p *Packer) KeyValueInt32 (key any, value int32, flags packs.Flags) { p.addKeyValue(key, func(){ p.PackInt64(int64(value),flags)}) }
func (p *Packer) KeyValueInt64 (key any, value int64, flags packs.Flags) { p.addKeyValue(key, func(){ p.PackInt64(value,flags)}) }
func (p *Packer) KeyValueUInt8 (key any, value uint8 , flags packs.Flags) { p.addKeyValue(key, func(){ p.PackUInt64(uint64(value),flags)}) }
func (p *Packer) KeyValueUInt16 (key any, value uint16, flags packs.Flags) { p.addKeyValue(key, func(){ p.PackUInt64(uint64(value),flags)}) }
func (p *Packer) KeyValueUInt32 (key any, value uint32, flags packs.Flags) { p.addKeyValue(key, func(){ p.PackUInt64(uint64(value),flags)}) }
func (p *Packer) KeyValueUInt64 (key any, value uint64, flags packs.Flags) { p.addKeyValue(key, func(){ p.PackUInt64(value,flags)}) }
func (p *Packer) KeyValueFloat32(key any, value float32, flags packs.Flags){
p.addKeyValue(key,func(){ p.PackFloat32(value, flags) }) }
func (p *Packer) KeyValueFloat64(key any, value float64, flags packs.Flags){
p.addKeyValue(key,func(){ p.PackFloat64(value, flags) }) }
func (p *Packer) KeyValueDate (key any, value time.Time, flags packs.Flags){
p.addKeyValue(key,func(){ p.PackDate(value, flags) }) }
func (p *Packer) KeyValueList (key any, builder func(packs.ListPackBuilder, packs.Flags), flags packs.Flags) {
p.addKeyValue(key,func(){ p.PackList(builder, flags) }) }
func (p *Packer) KeyValueMap (key any, builder func(packs.MapPackBuilder, packs.Flags), flags packs.Flags) {
p.addKeyValue(key,func(){ p.PackMap(builder, flags) }) }
func (p *Packer) KeyValueObject (key any, builder func(packs.ObjectPackBuilder,packs.Flags), flags packs.Flags) {
p.addKeyValue(key,func(){ p.PackObject(builder, flags) }) }
func (p *Packer) addKeyValue(key any, caller func()) {
if p.state.Count() > 0 {
p.state.WriteByte(',')
}
p.packStringifyKey(key)
if p.state.Error != nil {
return
}
p.state.WriteByte(':')
caller()
p.state.Inc()
}
func (p *Packer) packStringifyKey(key any) {
switch v := key.(type) {
case string : p.PackString(v,packs.FlagScapeHTML)
case float32: p.PackFloat32(v,packs.FlagScapeHTML|packs.FlagQuote)
case float64: p.PackFloat64(v,packs.FlagScapeHTML|packs.FlagQuote)
default:
me := &rMapEntry{
v : reflect.ValueOf(key),
}
fn := stringifyRKey(me.v.Type())
if fn == nil {
p.state.SetError(packs.G.ErrInvalidKey)
return
}
if er := fn(me,me.v); er != nil {
p.state.SetError(er)
return
}
p.PackString(me.k,me.f)
}
}

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package packs
package pkrs
import (
"errs"

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package packs
package pkrs

@ -21,13 +21,12 @@
// limitations under the License.
// ------------------------------------------------------------------------
package packs
package pkrs
import "errs"
type (
Err struct { errs.ErrCode }
)
)

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package packs
package pkrs
import (
"bits"

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package packs
package pkrs
import (
"bins/chars"

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package packs
package pkrs
import "time"

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package packs
package pkrs
import "time"

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package packs
package pkrs
import "time"

@ -2,7 +2,7 @@
// Project atila
// Active Thing (activething.com) git.activething.com/go
//
// File name packer.go
// File name json_packer.go
// Created by DEV
// Modified 23/01/2024
//
@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package packs
package pkrs
type (

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package packs
package pkrs
import (

@ -41,7 +41,7 @@ const (
maxCyclesAfter = 100
defaultFlags = packs.Flags(0)
defaultFlags = pkrs.Flags(0)
)
@ -86,7 +86,7 @@ var (
jsonMarshalerType: reflect.TypeOf((*json.Marshaler)(nil)).Elem(),
textMarshalerType: reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem(),
packMarshalerType: reflect.TypeOf((*packs.Packer)(nil)).Elem(),
packMarshalerType: reflect.TypeOf((*pkrs.Packer)(nil)).Elem(),
}
)
@ -109,8 +109,8 @@ func initJSONChars() [utf8.RuneSelf]jsonCharAtt {
func Pack (value any, options ... PackerFnc) (res rsts.Bytes) {
p := NewPacker(options ...)
func Pack (value any, options ...JSONPackerFnc) (res rsts.Bytes) {
p := NewJSONPacker(options ...)
p.Pack(value,defaultFlags)
if p.Error() != nil {
res = rsts.MakeBytesError(p.Error())

@ -41,19 +41,19 @@ import (
type (
Packer struct {
state *packs.State
JSONPacker struct {
state *pkrs.State
dirty bool
}
PackerFnc func (packer *Packer)
JSONPackerFnc func (packer *JSONPacker)
)
func NewPacker (options ... PackerFnc) *Packer {
pk := &Packer {
state : packs.NewState(),
func NewJSONPacker(options ...JSONPackerFnc) *JSONPacker {
pk := &JSONPacker{
state : pkrs.NewState(),
}
for _,o := range options {
o(pk)
@ -63,46 +63,46 @@ func NewPacker (options ... PackerFnc) *Packer {
func (p *Packer) Pack (value any, flags packs.Flags) {
func (p *JSONPacker) Pack (value any, flags pkrs.Flags) {
if p.state.Error != nil {
panic(packs.G.ErrInvalidState.WrapWithCause(p.state.Error))
panic(pkrs.G.ErrInvalidState.WrapWithCause(p.state.Error))
}
if p.dirty {
panic(packs.G.ErrInvalidState)
panic(pkrs.G.ErrInvalidState)
}
p.dirty = true
p.PackAny(value,flags)
}
func (p *Packer) Packet () []byte {
func (p *JSONPacker) Packet () []byte {
return p.state.Bytes() }
func (p *Packer) Error () error {
func (p *JSONPacker) Error () error {
return p.state.Error }
func (p *Packer) Reset () {
func (p *JSONPacker) Reset () {
p.state.Reset()
p.dirty = false
}
func (p *Packer) Release () {
func (p *JSONPacker) Release () {
p.dirty = false
packs.ReleaseState(p.state)
pkrs.ReleaseState(p.state)
}
func (p *Packer) PackAny (value any, flags packs.Flags) {
func (p *JSONPacker) PackAny (value any, flags pkrs.Flags) {
if p.state.Error != nil {
panic(packs.G.ErrInvalidState.WrapWithCause(p.state.Error))
panic(pkrs.G.ErrInvalidState.WrapWithCause(p.state.Error))
}
if value == nil {
p.state.W(g.nullValue)
@ -110,7 +110,7 @@ func (p *Packer) PackAny (value any, flags packs.Flags) {
}
switch v := value.(type) {
case packs.Builder:
case pkrs.Builder:
p.state.SetError(v.Build(p))
case json.Marshaler:
@ -175,45 +175,45 @@ func (p *Packer) PackAny (value any, flags packs.Flags) {
func (p *Packer) PackRaw (value []byte, flags packs.Flags) { packRaw(p.state,value,flags) }
func (p *JSONPacker) PackRaw (value []byte, flags pkrs.Flags) { packRaw(p.state,value,flags) }
func (p *Packer) PackBinary (value []byte, flags packs.Flags) { packBinary(p.state,value,flags) }
func (p *Packer) PackString (value string, flags packs.Flags) { packString (p.state,value,flags) }
func (p *JSONPacker) PackBinary (value []byte, flags pkrs.Flags) { packBinary(p.state,value,flags) }
func (p *JSONPacker) PackString (value string, flags pkrs.Flags) { packString (p.state,value,flags) }
func (p *Packer) PackBool (value bool, flags packs.Flags) { packBool(p.state,value,flags) }
func (p *JSONPacker) PackBool (value bool, flags pkrs.Flags) { packBool(p.state,value,flags) }
func (p *Packer) PackInt8 (value int8 , flags packs.Flags) { packInt(p.state,int64(value),flags) }
func (p *Packer) PackInt16 (value int16, flags packs.Flags) { packInt(p.state,int64(value),flags) }
func (p *Packer) PackInt32 (value int32, flags packs.Flags) { packInt(p.state,int64(value),flags) }
func (p *Packer) PackInt64 (value int64, flags packs.Flags) { packInt(p.state,value,flags)}
func (p *JSONPacker) PackInt8 (value int8 , flags pkrs.Flags) { packInt(p.state,int64(value),flags) }
func (p *JSONPacker) PackInt16 (value int16, flags pkrs.Flags) { packInt(p.state,int64(value),flags) }
func (p *JSONPacker) PackInt32 (value int32, flags pkrs.Flags) { packInt(p.state,int64(value),flags) }
func (p *JSONPacker) PackInt64 (value int64, flags pkrs.Flags) { packInt(p.state,value,flags)}
func (p *Packer) PackUInt8 (value uint8 , flags packs.Flags) { packUInt(p.state,uint64(value),flags) }
func (p *Packer) PackUInt16 (value uint16, flags packs.Flags) { packUInt(p.state,uint64(value),flags) }
func (p *Packer) PackUInt32 (value uint32, flags packs.Flags) { packUInt(p.state,uint64(value),flags) }
func (p *Packer) PackUInt64 (value uint64, flags packs.Flags) { packUInt(p.state, value,flags) }
func (p *JSONPacker) PackUInt8 (value uint8 , flags pkrs.Flags) { packUInt(p.state,uint64(value),flags) }
func (p *JSONPacker) PackUInt16 (value uint16, flags pkrs.Flags) { packUInt(p.state,uint64(value),flags) }
func (p *JSONPacker) PackUInt32 (value uint32, flags pkrs.Flags) { packUInt(p.state,uint64(value),flags) }
func (p *JSONPacker) PackUInt64 (value uint64, flags pkrs.Flags) { packUInt(p.state, value,flags) }
func (p *Packer) PackFloat32 (value float32, flags packs.Flags) { packFloatString(p.state, float64(value), flags, 32) }
func (p *Packer) PackFloat64 (value float64, flags packs.Flags) { packFloatString(p.state, value, flags, 64) }
func (p *JSONPacker) PackFloat32 (value float32, flags pkrs.Flags) { packFloatString(p.state, float64(value), flags, 32) }
func (p *JSONPacker) PackFloat64 (value float64, flags pkrs.Flags) { packFloatString(p.state, value, flags, 64) }
func (p *Packer) PackDate (value time.Time, flags packs.Flags) { packDate(p.state,value,flags) }
func (p *JSONPacker) PackDate (value time.Time, flags pkrs.Flags) { packDate(p.state,value,flags) }
func (p *Packer) PackList (builder func(packs.ListPackBuilder, packs.Flags), flags packs.Flags) {
p.packCompound('[',']',builder==nil,func(pk *Packer, fl packs.Flags){ builder(pk,fl) },flags)
func (p *JSONPacker) PackList (builder func(pkrs.ListPackBuilder, pkrs.Flags), flags pkrs.Flags) {
p.packCompound('[',']',builder==nil,func(pk *JSONPacker, fl pkrs.Flags){ builder(pk,fl) },flags)
}
func (p *Packer) PackMap (builder func(packs.MapPackBuilder, packs.Flags), flags packs.Flags) {
p.packCompound('{','}',builder==nil,func(pk *Packer, fl packs.Flags){ builder(pk,fl) },flags)
func (p *JSONPacker) PackMap (builder func(pkrs.MapPackBuilder, pkrs.Flags), flags pkrs.Flags) {
p.packCompound('{','}',builder==nil,func(pk *JSONPacker, fl pkrs.Flags){ builder(pk,fl) },flags)
}
func (p *Packer) PackObject(builder func(packs.ObjectPackBuilder, packs.Flags), flags packs.Flags) {
p.packCompound('{','}',builder==nil,func(pk *Packer, fl packs.Flags){ builder(pk,fl) },flags)
func (p *JSONPacker) PackObject(builder func(pkrs.ObjectPackBuilder, pkrs.Flags), flags pkrs.Flags) {
p.packCompound('{','}',builder==nil,func(pk *JSONPacker, fl pkrs.Flags){ builder(pk,fl) },flags)
}
func (p *Packer) packCompound (opener, closer byte, isnil bool, buildFn func(*Packer,packs.Flags), flags packs.Flags) {
func (p *JSONPacker) packCompound (opener, closer byte, isnil bool, buildFn func(*JSONPacker, pkrs.Flags), flags pkrs.Flags) {
if p.state.Error != nil {
panic(packs.G.ErrInvalidState.WrapWithCause(p.state.Error))
panic(pkrs.G.ErrInvalidState.WrapWithCause(p.state.Error))
}
if isnil {
p.state.W(g.nullValue)
@ -238,9 +238,9 @@ func (p *Packer) packCompound (opener, closer byte, isnil bool, buildFn func(*Pa
func packRaw (state *packs.State, value []byte, flags packs.Flags) {
func packRaw (state *pkrs.State, value []byte, flags pkrs.Flags) {
if state.Error != nil {
panic(packs.G.ErrInvalidState.WrapWithCause(state.Error))
panic(pkrs.G.ErrInvalidState.WrapWithCause(state.Error))
}
if value == nil {
state.W(g.nullValue)
@ -253,9 +253,9 @@ func packRaw (state *packs.State, value []byte, flags packs.Flags) {
state.WOQ(flags.Quote(),'"',value)
}
func packString (state *packs.State, value string , flags packs.Flags) {
func packString (state *pkrs.State, value string , flags pkrs.Flags) {
if state.Error != nil {
panic(packs.G.ErrInvalidState.WrapWithCause(state.Error))
panic(pkrs.G.ErrInvalidState.WrapWithCause(state.Error))
}
if len(value) == 0 {
@ -264,7 +264,7 @@ func packString (state *packs.State, value string , flags packs.Flags) {
}
if flags.Encrypted() {
packEncrypted(state, []byte(value), flags|packs.FlagBinary64)
packEncrypted(state, []byte(value), flags|pkrs.FlagBinary64)
return
}
@ -330,7 +330,7 @@ func packString (state *packs.State, value string , flags packs.Flags) {
}
func packEncrypted (state *packs.State, value []byte, flags packs.Flags) {
func packEncrypted (state *pkrs.State, value []byte, flags pkrs.Flags) {
r := state.Encrypter.Encrypt(value)
if !r.Ok() {
state.SetError(r.Fail())
@ -349,9 +349,9 @@ func packEncrypted (state *packs.State, value []byte, flags packs.Flags) {
return
}
func packBinary (state *packs.State, value []byte , flags packs.Flags) {
func packBinary (state *pkrs.State, value []byte , flags pkrs.Flags) {
if state.Error != nil {
panic(packs.G.ErrInvalidState.WrapWithCause(state.Error))
panic(pkrs.G.ErrInvalidState.WrapWithCause(state.Error))
}
if value == nil {
@ -374,7 +374,7 @@ func packBinary (state *packs.State, value []byte , flags packs.Flags) {
} else if flags.Bin32() {
packBinary32(state, value, 0)
} else {
state.SetError(packs.G.ErrUnsupportedType)
state.SetError(pkrs.G.ErrUnsupportedType)
return
}
if state.Error == nil {
@ -383,7 +383,7 @@ func packBinary (state *packs.State, value []byte , flags packs.Flags) {
}
func packBinary64 (state *packs.State, value []byte , _ packs.Flags) {
func packBinary64 (state *pkrs.State, value []byte , _ pkrs.Flags) {
ln := base64.StdEncoding.EncodedLen(len(value))
if ln <= len(state.Temp) {
dst := state.Temp[:ln]
@ -400,7 +400,7 @@ func packBinary64 (state *packs.State, value []byte , _ packs.Flags) {
}
}
func packBinary32 (state *packs.State, value []byte , _ packs.Flags) {
func packBinary32 (state *pkrs.State, value []byte , _ pkrs.Flags) {
ln := base32.StdEncoding.EncodedLen(len(value))
if ln <= len(state.Temp) {
dst := state.Temp[:ln]
@ -420,9 +420,9 @@ func packBinary32 (state *packs.State, value []byte , _ packs.Flags) {
func packBool (state *packs.State, value bool , flags packs.Flags) {
func packBool (state *pkrs.State, value bool , flags pkrs.Flags) {
if state.Error != nil {
panic(packs.G.ErrInvalidState.WrapWithCause(state.Error))
panic(pkrs.G.ErrInvalidState.WrapWithCause(state.Error))
}
if value {
state.WOQ(flags.Quote(),'"', g.trueValue)
@ -431,9 +431,9 @@ func packBool (state *packs.State, value bool , flags packs.Flags) {
}
}
func packInt (state *packs.State, value int64 , flags packs.Flags) {
func packInt (state *pkrs.State, value int64 , flags pkrs.Flags) {
if state.Error != nil {
panic(packs.G.ErrInvalidState.WrapWithCause(state.Error))
panic(pkrs.G.ErrInvalidState.WrapWithCause(state.Error))
}
q :=flags.Quote()
@ -451,9 +451,9 @@ func packInt (state *packs.State, value int64 , flags packs.Flags) {
}
func packUInt (state *packs.State, value uint64 , flags packs.Flags) {
func packUInt (state *pkrs.State, value uint64 , flags pkrs.Flags) {
if state.Error != nil {
panic(packs.G.ErrInvalidState.WrapWithCause(state.Error))
panic(pkrs.G.ErrInvalidState.WrapWithCause(state.Error))
}
q :=flags.Quote()
if q {
@ -469,17 +469,17 @@ func packUInt (state *packs.State, value uint64 , flags packs.Flags) {
}
}
func packFloat (state *packs.State, value float64, flags packs.Flags) {
func packFloat (state *pkrs.State, value float64, flags pkrs.Flags) {
packFloatString(state, value, flags, 64)
}
func packFloatString (state *packs.State, f float64, flags packs.Flags, bits int) {
func packFloatString (state *pkrs.State, f float64, flags pkrs.Flags, bits int) {
if state.Error != nil {
panic(packs.G.ErrInvalidState.WrapWithCause(state.Error))
panic(pkrs.G.ErrInvalidState.WrapWithCause(state.Error))
}
if math.IsInf(f, 0) || math.IsNaN(f) {
state.SetError(packs.G.ErrInvalidValue)
state.SetError(pkrs.G.ErrInvalidValue)
return
}
@ -506,9 +506,9 @@ func packFloatString (state *packs.State, f float64, flags packs.Flags, bits int
}
func packDate(state *packs.State, value time.Time, flags packs.Flags) {
func packDate(state *pkrs.State, value time.Time, flags pkrs.Flags) {
if state.Error != nil {
panic(packs.G.ErrInvalidState.WrapWithCause(state.Error))
panic(pkrs.G.ErrInvalidState.WrapWithCause(state.Error))
}
state.WOQ(true,'"',value.AppendFormat(state.Temp[:0],flags.DateFormat()))
}
@ -518,7 +518,7 @@ func packDate(state *packs.State, value time.Time, flags packs.Flags) {
func packListString (state *packs.State, list []string, flags packs.Flags) {
func packListString (state *pkrs.State, list []string, flags pkrs.Flags) {
if !checkList(state,list == nil, len(list) ==0) {
return
}
@ -536,7 +536,7 @@ func packListString (state *packs.State, list []string, flags packs.Flags) {
}
func packListBool (state *packs.State, list []bool, flags packs.Flags) {
func packListBool (state *pkrs.State, list []bool, flags pkrs.Flags) {
if !checkList(state,list == nil, len(list) ==0) {
return
}
@ -554,7 +554,7 @@ func packListBool (state *packs.State, list []bool, flags packs.Flags) {
}
func packListInt [T int | int8 | int16 | int32 | int64](state *packs.State, list []T, flags packs.Flags) {
func packListInt [T int | int8 | int16 | int32 | int64](state *pkrs.State, list []T, flags pkrs.Flags) {
if !checkList(state,list == nil, len(list) ==0) {
return
}
@ -572,7 +572,7 @@ func packListInt [T int | int8 | int16 | int32 | int64](state *packs.State, lis
}
func packListUInt [T uint | uint8 | uint16 | uint32 | uint64](state *packs.State, list []T, flags packs.Flags) {
func packListUInt [T uint | uint8 | uint16 | uint32 | uint64](state *pkrs.State, list []T, flags pkrs.Flags) {
if !checkList(state,list == nil, len(list) ==0) {
return
}
@ -590,7 +590,7 @@ func packListUInt [T uint | uint8 | uint16 | uint32 | uint64](state *packs.State
}
func packListFloat[T float32 | float64](state *packs.State, list []T, flags packs.Flags, bits int) {
func packListFloat[T float32 | float64](state *pkrs.State, list []T, flags pkrs.Flags, bits int) {
if !checkList(state,list == nil, len(list) ==0) {
return
}
@ -607,7 +607,7 @@ func packListFloat[T float32 | float64](state *packs.State, list []T, flags pack
state.WriteByte(']')
}
func packListDate (state *packs.State, list []time.Time, flags packs.Flags) {
func packListDate (state *pkrs.State, list []time.Time, flags pkrs.Flags) {
if !checkList(state,list == nil, len(list) ==0) {
return
}
@ -625,7 +625,7 @@ func packListDate (state *packs.State, list []time.Time, flags packs.Flags) {
}
func checkList (state *packs.State, isNull, isEmpty bool) bool {
func checkList (state *pkrs.State, isNull, isEmpty bool) bool {
if isNull {
state.Write2Byte('"','"')
return false
@ -639,8 +639,8 @@ func checkList (state *packs.State, isNull, isEmpty bool) bool {
func WithEncrypter(encrypter crypts.Encrypter) PackerFnc {
return func(p *Packer) {
func WithEncrypter(encrypter crypts.Encrypter) JSONPackerFnc {
return func(p *JSONPacker) {
if encrypter == nil {
encrypter = crypts.EncrypterNop{}
}

@ -0,0 +1,80 @@
// ------------------------------------------------------------------------
// Project atila
// Active Thing (activething.com) git.activething.com/go
//
// File name builder_list.go
// Created by DEV
// Modified 06/02/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 pjson
import (
"packs"
"time"
)
func (p *JSONPacker) ItemAny (value any, flags pkrs.Flags) { p.addItem(func(){ p.PackAny(value,flags) }) }
func (p *JSONPacker) ItemRaw (value []byte, flags pkrs.Flags) { p.addItem(func() { p.PackRaw(value,flags) }) }
func (p *JSONPacker) ItemBinary (value []byte, flags pkrs.Flags) { p.addItem(func (){ p.PackBinary(value,flags) }) }
func (p *JSONPacker) ItemString (value string, flags pkrs.Flags) { p.addItem(func (){ p.PackString(value,flags) }) }
func (p *JSONPacker) ItemBool (value bool, flags pkrs.Flags) { p.addItem(func (){ p.PackBool(value,flags) }) }
func (p *JSONPacker) ItemInt8 (value int8 , flags pkrs.Flags) { p.addItem(func (){ packInt(p.state,int64(value),flags) }) }
func (p *JSONPacker) ItemInt16 (value int16, flags pkrs.Flags) { p.addItem(func (){ packInt(p.state,int64(value),flags) }) }
func (p *JSONPacker) ItemInt32 (value int32, flags pkrs.Flags) { p.addItem(func (){ packInt(p.state,int64(value),flags) }) }
func (p *JSONPacker) ItemInt64 (value int64, flags pkrs.Flags) { p.addItem(func (){ packInt(p.state,value,flags) }) }
func (p *JSONPacker) ItemUInt8 (value uint8 , flags pkrs.Flags){ p.addItem(func (){ packUInt(p.state,uint64(value),flags) }) }
func (p *JSONPacker) ItemUInt16 (value uint16, flags pkrs.Flags){ p.addItem(func (){ packUInt(p.state,uint64(value),flags) }) }
func (p *JSONPacker) ItemUInt32 (value uint32, flags pkrs.Flags){ p.addItem(func (){ packUInt(p.state,uint64(value),flags) }) }
func (p *JSONPacker) ItemUInt64 (value uint64, flags pkrs.Flags){ p.addItem(func (){ packUInt(p.state,uint64(value),flags) }) }
func (p *JSONPacker) ItemFloat32 (value float32, flags pkrs.Flags){ p.addItem(func (){ packFloatString(p.state,float64(value),flags,32)})}
func (p *JSONPacker) ItemFloat64 (value float64, flags pkrs.Flags){ p.addItem(func (){ packFloatString(p.state,value,flags,64) })}
func (p *JSONPacker) ItemDate (value time.Time, flags pkrs.Flags) { p.addItem(func(){ packDate(p.state,value,flags); p.state.Inc() })}
func (p *JSONPacker) ItemList (builder func(pkrs.ListPackBuilder, pkrs.Flags), flags pkrs.Flags) {
p.addItem(func(){ p.PackList(builder,flags) }) }
func (p *JSONPacker) ItemMap (builder func(pkrs.MapPackBuilder, pkrs.Flags), flags pkrs.Flags) {
p.addItem(func(){ p.PackMap(builder,flags) }) }
func (p *JSONPacker) ItemObject(builder func(pkrs.ObjectPackBuilder, pkrs.Flags), flags pkrs.Flags) {
p.addItem(func(){ p.PackObject(builder,flags) }) }
func (p *JSONPacker) addItem (caller func()) {
if p.state.Count() > 0 {
p.state.WriteByte(',')
}
caller()
if p.state != nil {
p.state.Inc()
}
}

@ -0,0 +1,115 @@
// ------------------------------------------------------------------------
// Project atila
// Active Thing (activething.com) git.activething.com/go
//
// File name builderMap.go
// Created by DEV
// Modified 06/02/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 pjson
import (
"packs"
"reflect"
"time"
)
func (p *JSONPacker) KeyValueAny (key any, value any, flags pkrs.Flags) {
p.addKeyValue(key, func(){ p.PackAny(value,flags)}) }
func (p *JSONPacker) KeyValueRaw (key any,value []byte, flags pkrs.Flags) {
p.addKeyValue(key, func(){ p.PackRaw(value,flags)}) }
func (p *JSONPacker) KeyValueBinary (key any, value []byte, flags pkrs.Flags) {
p.addKeyValue(key, func(){ p.PackBinary(value,flags)}) }
func (p *JSONPacker) KeyValueString (key any, value string, flags pkrs.Flags) {
p.addKeyValue(key, func(){ p.PackString(value,flags)}) }
func (p *JSONPacker) KeyValueBool (key any, value bool, flags pkrs.Flags) {
p.addKeyValue(key, func(){ p.PackBool(value,flags)}) }
func (p *JSONPacker) KeyValueInt8 (key any, value int8 , flags pkrs.Flags) { p.addKeyValue(key, func(){ p.PackInt64(int64(value),flags)}) }
func (p *JSONPacker) KeyValueInt16 (key any, value int16, flags pkrs.Flags) { p.addKeyValue(key, func(){ p.PackInt64(int64(value),flags)}) }
func (p *JSONPacker) KeyValueInt32 (key any, value int32, flags pkrs.Flags) { p.addKeyValue(key, func(){ p.PackInt64(int64(value),flags)}) }
func (p *JSONPacker) KeyValueInt64 (key any, value int64, flags pkrs.Flags) { p.addKeyValue(key, func(){ p.PackInt64(value,flags)}) }
func (p *JSONPacker) KeyValueUInt8 (key any, value uint8 , flags pkrs.Flags) { p.addKeyValue(key, func(){ p.PackUInt64(uint64(value),flags)}) }
func (p *JSONPacker) KeyValueUInt16 (key any, value uint16, flags pkrs.Flags) { p.addKeyValue(key, func(){ p.PackUInt64(uint64(value),flags)}) }
func (p *JSONPacker) KeyValueUInt32 (key any, value uint32, flags pkrs.Flags) { p.addKeyValue(key, func(){ p.PackUInt64(uint64(value),flags)}) }
func (p *JSONPacker) KeyValueUInt64 (key any, value uint64, flags pkrs.Flags) { p.addKeyValue(key, func(){ p.PackUInt64(value,flags)}) }
func (p *JSONPacker) KeyValueFloat32(key any, value float32, flags pkrs.Flags){
p.addKeyValue(key,func(){ p.PackFloat32(value, flags) }) }
func (p *JSONPacker) KeyValueFloat64(key any, value float64, flags pkrs.Flags){
p.addKeyValue(key,func(){ p.PackFloat64(value, flags) }) }
func (p *JSONPacker) KeyValueDate (key any, value time.Time, flags pkrs.Flags){
p.addKeyValue(key,func(){ p.PackDate(value, flags) }) }
func (p *JSONPacker) KeyValueList (key any, builder func(pkrs.ListPackBuilder, pkrs.Flags), flags pkrs.Flags) {
p.addKeyValue(key,func(){ p.PackList(builder, flags) }) }
func (p *JSONPacker) KeyValueMap (key any, builder func(pkrs.MapPackBuilder, pkrs.Flags), flags pkrs.Flags) {
p.addKeyValue(key,func(){ p.PackMap(builder, flags) }) }
func (p *JSONPacker) KeyValueObject (key any, builder func(pkrs.ObjectPackBuilder, pkrs.Flags), flags pkrs.Flags) {
p.addKeyValue(key,func(){ p.PackObject(builder, flags) }) }
func (p *JSONPacker) addKeyValue(key any, caller func()) {
if p.state.Count() > 0 {
p.state.WriteByte(',')
}
p.packStringifyKey(key)
if p.state.Error != nil {
return
}
p.state.WriteByte(':')
caller()
p.state.Inc()
}
func (p *JSONPacker) packStringifyKey(key any) {
switch v := key.(type) {
case string : p.PackString(v, pkrs.FlagScapeHTML)
case float32: p.PackFloat32(v, pkrs.FlagScapeHTML|pkrs.FlagQuote)
case float64: p.PackFloat64(v, pkrs.FlagScapeHTML|pkrs.FlagQuote)
default:
me := &rMapEntry{
v : reflect.ValueOf(key),
}
fn := stringifyRKey(me.v.Type())
if fn == nil {
p.state.SetError(pkrs.G.ErrInvalidKey)
return
}
if er := fn(me,me.v); er != nil {
p.state.SetError(er)
return
}
p.PackString(me.k,me.f)
}
}

@ -28,81 +28,81 @@ import (
"time"
)
func (p *Packer) FieldAny (name string, value any, flags packs.Flags) {
func (p *JSONPacker) FieldAny (name string, value any, flags pkrs.Flags) {
p.addField(name, func() { p.PackAny(value, flags) }) }
func (p *Packer) FieldRaw (name string, value []byte, flags packs.Flags){
func (p *JSONPacker) FieldRaw (name string, value []byte, flags pkrs.Flags){
p.addField(name, func() { p.PackRaw(value, flags) }) }
func (p *Packer) FieldBinary (name string, value []byte, flags packs.Flags) {
func (p *JSONPacker) FieldBinary (name string, value []byte, flags pkrs.Flags) {
p.addField(name, func() { p.PackBinary(value, flags) }) }
func (p *Packer) FieldString (name string, value string, flags packs.Flags) {
func (p *JSONPacker) FieldString (name string, value string, flags pkrs.Flags) {
p.addField(name, func() { p.PackString(value, flags) })
}
func (p *Packer) FieldBool (name string, value bool, flags packs.Flags) {
func (p *JSONPacker) FieldBool (name string, value bool, flags pkrs.Flags) {
p.addField(name, func() { p.PackBool(value, flags) })
}
func (p *Packer) FieldInt8 (name string, value int8 , flags packs.Flags) {
func (p *JSONPacker) FieldInt8 (name string, value int8 , flags pkrs.Flags) {
p.addField(name, func() { p.PackInt64(int64(value), flags) })}
func (p *Packer) FieldInt16 (name string, value int16, flags packs.Flags) {
func (p *JSONPacker) FieldInt16 (name string, value int16, flags pkrs.Flags) {
p.addField(name, func() { p.PackInt64(int64(value), flags) })}
func (p *Packer) FieldInt32 (name string, value int32, flags packs.Flags) {
func (p *JSONPacker) FieldInt32 (name string, value int32, flags pkrs.Flags) {
p.addField(name, func() { p.PackInt64(int64(value), flags) }) }
func (p *Packer) FieldInt64 (name string, value int64, flags packs.Flags) {
func (p *JSONPacker) FieldInt64 (name string, value int64, flags pkrs.Flags) {
p.addField(name, func() { p.PackInt64(value, flags) }) }
func (p *Packer) FieldUInt8 (name string, value uint8, flags packs.Flags) {
func (p *JSONPacker) FieldUInt8 (name string, value uint8, flags pkrs.Flags) {
p.addField(name, func() { p.PackUInt64(uint64(value), flags) }) }
func (p *Packer) FieldUInt16 (name string, value uint16, flags packs.Flags) {
func (p *JSONPacker) FieldUInt16 (name string, value uint16, flags pkrs.Flags) {
p.addField(name,func(){ p.PackUInt64(uint64(value), flags) }) }
func (p *Packer) FieldUInt32 (name string, value uint32, flags packs.Flags) {
func (p *JSONPacker) FieldUInt32 (name string, value uint32, flags pkrs.Flags) {
p.addField(name,func(){ p.PackUInt64(uint64(value), flags) }) }
func (p *Packer) FieldUInt64 (name string, value uint64, flags packs.Flags) {
func (p *JSONPacker) FieldUInt64 (name string, value uint64, flags pkrs.Flags) {
p.addField(name,func(){ p.PackUInt64(value, flags) }) }
func (p *Packer) FieldFloat32(name string, value float32,flags packs.Flags) {
func (p *JSONPacker) FieldFloat32(name string, value float32,flags pkrs.Flags) {
p.addField(name,func(){ p.PackFloat32(value, flags) })
}
func (p *Packer) FieldFloat64(name string, value float64,flags packs.Flags) {
func (p *JSONPacker) FieldFloat64(name string, value float64,flags pkrs.Flags) {
p.addField(name,func(){ p.PackFloat64(value, flags) })
}
func (p *Packer) FieldDate (name string, value time.Time,flags packs.Flags) {
func (p *JSONPacker) FieldDate (name string, value time.Time,flags pkrs.Flags) {
p.addField(name,func(){ p.PackDate(value, flags) })
}
func (p *Packer) FieldList (name string, builder func(packs.ListPackBuilder,packs.Flags), flags packs.Flags) {
func (p *JSONPacker) FieldList (name string, builder func(pkrs.ListPackBuilder, pkrs.Flags), flags pkrs.Flags) {
p.addField(name,func(){ p.PackList(builder, flags) })
}
func (p *Packer) FieldMap (name string, builder func(packs.MapPackBuilder, packs.Flags), flags packs.Flags) {
func (p *JSONPacker) FieldMap (name string, builder func(pkrs.MapPackBuilder, pkrs.Flags), flags pkrs.Flags) {
p.addField(name,func(){ p.PackMap(builder, flags) })
}
func (p *Packer) FieldObject (name string, builder func(packs.ObjectPackBuilder,packs.Flags), flags packs.Flags) {
func (p *JSONPacker) FieldObject (name string, builder func(pkrs.ObjectPackBuilder, pkrs.Flags), flags pkrs.Flags) {
p.addField(name,func(){ p.PackObject(builder, flags) })
}
func (p *Packer) addField(name string, caller func()) {
func (p *JSONPacker) addField(name string, caller func()) {
if p.state.Count() > 0 {
p.state.WriteByte(',')
}
packString(p.state,name,packs.FlagScapeHTML)
packString(p.state,name, pkrs.FlagScapeHTML)
if p.state.Error != nil {
return
}

@ -33,7 +33,7 @@ type (
rPackerFnc func (state *packs.State, rvalue reflect.Value, flags packs.Flags)
rPackerFnc func (state *pkrs.State, rvalue reflect.Value, flags pkrs.Flags)
)
@ -49,7 +49,7 @@ func rTypePacker (rtype reflect.Type) rPackerFnc {
wg.Add(1)
fi,ld := g.packersCache.LoadOrStore(rtype, rPackerFnc(func (e *packs.State, v reflect.Value, f packs.Flags) {
fi,ld := g.packersCache.LoadOrStore(rtype, rPackerFnc(func (e *pkrs.State, v reflect.Value, f pkrs.Flags) {
wg.Wait()
fn(e,v,f)
}))
@ -68,20 +68,20 @@ func rTypePacker (rtype reflect.Type) rPackerFnc {
func _rTypePacker(rtype reflect.Type) rPackerFnc {
switch rtype.Kind() {
case reflect.Bool : return func(s *packs.State, v reflect.Value, f packs.Flags) { packBool (s,v.Bool (),f )}
case reflect.String : return func(s *packs.State, v reflect.Value, f packs.Flags) { packString(s,v.String(),f )}
case reflect.Bool : return func(s *pkrs.State, v reflect.Value, f pkrs.Flags) { packBool (s,v.Bool (),f )}
case reflect.String : return func(s *pkrs.State, v reflect.Value, f pkrs.Flags) { packString(s,v.String(),f )}
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64 : return func(s *packs.State, v reflect.Value, f packs.Flags) { packInt (s,v.Int (),f )}
reflect.Int64 : return func(s *pkrs.State, v reflect.Value, f pkrs.Flags) { packInt (s,v.Int (),f )}
case reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64 : return func(s *packs.State, v reflect.Value, f packs.Flags) { packUInt (s,v.Uint (),f )}
case reflect.Float32: return func(s *packs.State, v reflect.Value, f packs.Flags) { packFloatString(s,v.Float(),f,32) }
case reflect.Float64: return func(s *packs.State, v reflect.Value, f packs.Flags) { packFloatString(s,v.Float(),f,64) }
reflect.Uint64 : return func(s *pkrs.State, v reflect.Value, f pkrs.Flags) { packUInt (s,v.Uint (),f )}
case reflect.Float32: return func(s *pkrs.State, v reflect.Value, f pkrs.Flags) { packFloatString(s,v.Float(),f,32) }
case reflect.Float64: return func(s *pkrs.State, v reflect.Value, f pkrs.Flags) { packFloatString(s,v.Float(),f,64) }
case reflect.Pointer:
return rPointerPacker(rtype)
@ -106,8 +106,8 @@ func _rTypePacker(rtype reflect.Type) rPackerFnc {
}
func unsupportedTypePacker(state *packs.State, _ reflect.Value, _ packs.Flags) {
state.SetError(packs.G.ErrUnsupportedType)
func unsupportedTypePacker(state *pkrs.State, _ reflect.Value, _ pkrs.Flags) {
state.SetError(pkrs.G.ErrUnsupportedType)
}

@ -28,7 +28,7 @@ import (
"reflect"
)
func rInterfacePacker (s *packs.State, v reflect.Value, f packs.Flags) {
func rInterfacePacker (s *pkrs.State, v reflect.Value, f pkrs.Flags) {
if v.IsNil() {
s.W(g.nullValue)
return

@ -45,7 +45,7 @@ func _stringifyRKey(rt reflect.Type) rMapEntryFnc {
if rt.Kind() == reflect.String {
return func(e *rMapEntry, k reflect.Value) error {
e.k = k.String()
e.f = packs.FlagScapeHTML
e.f = pkrs.FlagScapeHTML
return nil
}
}
@ -58,7 +58,7 @@ func _stringifyRKey(rt reflect.Type) rMapEntryFnc {
bf, er := (k.Interface().(encoding.TextMarshaler)).MarshalText()
if er == nil {
e.k = string(bf)
e.f = packs.FlagScapeHTML
e.f = pkrs.FlagScapeHTML
}
return er
}
@ -92,7 +92,7 @@ func _stringifyRKey(rt reflect.Type) rMapEntryFnc {
if k.Type().Kind() == reflect.String {
e.k = k.String()
e.f = packs.FlagScapeHTML
e.f = pkrs.FlagScapeHTML
return nil
}
@ -103,7 +103,7 @@ func _stringifyRKey(rt reflect.Type) rMapEntryFnc {
bf, er := t.MarshalText()
if er == nil {
e.k = string(bf)
e.f = packs.FlagScapeHTML
e.f = pkrs.FlagScapeHTML
}
return er
}
@ -128,7 +128,7 @@ func _stringifyRKey(rt reflect.Type) rMapEntryFnc {
return nil
default:
return packs.G.ErrInvalidKey
return pkrs.G.ErrInvalidKey
}
}

@ -57,7 +57,7 @@ func newRList(rtype reflect.Type) *rList {
func (p rList) packBinary (s *packs.State, v reflect.Value, f packs.Flags) {
func (p rList) packBinary (s *pkrs.State, v reflect.Value, f pkrs.Flags) {
if f.Binary() {
packBinary(s, v.Interface().([]byte), f)
return
@ -66,7 +66,7 @@ func (p rList) packBinary (s *packs.State, v reflect.Value, f packs.Flags) {
}
func (p rList) packBinarySlice (s *packs.State, v reflect.Value, f packs.Flags) {
func (p rList) packBinarySlice (s *pkrs.State, v reflect.Value, f pkrs.Flags) {
if f.Binary() {
packBinary(s, v.Interface().([]byte), f)
return
@ -77,7 +77,7 @@ func (p rList) packBinarySlice (s *packs.State, v reflect.Value, f packs.Flags)
func (p rList) packSlice(s *packs.State, v reflect.Value, f packs.Flags) {
func (p rList) packSlice(s *pkrs.State, v reflect.Value, f pkrs.Flags) {
if v.IsNil() {
s.W(g.nullValue)
return
@ -94,7 +94,7 @@ func (p rList) packSlice(s *packs.State, v reflect.Value, f packs.Flags) {
if fn,ok := s.CheckCycle(cp); ok {
defer fn()
} else {
s.SetError(packs.G.ErrRedundantPack)
s.SetError(pkrs.G.ErrRedundantPack)
return
}
}
@ -104,7 +104,7 @@ func (p rList) packSlice(s *packs.State, v reflect.Value, f packs.Flags) {
func (p rList) pack (s *packs.State, v reflect.Value, f packs.Flags) {
func (p rList) pack (s *pkrs.State, v reflect.Value, f pkrs.Flags) {
if v.IsNil() {
s.W(g.nullValue)
return

@ -52,9 +52,9 @@ func newRMap(rtype reflect.Type) *rMap {
}
func (p *rMap) pack (s *packs.State, v reflect.Value, f packs.Flags) {
func (p *rMap) pack (s *pkrs.State, v reflect.Value, f pkrs.Flags) {
if p.keyFn == nil {
s.SetError(packs.G.ErrInvalidKey)
s.SetError(pkrs.G.ErrInvalidKey)
return
}
@ -71,7 +71,7 @@ func (p *rMap) pack (s *packs.State, v reflect.Value, f packs.Flags) {
if fn,ok := s.CheckCycle(v.UnsafePointer()); ok {
defer fn()
} else {
s.SetError(packs.G.ErrRedundantPack)
s.SetError(pkrs.G.ErrRedundantPack)
return
}
}

@ -32,7 +32,7 @@ type (
rMapEntry struct {
f packs.Flags // flags
f pkrs.Flags // flags
v reflect.Value // value
k string // Key
}

@ -46,7 +46,7 @@ func rObjectPacker (rtype reflect.Type) rPackerFnc {
if !ok {
op,_ = g.fieldsCache.LoadOrStore(rtype,*newRObject(rtype))
}
return func(s *packs.State, value reflect.Value, flags packs.Flags) {
return func(s *pkrs.State, value reflect.Value, flags pkrs.Flags) {
op.(rObject).pack(s,value,flags)
}
//return (op.(rObject)).pack
@ -85,7 +85,7 @@ func newRObject(rtype reflect.Type) *rObject {
continue
}
nm, fg, has := packs.FlagsFromPackTag(sf.Tag)
nm, fg, has := pkrs.FlagsFromPackTag(sf.Tag)
if !has {
nm, fg, has = jsonTags(sf.Tag)
}
@ -93,7 +93,7 @@ func newRObject(rtype reflect.Type) *rObject {
if nm == "-" || fg.Omit() {
continue
}
if !packs.CheckTag(nm) {
if !pkrs.CheckTag(nm) {
nm = ""
}
}
@ -114,7 +114,7 @@ func newRObject(rtype reflect.Type) *rObject {
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
reflect.Float32, reflect.Float64,
reflect.String:
fg = fg|packs.FlagQuote
fg = fg| pkrs.FlagQuote
}
}
@ -131,7 +131,7 @@ func newRObject(rtype reflect.Type) *rObject {
flags : fg,
}
of.nameB = []byte(of.name)
of.foldFn= packs.GetFoldFunc(of.nameB)
of.foldFn= pkrs.GetFoldFunc(of.nameB)
// Build nameEscHTML and nameNonEsc ahead of time.
nms.Reset()
@ -211,7 +211,7 @@ func newRObject(rtype reflect.Type) *rObject {
func (o rObject) pack (s *packs.State, v reflect.Value, f packs.Flags) {
func (o rObject) pack (s *pkrs.State, v reflect.Value, f pkrs.Flags) {
nx := byte('{')
FieldLoop:
for i:= range o.fields {
@ -248,11 +248,11 @@ FieldLoop:
}
func jsonTags (tag reflect.StructTag) (string,packs.Flags,bool) {
return packs.FlagsFromTag(tag,jsonTag,func(n string) packs.Flags {
func jsonTags (tag reflect.StructTag) (string, pkrs.Flags,bool) {
return pkrs.FlagsFromTag(tag,jsonTag,func(n string) pkrs.Flags {
switch n {
case "omitempty" : return packs.FlagOmitEmpty
case "quote" : return packs.FlagQuote
case "omitempty" : return pkrs.FlagOmitEmpty
case "quote" : return pkrs.FlagQuote
default : return 0
}
})

@ -39,9 +39,9 @@ type (
isTagged bool
index []int
rtype reflect.Type
flags packs.Flags
flags pkrs.Flags
foldFn packs.FoldFnc
foldFn pkrs.FoldFnc
packerFn rPackerFnc
}

@ -46,7 +46,7 @@ func newRPointer (rtype reflect.Type) *rPointer {
}
func (p rPointer) pack (s *packs.State, v reflect.Value, f packs.Flags) {
func (p rPointer) pack (s *pkrs.State, v reflect.Value, f pkrs.Flags) {
if v.IsNil() {
s.W(g.nullValue)
return

@ -36,6 +36,6 @@ func rValuePacker (rvalue reflect.Value) rPackerFnc {
}
func invalidValuePacker(state *packs.State, _ reflect.Value, _ packs.Flags) {
state.SetError(packs.G.ErrInvalidValue)
func invalidValuePacker(state *pkrs.State, _ reflect.Value, _ pkrs.Flags) {
state.SetError(pkrs.G.ErrInvalidValue)
}

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package packs
package pkrs
import "time"

@ -0,0 +1,3 @@
module tbase
go 1.22

@ -14,8 +14,8 @@ use (
./core/times
./core/timers
./core/ctxs
./core/atypes
./core/packs
./core/tbase
./core/pkrs
./app

Loading…
Cancel
Save

Powered by TurnKey Linux.