|
|
|
|
@ -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) {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|