// ------------------------------------------------------------------------ // Project atila // Active Thing (activething.com) git.activething.com/go // // File name builderMap.go // Created by DEV // Modified 06/02/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" "reflect" "time" ) func (p *Packer) KeyValueAny (key any, value any, flags packs.Flags) { p.addKeyValue(key, func(){ p.PackAny(value,flags)}) } func (p *Packer) KeyValueRaw (key any,value []byte, flags packs.Flags) { p.addKeyValue(key, func(){ p.PackRaw(value,flags)}) } func (p *Packer) KeyValueBinary (key any, value []byte, flags packs.Flags) { p.addKeyValue(key, func(){ p.PackBinary(value,flags)}) } func (p *Packer) KeyValueString (key any, value string, flags packs.Flags) { p.addKeyValue(key, func(){ p.PackString(value,flags)}) } func (p *Packer) KeyValueBool (key any, value bool, flags packs.Flags) { p.addKeyValue(key, func(){ p.PackBool(value,flags)}) } func (p *Packer) KeyValueInt8 (key any, value int8 , flags packs.Flags) { p.addKeyValue(key, func(){ p.PackInt64(int64(value),flags)}) } func (p *Packer) KeyValueInt16 (key any, value int16, flags packs.Flags) { p.addKeyValue(key, func(){ p.PackInt64(int64(value),flags)}) } func (p *Packer) KeyValueInt32 (key any, value int32, flags packs.Flags) { p.addKeyValue(key, func(){ p.PackInt64(int64(value),flags)}) } func (p *Packer) KeyValueInt64 (key any, value int64, flags packs.Flags) { p.addKeyValue(key, func(){ p.PackInt64(value,flags)}) } func (p *Packer) KeyValueUInt8 (key any, value uint8 , flags packs.Flags) { p.addKeyValue(key, func(){ p.PackUInt64(uint64(value),flags)}) } func (p *Packer) KeyValueUInt16 (key any, value uint16, flags packs.Flags) { p.addKeyValue(key, func(){ p.PackUInt64(uint64(value),flags)}) } func (p *Packer) KeyValueUInt32 (key any, value uint32, flags packs.Flags) { p.addKeyValue(key, func(){ p.PackUInt64(uint64(value),flags)}) } func (p *Packer) KeyValueUInt64 (key any, value uint64, flags packs.Flags) { p.addKeyValue(key, func(){ p.PackUInt64(value,flags)}) } func (p *Packer) KeyValueFloat32(key any, value float32, flags packs.Flags){ p.addKeyValue(key,func(){ p.PackFloat32(value, flags) }) } func (p *Packer) KeyValueFloat64(key any, value float64, flags packs.Flags){ p.addKeyValue(key,func(){ p.PackFloat64(value, flags) }) } func (p *Packer) KeyValueDate (key any, value time.Time, flags packs.Flags){ p.addKeyValue(key,func(){ p.PackDate(value, flags) }) } func (p *Packer) KeyValueList (key any, builder func(packs.ListPackBuilder, packs.Flags), flags packs.Flags) { p.addKeyValue(key,func(){ p.PackList(builder, flags) }) } func (p *Packer) KeyValueMap (key any, builder func(packs.MapPackBuilder, packs.Flags), flags packs.Flags) { p.addKeyValue(key,func(){ p.PackMap(builder, flags) }) } func (p *Packer) KeyValueObject (key any, builder func(packs.ObjectPackBuilder,packs.Flags), flags packs.Flags) { p.addKeyValue(key,func(){ p.PackObject(builder, flags) }) } func (p *Packer) addKeyValue(key any, caller func()) { if p.state.Count() > 0 { p.state.WriteByte(',') } p.packStringifyKey(key) if p.state.Error != nil { return } p.state.WriteByte(':') caller() p.state.Inc() } func (p *Packer) packStringifyKey(key any) { switch v := key.(type) { case string : p.PackString(v,packs.FlagScapeHTML) case float32: p.PackFloat32(v,packs.FlagScapeHTML|packs.FlagQuote) case float64: p.PackFloat64(v,packs.FlagScapeHTML|packs.FlagQuote) default: me := &rMapEntry{ v : reflect.ValueOf(key), } fn := stringifyRKey(me.v.Type()) if fn == nil { p.state.SetError(packs.G.ErrInvalidKey) return } if er := fn(me,me.v); er != nil { p.state.SetError(er) return } p.PackString(me.k,me.f) } }