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.
111 lines
3.4 KiB
111 lines
3.4 KiB
// ------------------------------------------------------------------------
|
|
// Project atila
|
|
// Active Thing (activething.com) git.activething.com/go
|
|
//
|
|
// File name list_builder.go
|
|
// Created by DEV
|
|
// Modified 23/01/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 jjson
|
|
|
|
import (
|
|
"activething.com/go/gogo/packs"
|
|
"reflect"
|
|
"time"
|
|
)
|
|
|
|
|
|
|
|
func (b *Builder) listPacker(rv reflect.Value) {
|
|
if rv.IsNil() {
|
|
b.state.Write(G.nullValue)
|
|
return
|
|
}
|
|
if rv.Len() == 0 {
|
|
b.state.Write2Byte('[',']')
|
|
return
|
|
}
|
|
if b.state.Node.Level > maxDeepUncheck {
|
|
p := struct {
|
|
ptr interface{} // always an unsafe.Pointer, but avoids a dependency on package unsafe
|
|
len int
|
|
}{
|
|
ptr: rv.UnsafePointer(),
|
|
len: rv.Len(),
|
|
}
|
|
if _,ok := b.state.Seen[p]; ok {
|
|
b.state.SetError(__packs.G.ErrRedundantPack)
|
|
return
|
|
}
|
|
b.state.Seen[p] = struct{}{}
|
|
defer delete(b.state.Seen,p)
|
|
}
|
|
|
|
switch rv.Type().Elem().Kind() {
|
|
case reflect.String: b._listPacker (rv, func(lb __packs.BuilderList, ev reflect.Value) { lb.ItemString(ev.String()) })
|
|
case reflect.Bool : b._listPacker (rv, func(lb __packs.BuilderList, ev reflect.Value) { lb.ItemBool(ev.Bool()) })
|
|
case reflect.Int8,
|
|
reflect.Int16,
|
|
reflect.Int32,
|
|
reflect.Int64,
|
|
reflect.Int : b._listPacker (rv, func(lb __packs.BuilderList, ev reflect.Value) { lb.ItemInt64(ev.Int()) })
|
|
case reflect.Uint8,
|
|
reflect.Uint16,
|
|
reflect.Uint32,
|
|
reflect.Uint64,
|
|
reflect.Uint : b._listPacker (rv, func(lb __packs.BuilderList, ev reflect.Value) { lb.ItemUInt64(ev.Uint()) })
|
|
case reflect.Float32,
|
|
reflect.Float64: b._listPacker (rv, func(lb __packs.BuilderList, ev reflect.Value) { lb.ItemFloat64(ev.Float()) })
|
|
case reflect.Complex64,
|
|
reflect.Complex128: b._listPacker (rv, func(lb __packs.BuilderList, ev reflect.Value) { lb.ItemComplex128(ev.Complex()) })
|
|
|
|
|
|
case reflect.Map:
|
|
b._listPacker (rv, func(lb __packs.BuilderList, ev reflect.Value) { lb.ItemAny(rv.Interface()) })
|
|
|
|
case reflect.Struct:
|
|
if rv.Type() == reflect.TypeOf(time.Time{}) {
|
|
b._listPacker (rv, func(lb __packs.BuilderList, ev reflect.Value) { lb.ItemDate(rv.Interface().(time.Time)) })
|
|
return
|
|
}
|
|
b._listPacker (rv, func(lb __packs.BuilderList, ev reflect.Value) { lb.ItemAny(rv.Interface()) })
|
|
|
|
case reflect.Array,
|
|
reflect.Slice :
|
|
b._listPacker (rv, func(lb __packs.BuilderList, ev reflect.Value) { lb.ItemAny(rv.Interface()) })
|
|
|
|
|
|
|
|
default:
|
|
b._listPacker(rv, func(lb __packs.BuilderList, ev reflect.Value) { lb.ItemAny(rv.Interface()) })
|
|
}
|
|
}
|
|
|
|
|
|
func (b *Builder) _listPacker (list reflect.Value, pack func(lb __packs.BuilderList, ev reflect.Value)) {
|
|
b.BuildList(func(l __packs.BuilderList) {
|
|
for i := 0; i < list.Len(); i++ {
|
|
pack(l, list.Index(i))
|
|
if b.state.Error != nil {
|
|
return
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|