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.

440 lines
8.5 KiB

// ------------------------------------------------------------------------
// 🚀 Project active3
// 🔗 Active Thing (activething.com) git.activething.com/go
//
// File name state.go
// ✍️ Created by DEV
// 📅 Modified 10/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"
"blds"
"cdcs/cjson"
"encoding/json"
"reflect"
"time"
)
type (
Builder struct {
*pstate.State
f apack.Flags
}
)
func newBuilder (flags apack.Flags) *Builder {
return &Builder{
State : pstate.AcquireState(),
f : flags,
}
}
func (b *Builder) release () {
pstate.ReleaseState(b.State)
b.State = nil
}
func (b *Builder) Pack (value any) []byte {
b.Any(value)
return b.B.Bytes()
}
func (b *Builder) Any (value any) {
if value == nil {
b.checkValue()
_,_ = b.B.Write(cjson.G.NullValue)
b.postValue()
return
}
if bl,is := value.(blds.Buildable); is {
b.Buildable(bl)
return
}
if jm,is := value.(json.Marshaler); is {
b.Marshaller(jm)
return
}
switch v := value.(type) {
case bool : b.Bool (v)
case string : b.String (v)
case int : b.Int64(int64(v))
case int8 : b.Int64(int64(v))
case int16 : b.Int64(int64(v))
case int32 : b.Int64(int64(v))
case int64 : b.Int64(v)
case uint : b.Uint64(uint64(v))
case uint16 : b.Uint64(uint64(v))
case uint32 : b.Uint64(uint64(v))
case uintptr : b.Uint64(uint64(v))
case uint64 : b.Uint64(v)
case float32 : b.Float32(v)
case float64 : b.Float64(v)
case error : b.Error(v)
case time.Time : b.Time(v)
case time.Duration : b.Duration(v)
case reflect.Value : packReflected(v.Type())(b,v)
case []byte :
if b.f.Binary() {
b.Binary(v)
return
}
case []uint : packListUInt [uint ] (b,v,b.f)
case []uint16 : packListUInt [uint16] (b,v,b.f)
case []uint32 : packListUInt [uint32] (b,v,b.f)
case []uint64 : packListUInt [uint64] (b,v,b.f)
case []int : packListInt [int ] (b,v,b.f)
case []int8 : packListInt [int8 ] (b,v,b.f)
case []int16 : packListInt [int16] (b,v,b.f)
case []int32 : packListInt [int32] (b,v,b.f)
case []int64 : packListInt [int64] (b,v,b.f)
case []float32: packListFloat[float32](b,v,b.f,32)
case []float64: packListFloat[float64](b,v,b.f,64)
case []bool : packListBool (b,v,b.f)
case []string : packListString(b,v,b.f)
case []time.Time: packListDate (b,v,b.f)
case []time.Duration: packListInt[time.Duration](b,v,b.f)
case []error : packListError(b,v,b.f)
default:
rv := reflect.ValueOf (value)
packReflected(rv.Type())(b,rv)
}
}
func (b *Builder) Raw (value []byte){
b.checkValue()
_,_ = b.B.Write(value)
b.checkValue()
}
func (b *Builder) Binary (value []byte){
b.checkValue()
b.packBinary(value,b.f)
b.postValue()
}
func (b *Builder) String (value string){
b.checkValue()
b.packString(value,b.f)
b.postValue()
}
func (b *Builder) Bool (value bool){
b.checkValue()
cjson.WriteBool(b.B,value)
b.postValue()
}
func (b *Builder) Int (value int ){ b.Int64(int64(value)) }
func (b *Builder) Int8 (value int8 ){ b.Int64(int64(value)) }
func (b *Builder) Int16 (value int16 ){ b.Int64(int64(value)) }
func (b *Builder) Int32 (value int32 ){ b.Int64(int64(value)) }
func (b *Builder) Int64 (value int64 ){
b.checkValue()
cjson.WriteInt(b.B,value)
b.postValue()
}
func (b *Builder) Uint (value uint ){ b.Uint64(uint64(value)) }
func (b *Builder) Uint8 (value uint8 ){ b.Uint64(uint64(value)) }
func (b *Builder) Uint16 (value uint16 ){ b.Uint64(uint64(value)) }
func (b *Builder) Uint32 (value uint32 ){ b.Uint64(uint64(value)) }
func (b *Builder) Uintptr (value uintptr){ b.Uint64(uint64(value)) }
func (b *Builder) Uint64 (value uint64 ){
b.checkValue()
cjson.WriteUint(b.B,value)
b.postValue()
}
func (b *Builder) Float32 (value float32){
b.float(float64(value),32) }
func (b *Builder) Float64 (value float64){
b.float(value,64) }
func (b *Builder) float (value float64, bits int) {
b.checkValue()
cjson.WriteFloat(b.B,value,bits)
b.postValue()
}
func (b *Builder) Error (value error){
b.checkValue()
if value == nil {
b.B.WOQ(true,'"',cjson.G.NullValue)
} else {
b.packString(value.Error(),b.f)
}
b.postValue()
}
func (b *Builder) Time (value time.Time){
b.checkValue()
cjson.WriteTime(b.B,value,b.f.DateFormat())
b.postValue()
}
func (b *Builder) Duration(value time.Duration){
b.Int64(int64(value)) }
func (b *Builder) Reflect (value reflect.Value){
//todo reflect
}
func (b *Builder) Buildable(value blds.Buildable) {
b.checkValue()
if value == nil {
_,_ = b.B.Write(cjson.G.NullValue)
} else {
value.Build(b)
}
b.postValue()
}
func (b *Builder) Marshaller(value json.Marshaler) {
b.checkValue()
if value == nil {
_,_ = b.B.Write(cjson.G.NullValue)
} else {
dt, er := value.MarshalJSON()
if b.SetError(er) {
return
}
b.B.W(dt)
}
b.postValue()
}
func (b *Builder) List (fnc func (listBuilder blds.ListBuilder )){
b.makeLevel(pstate.ModeList,fnc == nil,false,func(bl *Builder){
fnc(bl)
})
}
func (b *Builder) Map (fnc func (mapBuilder blds.MapBuilder )){
b.makeLevel(pstate.ModeMap,fnc == nil,false,func(bl *Builder){
fnc(bl)
})
}
func (b *Builder) Object(fnc func (objBuilder blds.ObjectBuilder)){
b.makeLevel(pstate.ModeObject,fnc == nil,false,func(bl *Builder){
fnc(bl)
})
}
func (b *Builder) Item() blds.ValueBuilder {
b.checkState()
b.writeSepIf(b.Current.Count > 0)
return b
}
func (b *Builder) Key(key any) blds.ValueBuilder {
b.checkState()
b.writeSepIf(b.Current.Count > 0)
g.keyStringer.packStringifyKey(b,key)
if b.Current.IsValue = b.Err == nil; b.Current.IsValue {
b.B.WriteByte(fsep)
}
return b
}
func (b *Builder) Field(name string) blds.ValueBuilder {
b.checkState()
b.writeSepIf(b.Current.Count > 0)
b.packString(name,apack.FlagScapeHTML)
if b.Current.IsValue = b.Err == nil; b.Current.IsValue {
b.B.WriteByte(fsep)
}
return b
}
func (b *Builder) packString (value string, flags apack.Flags) {
if len(value) == 0 {
b.B.Write2Byte('"','"')
return
}
if flags.Encrypted() {
b.packEncrypted([]byte(value),flags)
return
}
if flags.Binary() {
b.packBinary([]byte(value),flags)
return
}
cjson.WriteString(b.B,value,flags.EscapeHTML())
}
func (b *Builder) packEncrypted (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
}
if !flags.Binary() {
flags|=apack.FlagBinary64
}
b.packBinary (rs.Value(),flags)
}
func (b *Builder) packBinary (value []byte, flags apack.Flags) {
if flags.Bin64() {
b.SetError(cjson.WriteBase64(b.B,value))
return
}
if flags.Bin32() {
b.SetError(cjson.WriteBase64(b.B,value))
return
}
}
func (b *Builder) makeLevel (mode pstate.Mode, isNil, isEmpty bool, maker func(builder *Builder)) {
b.checkValue()
if isNil {
b.B.WOQ(true, '"', cjson.G.NullValue)
b.postValue()
return
}
if isEmpty {
b.B.WOQ(true, '"', cjson.G.NullValue)
b.postValue()
return
}
b.Open(mode)
if b.Err != nil {
return
}
be := byte('}')
if mode == pstate.ModeList {
b.B.WriteByte('[')
be = ']'
} else {
b.B.WriteByte('{')
}
maker(b)
if b.Err != nil {
return
}
b.Close(mode)
b.B.WriteByte(be)
b.postValue()
}
func (b *Builder) checkState () {
if b.Err != nil {
panic(apack.G.ErrInvalidState.WithCause(b.Err))
}
}
func (b *Builder) checkValue () {
if b.Err != nil {
panic(apack.G.ErrInvalidState.WithCause(b.Err))
}
if !b.Current.IsValue {
b.SetError(apack.G.ErrStateInvalidElem)
}
}
func (b *Builder) postValue() {
if b.Err == nil {
b.Current.IsValue = false
b.Current.Count++
if b.Current.Mode == pstate.ModeValue {
b.Current.Mode = pstate.ModeNone
}
}
}
func (b *Builder) writeSepIf(cond bool) {
if cond {
b.B.WriteByte(sep) } }

Powered by TurnKey Linux.