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.
210 lines
4.3 KiB
210 lines
4.3 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"
|
|
"reflect"
|
|
"time"
|
|
)
|
|
|
|
type (
|
|
|
|
Builder struct {
|
|
pstate.State
|
|
f apack.Flags
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
func newState () *Builder {
|
|
return &Builder{
|
|
State : *pstate.NewState(),
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *Builder) Any (value any) {
|
|
|
|
}
|
|
|
|
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,true)
|
|
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,true)
|
|
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,true)
|
|
b.postValue()
|
|
}
|
|
|
|
func (b *Builder) Float32 (value float32){
|
|
b.checkValue()
|
|
cjson.WriteFloat(b.B,float64(value),32,true)
|
|
b.postValue()
|
|
}
|
|
func (b *Builder) Float64 (value float64){
|
|
b.checkValue()
|
|
cjson.WriteFloat(b.B,value,64,true)
|
|
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(),true)
|
|
b.postValue()
|
|
}
|
|
|
|
func (b *Builder) Duration(value time.Duration){ b.Int64(int64(value)) }
|
|
|
|
func (b *Builder) Reflect (value reflect.Value){}
|
|
|
|
func (b *Builder) List (fnc func (listBuilder blds.ListBuilder )){}
|
|
|
|
func (b *Builder) Map (fnc func (mapBuilder blds.MapBuilder )){}
|
|
|
|
func (b *Builder) Object(fnc func (objBuilder blds.ObjectBuilder)){}
|
|
|
|
|
|
|
|
|
|
func (b *Builder) Item () blds.ValueBuilder {
|
|
b.checkState()
|
|
b.writeSepIf(b.Items() > 0)
|
|
return b
|
|
}
|
|
|
|
func (b *Builder) Key (key any) blds.ValueBuilder {
|
|
b.checkState()
|
|
b.writeSepIf(b.Items() > 0)
|
|
|
|
g.keyStringer.packStringifyKey(b,key)
|
|
if b.IsValue = b.Err == nil; b.IsValue {
|
|
b.B.WriteByte(fsep)
|
|
}
|
|
return b
|
|
}
|
|
|
|
|
|
func (b *Builder) Field (name string) blds.ValueBuilder {
|
|
b.checkState()
|
|
b.writeSepIf(b.Items() > 0)
|
|
|
|
b.packString(name,apack.FlagScapeHTML)
|
|
if b.IsValue = b.Err == nil; b.IsValue {
|
|
b.B.WriteByte(fsep)
|
|
}
|
|
return b
|
|
}
|
|
|
|
|
|
|
|
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.IsValue {
|
|
b.SetError(apack.G.ErrStateInvalidElem)
|
|
}
|
|
}
|
|
|
|
func (b *Builder) postValue() {
|
|
if b.Err != nil {
|
|
b.IsValue = false
|
|
b.IncItems()
|
|
}
|
|
}
|
|
|
|
func (b *Builder) writeSepIf(cond bool) {
|
|
if cond {
|
|
b.B.WriteByte(sep) } }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|