|
|
|
|
@ -27,6 +27,7 @@ import (
|
|
|
|
|
"activething.com/go/gogo/packs"
|
|
|
|
|
"encoding"
|
|
|
|
|
"reflect"
|
|
|
|
|
"sort"
|
|
|
|
|
"strconv"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@ -38,17 +39,35 @@ type (
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StringerFn func(entry *typeMapEntry, value reflect.Value) error
|
|
|
|
|
StringerFn func(entry *TypeMapEntry, value reflect.Value) error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typeMapEntry struct {
|
|
|
|
|
val reflect.Value
|
|
|
|
|
key string
|
|
|
|
|
TypeMapEntry struct {
|
|
|
|
|
Val reflect.Value
|
|
|
|
|
Key string
|
|
|
|
|
flags Flags
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Entries []TypeMapEntry
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (e Entries) Len() int {
|
|
|
|
|
return len(e)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Less compares two entries based on their keys
|
|
|
|
|
func (e Entries) Less(i, j int) bool {
|
|
|
|
|
return e[i].Key < e[j].Key
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Swap swaps two entries in the slice
|
|
|
|
|
func (e Entries) Swap(i, j int) {
|
|
|
|
|
e[i], e[j] = e[j], e[i]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func newTypeMapPacker (rtype reflect.Type) *typeMapPacker {
|
|
|
|
|
return &typeMapPacker{
|
|
|
|
|
valPacker : typePacker (rtype.Elem()),
|
|
|
|
|
@ -64,7 +83,7 @@ func (m typeMapPacker) pack (s *packs.State, v reflect.Value, _ Flags) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mi := v.MapRange()
|
|
|
|
|
ls := make([]typeMapEntry,v.Len())
|
|
|
|
|
ls := make(Entries,v.Len())
|
|
|
|
|
|
|
|
|
|
if s.Level > startDetectingCyclesAfter {
|
|
|
|
|
if fn,ok := s.CheckCycle(v.UnsafePointer()); ok {
|
|
|
|
|
@ -76,8 +95,8 @@ func (m typeMapPacker) pack (s *packs.State, v reflect.Value, _ Flags) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i:=0; mi.Next(); i++ {
|
|
|
|
|
ls[i] = typeMapEntry{
|
|
|
|
|
val: mi.Value(),
|
|
|
|
|
ls[i] = TypeMapEntry{
|
|
|
|
|
Val: mi.Value(),
|
|
|
|
|
}
|
|
|
|
|
if er := m.keyStringer(&ls[i], mi.Key()); er != nil {
|
|
|
|
|
s.SetError(er)
|
|
|
|
|
@ -85,19 +104,20 @@ func (m typeMapPacker) pack (s *packs.State, v reflect.Value, _ Flags) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//sort.Slice(ls, func(i, j int) bool { return ls[i].key < ls[j].key })
|
|
|
|
|
sort.Sort(ls)
|
|
|
|
|
//sort.Slice(ls, func(i, j int) bool { return ls[i].Key < ls[j].Key })
|
|
|
|
|
|
|
|
|
|
s.Open ('{')
|
|
|
|
|
for i:=0; i<len(ls); i++ {
|
|
|
|
|
if i>0 {
|
|
|
|
|
s.WriteByte(',')
|
|
|
|
|
}
|
|
|
|
|
packString(s,ls[i].key,ls[i].flags)
|
|
|
|
|
packString(s,ls[i].Key,ls[i].flags)
|
|
|
|
|
if s.Error != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
s.WriteByte(':')
|
|
|
|
|
m.valPacker(s,ls[i].val,0)
|
|
|
|
|
m.valPacker(s,ls[i].Val,0)
|
|
|
|
|
if s.Error != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
@ -108,23 +128,23 @@ func (m typeMapPacker) pack (s *packs.State, v reflect.Value, _ Flags) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func buildKeyStringer(rt reflect.Type) func(me *typeMapEntry, key reflect.Value) error {
|
|
|
|
|
func buildKeyStringer(rt reflect.Type) func(me *TypeMapEntry, key reflect.Value) error {
|
|
|
|
|
if rt.Kind() == reflect.String {
|
|
|
|
|
return func(e *typeMapEntry, k reflect.Value) error {
|
|
|
|
|
e.key = k.String()
|
|
|
|
|
return func(e *TypeMapEntry, k reflect.Value) error {
|
|
|
|
|
e.Key = k.String()
|
|
|
|
|
e.flags = FlagScapeHTML
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if rt.Implements(reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()) {
|
|
|
|
|
return func(e *typeMapEntry, k reflect.Value) error {
|
|
|
|
|
return func(e *TypeMapEntry, k reflect.Value) error {
|
|
|
|
|
if k.Kind() == reflect.Pointer && k.IsNil() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
bf, er := (k.Interface().(encoding.TextMarshaler)).MarshalText()
|
|
|
|
|
if er == nil {
|
|
|
|
|
e.key = string(bf)
|
|
|
|
|
e.Key = string(bf)
|
|
|
|
|
e.flags = FlagScapeHTML
|
|
|
|
|
}
|
|
|
|
|
return er
|
|
|
|
|
@ -138,8 +158,8 @@ func buildKeyStringer(rt reflect.Type) func(me *typeMapEntry, key reflect.Value)
|
|
|
|
|
reflect.Int16,
|
|
|
|
|
reflect.Int32,
|
|
|
|
|
reflect.Int64:
|
|
|
|
|
return func(e *typeMapEntry, k reflect.Value) error {
|
|
|
|
|
e.key = strconv.FormatInt(k.Int(), 10)
|
|
|
|
|
return func(e *TypeMapEntry, k reflect.Value) error {
|
|
|
|
|
e.Key = strconv.FormatInt(k.Int(), 10)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
case reflect.Uint,
|
|
|
|
|
@ -148,17 +168,17 @@ func buildKeyStringer(rt reflect.Type) func(me *typeMapEntry, key reflect.Value)
|
|
|
|
|
reflect.Uint32,
|
|
|
|
|
reflect.Uint64,
|
|
|
|
|
reflect.Uintptr:
|
|
|
|
|
return func(e *typeMapEntry, k reflect.Value) error {
|
|
|
|
|
e.key = strconv.FormatUint(k.Uint(), 10)
|
|
|
|
|
return func(e *TypeMapEntry, k reflect.Value) error {
|
|
|
|
|
e.Key = strconv.FormatUint(k.Uint(), 10)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case reflect.Interface:
|
|
|
|
|
return func(e *typeMapEntry, k reflect.Value) error {
|
|
|
|
|
return func(e *TypeMapEntry, k reflect.Value) error {
|
|
|
|
|
k = k.Elem()
|
|
|
|
|
|
|
|
|
|
if k.Type().Kind() == reflect.String {
|
|
|
|
|
e.key = k.String()
|
|
|
|
|
e.Key = k.String()
|
|
|
|
|
e.flags = FlagScapeHTML
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
@ -169,7 +189,7 @@ func buildKeyStringer(rt reflect.Type) func(me *typeMapEntry, key reflect.Value)
|
|
|
|
|
}
|
|
|
|
|
bf, er := t.MarshalText()
|
|
|
|
|
if er == nil {
|
|
|
|
|
e.key = string(bf)
|
|
|
|
|
e.Key = string(bf)
|
|
|
|
|
e.flags = FlagScapeHTML
|
|
|
|
|
}
|
|
|
|
|
return er
|
|
|
|
|
@ -182,7 +202,7 @@ func buildKeyStringer(rt reflect.Type) func(me *typeMapEntry, key reflect.Value)
|
|
|
|
|
reflect.Int16,
|
|
|
|
|
reflect.Int32,
|
|
|
|
|
reflect.Int64:
|
|
|
|
|
e.key = strconv.FormatInt(k.Int(), 10)
|
|
|
|
|
e.Key = strconv.FormatInt(k.Int(), 10)
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
|
|
case reflect.Uint,
|
|
|
|
|
@ -191,7 +211,7 @@ func buildKeyStringer(rt reflect.Type) func(me *typeMapEntry, key reflect.Value)
|
|
|
|
|
reflect.Uint32,
|
|
|
|
|
reflect.Uint64,
|
|
|
|
|
reflect.Uintptr:
|
|
|
|
|
e.key = strconv.FormatUint(k.Uint(), 10)
|
|
|
|
|
e.Key = strconv.FormatUint(k.Uint(), 10)
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
@ -200,19 +220,19 @@ func buildKeyStringer(rt reflect.Type) func(me *typeMapEntry, key reflect.Value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return func(e *typeMapEntry, k reflect.Value) error {
|
|
|
|
|
return func(e *TypeMapEntry, k reflect.Value) error {
|
|
|
|
|
return packs.G.ErrInvalidKey
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// caso de map[any]cualquiertypo
|
|
|
|
|
/*
|
|
|
|
|
return func(e *typeMapEntry, k reflect.Value) error {
|
|
|
|
|
return func(e *TypeMapEntry, k reflect.Value) error {
|
|
|
|
|
k = k.Elem()
|
|
|
|
|
|
|
|
|
|
fmt.Printf("Ejecutando packer general\n %v\n",k)
|
|
|
|
|
if k.Type().Kind() == reflect.String {
|
|
|
|
|
e.key = k.String()
|
|
|
|
|
e.Key = k.String()
|
|
|
|
|
e.flags = FlagScapeHTML
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
@ -223,7 +243,7 @@ func buildKeyStringer(rt reflect.Type) func(me *typeMapEntry, key reflect.Value)
|
|
|
|
|
}
|
|
|
|
|
bf, er := t.MarshalText()
|
|
|
|
|
if er == nil {
|
|
|
|
|
e.key = string(bf)
|
|
|
|
|
e.Key = string(bf)
|
|
|
|
|
e.flags = FlagScapeHTML
|
|
|
|
|
}
|
|
|
|
|
return er
|
|
|
|
|
@ -236,7 +256,7 @@ func buildKeyStringer(rt reflect.Type) func(me *typeMapEntry, key reflect.Value)
|
|
|
|
|
reflect.Int16,
|
|
|
|
|
reflect.Int32,
|
|
|
|
|
reflect.Int64:
|
|
|
|
|
e.key = strconv.FormatInt(k.Int(), 10)
|
|
|
|
|
e.Key = strconv.FormatInt(k.Int(), 10)
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
|
|
case reflect.Uint,
|
|
|
|
|
@ -245,7 +265,7 @@ func buildKeyStringer(rt reflect.Type) func(me *typeMapEntry, key reflect.Value)
|
|
|
|
|
reflect.Uint32,
|
|
|
|
|
reflect.Uint64,
|
|
|
|
|
reflect.Uintptr:
|
|
|
|
|
e.key = strconv.FormatUint(k.Uint(), 10)
|
|
|
|
|
e.Key = strconv.FormatUint(k.Uint(), 10)
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|