restructurando el codigo para actualizar los packs con la nueva arquitectura del builder

master
DEV 2 years ago
parent 962bf90885
commit 05afeda60e

@ -405,14 +405,11 @@ func (b *Builder) Item () blds.ValueBuilder {
}
func (b *Builder) Key (key any) blds.ValueBuilder {
if b.preElem() {
if key == nil {
b.SetError(apack.G.ErrInvalidKey.WithMessage("key is null"))
return b
}
g.stringer.packKey(b,key)
if key == nil {
b.SetError(apack.G.ErrInvalidKey.WithMessage("key is null"))
return b
}
return b.postElem()
return b.Field(g.stringer.getStringer(key))
}
func (b *Builder) Field (name string) blds.ValueBuilder {

@ -26,7 +26,9 @@ package pkj
import (
"reflect"
"strconv"
"sync"
"time"
)
type (
@ -34,18 +36,36 @@ type (
stringers struct {
mux sync.RWMutex
fncs map[reflect.Type]stringerFnc
ifcs []func(t reflect.Type) stringerFnc
}
stringersFnc func (s *stringers)
stringerFnc func (rvalue reflect.Value) string
stringerFnc func (b *Builder, v reflect.Value) string
)
func newStringers (options ...stringersFnc) *stringers {
fncs := map[reflect.Type]stringerFnc {
reflect.TypeOf("") : func(b *Builder, v reflect.Value) string {
return v.String() },
reflect.TypeOf(int (0)) : func(b *Builder, v reflect.Value) string { return strconv.FormatInt (v.Int (),10) },
reflect.TypeOf(int8 (0)) : func(b *Builder, v reflect.Value) string { return strconv.FormatInt (v.Int (),10) },
reflect.TypeOf(int16 (0)) : func(b *Builder, v reflect.Value) string { return strconv.FormatInt (v.Int (),10) },
reflect.TypeOf(int32 (0)) : func(b *Builder, v reflect.Value) string { return strconv.FormatInt (v.Int (),10) },
reflect.TypeOf(int64 (0)) : func(b *Builder, v reflect.Value) string { return strconv.FormatInt (v.Int (),10) },
reflect.TypeOf(uint (0)) : func(b *Builder, v reflect.Value) string { return strconv.FormatUint(v.Uint(),10) } ,
reflect.TypeOf(uint8 (0)) : func(b *Builder, v reflect.Value) string { return strconv.FormatUint(v.Uint(),10) },
reflect.TypeOf(uint16 (0)) : func(b *Builder, v reflect.Value) string { return strconv.FormatUint(v.Uint(),10) },
reflect.TypeOf(uint32 (0)) : func(b *Builder, v reflect.Value) string { return strconv.FormatUint(v.Uint(),10) },
reflect.TypeOf(uint64 (0)) : func(b *Builder, v reflect.Value) string { return strconv.FormatUint(v.Uint(),10) },
reflect.TypeOf(uintptr(0)) : func(b *Builder, v reflect.Value) string { return strconv.FormatUint(v.Uint(),10) },
reflect.TypeOf(time.Duration(0)): func(b *Builder, v reflect.Value) string {
return strconv.FormatInt (int64(v.Interface().(time.Duration)),10) },
}
s := &stringers{ fncs : fncs }
@ -56,12 +76,38 @@ func newStringers (options ...stringersFnc) *stringers {
}
func (s *stringers) getStringer (b *Builder, key any) string {
switch k := key.(type) {
case string : return k
default:
rv := reflect.ValueOf(key)
kf := s.getRStringer(rv.Type())
if kf != nil {
return kf(b,rv)
}
}
}
func (s *stringers) getRStringer (rtype reflect.Type) stringerFnc {
s.mux.RLock()
fn,ok := s.fncs[rtype]
if ok {
s.mux.RUnlock()
return fn
}
for i:=0; i<len(s.ifcs); i++ {
if fn = s.ifcs[i](rtype); fn != nil {
break
}
}
s.mux.RUnlock()
func (s *stringers) getStringer (rtype reflect.Type) stringerFnc {
return nil
return fn
}
func (s *stringers) packKey (b *Builder, value any) {}

Loading…
Cancel
Save

Powered by TurnKey Linux.