redefiniendo el builder

packs2
DEV 3 years ago
parent 5910a75a50
commit 20c9b63cea

@ -2,7 +2,9 @@
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/_pjson" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package pjson
package _pjson
import (
"encoding"

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package pjson
package _pjson
const (
jsonCharAttUnsafe jsonCharAtt = 1 << iota

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package pjson
package _pjson

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package pjson
package _pjson
import (
"activething.com/go/gogo/packs"

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package pjson
package _pjson
import (
"activething.com/go/gogo/packs"

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package pjson
package _pjson
import (
"activething.com/go/gogo/packs"

@ -21,16 +21,16 @@
// limitations under the License.
// ------------------------------------------------------------------------
package pjson
package _pjson
import "time"
type (
Options struct {
Flags /*embedded*/
encoder JSONEncoder
dateLayout string
Flags /*embedded*/
encoder JSONEncoder
dateLayout string
}
)

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package pjson
package _pjson
const (

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package pjson
package _pjson
import (
"activething.com/go/gogo/core/bins"
@ -45,7 +45,7 @@ type (
Packer struct {
options Options
state *packs.State
state *packs.State
}
@ -54,7 +54,7 @@ type (
func NewPacker (options ...opts.OptionFnc[Packer]) *Packer {
p := opts.Apply(&Packer{
options: Options{
Flags: JSONFlagQuote | JSONFlagEscapeHTML | JSONByteSliceAsBinary,
Flags: JSONFlagQuote | JSONFlagEscapeHTML | JSONByteSliceAsBinary,
dateLayout: time.RFC822Z,
},
state : packs.NewState(),

@ -21,7 +21,7 @@
// limitations under the License.
// ------------------------------------------------------------------------
package pjson
package _pjson
import (
"activething.com/go/gogo/packs"
@ -104,7 +104,7 @@ func reflectTypePacker(packer *Packer, value reflect.Value, flags Flags) {
func reflectTypePacker(value reflect.Value) PackerTypeFn {
func reflectTypePacker(value reflect.Value) PackerTypeFn {
tp := value.Type()
switch tp.Kind() {

@ -27,7 +27,6 @@ import (
"activething.com/go/gogo/packs"
"activething.com/go/gogo/packs/js"
"fmt"
"reflect"
)
func main() {
@ -37,12 +36,10 @@ func main() {
cas := []string {"a1111","b2222","c3333"}
js.TypePacker(reflect.TypeOf(cas))(st,reflect.ValueOf(cas),0)
js.RPacker(st,cas,0)
fmt.Printf("JSON \n%s\n",st.Bytes())
st.Reset()
js.RPack(st,cas)
fmt.Printf("JSON \n%s\n",st.Bytes())
}

@ -23,92 +23,8 @@
package main
import (
"activething.com/go/gogo/core/errs"
"activething.com/go/gogo/packs/pjson"
"fmt"
"time"
)
func packs_main () {
pk := pjson.NewPacker()
/*
slc2 := [][]int16 {{1,2,3,4},{16,26,36,46}}
//slc2:= [][]int16 {{}}
slc2 = append(slc2,slc2 ...)
pk.PackMap(func(mp packs.MapPacker) {
mp.KeyValue("fb0",slc2)
mp.KeyValueList("l01",func(l packs.ListPacker){
l.ItemList(func(l1 packs.ListPacker){
l1.ItemUInt64(11)
l1.ItemUInt64(22)
})
l.ItemList(func(l1 packs.ListPacker){
l1.ItemUInt64(33)
l1.ItemUInt64(44)
})
})
mp.KeyValueBool("boolean",true)
mp.KeyValueFloat32("bf",12223234.34234)
mp.KeyValueBinary("binary",[]byte{22,121,21,23,23,54,67})
mp.KeyValueDate("date",time.Now())
mp.KeyValueInt16("una",34)
mp.KeyValueObject("MiObjeto",func(op packs.ObjectPacker){
op.FieldInt32("fb1",43433)
op.FieldString ("fb2","aaaaa")
op.Field("fb3",slc)
})
})
*/
/*
var s = struct {
F1Int int
F2Uint uint
F3String string
F4Binary []byte
}{
F1Int :-111,
F2Uint: 333,
F3String: "Esto es un string <quoted & scaped >",
F4Binary: []byte("dklsdjflsdjflsdkfjlsdkfjlsdkfjlsdfjdlsfjsdlf"),
}
pk.Pack(s)
errs.Test(pk.Error(),true)
fmt.Printf("JSON \n%s",pk.Bytes())
*/
as := 44
mp := map[int]any {
3:"23434434",
1:time.Now(),
4:233,
9:as,
7:[]uint16{2323,2323,2323},
}
pk.Pack(mp)
errs.Test(pk.Error(),true)
fmt.Printf("JSON \n%s",pk.Bytes())
}

@ -27,7 +27,6 @@ import (
"activething.com/go/gogo/packs"
"activething.com/go/gogo/packs/js"
j "encoding/json"
"reflect"
"testing"
)
@ -131,21 +130,12 @@ func BenchmarkFlagsParams (b *testing.B) {
//var cas = []int32 {1111,2222,3333}
var cas = []float64 {1111,2222,3333}
func BenchmarkListPack (b *testing.B) {
s := packs.NewState()
//t := reflect.TypeOf(cas)
//v := reflect.ValueOf(cas)
for i:=0; i< b.N; i++ {
v := reflect.ValueOf(cas)
js.TypePacker(v.Type())(s,v,0)
s.Reset()
}
}
func BenchmarkListMapPack (b *testing.B) {
func BenchmarkListPack (b *testing.B) {
s := packs.NewState()
for i:=0; i< b.N; i++ {
js.RPack(s,cas)
js.RPacker(s,cas,0)
s.Reset()
}
}

@ -0,0 +1,45 @@
// ------------------------------------------------------------------------
// Project atila
// Active Thing (activething.com) git.activething.com/go
//
// File name cache.go
// Created by DEV
// Modified 30/01/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
// 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 js
import (
"reflect"
"sync"
)
type (
cachePackers struct {
sync.RWMutex
packers map[reflect.Type]packerFn
}
)
func NewCachePackers () *cachePackers {
return &cachePackers{
packers: make(map[reflect.Type]packerFn),
}
}

@ -49,7 +49,7 @@ var (
floatInfNeg []byte
*/
packers *cachePackers
}{
@ -70,6 +70,7 @@ var (
floatInfNeg: []byte{'"', '-', 'I', 'n', 'f', '"'},
*/
packers: NewCachePackers(),
}
)

@ -39,25 +39,35 @@ type (
)
var cache = make(map[reflect.Type]packerFn)
func RPack (state *packs.State, value any) {
func RPacker(state *packs.State, value any, flags Flags ) {
v := reflect.ValueOf(value)
t := v.Type()
if f,h := cache[t]; h {
f(state,v,0)
return
}
f := TypePacker(t)
cache[t]=f
f(state,v,0)
typePacker(t)(state,v,flags)
}
func typePacker (rtype reflect.Type) packerFn {
G.packers.RLock()
if f,h := G.packers.packers[rtype]; h {
G.packers.RUnlock()
return f
}
G.packers.RUnlock()
f := buildTypePacker(rtype)
func TypePacker(t reflect.Type) packerFn {
G.packers.Lock()
G.packers.packers[rtype]=f
G.packers.Unlock()
return f
}
func buildTypePacker(t reflect.Type) packerFn {
switch t.Kind() {
case reflect.Bool : return func(s *packs.State, v reflect.Value, f Flags) { packBool (s,v.Bool (),f )}
@ -76,56 +86,16 @@ func TypePacker(t reflect.Type) packerFn {
case reflect.Float64: return func(s *packs.State, v reflect.Value, f Flags) { packFloatString(s,v.Float(),64, f.Quote()) }
case reflect.Slice :
return listPacker(TypePacker(t.Elem()))
return (typeSlicePacker{ packer: typePacker(t.Elem()) }).pack
}
return func (s *packs.State, v reflect.Value, _ Flags) { unsupportedPacker(s,v,0)}
return func (s *packs.State, v reflect.Value, _ Flags) { unsupportedTypePacker(s,v,0)}
}
func typeListPacker (t reflect.Type) packerFn {
switch t.Kind() {
case reflect.Bool : return listPacker(func(s *packs.State, v reflect.Value, f Flags) { packBool (s,v.Bool (),f )})
case reflect.String : return listPacker(func(s *packs.State, v reflect.Value, f Flags) { packString (s,v.Interface().(string),f )})
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64 : return listPacker(func(s *packs.State, v reflect.Value, f Flags) { packInt (s,v.Int (),f )})
case reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64 : return listPacker(func(s *packs.State, v reflect.Value, f Flags) { packUInt (s,v.Uint (),f )})
case reflect.Float32: return listPacker(func(s *packs.State, v reflect.Value, f Flags) { packFloatString(s,v.Float(),32, f.Quote()) })
case reflect.Float64: return listPacker(func(s *packs.State, v reflect.Value, f Flags) { packFloatString(s,v.Float(),64, f.Quote()) })
default:
return listPacker(func(s *packs.State, v reflect.Value, f Flags) { TypePacker(v.Type())(s, v ,f) })
}
}
func listPacker(elementPacker packerFn) packerFn {
return func(s *packs.State, v reflect.Value, f Flags) {
fn := elementPacker
s.Open('[')
for i:=0; i<v.Len(); i++ {
if i > 0 {
_ = s.WriteByte(',')
}
fn(s,v.Index(i),f)
if s.Error != nil {
return
}
}
s.Close(']')
}
}
func unsupportedPacker(state *packs.State, _ reflect.Value, _ Flags) {
func unsupportedTypePacker(state *packs.State, _ reflect.Value, _ Flags) {
state.SetError(packs.G.ErrUnsupportedType)
}

@ -0,0 +1,59 @@
// ------------------------------------------------------------------------
// Project atila
// Active Thing (activething.com) git.activething.com/go
//
// File name type_slice_packer.go
// Created by DEV
// Modified 30/01/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
// 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 js
import (
"activething.com/go/gogo/packs"
"reflect"
)
type (
typeSlicePacker struct {
packer packerFn
}
)
func (p typeSlicePacker) pack (s *packs.State, v reflect.Value, f Flags) {
if v.IsNil() {
_,_ = s.Write(G.nullValue)
return
}
if v.Len() == 0 {
s.Write2Byte('[',']')
return
}
s.Open('[')
for i:=0; i<v.Len(); i++ {
if i > 0 {
_ = s.WriteByte(',')
}
p.packer(s,v.Index(i),f)
if s.Error != nil {
return
}
}
s.Close(']')
}
Loading…
Cancel
Save

Powered by TurnKey Linux.