From e637d6752e9c2242c4dfe839f1ddaf4e1ad48f11 Mon Sep 17 00:00:00 2001 From: DEV Date: Wed, 12 Jun 2024 18:42:30 +0200 Subject: [PATCH] pack reestructurado terminado --- a/main.go | 40 ++++- base/apack/pkj/builder.go | 13 +- base/apack/pkj/packers.go | 11 +- base/apack/pkj/rfield.go | 60 +++++++ base/apack/pkj/rfields.go | 60 +++++++ base/apack/pkj/robject.go | 275 ++++++++++++++++++++++++++++++ base/apack/pkj/stringers.go | 134 +++++++++++---- base/apack/pkjson/key_stringer.go | 2 +- core/errs/active.go | 6 +- core/rsls/string.go | 4 +- 10 files changed, 554 insertions(+), 51 deletions(-) create mode 100644 base/apack/pkj/rfield.go create mode 100644 base/apack/pkj/rfields.go create mode 100644 base/apack/pkj/robject.go diff --git a/a/main.go b/a/main.go index b9eadee..e4a8211 100644 --- a/a/main.go +++ b/a/main.go @@ -2,7 +2,7 @@ package main import ( "apack/pkj" - "blds" + "errs" "fmt" "math/rand" "time" @@ -25,11 +25,13 @@ func randomInt(min, max int) int { return rand.Intn(max-min+1) + min } - +/* func (s S) Build (b blds.Builder) { b.String("Esto es un campo S") } + */ + func main() { @@ -62,21 +64,47 @@ func main() { r := p.Pack(myMap) if !r.Ok() { - fmt.Printf("%s \n",string(errs.Encode(r.Fail(), eftext.Formatter()))) + fmt.Printf("%s \n",string(errs.Format(r.Fail(), eftext.Formatter()))) return } fmt.Printf("Data\n\n%s",string(r.Value())) -*/ + //l := []int {1,2,3,4} //m := map[string]error {"1" : fmt.Errorf("un Error %s","ww"), "443" : fmt.Errorf("otro Error %s","des"),"UnoNulo":nil, "null":nil} + + */ b := pkj.NewBuilder() - r := b.Pack(S{ A : "SOOOOOO"}) + //r := b.Pack(S{ A : "SOOOOOO"}) + r := b.Pack( + map[interface{}]interface{}{ + //1: "uno", + "dos": 2, + + 3.0: true, + + true: 4.5, + "cinco": "seis", + + 7: 8, + "nueve": false, + 10.11: "doce", + false: 13, + "catorce": 15.16, + "dfate": time.Now(), + time.Now(): "ff", + + + "SSSS" : S{A:"a"}, + + + }, + ) if !r.Ok() { - fmt.Printf("ERROR %s\n",r.Fail()) + fmt.Printf("ERROR %s\n",errs.Format(r.Fail(),nil)) return } fmt.Printf("Data\n\n%s\n",string(r.Value())) diff --git a/base/apack/pkj/builder.go b/base/apack/pkj/builder.go index 1153e23..7a82864 100644 --- a/base/apack/pkj/builder.go +++ b/base/apack/pkj/builder.go @@ -249,7 +249,11 @@ func (b *Builder) String (value string) { b.postValue() } -func (b *Builder) Bool (value bool) {} +func (b *Builder) Bool (value bool) { + if !b.preValue() { return } + cjson.WriteBool(b.B,value) + b.postValue() +} func (b *Builder) Int (value int ){ b.Int64(int64(value)) } func (b *Builder) Int8 (value int8 ){ b.Int64(int64(value)) } @@ -409,7 +413,12 @@ func (b *Builder) Key (key any) blds.ValueBuilder { b.SetError(apack.G.ErrInvalidKey.WithMessage("key is null")) return b } - return b.Field(g.stringer.getStringer(key)) + rs := g.stringer.getStringer(key) + if !rs.Ok() { + b.SetError(apack.G.ErrInvalidKey.WithCause(rs.Fail())) + return b + } + return b.Field(rs.Value()) } func (b *Builder) Field (name string) blds.ValueBuilder { diff --git a/base/apack/pkj/packers.go b/base/apack/pkj/packers.go index df8c6fd..14ce098 100644 --- a/base/apack/pkj/packers.go +++ b/base/apack/pkj/packers.go @@ -312,7 +312,7 @@ func (p *rpackers) buildRPacker (rtype reflect.Type) rPackFnc { } case reflect.Map : - kfn := g.stringer.getStringer(rtype.Key()) + kfn := g.stringer.getRStringer(rtype.Key()) if kfn == nil { return func(b *Builder, _ reflect.Value) { b.SetError(apack.G.ErrInvalidKey) @@ -333,8 +333,13 @@ func (p *rpackers) buildRPacker (rtype reflect.Type) rPackFnc { mi := v.MapRange() ls := make([]tups.GTuple[string,reflect.Value],v.Len()) for i:=0; mi.Next(); i++ { + rs := kfn(mi.Key()) + if !rs.Ok() { + b.SetError(apack.G.ErrInvalidKey.WithCause(rs.Fail())) + return + } ls[i]=tups.GTuple[string,reflect.Value]{ - A : kfn(mi.Key()), + A : rs.Value(), B : mi.Value(), } } @@ -358,7 +363,7 @@ func (p *rpackers) buildRPacker (rtype reflect.Type) rPackFnc { case reflect.Struct : - return nil + return (*newRObject(rtype)).pack default: diff --git a/base/apack/pkj/rfield.go b/base/apack/pkj/rfield.go new file mode 100644 index 0000000..c637cc1 --- /dev/null +++ b/base/apack/pkj/rfield.go @@ -0,0 +1,60 @@ +// ------------------------------------------------------------------------ +// 🚀 Project active3 +// 🔗 Active Thing (activething.com) git.activething.com/go +// +// File name rfield.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 pkj + + +import ( + "apack" + "reflect" +) + +type ( + + rField struct { + name string + nameUns string + nameHtm string + nameBin []byte + + isTagged bool + index []int + rtype reflect.Type + flags apack.Flags + + foldFn apack.FoldFnc + packFn func(b *Builder, v reflect.Value) + } +) + + +func (f rField) typeByIndex (rtype reflect.Type) reflect.Type { + for _,i:= range f.index { + if rtype.Kind() == reflect.Pointer { + rtype = rtype.Elem() + } + rtype = rtype.Field(i).Type + } + return rtype +} diff --git a/base/apack/pkj/rfields.go b/base/apack/pkj/rfields.go new file mode 100644 index 0000000..a56e2d0 --- /dev/null +++ b/base/apack/pkj/rfields.go @@ -0,0 +1,60 @@ +// ------------------------------------------------------------------------ +// 🚀 Project active3 +// 🔗 Active Thing (activething.com) git.activething.com/go +// +// File name rfields.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 pkj + + + +type rFields []rField + + + + +func (l rFields) dominant () (rField,bool) { + if len(l) > 1 && len(l[0].index) == len(l[1].index) && l[0].isTagged == l[1].isTagged { + return rField{},false + } + return l[0],true +} + + + +func (l rFields) Len() int { return len(l) } + +func (l rFields) Swap(i, j int) { l[i], l[j] = l[j], l[i] } + +func (l rFields) Less(i, j int) bool { + for k, xik := range l[i].index { + if k >= len(l[j].index) { + return false + } + if xik != l[j].index[k] { + return xik < l[j].index[k] + } + } + return len(l[i].index) < len(l[j].index) +} + + + diff --git a/base/apack/pkj/robject.go b/base/apack/pkj/robject.go new file mode 100644 index 0000000..14cd87c --- /dev/null +++ b/base/apack/pkj/robject.go @@ -0,0 +1,275 @@ +// ------------------------------------------------------------------------ +// 🚀 Project active3 +// 🔗 Active Thing (activething.com) git.activething.com/go +// +// File name rstruct.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 pkj + +import ( + "apack" + "bfrs" + "reflect" + "sort" +) + +type ( + + rObject struct { + fields rFields + idxs map[string]int + } +) + + + + +func newRObject (rtype reflect.Type) *rObject { + var ( + cur = rFields{} + nxt = rFields{{ rtype : rtype }} + vis = map[reflect.Type]bool{} + nms = bfrs.NewABuffer() + fls rFields + cnt,nxCnt map[reflect.Type]int + ) + + for len(nxt) > 0 { + cur,nxt = nxt, cur[:0] + cnt,nxCnt = nxCnt,map[reflect.Type]int{} + + for _,f := range cur { + if vis[f.rtype] { + continue + } + vis[f.rtype]=true + + for i:=0; i 1 { + fls = append(fls,fls[len(fls)-1]) + } + continue + } + + nxCnt[ft]++ + if nxCnt[ft] == 1 { + nxt = append(nxt,rField{ name: ft.Name(), index: index, rtype: ft}) + } + + } + + + } + + + + } + + sort.Slice(fls,func(i,j int) bool { + ls := fls + if ls[i].name != ls[j].name { + return ls[i].name < ls[j].name + } + if len(ls[i].index) != len(ls[j].index) { + return len(ls[i].index) < len(ls[j].index) + } + if ls[i].isTagged != ls[j].isTagged { + return ls[i].isTagged + } + return ls.Less(i, j) + }) + + out := fls[:0] + for ad,i :=0,0; i< len(fls); i += ad { + fi := fls[i] + nm := fi.name + for ad=1; i+ad Result, G.Result Implementation -func MakeStringError(cause error) IntError { - return IntError{cause} } +func MakeStringError(cause error) StringError { + return StringError{cause} } func (e StringError) Ok() bool { return false }