parent
867a3cbd9b
commit
c8c699b3b6
@ -0,0 +1,53 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project active3
|
||||
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name active.go
|
||||
// ✍️ Created by DEV
|
||||
// 📅 Modified 28/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 pkj
|
||||
|
||||
import "time"
|
||||
|
||||
type (
|
||||
|
||||
basicTypes interface {
|
||||
~bool | ~string |
|
||||
~int | ~int8 | ~int16 | ~int32 | ~int64 |
|
||||
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
|
||||
~float32 | ~float64 |
|
||||
time.Time
|
||||
}
|
||||
|
||||
)
|
||||
|
||||
|
||||
var (
|
||||
|
||||
|
||||
g = struct {
|
||||
stringer keyStringer
|
||||
}{
|
||||
|
||||
stringer: keyStringer{},
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@ -0,0 +1,255 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project active3
|
||||
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name builder.go
|
||||
// ✍️ Created by DEV
|
||||
// 📅 Modified 28/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 pkj
|
||||
|
||||
import (
|
||||
"apack"
|
||||
"apack/pstate"
|
||||
"blds"
|
||||
"cdcs/cjson"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
Builder struct {
|
||||
*pstate.State
|
||||
}
|
||||
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
func (b *Builder) String (value string) {}
|
||||
|
||||
func (b *Builder) Bool (value bool) {}
|
||||
|
||||
|
||||
func (b *Builder) writeString (value string, flags apack.Flags) {
|
||||
if len(value) == 0 {
|
||||
b.B.W(cjson.G.EmptyValue)
|
||||
return
|
||||
}
|
||||
if flags.Encrypted() {
|
||||
b.writeEncrypted([]byte(value),flags)
|
||||
return
|
||||
}
|
||||
if flags.Binary() {
|
||||
b.writeBinary([]byte(value),flags)
|
||||
return
|
||||
}
|
||||
cjson.WriteString(b.B,value,flags.EscapeHTML())
|
||||
}
|
||||
|
||||
|
||||
func (b *Builder) writeEncrypted(value []byte, flags apack.Flags) {
|
||||
if b.Encrypter == nil {
|
||||
b.SetError(apack.G.ErrInvalidEncrypter)
|
||||
return
|
||||
}
|
||||
rs := b.Encrypter.Encrypt(value)
|
||||
if !rs.Ok() {
|
||||
b.SetError(rs.Fail())
|
||||
return
|
||||
}
|
||||
b.writeBinary(rs.Value(),flags)
|
||||
}
|
||||
|
||||
func (b *Builder) writeBinary (value []byte, flags apack.Flags) {
|
||||
if flags.Bin32() {
|
||||
b.SetError(cjson.WriteBase32(b.B,value))
|
||||
return
|
||||
}
|
||||
b.SetError(cjson.WriteBase64(b.B,value))
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (b *Builder) List (fnc func(builder blds.ListBuilder)) {
|
||||
b.buildLevel(pstate.ModeList,fnc==nil,false,func(bl *Builder){
|
||||
fnc(b) }) }
|
||||
|
||||
|
||||
func (b *Builder) Map (fnc func(builder blds.MapBuilder)) {
|
||||
b.buildLevel(pstate.ModeMap,fnc == nil, false, func(bl *Builder){
|
||||
fnc(b) }) }
|
||||
|
||||
|
||||
func (b *Builder) Object (fnc func(builder blds.ObjectBuilder)) {
|
||||
b.buildLevel(pstate.ModeObject,fnc == nil, false, func(bl *Builder){
|
||||
fnc(b) }) }
|
||||
|
||||
|
||||
|
||||
func (b *Builder) Item () blds.ValueBuilder {
|
||||
if b.preElem() {
|
||||
//nothing siempre es mode value
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *Builder) Key (key any) blds.ValueBuilder {
|
||||
if b.preElem() {
|
||||
if key == nil {
|
||||
b.SetError(apack.G.ErrInvalidKey.WithMessage("key is null"))
|
||||
return b
|
||||
}
|
||||
g.stringer.packKey(b,key)
|
||||
}
|
||||
return b.postElem()
|
||||
}
|
||||
|
||||
func (b *Builder) Field (name string) blds.ValueBuilder {
|
||||
if len(name) == 0 {
|
||||
b.SetError(apack.G.ErrInvalidKey.WithMessage("key is empty"))
|
||||
return b
|
||||
}
|
||||
if b.preElem() {
|
||||
b.writeString(name,apack.FlagScapeHTML)
|
||||
}
|
||||
return b.postElem()
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (b *Builder) buildLevel (mode pstate.Mode, isNil, isEmpty bool, buildFnc func(builder *Builder)) {
|
||||
if !b.preValue() {
|
||||
return
|
||||
}
|
||||
|
||||
if isNil {
|
||||
b.B.W(cjson.G.NullValue)
|
||||
b.postValue()
|
||||
return
|
||||
}
|
||||
|
||||
if isEmpty {
|
||||
if mode == pstate.ModeList {
|
||||
b.B.Write2Byte('[',']')
|
||||
} else {
|
||||
b.B.Write2Byte('{','}')
|
||||
}
|
||||
b.postValue()
|
||||
return
|
||||
}
|
||||
|
||||
b.Open(mode)
|
||||
be := byte('}')
|
||||
if mode == pstate.ModeList {
|
||||
b.B.WriteByte('[')
|
||||
be = ']'
|
||||
} else {
|
||||
b.B.WriteByte('{')
|
||||
}
|
||||
buildFnc(b)
|
||||
if b.Err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
b.B.WriteByte(be)
|
||||
b.Close(mode)
|
||||
b.postValue()
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (b *Builder) preValue () bool {
|
||||
if b.Err != nil {
|
||||
panic(apack.G.ErrInvalidState.WithCause(b.Err))
|
||||
}
|
||||
if !b.Current.IsValue {
|
||||
_ = b.SetError(apack.G.ErrStateInvalidElem)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
func (b *Builder) postValue () {
|
||||
if b.Err != nil {
|
||||
return
|
||||
}
|
||||
b.Current.IsValue = false
|
||||
b.Current.Count++
|
||||
if b.Current.Mode == pstate.ModeValue {
|
||||
b.Current.Mode = pstate.ModeNone
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (b *Builder) preElem () bool {
|
||||
if b.Err != nil {
|
||||
panic(apack.G.ErrInvalidState.WithCause(b.Err))
|
||||
}
|
||||
if b.Current.IsValue {
|
||||
_ = b.SetError(apack.G.ErrStateInvalidElem)
|
||||
return false
|
||||
}
|
||||
if b.Current.Count > 0 {
|
||||
b.B.WriteByte(',')
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
func (b *Builder) postElem () *Builder {
|
||||
if b.Err == nil {
|
||||
b.Current.IsValue = true
|
||||
b.B.WriteByte(':')
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
|
||||
func packAny (b *Builder, value any) {
|
||||
|
||||
switch tp := value.(type) {
|
||||
case []bool : packList[bool ](b,tp,0,b.Bool)
|
||||
case []string : packList[string](b,tp,0,b.String)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func packList [T basicTypes](b *Builder, list []T, flags apack.Flags, fncType func(vl T)) {
|
||||
b.buildLevel(pstate.ModeList,list == nil, len(list) == 0,func(bl *Builder){
|
||||
for i,v := range list {
|
||||
if i > 0 {
|
||||
bl.B.WriteByte(',')
|
||||
}
|
||||
fncType(v)
|
||||
if bl.Err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,40 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project active3
|
||||
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name stringer.go
|
||||
// ✍️ Created by DEV
|
||||
// 📅 Modified 28/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 pkj
|
||||
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
keyStringer struct {}
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
func (s *keyStringer) packKey (b *Builder, value any) {}
|
||||
|
||||
@ -0,0 +1,122 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// 🚀 Project active3
|
||||
// 🔗 Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name list_packer.go
|
||||
// ✍️ Created by DEV
|
||||
// 📅 Modified 28/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"
|
||||
"time"
|
||||
)
|
||||
|
||||
func packListString (p *Builder, list []string, flags apack.Flags) {
|
||||
p.makeLevel(pstate.ModeList,list == nil, len(list) == 0,func(b *Builder){
|
||||
for i,v := range list {
|
||||
if i > 0 {
|
||||
p.B.WriteByte(sep)
|
||||
}
|
||||
p.packString(v, flags)
|
||||
if p.Err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
func packListError (p *Builder, list []error, flags apack.Flags) {
|
||||
p.makeLevel(pstate.ModeList,list == nil, len(list) == 0,func(b *Builder){
|
||||
for i,v := range list {
|
||||
if i > 0 {
|
||||
p.B.WriteByte(sep)
|
||||
}
|
||||
if v == nil {
|
||||
b.B.WOQ(true,'"',cjson.G.NullValue)
|
||||
continue
|
||||
}
|
||||
p.packString(v.Error(), apack.FlagScapeHTML)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
func packListBool (p *Builder, list []bool, flags apack.Flags) {
|
||||
p.makeLevel(pstate.ModeList,list == nil, len(list) == 0,func(b *Builder){
|
||||
for i,v := range list {
|
||||
if i > 0 {
|
||||
p.B.WriteByte(sep)
|
||||
}
|
||||
cjson.WriteBool(p.B,v)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
func packListInt [T int | int8 | int16 | int32 | int64 | time.Duration](p *Builder, list []T, flags apack.Flags) {
|
||||
p.makeLevel(pstate.ModeList,list == nil, len(list) == 0,func(b *Builder){
|
||||
for i,v := range list {
|
||||
if i > 0 {
|
||||
p.B.WriteByte(sep)
|
||||
}
|
||||
cjson.WriteInt(p.B,int64(v))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
func packListUInt [T uint | uint8 | uint16 | uint32 | uint64|uintptr](p *Builder, list []T, flags apack.Flags) {
|
||||
p.makeLevel(pstate.ModeList,list == nil, len(list) == 0,func(b *Builder){
|
||||
for i,v := range list {
|
||||
if i > 0 {
|
||||
p.B.WriteByte(sep)
|
||||
}
|
||||
cjson.WriteUint(p.B,uint64(v))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
func packListFloat[T float32 | float64](p *Builder, list []T, flags apack.Flags, bits int) {
|
||||
p.makeLevel(pstate.ModeList,list == nil, len(list) == 0,func(b *Builder){
|
||||
for i,v := range list {
|
||||
if i > 0 {
|
||||
p.B.WriteByte(sep)
|
||||
}
|
||||
cjson.WriteFloat(p.B,float64(v),bits)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
func packListDate (p *Builder, list []time.Time, flags apack.Flags) {
|
||||
p.makeLevel(pstate.ModeList,list == nil, len(list) == 0,func(b *Builder){
|
||||
for i,v := range list {
|
||||
if i > 0 {
|
||||
p.B.WriteByte(sep)
|
||||
}
|
||||
cjson.WriteTime(p.B,v,flags.DateFormat())
|
||||
}
|
||||
})
|
||||
}
|
||||
Loading…
Reference in new issue