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 (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type encoder struct {
|
|
|
|
w *writer
|
|
|
|
}
|
|
|
|
|
2017-02-09 20:35:19 +00:00
|
|
|
// encode encodes an interface using the unicode collation algorithm.
|
2016-05-17 23:55:50 +00:00
|
|
|
func encode(items ...interface{}) []byte {
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
newEncoder(b).Encode(items...)
|
|
|
|
return b.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func newEncoder(w io.Writer) *encoder {
|
|
|
|
return &encoder{
|
|
|
|
w: newWriter(w),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *encoder) Encode(items ...interface{}) {
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
|
|
|
|
switch value := item.(type) {
|
|
|
|
|
|
|
|
case nil:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bNIL)
|
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case bool:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bVAL)
|
2016-05-17 23:55:50 +00:00
|
|
|
if value {
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bVAL)
|
2016-05-17 23:55:50 +00:00
|
|
|
}
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case time.Time:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bTME)
|
|
|
|
e.w.write(value)
|
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case []byte:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bSTR)
|
|
|
|
e.w.write(value)
|
|
|
|
e.w.write(bEND)
|
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case string:
|
|
|
|
|
2016-05-27 15:25:46 +00:00
|
|
|
if value == Ignore {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-05-17 23:55:50 +00:00
|
|
|
if value == Prefix {
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bPRE)
|
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if value == Suffix {
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bSUF)
|
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bSTR)
|
|
|
|
e.w.write(value)
|
|
|
|
e.w.write(bEND)
|
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case float32, float64:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(value)
|
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
2016-09-09 23:21:58 +00:00
|
|
|
case int, int8, int16, int32, int64:
|
2016-05-17 23:55:50 +00:00
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(value)
|
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
2016-09-09 23:21:58 +00:00
|
|
|
case uint, uint8, uint16, uint32, uint64:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(value)
|
|
|
|
e.w.write(bEND)
|
2016-09-09 23:21:58 +00:00
|
|
|
|
|
|
|
case []time.Time:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bARR)
|
2016-09-09 23:21:58 +00:00
|
|
|
for _, val := range value {
|
|
|
|
e.Encode(val)
|
|
|
|
}
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bEND)
|
2016-09-09 23:21:58 +00:00
|
|
|
|
2016-05-17 23:55:50 +00:00
|
|
|
case []bool:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bARR)
|
2016-05-17 23:55:50 +00:00
|
|
|
for _, val := range value {
|
|
|
|
e.Encode(val)
|
|
|
|
}
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case []string:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bARR)
|
2016-05-17 23:55:50 +00:00
|
|
|
for _, val := range value {
|
|
|
|
e.Encode(val)
|
|
|
|
}
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case []int:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bARR)
|
2016-05-17 23:55:50 +00:00
|
|
|
for _, val := range value {
|
|
|
|
e.Encode(val)
|
|
|
|
}
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case []int8:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bARR)
|
2016-05-17 23:55:50 +00:00
|
|
|
for _, val := range value {
|
|
|
|
e.Encode(val)
|
|
|
|
}
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case []int16:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bARR)
|
2016-05-17 23:55:50 +00:00
|
|
|
for _, val := range value {
|
|
|
|
e.Encode(val)
|
|
|
|
}
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case []int32:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bARR)
|
2016-05-17 23:55:50 +00:00
|
|
|
for _, val := range value {
|
|
|
|
e.Encode(val)
|
|
|
|
}
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case []int64:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bARR)
|
2016-05-17 23:55:50 +00:00
|
|
|
for _, val := range value {
|
|
|
|
e.Encode(val)
|
|
|
|
}
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case []uint:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bARR)
|
2016-05-17 23:55:50 +00:00
|
|
|
for _, val := range value {
|
|
|
|
e.Encode(val)
|
|
|
|
}
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case []uint16:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bARR)
|
2016-05-17 23:55:50 +00:00
|
|
|
for _, val := range value {
|
|
|
|
e.Encode(val)
|
|
|
|
}
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case []uint32:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bARR)
|
2016-05-17 23:55:50 +00:00
|
|
|
for _, val := range value {
|
|
|
|
e.Encode(val)
|
|
|
|
}
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case []uint64:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bARR)
|
2016-05-17 23:55:50 +00:00
|
|
|
for _, val := range value {
|
|
|
|
e.Encode(val)
|
|
|
|
}
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case []float32:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bARR)
|
2016-05-17 23:55:50 +00:00
|
|
|
for _, val := range value {
|
|
|
|
e.Encode(val)
|
|
|
|
}
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case []float64:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bARR)
|
2016-05-17 23:55:50 +00:00
|
|
|
for _, val := range value {
|
|
|
|
e.Encode(val)
|
|
|
|
}
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
case []interface{}:
|
|
|
|
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bARR)
|
2016-05-17 23:55:50 +00:00
|
|
|
for _, val := range value {
|
|
|
|
e.Encode(val)
|
|
|
|
}
|
2016-10-14 06:52:00 +00:00
|
|
|
e.w.write(bEND)
|
2016-05-17 23:55:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|