From a6b80c0dac8010aa28493e3791311983b1b3f413 Mon Sep 17 00:00:00 2001 From: DEV Date: Sun, 21 Jan 2024 01:25:23 +0100 Subject: [PATCH] redefiniendo el builder --- core/chars/ascii_char_atts.go | 10 +- packs/builder_map.go | 42 ++--- packs/builder_object.go | 42 ++--- packs/gogo.go | 5 +- packs/jjson/builder.go | 35 +++++ packs/jjson/state.go | 27 ++++ packs/pjson/BuilderObject.go | 116 -------------- .../pjson/{builderList.go => builder_list.go} | 0 packs/pjson/{BuilderMap.go => builder_map.go} | 42 ++--- packs/pjson/builder_object.go | 143 ++++++++++++++++++ packs/pjson/gogo.go | 2 +- 11 files changed, 277 insertions(+), 187 deletions(-) create mode 100644 packs/jjson/builder.go create mode 100644 packs/jjson/state.go delete mode 100644 packs/pjson/BuilderObject.go rename packs/pjson/{builderList.go => builder_list.go} (100%) rename packs/pjson/{BuilderMap.go => builder_map.go} (50%) create mode 100644 packs/pjson/builder_object.go diff --git a/core/chars/ascii_char_atts.go b/core/chars/ascii_char_atts.go index 6b4896a..5645a7e 100644 --- a/core/chars/ascii_char_atts.go +++ b/core/chars/ascii_char_atts.go @@ -62,16 +62,16 @@ func NewASCIIAtts() *ASCIIAtts { return as } -func (t ASCIIAtts) EqualFold(sliceA, sliceB []byte) bool { - ln := len(sliceA) - if ln != len(sliceB) { +func (t ASCIIAtts) EqualFold(source, target []byte) bool { + ln := len(source) + if ln != len(target) { return false } for ix := 0; ix < ln; ix++ { - if sliceA[ix] == sliceB[ix] { + if source[ix] == target[ix] { continue } - if t.Atts[sliceA[ix]]&ASCIIAttAlpha != 0 && (sliceA[ix]+32 == sliceB[ix] || sliceA[ix]-32 == sliceB[ix]) { + if t.Atts[source[ix]]&ASCIIAttAlpha != 0 && (source[ix]+32 == target[ix] || source[ix]-32 == target[ix]) { continue } return false diff --git a/packs/builder_map.go b/packs/builder_map.go index 2f0461c..e5d0451 100644 --- a/packs/builder_map.go +++ b/packs/builder_map.go @@ -30,35 +30,35 @@ type ( BuilderMap interface { - ElementAny (key any, value any) + ElemAny (key any, value any) - ElementBinary (key any, value []byte) - ElementString (key any, value string) + ElemBinary (key any, value []byte) + ElemString (key any, value string) - ElementBool (key any, value bool) - ElementRune (key any, value rune) + ElemBool (key any, value bool) + ElemRune (key any, value rune) - ElementInt8 (key any, value int8) - ElementInt16 (key any, value int16) - ElementInt32 (key any, value int32) - ElementInt64 (key any, value int64) + ElemInt8 (key any, value int8) + ElemInt16 (key any, value int16) + ElemInt32 (key any, value int32) + ElemInt64 (key any, value int64) - ElementUInt8 (key any, value uint8) - ElementUInt16 (key any, value uint16) - ElementUInt32 (key any, value uint32) - ElementUInt64 (key any, value uint64) + ElemUInt8 (key any, value uint8) + ElemUInt16 (key any, value uint16) + ElemUInt32 (key any, value uint32) + ElemUInt64 (key any, value uint64) - ElementFloat32(key any, value float32) - ElementFloat64(key any, value float64) + ElemFloat32(key any, value float32) + ElemFloat64(key any, value float64) - ElementComplex64 (key any, value complex64) - ElementComplex128(key any, value complex128) + ElemComplex64 (key any, value complex64) + ElemComplex128(key any, value complex128) - ElementDate(key any, value time.Time) + ElemDate(key any, value time.Time) - ElementList (key any, builder func(packer BuilderList)) - ElementMap (key any, builder func(packer BuilderMap)) - ElementObject(key any, builder func(packer BuilderObject)) + ElemList (key any, builder func(packer BuilderList)) + ElemMap (key any, builder func(packer BuilderMap)) + ElemObject(key any, builder func(packer BuilderObject)) } ) diff --git a/packs/builder_object.go b/packs/builder_object.go index 1f159f6..65d4e59 100644 --- a/packs/builder_object.go +++ b/packs/builder_object.go @@ -28,34 +28,34 @@ import "time" type ( BuilderObject interface { - FieldAny (name string, value any) + PropAny (name string, value any) - FieldBinary (name string, value []byte) - FieldString (name string, value string) + PropBinary (name string, value []byte) + PropString (name string, value string) - FieldBool (name string, value bool) - FieldRune (name string, value rune) + PropBool (name string, value bool) + PropRune (name string, value rune) - FieldInt8 (name string, value int8) - FieldInt16 (name string, value int16) - FieldInt32 (name string, value int32) - FieldInt64 (name string, value int64) + PropInt8 (name string, value int8) + PropInt16 (name string, value int16) + PropInt32 (name string, value int32) + PropInt64 (name string, value int64) - FieldUInt8 (name string, value uint8) - FieldUInt16 (name string, value uint16) - FieldUInt32 (name string, value uint32) - FieldUInt64 (name string, value uint64) + PropUInt8 (name string, value uint8) + PropUInt16 (name string, value uint16) + PropUInt32 (name string, value uint32) + PropUInt64 (name string, value uint64) - FieldFloat32(name string, value float32) - FieldFloat64(name string, value float64) + PropFloat32(name string, value float32) + PropFloat64(name string, value float64) - FieldComplex64 (name string, value complex64) - FieldComplex128(name string, value complex128) + PropComplex64 (name string, value complex64) + PropComplex128(name string, value complex128) - FieldDate(name string, value time.Time) + PropDate(name string, value time.Time) - FieldList (name string, builder func(packer BuilderList)) - FieldMap (name string, builder func(packer BuilderMap)) - FieldObject(name string, builder func(packer BuilderObject)) + PropList (name string, builder func(packer BuilderList)) + PropMap (name string, builder func(packer BuilderMap)) + PropObject(name string, builder func(packer BuilderObject)) } ) diff --git a/packs/gogo.go b/packs/gogo.go index a6fc210..53750ba 100644 --- a/packs/gogo.go +++ b/packs/gogo.go @@ -131,10 +131,11 @@ func packListReflected(builder Builder, value reflect.Value) { } func packMapReflected(builder Builder, value reflect.Value) { + value.Type().Elem().Key().Kind() builder.Map(func (p BuilderMap) { r := value for _,k := range r.MapKeys() { - p.ElementAny(k.Interface(),r.MapIndex(k).Interface()) + p.ElemAny(k.Interface(),r.MapIndex(k).Interface()) if builder.Error() != nil { return } @@ -148,7 +149,7 @@ func packObjectReflected(builder Builder, value reflect.Value) { t := r.Type() for i:=0; i < r.NumField(); i++ { if t.Field(i).IsExported() { - p.FieldAny(t.Field(i).Name,r.Field(i).Interface()) + p.PropAny(t.Field(i).Name,r.Field(i).Interface()) if builder.Error() != nil { return } diff --git a/packs/jjson/builder.go b/packs/jjson/builder.go new file mode 100644 index 0000000..7fe66e3 --- /dev/null +++ b/packs/jjson/builder.go @@ -0,0 +1,35 @@ +// ------------------------------------------------------------------------ +// Project atila +// Active Thing (activething.com) git.activething.com/go +// +// File name builder.go +// Created by DEV +// Modified 21/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 jjson + + + +type ( + + + + +) + + diff --git a/packs/jjson/state.go b/packs/jjson/state.go new file mode 100644 index 0000000..570e6ad --- /dev/null +++ b/packs/jjson/state.go @@ -0,0 +1,27 @@ +// ------------------------------------------------------------------------ +// Project atila +// Active Thing (activething.com) git.activething.com/go +// +// File name state.go +// Created by DEV +// Modified 21/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 jjson + + + diff --git a/packs/pjson/BuilderObject.go b/packs/pjson/BuilderObject.go deleted file mode 100644 index 9d33dab..0000000 --- a/packs/pjson/BuilderObject.go +++ /dev/null @@ -1,116 +0,0 @@ -// ------------------------------------------------------------------------ -// Project atila -// Active Thing (activething.com) git.activething.com/go -// -// File name BuilderObject.go -// Created by DEV -// Modified 18/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 pjson - -import ( - "activething.com/go/gogo/packs" - "time" -) - -func (p *Builder) FieldAny (name string, value any) { - if p.addName(name) { - p.Any(value) - } -} - -func (p *Builder) FieldBinary (name string, value []byte){ - if p.addName(name) { - p.Binary(value) - } -} -func (p *Builder) FieldString (name string, value string){ - if p.addName(name) { - p.String(value) - } -} - -func (p *Builder) FieldBool (name string, value bool ){ - if p.addName(name) { - p.Bool(value) - } -} - -func (p *Builder) FieldRune (name string, value rune ){ - if p.addName(name) { - p.Rune(value) - } -} - -func (p *Builder) FieldInt8 (name string, value int8 ){ p.FieldInt64(name, int64(value)) } -func (p *Builder) FieldInt16 (name string, value int16 ){ p.FieldInt64(name, int64(value)) } -func (p *Builder) FieldInt32 (name string, value int32 ){ p.FieldInt64(name, int64(value)) } -func (p *Builder) FieldInt64 (name string, value int64 ){ - if p.addName(name) { - p.Int64(value) - } -} - -func (p *Builder) FieldUInt8 (name string, value uint8 ){ p.FieldUInt64(name, uint64(value)) } -func (p *Builder) FieldUInt16 (name string, value uint16){ p.FieldUInt64(name, uint64(value)) } -func (p *Builder) FieldUInt32 (name string, value uint32){ p.FieldUInt64(name, uint64(value)) } -func (p *Builder) FieldUInt64 (name string, value uint64){ - if p.addName(name) { - p.UInt64(value) - } -} - -func (p *Builder) FieldFloat32(name string, value float32){ p.FieldFloat64(name, float64(value)) } -func (p *Builder) FieldFloat64(name string, value float64){ - if p.addName(name) { - p.Float64(value) - } -} - -func (p *Builder) FieldComplex64 (name string, value complex64 ){ p.FieldComplex128(name, complex128(value)) } -func (p *Builder) FieldComplex128(name string, value complex128){ - if p.addName(name) { - p.Complex128(value) - } -} - -func (p *Builder) FieldDate (name string, value time.Time) { - if p.addName(name) { - p.Date(value) - } -} - -func (p *Builder) FieldList (name string, builder func(packer packs.BuilderList)) { - if p.addName(name) { - p.List(builder) - } -} - -func (p *Builder) FieldMap (name string, builder func(packer packs.BuilderMap)) { - if p.addName(name) { - p.Map(builder) - } -} - -func (p *Builder) FieldObject(name string, builder func(packer packs.BuilderObject)) { - if p.addName(name) { - p.Object(builder) - } -} - - diff --git a/packs/pjson/builderList.go b/packs/pjson/builder_list.go similarity index 100% rename from packs/pjson/builderList.go rename to packs/pjson/builder_list.go diff --git a/packs/pjson/BuilderMap.go b/packs/pjson/builder_map.go similarity index 50% rename from packs/pjson/BuilderMap.go rename to packs/pjson/builder_map.go index 4a2bdb8..9b8c86a 100644 --- a/packs/pjson/BuilderMap.go +++ b/packs/pjson/builder_map.go @@ -28,86 +28,86 @@ import ( "time" ) -func (p *Builder) ElementAny (key any, value any) { +func (p *Builder) ElemAny (key any, value any) { if p.addKey(key) { p.Any(value) } } -func (p *Builder) ElementBinary (key any, value []byte){ +func (p *Builder) ElemBinary (key any, value []byte){ if p.addKey(key) { p.Binary(value) } } -func (p *Builder) ElementString (key any, value string){ +func (p *Builder) ElemString (key any, value string){ if p.addKey(key) { p.String(value) } } -func (p *Builder) ElementBool (key any, value bool ){ +func (p *Builder) ElemBool (key any, value bool ){ if p.addKey(key) { p.Bool(value) } } -func (p *Builder) ElementRune (key any, value rune ){ +func (p *Builder) ElemRune (key any, value rune ){ if p.addKey(key) { p.Rune(value) } } -func (p *Builder) ElementInt8 (key any, value int8 ){ p.ElementInt64(key, int64(value)) } -func (p *Builder) ElementInt16 (key any, value int16 ){ p.ElementInt64(key, int64(value)) } -func (p *Builder) ElementInt32 (key any, value int32 ){ p.ElementInt64(key, int64(value)) } -func (p *Builder) ElementInt64 (key any, value int64 ){ +func (p *Builder) ElemInt8 (key any, value int8 ){ p.ElemInt64(key, int64(value)) } +func (p *Builder) ElemInt16 (key any, value int16 ){ p.ElemInt64(key, int64(value)) } +func (p *Builder) ElemInt32 (key any, value int32 ){ p.ElemInt64(key, int64(value)) } +func (p *Builder) ElemInt64 (key any, value int64 ){ if p.addKey(key) { p.Int64(value) } } -func (p *Builder) ElementUInt8 (key any, value uint8 ){ p.ElementUInt64(key, uint64(value)) } -func (p *Builder) ElementUInt16 (key any, value uint16){ p.ElementUInt64(key, uint64(value)) } -func (p *Builder) ElementUInt32 (key any, value uint32){ p.ElementUInt64(key, uint64(value)) } -func (p *Builder) ElementUInt64 (key any, value uint64){ +func (p *Builder) ElemUInt8 (key any, value uint8 ){ p.ElemUInt64(key, uint64(value)) } +func (p *Builder) ElemUInt16 (key any, value uint16){ p.ElemUInt64(key, uint64(value)) } +func (p *Builder) ElemUInt32 (key any, value uint32){ p.ElemUInt64(key, uint64(value)) } +func (p *Builder) ElemUInt64 (key any, value uint64){ if p.addKey(key) { p.UInt64(value) } } -func (p *Builder) ElementFloat32(key any, value float32){ p.ElementFloat64(key, float64(value)) } -func (p *Builder) ElementFloat64(key any, value float64){ +func (p *Builder) ElemFloat32(key any, value float32){ p.ElemFloat64(key, float64(value)) } +func (p *Builder) ElemFloat64(key any, value float64){ if p.addKey(key) { p.Float64(value) } } -func (p *Builder) ElementComplex64 (key any, value complex64 ){ p.ElementComplex128(key, complex128(value)) } -func (p *Builder) ElementComplex128(key any, value complex128){ +func (p *Builder) ElemComplex64 (key any, value complex64 ){ p.ElemComplex128(key, complex128(value)) } +func (p *Builder) ElemComplex128(key any, value complex128){ if p.addKey(key) { p.Complex128(value) } } -func (p *Builder) ElementDate (key any, value time.Time) { +func (p *Builder) ElemDate (key any, value time.Time) { if p.addKey(key) { p.Date(value) } } -func (p *Builder) ElementList (key any, builder func(packer packs.BuilderList)) { +func (p *Builder) ElemList (key any, builder func(packer packs.BuilderList)) { if p.addKey(key) { p.List(builder) } } -func (p *Builder) ElementMap (key any, builder func(packer packs.BuilderMap)) { +func (p *Builder) ElemMap (key any, builder func(packer packs.BuilderMap)) { if p.addKey(key) { p.Map(builder) } } -func (p *Builder) ElementObject(key any, builder func(packer packs.BuilderObject)) { +func (p *Builder) ElemObject(key any, builder func(packer packs.BuilderObject)) { if p.addKey(key) { p.Object(builder) } diff --git a/packs/pjson/builder_object.go b/packs/pjson/builder_object.go new file mode 100644 index 0000000..385d4b2 --- /dev/null +++ b/packs/pjson/builder_object.go @@ -0,0 +1,143 @@ +// ------------------------------------------------------------------------ +// Project atila +// Active Thing (activething.com) git.activething.com/go +// +// File name BuilderObject.go +// Created by DEV +// Modified 18/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 pjson + +import ( + "activething.com/go/gogo/packs" + "time" +) + +func (p *Builder) PropAny (name string, value any) { + if p.addName(name) { + p.Any(value) + } +} + +func (p *Builder) PropBinary (name string, value []byte){ + if p.options.IgnoreDefaults() && len(value) == 0 { + return + } + if p.addName(name) { + p.Binary(value) + } +} +func (p *Builder) PropString (name string, value string){ + if p.options.IgnoreDefaults() && len(value) == 0 { + return + } + if p.addName(name) { + p.String(value) + } +} + +func (p *Builder) PropBool (name string, value bool ){ + if p.options.IgnoreDefaults() && !value { + return + } + if p.addName(name) { + p.Bool(value) + } +} + +func (p *Builder) PropRune (name string, value rune ){ + if p.options.IgnoreDefaults() && value == 0 { + return + } + if p.addName(name) { + p.Rune(value) + } +} + +func (p *Builder) PropInt8 (name string, value int8 ){ p.PropInt64(name, int64(value)) } +func (p *Builder) PropInt16 (name string, value int16 ){ p.PropInt64(name, int64(value)) } +func (p *Builder) PropInt32 (name string, value int32 ){ p.PropInt64(name, int64(value)) } +func (p *Builder) PropInt64 (name string, value int64 ){ + if p.options.IgnoreDefaults() && value == 0 { + return + } + if p.addName(name) { + p.Int64(value) + } +} + +func (p *Builder) PropUInt8 (name string, value uint8 ){ p.PropUInt64(name, uint64(value)) } +func (p *Builder) PropUInt16 (name string, value uint16){ p.PropUInt64(name, uint64(value)) } +func (p *Builder) PropUInt32 (name string, value uint32){ p.PropUInt64(name, uint64(value)) } +func (p *Builder) PropUInt64 (name string, value uint64){ + if p.options.IgnoreDefaults() && value == 0 { + return + } + if p.addName(name) { + p.UInt64(value) + } +} + +func (p *Builder) PropFloat32(name string, value float32){ p.PropFloat64(name, float64(value)) } +func (p *Builder) PropFloat64(name string, value float64){ + if p.options.IgnoreDefaults() && value == 0 { + return + } + if p.addName(name) { + p.Float64(value) + } +} + +func (p *Builder) PropComplex64 (name string, value complex64 ){ p.PropComplex128(name, complex128(value)) } +func (p *Builder) PropComplex128(name string, value complex128){ + if p.options.IgnoreDefaults() && value == (0+0i) { + return + } + if p.addName(name) { + p.Complex128(value) + } +} + +func (p *Builder) PropDate (name string, value time.Time) { + if p.options.IgnoreDefaults() && value.IsZero() { + return + } + if p.addName(name) { + p.Date(value) + } +} + +func (p *Builder) PropList (name string, builder func(packer packs.BuilderList)) { + if p.addName(name) { + p.List(builder) + } +} + +func (p *Builder) PropMap (name string, builder func(packer packs.BuilderMap)) { + if p.addName(name) { + p.Map(builder) + } +} + +func (p *Builder) PropObject(name string, builder func(packer packs.BuilderObject)) { + if p.addName(name) { + p.Object(builder) + } +} + + diff --git a/packs/pjson/gogo.go b/packs/pjson/gogo.go index 2b71646..a3feba3 100644 --- a/packs/pjson/gogo.go +++ b/packs/pjson/gogo.go @@ -48,7 +48,7 @@ var ( jsonChars: initJSONChars(), sep: ',', - nullValue: []byte{'"', 'n', 'u', 'l', 'l', '"'}, + nullValue: []byte{'n', 'u', 'l', 'l'}, zeroValue: []byte{'0'}, trueValue: []byte{'"','t', 'r', 'u', 'e','"'}, falseValue: []byte{'"','f', 'a', 'l', 's', 'e','"'},