2016-05-17 23:55:50 +00:00
|
|
|
// Copyright © 2016 Abcum Ltd
|
|
|
|
//
|
|
|
|
// 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 keys
|
|
|
|
|
|
|
|
import (
|
2017-11-16 20:18:42 +00:00
|
|
|
"sync"
|
2016-05-17 23:55:50 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type decoder struct {
|
|
|
|
r *reader
|
|
|
|
}
|
|
|
|
|
2017-11-16 20:18:42 +00:00
|
|
|
var decoders = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &decoder{
|
|
|
|
r: newReader(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-02-09 20:35:19 +00:00
|
|
|
// decode decodes an encoded string using the unicode collation algorithm.
|
2016-05-17 23:55:50 +00:00
|
|
|
func decode(data []byte, items ...interface{}) {
|
2017-11-16 20:18:42 +00:00
|
|
|
dec := newDecoder(data)
|
|
|
|
dec.Decode(items...)
|
|
|
|
dec.Reset()
|
|
|
|
return
|
2016-05-17 23:55:50 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 20:18:42 +00:00
|
|
|
func newDecoder(b []byte) *decoder {
|
|
|
|
d := decoders.Get().(*decoder)
|
|
|
|
d.r.r.ResetBytes(b)
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *decoder) Reset() {
|
|
|
|
decoders.Put(d)
|
2016-05-17 23:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *decoder) Decode(items ...interface{}) {
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
|
|
|
|
switch value := item.(type) {
|
|
|
|
|
|
|
|
case *time.Time:
|
2017-11-16 20:18:42 +00:00
|
|
|
*value = d.r.readTime()
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case *bool:
|
2017-11-16 20:18:42 +00:00
|
|
|
*value = d.r.readBool()
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case *[]byte:
|
2017-11-16 20:18:42 +00:00
|
|
|
*value = d.r.readBytes()
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case *string:
|
2017-11-16 20:18:42 +00:00
|
|
|
*value = d.r.readString()
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case *int:
|
2017-11-16 20:18:42 +00:00
|
|
|
*value = int(d.r.readFloat())
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case *int8:
|
2017-11-16 20:18:42 +00:00
|
|
|
*value = int8(d.r.readFloat())
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case *int16:
|
2017-11-16 20:18:42 +00:00
|
|
|
*value = int16(d.r.readFloat())
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case *int32:
|
2017-11-16 20:18:42 +00:00
|
|
|
*value = int32(d.r.readFloat())
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case *int64:
|
2017-11-16 20:18:42 +00:00
|
|
|
*value = int64(d.r.readFloat())
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case *uint:
|
2017-11-16 20:18:42 +00:00
|
|
|
*value = uint(d.r.readFloat())
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case *uint8:
|
2017-11-16 20:18:42 +00:00
|
|
|
*value = uint8(d.r.readFloat())
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case *uint16:
|
2017-11-16 20:18:42 +00:00
|
|
|
*value = uint16(d.r.readFloat())
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case *uint32:
|
2017-11-16 20:18:42 +00:00
|
|
|
*value = uint32(d.r.readFloat())
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case *uint64:
|
2017-11-16 20:18:42 +00:00
|
|
|
*value = uint64(d.r.readFloat())
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case *float32:
|
2017-11-16 20:18:42 +00:00
|
|
|
*value = float32(d.r.readFloat())
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case *float64:
|
2017-11-16 20:18:42 +00:00
|
|
|
*value = float64(d.r.readFloat())
|
2016-05-17 23:55:50 +00:00
|
|
|
|
2016-09-09 23:24:32 +00:00
|
|
|
case *[]time.Time:
|
2017-11-16 20:18:42 +00:00
|
|
|
if d.r.readNext(bARR) {
|
|
|
|
for !d.r.readNext(bEND) {
|
|
|
|
*value = append(*value, d.r.readTime())
|
2016-09-09 23:24:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-17 23:55:50 +00:00
|
|
|
case *[]bool:
|
2017-11-16 20:18:42 +00:00
|
|
|
if d.r.readNext(bARR) {
|
|
|
|
for !d.r.readNext(bEND) {
|
|
|
|
*value = append(*value, d.r.readBool())
|
2016-05-17 23:55:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *[]string:
|
2017-11-16 20:18:42 +00:00
|
|
|
if d.r.readNext(bARR) {
|
|
|
|
for !d.r.readNext(bEND) {
|
|
|
|
*value = append(*value, d.r.readString())
|
2016-05-17 23:55:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *[]int:
|
2017-11-16 20:18:42 +00:00
|
|
|
if d.r.readNext(bARR) {
|
|
|
|
for !d.r.readNext(bEND) {
|
|
|
|
*value = append(*value, int(d.r.readFloat()))
|
2016-05-17 23:55:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *[]int8:
|
2017-11-16 20:18:42 +00:00
|
|
|
if d.r.readNext(bARR) {
|
|
|
|
for !d.r.readNext(bEND) {
|
|
|
|
*value = append(*value, int8(d.r.readFloat()))
|
2016-05-17 23:55:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *[]int16:
|
2017-11-16 20:18:42 +00:00
|
|
|
if d.r.readNext(bARR) {
|
|
|
|
for !d.r.readNext(bEND) {
|
|
|
|
*value = append(*value, int16(d.r.readFloat()))
|
2016-05-17 23:55:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *[]int32:
|
2017-11-16 20:18:42 +00:00
|
|
|
if d.r.readNext(bARR) {
|
|
|
|
for !d.r.readNext(bEND) {
|
|
|
|
*value = append(*value, int32(d.r.readFloat()))
|
2016-05-17 23:55:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *[]int64:
|
2017-11-16 20:18:42 +00:00
|
|
|
if d.r.readNext(bARR) {
|
|
|
|
for !d.r.readNext(bEND) {
|
|
|
|
*value = append(*value, int64(d.r.readFloat()))
|
2016-05-17 23:55:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *[]uint:
|
2017-11-16 20:18:42 +00:00
|
|
|
if d.r.readNext(bARR) {
|
|
|
|
for !d.r.readNext(bEND) {
|
|
|
|
*value = append(*value, uint(d.r.readFloat()))
|
2016-05-17 23:55:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *[]uint16:
|
2017-11-16 20:18:42 +00:00
|
|
|
if d.r.readNext(bARR) {
|
|
|
|
for !d.r.readNext(bEND) {
|
|
|
|
*value = append(*value, uint16(d.r.readFloat()))
|
2016-05-17 23:55:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *[]uint32:
|
2017-11-16 20:18:42 +00:00
|
|
|
if d.r.readNext(bARR) {
|
|
|
|
for !d.r.readNext(bEND) {
|
|
|
|
*value = append(*value, uint32(d.r.readFloat()))
|
2016-05-17 23:55:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *[]uint64:
|
2017-11-16 20:18:42 +00:00
|
|
|
if d.r.readNext(bARR) {
|
|
|
|
for !d.r.readNext(bEND) {
|
|
|
|
*value = append(*value, uint64(d.r.readFloat()))
|
2016-05-17 23:55:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *[]float32:
|
2017-11-16 20:18:42 +00:00
|
|
|
if d.r.readNext(bARR) {
|
|
|
|
for !d.r.readNext(bEND) {
|
|
|
|
*value = append(*value, float32(d.r.readFloat()))
|
2016-05-17 23:55:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *[]float64:
|
2017-11-16 20:18:42 +00:00
|
|
|
if d.r.readNext(bARR) {
|
|
|
|
for !d.r.readNext(bEND) {
|
|
|
|
*value = append(*value, float64(d.r.readFloat()))
|
2016-05-17 23:55:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *[]interface{}:
|
2017-11-16 20:18:42 +00:00
|
|
|
*value = d.r.readArray()
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case *interface{}:
|
|
|
|
|
2017-11-16 20:18:42 +00:00
|
|
|
switch d.r.lookNext() {
|
2016-05-17 23:55:50 +00:00
|
|
|
default:
|
2017-11-16 20:18:42 +00:00
|
|
|
*value = d.r.readAny()
|
|
|
|
case bNIL:
|
|
|
|
*value = d.r.readNull()
|
|
|
|
case bVAL:
|
|
|
|
*value = d.r.readBool()
|
|
|
|
case bTME:
|
|
|
|
*value = d.r.readTime()
|
|
|
|
case bNEG, bPOS:
|
|
|
|
*value = d.r.readFloat()
|
|
|
|
case bSTR, bPRE, bSUF:
|
|
|
|
*value = d.r.readString()
|
|
|
|
case bARR:
|
|
|
|
*value = d.r.readArray()
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|