You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.2 KiB

// ------------------------------------------------------------------------
// 🚀 Project active3
// 🔗 Active Thing (activething.com) git.activething.com/go
//
// File name reflected_map.go
// ✍️ Created by DEV
// 📅 Modified 16/05/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 pkjson
import (
"apack"
"apack/pstate"
"cdcs/cjson"
"reflect"
"sort"
)
type (
rMap struct {
keyFn rMapEntryFnc
packFn func(builder *Builder, value reflect.Value)
}
)
func newRMap (rtype reflect.Type) *rMap {
return &rMap{
packFn: packReflected(rtype.Elem()),
keyFn : g.keyStringer.getOrBuildStringer(rtype.Key()),
}
}
func (m *rMap) pack (builder *Builder, value reflect.Value) {
if m.keyFn == nil {
builder.SetError(apack.G.ErrInvalidKey)
return
}
if value.IsNil() {
builder.B.W(cjson.G.NullValue)
return
}
if builder.Level > maxCyclesAfter {
if fn,ok := builder.CheckCycle(value.UnsafePointer()); ok {
defer fn()
} else {
builder.SetError(apack.G.ErrRedundantPack)
return
}
}
mi := value.MapRange()
ls := make(rMapEntries,value.Len())
for i:=0; mi.Next(); i++ {
ls[i]=rMapEntry{
v: mi.Value(),
}
if er := m.keyFn(&ls[i],mi.Key()); er != nil {
builder.SetError(er)
return
}
}
sort.Sort(ls)
builder.makeLevel(pstate.ModeMap,false,len(ls)==0,func (b *Builder){
for i:=0; i<len(ls); i++ {
b.writeSepIf(i>0)
b.packString(ls[i].k,0)
//b.B.WriteString(ls[i].k)
b.B.WriteByte(':')
b.Current.IsValue = true
m.packFn(b,ls[i].v)
if b.Err != nil {
return
}
}
})
}

Powered by TurnKey Linux.