diff --git a/app/main.go b/app/main.go index 6dcc9de..5fb2fe5 100644 --- a/app/main.go +++ b/app/main.go @@ -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))) } diff --git a/core/atypes/go.mod b/core/atypes/go.mod deleted file mode 100644 index 0ded963..0000000 --- a/core/atypes/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module atypes - -go 1.22 diff --git a/core/bits/g_bflags.go b/core/bits/g_bflags.go index dcc5de0..5694345 100644 --- a/core/bits/g_bflags.go +++ b/core/bits/g_bflags.go @@ -24,7 +24,6 @@ package bits import ( - "atypes" "encoding/binary" "math/bits" ) diff --git a/core/btes/bfers/active.go b/core/btes/bfers/active.go index a4c436d..af79b5e 100644 --- a/core/btes/bfers/active.go +++ b/core/btes/bfers/active.go @@ -56,9 +56,7 @@ var ( } g = struct { - bufferPool *Pool - }{ bufferPool: NewPool(), } diff --git a/core/btes/bfers/err.go b/core/btes/bfers/err.go index b102b67..df54879 100644 --- a/core/btes/bfers/err.go +++ b/core/btes/bfers/err.go @@ -23,11 +23,12 @@ package bfers -import "errs" +import ( + "errs" +) type ( Err struct { errs.ErrCode } ) - diff --git a/core/errs/err_code.go b/core/errs/err_code.go index e4defc3..d97d490 100644 --- a/core/errs/err_code.go +++ b/core/errs/err_code.go @@ -26,6 +26,7 @@ package errs import ( "errs/fmtr" + "errs/fmtr/collr" ) type ( @@ -74,4 +75,9 @@ func (e ErrCode) String () string { func (e ErrCode) BuildFormat(builder fmtr.Builder) { - builder.Build(e,nil) } \ No newline at end of file + builder.Build(e,nil) } + + +func (e ErrCode) Collect (collector collr.Collector) { + collector.AddFieldString(collr.FieldNameCode,string(e)) +} \ No newline at end of file diff --git a/core/errs/err_info.go b/core/errs/err_info.go index 3ed4bed..940a0e4 100644 --- a/core/errs/err_info.go +++ b/core/errs/err_info.go @@ -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) + } + } +} + + diff --git a/core/errs/fmtr/builder.go b/core/errs/fmtr/builder.go index f7e2507..09f2d50 100644 --- a/core/errs/fmtr/builder.go +++ b/core/errs/fmtr/builder.go @@ -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 // diff --git a/core/errs/fmtr/collr/active.go b/core/errs/fmtr/collr/active.go new file mode 100644 index 0000000..5c530fd --- /dev/null +++ b/core/errs/fmtr/collr/active.go @@ -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 + }), + + } +} + + + + + + diff --git a/core/errs/fmtr/collr/collectable.go b/core/errs/fmtr/collr/collectable.go new file mode 100644 index 0000000..8ab240b --- /dev/null +++ b/core/errs/fmtr/collr/collectable.go @@ -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) + } +) \ No newline at end of file diff --git a/core/errs/fmtr/collr/collector.go b/core/errs/fmtr/collr/collector.go new file mode 100644 index 0000000..6d64fbe --- /dev/null +++ b/core/errs/fmtr/collr/collector.go @@ -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) + } +) + + diff --git a/core/errs/fmtr/collr/error_collection.go b/core/errs/fmtr/collr/error_collection.go new file mode 100644 index 0000000..e225a9c --- /dev/null +++ b/core/errs/fmtr/collr/error_collection.go @@ -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 +} + + + + + + + + diff --git a/core/errs/fmtr/collr/error_collector.go b/core/errs/fmtr/collr/error_collector.go new file mode 100644 index 0000000..cf2a9fb --- /dev/null +++ b/core/errs/fmtr/collr/error_collector.go @@ -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) {} \ No newline at end of file diff --git a/core/errs/fmtr/collr/field_collector.go b/core/errs/fmtr/collr/field_collector.go new file mode 100644 index 0000000..da47264 --- /dev/null +++ b/core/errs/fmtr/collr/field_collector.go @@ -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) } + diff --git a/core/errs/fmtr/collr/field_collectors.go b/core/errs/fmtr/collr/field_collectors.go new file mode 100644 index 0000000..51b761b --- /dev/null +++ b/core/errs/fmtr/collr/field_collectors.go @@ -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 +} + + diff --git a/core/errs/fmtr/collr/field_head.go b/core/errs/fmtr/collr/field_head.go new file mode 100644 index 0000000..3d2d293 --- /dev/null +++ b/core/errs/fmtr/collr/field_head.go @@ -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 + } +) + + diff --git a/core/errs/fmtr/collr/field_kind.go b/core/errs/fmtr/collr/field_kind.go new file mode 100644 index 0000000..6f94ece --- /dev/null +++ b/core/errs/fmtr/collr/field_kind.go @@ -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 +) + + diff --git a/core/errs/fmtr/collr/fields.go b/core/errs/fmtr/collr/fields.go new file mode 100644 index 0000000..1b0d0c0 --- /dev/null +++ b/core/errs/fmtr/collr/fields.go @@ -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 + }) +} diff --git a/core/errs/fmtr/field_builder.go b/core/errs/fmtr/field_builder.go index 43b0437..3bf361c 100644 --- a/core/errs/fmtr/field_builder.go +++ b/core/errs/fmtr/field_builder.go @@ -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 // diff --git a/core/errs/fmtr/fjson/active.go b/core/errs/fmtr/fjson/active.go new file mode 100644 index 0000000..a4ac845 --- /dev/null +++ b/core/errs/fmtr/fjson/active.go @@ -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 { + + }{} +) + + diff --git a/core/errs/fmtr/fjson/json_builder.go b/core/errs/fmtr/fjson/json_builder.go new file mode 100644 index 0000000..210256f --- /dev/null +++ b/core/errs/fmtr/fjson/json_builder.go @@ -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('"') + } +} diff --git a/core/errs/fmtr/fjson/json_formatter.go b/core/errs/fmtr/fjson/json_formatter.go new file mode 100644 index 0000000..edc344c --- /dev/null +++ b/core/errs/fmtr/fjson/json_formatter.go @@ -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() +} diff --git a/core/errs/fmtr/fjson/option.go b/core/errs/fmtr/fjson/option.go new file mode 100644 index 0000000..c66f0d8 --- /dev/null +++ b/core/errs/fmtr/fjson/option.go @@ -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 } \ No newline at end of file diff --git a/core/errs/fmtr/ftext/active.go b/core/errs/fmtr/ftext/active.go index 436633f..52f89a6 100644 --- a/core/errs/fmtr/ftext/active.go +++ b/core/errs/fmtr/ftext/active.go @@ -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 - }{ - - } + }{} ) diff --git a/core/errs/fmtr/ftext/builder.go b/core/errs/fmtr/ftext/text_builder.go similarity index 100% rename from core/errs/fmtr/ftext/builder.go rename to core/errs/fmtr/ftext/text_builder.go diff --git a/core/errs/fmtr/ftext/text_formatter.go b/core/errs/fmtr/ftext/text_formatter.go index 4fa87d1..2a7712b 100644 --- a/core/errs/fmtr/ftext/text_formatter.go +++ b/core/errs/fmtr/ftext/text_formatter.go @@ -24,8 +24,8 @@ package ftext import ( - "atypes/astrings" "errs/stack" + "tbase/astrings" ) type ( diff --git a/core/packs/pjson/list_pack_builder.go b/core/packs/pjson/list_pack_builder.go deleted file mode 100644 index 037d057..0000000 --- a/core/packs/pjson/list_pack_builder.go +++ /dev/null @@ -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() - } -} - - diff --git a/core/packs/pjson/map_pack_builder.go b/core/packs/pjson/map_pack_builder.go deleted file mode 100644 index 15e3838..0000000 --- a/core/packs/pjson/map_pack_builder.go +++ /dev/null @@ -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) - } -} \ No newline at end of file diff --git a/core/packs/active.go b/core/pkrs/active.go similarity index 99% rename from core/packs/active.go rename to core/pkrs/active.go index 2433139..eebe601 100644 --- a/core/packs/active.go +++ b/core/pkrs/active.go @@ -21,7 +21,7 @@ // limitations under the License. // ------------------------------------------------------------------------ -package packs +package pkrs import ( "errs" diff --git a/core/packs/builder.go b/core/pkrs/builder.go similarity index 98% rename from core/packs/builder.go rename to core/pkrs/builder.go index c5e899b..2faad75 100644 --- a/core/packs/builder.go +++ b/core/pkrs/builder.go @@ -21,7 +21,7 @@ // limitations under the License. // ------------------------------------------------------------------------ -package packs +package pkrs diff --git a/core/packs/err.go b/core/pkrs/err.go similarity index 98% rename from core/packs/err.go rename to core/pkrs/err.go index d8b1b3b..4c4bc92 100644 --- a/core/packs/err.go +++ b/core/pkrs/err.go @@ -21,13 +21,12 @@ // limitations under the License. // ------------------------------------------------------------------------ -package packs +package pkrs import "errs" type ( Err struct { errs.ErrCode } -) - +) \ No newline at end of file diff --git a/core/packs/flags.go b/core/pkrs/flags.go similarity index 99% rename from core/packs/flags.go rename to core/pkrs/flags.go index 11799e1..bc29e6c 100644 --- a/core/packs/flags.go +++ b/core/pkrs/flags.go @@ -21,7 +21,7 @@ // limitations under the License. // ------------------------------------------------------------------------ -package packs +package pkrs import ( "bits" diff --git a/core/packs/fold.go b/core/pkrs/fold.go similarity index 99% rename from core/packs/fold.go rename to core/pkrs/fold.go index a270311..e30c284 100644 --- a/core/packs/fold.go +++ b/core/pkrs/fold.go @@ -21,7 +21,7 @@ // limitations under the License. // ------------------------------------------------------------------------ -package packs +package pkrs import ( "bins/chars" diff --git a/core/packs/go.mod b/core/pkrs/go.mod similarity index 100% rename from core/packs/go.mod rename to core/pkrs/go.mod diff --git a/core/packs/list_pack_builder.go b/core/pkrs/list_pack_builder.go similarity index 99% rename from core/packs/list_pack_builder.go rename to core/pkrs/list_pack_builder.go index a2dc618..57b98f4 100644 --- a/core/packs/list_pack_builder.go +++ b/core/pkrs/list_pack_builder.go @@ -21,7 +21,7 @@ // limitations under the License. // ------------------------------------------------------------------------ -package packs +package pkrs import "time" diff --git a/core/packs/map_pack_builder.go b/core/pkrs/map_pack_builder.go similarity index 99% rename from core/packs/map_pack_builder.go rename to core/pkrs/map_pack_builder.go index b30ccd3..e28fbbf 100644 --- a/core/packs/map_pack_builder.go +++ b/core/pkrs/map_pack_builder.go @@ -21,7 +21,7 @@ // limitations under the License. // ------------------------------------------------------------------------ -package packs +package pkrs import "time" diff --git a/core/packs/object_pack_builder.go b/core/pkrs/object_pack_builder.go similarity index 99% rename from core/packs/object_pack_builder.go rename to core/pkrs/object_pack_builder.go index 1ac74c0..6657bf0 100644 --- a/core/packs/object_pack_builder.go +++ b/core/pkrs/object_pack_builder.go @@ -21,7 +21,7 @@ // limitations under the License. // ------------------------------------------------------------------------ -package packs +package pkrs import "time" diff --git a/core/packs/packer.go b/core/pkrs/packer.go similarity index 96% rename from core/packs/packer.go rename to core/pkrs/packer.go index 8361acb..f8a216a 100644 --- a/core/packs/packer.go +++ b/core/pkrs/packer.go @@ -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 ( diff --git a/core/packs/packer_state.go b/core/pkrs/packer_state.go similarity index 99% rename from core/packs/packer_state.go rename to core/pkrs/packer_state.go index 04735b6..69ac09c 100644 --- a/core/packs/packer_state.go +++ b/core/pkrs/packer_state.go @@ -21,7 +21,7 @@ // limitations under the License. // ------------------------------------------------------------------------ -package packs +package pkrs import ( diff --git a/core/packs/pjson/active.go b/core/pkrs/pjson/active.go similarity index 92% rename from core/packs/pjson/active.go rename to core/pkrs/pjson/active.go index 51a7132..febbeb6 100644 --- a/core/packs/pjson/active.go +++ b/core/pkrs/pjson/active.go @@ -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()) diff --git a/core/packs/pjson/char_atts.go b/core/pkrs/pjson/char_atts.go similarity index 100% rename from core/packs/pjson/char_atts.go rename to core/pkrs/pjson/char_atts.go diff --git a/core/packs/pjson/packer.go b/core/pkrs/pjson/json_packer.go similarity index 66% rename from core/packs/pjson/packer.go rename to core/pkrs/pjson/json_packer.go index ff4a314..e0b9d09 100644 --- a/core/packs/pjson/packer.go +++ b/core/pkrs/pjson/json_packer.go @@ -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{} } diff --git a/core/pkrs/pjson/list_pack_builder.go b/core/pkrs/pjson/list_pack_builder.go new file mode 100644 index 0000000..66d9991 --- /dev/null +++ b/core/pkrs/pjson/list_pack_builder.go @@ -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() + } +} + + diff --git a/core/pkrs/pjson/map_pack_builder.go b/core/pkrs/pjson/map_pack_builder.go new file mode 100644 index 0000000..161a678 --- /dev/null +++ b/core/pkrs/pjson/map_pack_builder.go @@ -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) + } +} \ No newline at end of file diff --git a/core/packs/pjson/object_pack_builder.go b/core/pkrs/pjson/object_pack_builder.go similarity index 57% rename from core/packs/pjson/object_pack_builder.go rename to core/pkrs/pjson/object_pack_builder.go index 91efbd0..5f33c67 100644 --- a/core/packs/pjson/object_pack_builder.go +++ b/core/pkrs/pjson/object_pack_builder.go @@ -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 } diff --git a/core/packs/pjson/rpacker.go b/core/pkrs/pjson/rpacker.go similarity index 67% rename from core/packs/pjson/rpacker.go rename to core/pkrs/pjson/rpacker.go index 321d322..8a81e29 100644 --- a/core/packs/pjson/rpacker.go +++ b/core/pkrs/pjson/rpacker.go @@ -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) } diff --git a/core/packs/pjson/rpacker_interface.go b/core/pkrs/pjson/rpacker_interface.go similarity index 93% rename from core/packs/pjson/rpacker_interface.go rename to core/pkrs/pjson/rpacker_interface.go index e88ebd5..30a12a8 100644 --- a/core/packs/pjson/rpacker_interface.go +++ b/core/pkrs/pjson/rpacker_interface.go @@ -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 diff --git a/core/packs/pjson/rpacker_key.go b/core/pkrs/pjson/rpacker_key.go similarity index 95% rename from core/packs/pjson/rpacker_key.go rename to core/pkrs/pjson/rpacker_key.go index beb889e..1b549a2 100644 --- a/core/packs/pjson/rpacker_key.go +++ b/core/pkrs/pjson/rpacker_key.go @@ -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 } } diff --git a/core/packs/pjson/rpacker_list.go b/core/pkrs/pjson/rpacker_list.go similarity index 86% rename from core/packs/pjson/rpacker_list.go rename to core/pkrs/pjson/rpacker_list.go index 4eb22a9..cb8dc84 100644 --- a/core/packs/pjson/rpacker_list.go +++ b/core/pkrs/pjson/rpacker_list.go @@ -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 diff --git a/core/packs/pjson/rpacker_map.go b/core/pkrs/pjson/rpacker_map.go similarity index 93% rename from core/packs/pjson/rpacker_map.go rename to core/pkrs/pjson/rpacker_map.go index 48b6357..e6bc405 100644 --- a/core/packs/pjson/rpacker_map.go +++ b/core/pkrs/pjson/rpacker_map.go @@ -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 } } diff --git a/core/packs/pjson/rpacker_map_entry.go b/core/pkrs/pjson/rpacker_map_entry.go similarity index 94% rename from core/packs/pjson/rpacker_map_entry.go rename to core/pkrs/pjson/rpacker_map_entry.go index 807096f..7013668 100644 --- a/core/packs/pjson/rpacker_map_entry.go +++ b/core/pkrs/pjson/rpacker_map_entry.go @@ -32,9 +32,9 @@ type ( rMapEntry struct { - f packs.Flags // flags - v reflect.Value // value - k string // Key + f pkrs.Flags // flags + v reflect.Value // value + k string // Key } rMapEntryFnc func(entry *rMapEntry, rkey reflect.Value) error diff --git a/core/packs/pjson/rpacker_object.go b/core/pkrs/pjson/rpacker_object.go similarity index 91% rename from core/packs/pjson/rpacker_object.go rename to core/pkrs/pjson/rpacker_object.go index 2409ddb..b0a74f0 100644 --- a/core/packs/pjson/rpacker_object.go +++ b/core/pkrs/pjson/rpacker_object.go @@ -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 } }) diff --git a/core/packs/pjson/rpacker_object_field.go b/core/pkrs/pjson/rpacker_object_field.go similarity index 95% rename from core/packs/pjson/rpacker_object_field.go rename to core/pkrs/pjson/rpacker_object_field.go index 8bdd125..2cacfb6 100644 --- a/core/packs/pjson/rpacker_object_field.go +++ b/core/pkrs/pjson/rpacker_object_field.go @@ -38,11 +38,11 @@ type ( isTagged bool index []int - rtype reflect.Type - flags packs.Flags + rtype reflect.Type + flags pkrs.Flags - foldFn packs.FoldFnc - packerFn rPackerFnc + foldFn pkrs.FoldFnc + packerFn rPackerFnc } rFieldList []rObjectField diff --git a/core/packs/pjson/rpacker_pointer.go b/core/pkrs/pjson/rpacker_pointer.go similarity index 95% rename from core/packs/pjson/rpacker_pointer.go rename to core/pkrs/pjson/rpacker_pointer.go index a47693b..6dd5034 100644 --- a/core/packs/pjson/rpacker_pointer.go +++ b/core/pkrs/pjson/rpacker_pointer.go @@ -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 diff --git a/core/packs/pjson/rpacker_value.go b/core/pkrs/pjson/rpacker_value.go similarity index 90% rename from core/packs/pjson/rpacker_value.go rename to core/pkrs/pjson/rpacker_value.go index 599da0a..baa2b22 100644 --- a/core/packs/pjson/rpacker_value.go +++ b/core/pkrs/pjson/rpacker_value.go @@ -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) } \ No newline at end of file diff --git a/core/packs/value_packer.go b/core/pkrs/value_packer.go similarity index 99% rename from core/packs/value_packer.go rename to core/pkrs/value_packer.go index 41b6bd1..789d85f 100644 --- a/core/packs/value_packer.go +++ b/core/pkrs/value_packer.go @@ -21,7 +21,7 @@ // limitations under the License. // ------------------------------------------------------------------------ -package packs +package pkrs import "time" diff --git a/core/atypes/astrings/active.go b/core/tbase/astrings/active.go similarity index 100% rename from core/atypes/astrings/active.go rename to core/tbase/astrings/active.go diff --git a/core/atypes/astrings/camel.go b/core/tbase/astrings/camel.go similarity index 100% rename from core/atypes/astrings/camel.go rename to core/tbase/astrings/camel.go diff --git a/core/atypes/astrings/snake.go b/core/tbase/astrings/snake.go similarity index 100% rename from core/atypes/astrings/snake.go rename to core/tbase/astrings/snake.go diff --git a/core/atypes/astrings/string.go b/core/tbase/astrings/string.go similarity index 100% rename from core/atypes/astrings/string.go rename to core/tbase/astrings/string.go diff --git a/core/atypes/astrings/transformer.go b/core/tbase/astrings/transformer.go similarity index 100% rename from core/atypes/astrings/transformer.go rename to core/tbase/astrings/transformer.go diff --git a/core/atypes/astrings/transformers.go b/core/tbase/astrings/transformers.go similarity index 100% rename from core/atypes/astrings/transformers.go rename to core/tbase/astrings/transformers.go diff --git a/core/tbase/go.mod b/core/tbase/go.mod new file mode 100644 index 0000000..8fa5171 --- /dev/null +++ b/core/tbase/go.mod @@ -0,0 +1,3 @@ +module tbase + +go 1.22 diff --git a/core/atypes/numbers.go b/core/tbase/numbers.go similarity index 100% rename from core/atypes/numbers.go rename to core/tbase/numbers.go diff --git a/core/atypes/slices/strings.go b/core/tbase/slices/strings.go similarity index 100% rename from core/atypes/slices/strings.go rename to core/tbase/slices/strings.go diff --git a/go.work b/go.work index feee17d..1d130b4 100644 --- a/go.work +++ b/go.work @@ -14,8 +14,8 @@ use ( ./core/times ./core/timers ./core/ctxs - ./core/atypes - ./core/packs + ./core/tbase + ./core/pkrs ./app