// ------------------------------------------------------------------------ // 🚀 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) { b.checkValue() if value == nil { _,_ = b.B.Write(cjson.G.NullValue) b.postValue() } if bl,is := value.(blds.Buildable); is { bl.Build(b) b.postValue() return } if jm,is := value.(json.Marshaler); is { dt,er := jm.MarshalJSON() if er != nil { b.SetError(er) return } b.B.W(dt) b.postValue() 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) 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,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.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.f.Quote()) 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){ //todo reflect } 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.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.Current.Count > 0) b.packString(name,apack.FlagScapeHTML) if b.IsValue = b.Err == nil; b.IsValue { b.B.WriteByte(fsep) } return b } 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(pstate.ModeMap) if b.Err != nil { return } maker(b) if b.Err != nil { b.Close(mode) 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.IsValue { b.SetError(apack.G.ErrStateInvalidElem) } } func (b *Builder) postValue() { if b.Err != nil { b.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) } } func (b *Builder) unsupportedType (rvalue reflect.Value) { }