parent
1637297250
commit
b3e2a05590
@ -1,36 +0,0 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project active3
|
||||
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name reflect.go
|
||||
// ✍️ Created by DEV
|
||||
// 📅 Modified 11/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 "reflect"
|
||||
|
||||
|
||||
func stringifyRKey (rtype reflect.Type) {
|
||||
|
||||
}
|
||||
|
||||
func buildKeyStringer (rtype reflect.Type) {
|
||||
|
||||
}
|
||||
@ -0,0 +1,109 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 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++ {
|
||||
if i > 0 {
|
||||
b.B.WriteByte(',')
|
||||
}
|
||||
b.B.WriteString(ls[i].k)
|
||||
b.B.WriteByte(':')
|
||||
m.packFn(b,ls[i].v)
|
||||
if b.Err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in new issue