corregido el formateador de error para que no escriba la seccion Causes en caso de que no tenga ninguna
parent
acb1ffc10b
commit
3d4b322144
@ -1,3 +0,0 @@
|
||||
module atypes
|
||||
|
||||
go 1.22
|
||||
@ -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
|
||||
})
|
||||
}
|
||||
@ -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 }
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
module tbase
|
||||
|
||||
go 1.22
|
||||
Loading…
Reference in new issue