diff --git a/base/apack/pkjson/active.go b/Z/pkjson/active.go similarity index 100% rename from base/apack/pkjson/active.go rename to Z/pkjson/active.go diff --git a/base/apack/pkjson/builder.go b/Z/pkjson/builder.go similarity index 100% rename from base/apack/pkjson/builder.go rename to Z/pkjson/builder.go diff --git a/base/apack/pkjson/key_stringer.go b/Z/pkjson/key_stringer.go similarity index 100% rename from base/apack/pkjson/key_stringer.go rename to Z/pkjson/key_stringer.go diff --git a/base/apack/pkjson/packer.go b/Z/pkjson/packer.go similarity index 96% rename from base/apack/pkjson/packer.go rename to Z/pkjson/packer.go index d3334cd..e00a62f 100644 --- a/base/apack/pkjson/packer.go +++ b/Z/pkjson/packer.go @@ -50,7 +50,7 @@ func NewPacker (options ...PackerFnc) *Packer { } -func (p Packer) Pack (value any) rsls.Bytes { +func (p Packer) pack(value any) rsls.Bytes { b := newBuilder(p.f) defer b.release() diff --git a/base/apack/pkjson/packer_list.go b/Z/pkjson/packer_list.go similarity index 100% rename from base/apack/pkjson/packer_list.go rename to Z/pkjson/packer_list.go diff --git a/base/apack/pkjson/reflected.go b/Z/pkjson/reflected.go similarity index 100% rename from base/apack/pkjson/reflected.go rename to Z/pkjson/reflected.go diff --git a/base/apack/pkjson/reflected_list.go b/Z/pkjson/reflected_list.go similarity index 100% rename from base/apack/pkjson/reflected_list.go rename to Z/pkjson/reflected_list.go diff --git a/base/apack/pkjson/reflected_map.go b/Z/pkjson/reflected_map.go similarity index 100% rename from base/apack/pkjson/reflected_map.go rename to Z/pkjson/reflected_map.go diff --git a/base/apack/pkjson/reflected_map_entries.go b/Z/pkjson/reflected_map_entries.go similarity index 100% rename from base/apack/pkjson/reflected_map_entries.go rename to Z/pkjson/reflected_map_entries.go diff --git a/base/apack/pkjson/reflected_map_entry.go b/Z/pkjson/reflected_map_entry.go similarity index 100% rename from base/apack/pkjson/reflected_map_entry.go rename to Z/pkjson/reflected_map_entry.go diff --git a/base/apack/pkjson/reflected_object.go b/Z/pkjson/reflected_object.go similarity index 100% rename from base/apack/pkjson/reflected_object.go rename to Z/pkjson/reflected_object.go diff --git a/base/apack/pkjson/reflected_object_field.go b/Z/pkjson/reflected_object_field.go similarity index 100% rename from base/apack/pkjson/reflected_object_field.go rename to Z/pkjson/reflected_object_field.go diff --git a/base/apack/pkjson/reflected_object_fields.go b/Z/pkjson/reflected_object_fields.go similarity index 100% rename from base/apack/pkjson/reflected_object_fields.go rename to Z/pkjson/reflected_object_fields.go diff --git a/a/main.go b/a/main.go index e4a8211..49e7f9f 100644 --- a/a/main.go +++ b/a/main.go @@ -1,7 +1,7 @@ package main import ( - "apack/pkj" + "apacks/pkjson" "errs" "fmt" "math/rand" @@ -11,13 +11,17 @@ import ( type ( S struct { - A string + A string `pack:"encrypted"` + F float32 } ) func Una () S { fmt.Println("Es llamado ") - return S { A : "Un valiue" } + return S { + A : "sdsd", + F : 32.45, + } } func randomInt(min, max int) int { @@ -56,13 +60,13 @@ func main() { "catorce": 15.16, "dfate": time.Now(), time.Now(): "ff", - "SSSS" : S{A:"a"}, + "SSSS" : S{A:"a",F: 32.2}, } - r := p.Pack(myMap) + r := p.pack(myMap) if !r.Ok() { fmt.Printf("%s \n",string(errs.Format(r.Fail(), eftext.Formatter()))) return @@ -77,9 +81,8 @@ func main() { */ - b := pkj.NewBuilder() - //r := b.Pack(S{ A : "SOOOOOO"}) - r := b.Pack( + + r := pkjson.Pack( map[interface{}]interface{}{ //1: "uno", "dos": 2, @@ -98,7 +101,7 @@ func main() { time.Now(): "ff", - "SSSS" : S{A:"a"}, + "SSSS" : Una(), }, diff --git a/base/apack/go.mod b/base/apack/go.mod deleted file mode 100644 index 77f5758..0000000 --- a/base/apack/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module apack - -go 1.22 diff --git a/base/apack/active.go b/base/apacks/active.go similarity index 100% rename from base/apack/active.go rename to base/apacks/active.go diff --git a/base/apack/flags.go b/base/apacks/flags.go similarity index 100% rename from base/apack/flags.go rename to base/apacks/flags.go diff --git a/base/apack/fold.go b/base/apacks/fold.go similarity index 100% rename from base/apack/fold.go rename to base/apacks/fold.go diff --git a/base/apacks/go.mod b/base/apacks/go.mod new file mode 100644 index 0000000..ab23067 --- /dev/null +++ b/base/apacks/go.mod @@ -0,0 +1,3 @@ +module apacks + +go 1.22 diff --git a/base/apacks/packer.go b/base/apacks/packer.go new file mode 100644 index 0000000..6b7943f --- /dev/null +++ b/base/apacks/packer.go @@ -0,0 +1,45 @@ +// ------------------------------------------------------------------------ +// ๐Ÿš€ Project active3 +// ๐Ÿ”— Active Thing (activething.com) git.activething.com/go +// +// File name packer.go +// โœ๏ธ Created by DEV +// ๐Ÿ“… Modified 12/06/2024 +// +// ๐Ÿ”’ Copyright 2024 activething.com +// +// ๐Ÿ“œ LICENSE -------------------------------------------------------------- +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + +package apack + +import "rsls" + +type ( + + + Packer interface { + + pack(value any) rsls.Bytes + } + + + PackFnc func(value any) rsls.Bytes +) + + + +func (f PackFnc) pack(value any) rsls.Bytes { + return f(value) } + diff --git a/base/apack/pkj/active.go b/base/apacks/pkjson/active.go similarity index 94% rename from base/apack/pkj/active.go rename to base/apacks/pkjson/active.go index 2e2c1b7..10dfc3e 100644 --- a/base/apack/pkj/active.go +++ b/base/apacks/pkjson/active.go @@ -22,14 +22,15 @@ // limitations under the License. // ------------------------------------------------------------------------ -package pkj +package pkjson import ( - "apack" + "apacks" "blds" "encoding" "encoding/json" "reflect" + "rsls" "time" ) @@ -87,3 +88,6 @@ var ( ) + +func Pack (value any, flag...apack.Flags) rsls.Bytes { + return (NewBuilder(flag...)).Pack(value) } diff --git a/base/apack/pkj/builder.go b/base/apacks/pkjson/builder.go similarity index 85% rename from base/apack/pkj/builder.go rename to base/apacks/pkjson/builder.go index 7a82864..070b8d4 100644 --- a/base/apack/pkj/builder.go +++ b/base/apacks/pkjson/builder.go @@ -22,14 +22,15 @@ // limitations under the License. // ------------------------------------------------------------------------ -package pkj +package pkjson import ( - "apack" - "apack/pstate" + "apacks" + "apacks/pkstate" "blds" "cdcs/cjson" "encoding/json" + "fmt" "reflect" "rsls" "time" @@ -38,28 +39,75 @@ import ( type ( Builder struct { - *pstate.State + *pkstate.State e *json.Encoder f apack.Flags } + + ) -func NewBuilder () *Builder { + + + +func NewBuilder(flag ...apack.Flags) *Builder { + f := apack.Flags(0) + if len(flag) == 1 { + f = flag[0] + } else if len(flag) > 1 { + for _,fl := range flag { + f|=fl + } + } return &Builder{ - State : pstate.AcquireState(), + State : pkstate.AcquireState(), + f : f, } } -func (b *Builder) Pack (value any) rsls.Bytes { +func (b *Builder) release () { + pkstate.ReleaseState(b.State) + b.State = nil +} + + + +func (b *Builder) Pack(value any) (rs rsls.Bytes) { + d := false + + // Defer function to handle panic recovery and resource release + defer func(bl *Builder, dn *bool){ + // If not done, recover from any panic + if !(*dn) { + if rc := recover(); rc != nil { + if er, is := rc.(error); is { + rs = rsls.MakeBytesError(er) + } else { + rs = rsls.MakeBytesError(fmt.Errorf("%#v", rc)) + } + } + } + // Release resources + bl.release() + }(b, &d) + + // Attempt to process the value b.Any(value) + d = true // Mark as done if no panic occurred + + // Check for errors in the builder if b.Err != nil { return rsls.MakeBytesError(b.Err) } + + // Return success result with bytes return rsls.MakeBytesSuccess(b.B.Bytes()) } + + func (b *Builder) Any (value any) { if value == nil { @@ -153,7 +201,7 @@ func (b *Builder) Any (value any) { case []time.Duration: packListG[time.Duration](b, v, b.Duration) case []error: - b.buildLevel(pstate.ModeList, v == nil, len(v) == 0, func(bl *Builder) { + b.buildLevel(pkstate.ModeList, v == nil, len(v) == 0, func(bl *Builder) { for _, e := range v { bl.Item() if bl.Err == nil { @@ -196,7 +244,7 @@ func (b *Builder) Any (value any) { case map[string]time.Duration: packMapG[time.Duration](b, v, b.Duration) case map[string]error: - b.buildLevel(pstate.ModeMap, v == nil, len(v) == 0, func(bl *Builder) { + b.buildLevel(pkstate.ModeMap, v == nil, len(v) == 0, func(bl *Builder) { for k, v := range v { bl.Field(k) if bl.Err == nil { @@ -386,17 +434,17 @@ func (b *Builder) writeBinary (value []byte, flags apack.Flags) { func (b *Builder) List (fnc func(builder blds.ListBuilder)) { - b.buildLevel(pstate.ModeList,fnc==nil,false,func(bl *Builder){ + b.buildLevel(pkstate.ModeList,fnc==nil,false,func(bl *Builder){ fnc(b) }) } func (b *Builder) Map (fnc func(builder blds.MapBuilder)) { - b.buildLevel(pstate.ModeMap,fnc == nil, false, func(bl *Builder){ + b.buildLevel(pkstate.ModeMap,fnc == nil, false, func(bl *Builder){ fnc(b) }) } func (b *Builder) Object (fnc func(builder blds.ObjectBuilder)) { - b.buildLevel(pstate.ModeObject,fnc == nil, false, func(bl *Builder){ + b.buildLevel(pkstate.ModeObject,fnc == nil, false, func(bl *Builder){ fnc(b) }) } @@ -434,7 +482,7 @@ func (b *Builder) Field (name string) blds.ValueBuilder { -func (b *Builder) buildLevel (mode pstate.Mode, isNil, isEmpty bool, buildFnc func(builder *Builder)) { +func (b *Builder) buildLevel (mode pkstate.Mode, isNil, isEmpty bool, buildFnc func(builder *Builder)) { if !b.preValue() { return } @@ -446,7 +494,7 @@ func (b *Builder) buildLevel (mode pstate.Mode, isNil, isEmpty bool, buildFnc fu } if isEmpty { - if mode == pstate.ModeList { + if mode == pkstate.ModeList { b.B.Write2Byte('[',']') } else { b.B.Write2Byte('{','}') @@ -457,7 +505,7 @@ func (b *Builder) buildLevel (mode pstate.Mode, isNil, isEmpty bool, buildFnc fu b.Open(mode) be := byte('}') - if mode == pstate.ModeList { + if mode == pkstate.ModeList { b.B.WriteByte('[') be = ']' } else { @@ -493,8 +541,8 @@ func (b *Builder) postValue () { } b.Current.IsValue = false b.Current.Count++ - if b.Current.Mode == pstate.ModeValue { - b.Current.Mode = pstate.ModeNone + if b.Current.Mode == pkstate.ModeValue { + b.Current.Mode = pkstate.ModeNone } } @@ -533,7 +581,7 @@ func (b *Builder) encode (value any) error { func packListG[T basicTypes](b *Builder, list []T, fncType func(vl T)) { - b.buildLevel(pstate.ModeList,list == nil, len(list) == 0,func(bl *Builder){ + b.buildLevel(pkstate.ModeList,list == nil, len(list) == 0,func(bl *Builder){ for _,v := range list { b.Item() if b.Err == nil { @@ -547,7 +595,7 @@ func packListG[T basicTypes](b *Builder, list []T, fncType func(vl T)) { } func packMapG[T basicTypes](b *Builder, m map[string]T, fncType func(vl T)) { - b.buildLevel(pstate.ModeMap, m == nil, len(m) == 0, func(bl *Builder){ + b.buildLevel(pkstate.ModeMap, m == nil, len(m) == 0, func(bl *Builder){ for k,v := range m { // Usamos b.Field y no b.Key porque sabemos que no es necesario // hacerle un stringify y el objeto como el map la codificaciรณn diff --git a/base/apack/pkj/packers.go b/base/apacks/pkjson/packers.go similarity index 97% rename from base/apack/pkj/packers.go rename to base/apacks/pkjson/packers.go index 14ce098..67f44a3 100644 --- a/base/apack/pkj/packers.go +++ b/base/apacks/pkjson/packers.go @@ -22,11 +22,11 @@ // limitations under the License. // ------------------------------------------------------------------------ -package pkj +package pkjson import ( - "apack" - "apack/pstate" + "apacks" + "apacks/pkstate" "blds" "cdcs/cjson" "core/tups" @@ -136,7 +136,7 @@ func newRPackers (options ...rpackersFnc) *rpackers { if !v.IsNil() { l = v.Interface().([]error) } - b.buildLevel(pstate.ModeList, l == nil, len(l) == 0, func(bl *Builder) { + b.buildLevel(pkstate.ModeList, l == nil, len(l) == 0, func(bl *Builder) { for _, e := range l { bl.Item() if bl.Err == nil { @@ -176,7 +176,7 @@ func newRPackers (options ...rpackersFnc) *rpackers { if !v.IsNil() { l = v.Interface().(map[string]error) } - b.buildLevel(pstate.ModeMap, l == nil, len(l) == 0, func(bl *Builder) { + b.buildLevel(pkstate.ModeMap, l == nil, len(l) == 0, func(bl *Builder) { for k,v := range l { bl.Field(k) if bl.Err == nil { @@ -299,7 +299,7 @@ func (p *rpackers) buildRPacker (rtype reflect.Type) rPackFnc { } } - b.buildLevel(pstate.ModeList, false, v.Len() == 0, func(bl *Builder){ + b.buildLevel(pkstate.ModeList, false, v.Len() == 0, func(bl *Builder){ for i:=0; i' || c == '&' { + if st < i { + b.W(source[st:i]) + } + b.WriteString(`\u00`) + b.WriteByte(chrs.G.HEX[c>>4]) + b.WriteByte(chrs.G.HEX[c&0xF]) + st = i + 1 + } else if c == 0xE2 && i+2 < len(source) && source[i+1] == 0x80 && source[i+2]&^1 == 0xA8 { + if st < i { + b.W(source[st:i]) + } + b.WriteString(`\u202`) + b.WriteByte(chrs.G.HEX[source[i+2]&0xF]) + st = i + 3 + } + } + if st < len(source) { + b.W(source[st:]) + } + /* st := 0 for i,c := range source { if c == '<' || c == '>' || c == '&' { @@ -271,6 +294,8 @@ func (b *ABuffer) WriteHTML(source []byte) { b.W(source[st:]) } } + + */ } diff --git a/core/errs/active.go b/core/errs/active.go index 3dc5327..3030c7f 100644 --- a/core/errs/active.go +++ b/core/errs/active.go @@ -25,7 +25,7 @@ package errs import ( - "errs/eftext" + "errs/efjson" "errs/stack" "errs/wrap" "fmt" @@ -61,7 +61,7 @@ var ( ErrParamNil : ErrCode(CodeParamNilError), ErrParamOutOfRange : ErrCode(CodeParamOutOfRangeError), - Formatter : eftext.Formatter(), + Formatter : efjson.Formatter(), } ) diff --git a/go.work b/go.work index 363e5ae..8f56535 100644 --- a/go.work +++ b/go.work @@ -4,7 +4,7 @@ use ( ./a ./a/test ./base/alogs - ./base/apack + ./base/apacks ./core ./core/aios ./core/bfrs