mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-14 18:53:35 +00:00
vendor files
This commit is contained in:
278
vendor/github.com/json-iterator/go/extra/fuzzy_decoder.go
generated
vendored
Normal file
278
vendor/github.com/json-iterator/go/extra/fuzzy_decoder.go
generated
vendored
Normal file
@ -0,0 +1,278 @@
|
||||
package extra
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"math"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
"github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
const maxUint = ^uint(0)
|
||||
const maxInt = int(maxUint >> 1)
|
||||
const minInt = -maxInt - 1
|
||||
|
||||
// RegisterFuzzyDecoders decode input from PHP with tolerance.
|
||||
// It will handle string/number auto conversation, and treat empty [] as empty struct.
|
||||
func RegisterFuzzyDecoders() {
|
||||
jsoniter.RegisterExtension(&tolerateEmptyArrayExtension{})
|
||||
jsoniter.RegisterTypeDecoder("string", &fuzzyStringDecoder{})
|
||||
jsoniter.RegisterTypeDecoder("float32", &fuzzyFloat32Decoder{})
|
||||
jsoniter.RegisterTypeDecoder("float64", &fuzzyFloat64Decoder{})
|
||||
jsoniter.RegisterTypeDecoder("int", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(maxInt) || val < float64(minInt) {
|
||||
iter.ReportError("fuzzy decode int", "exceed range")
|
||||
return
|
||||
}
|
||||
*((*int)(ptr)) = int(val)
|
||||
} else {
|
||||
*((*int)(ptr)) = iter.ReadInt()
|
||||
}
|
||||
}})
|
||||
jsoniter.RegisterTypeDecoder("uint", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(maxUint) || val < 0 {
|
||||
iter.ReportError("fuzzy decode uint", "exceed range")
|
||||
return
|
||||
}
|
||||
*((*uint)(ptr)) = uint(val)
|
||||
} else {
|
||||
*((*uint)(ptr)) = iter.ReadUint()
|
||||
}
|
||||
}})
|
||||
jsoniter.RegisterTypeDecoder("int8", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(math.MaxInt8) || val < float64(math.MinInt8) {
|
||||
iter.ReportError("fuzzy decode int8", "exceed range")
|
||||
return
|
||||
}
|
||||
*((*int8)(ptr)) = int8(val)
|
||||
} else {
|
||||
*((*int8)(ptr)) = iter.ReadInt8()
|
||||
}
|
||||
}})
|
||||
jsoniter.RegisterTypeDecoder("uint8", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(math.MaxUint8) || val < 0 {
|
||||
iter.ReportError("fuzzy decode uint8", "exceed range")
|
||||
return
|
||||
}
|
||||
*((*uint8)(ptr)) = uint8(val)
|
||||
} else {
|
||||
*((*uint8)(ptr)) = iter.ReadUint8()
|
||||
}
|
||||
}})
|
||||
jsoniter.RegisterTypeDecoder("int16", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(math.MaxInt16) || val < float64(math.MinInt16) {
|
||||
iter.ReportError("fuzzy decode int16", "exceed range")
|
||||
return
|
||||
}
|
||||
*((*int16)(ptr)) = int16(val)
|
||||
} else {
|
||||
*((*int16)(ptr)) = iter.ReadInt16()
|
||||
}
|
||||
}})
|
||||
jsoniter.RegisterTypeDecoder("uint16", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(math.MaxUint16) || val < 0 {
|
||||
iter.ReportError("fuzzy decode uint16", "exceed range")
|
||||
return
|
||||
}
|
||||
*((*uint16)(ptr)) = uint16(val)
|
||||
} else {
|
||||
*((*uint16)(ptr)) = iter.ReadUint16()
|
||||
}
|
||||
}})
|
||||
jsoniter.RegisterTypeDecoder("int32", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(math.MaxInt32) || val < float64(math.MinInt32) {
|
||||
iter.ReportError("fuzzy decode int32", "exceed range")
|
||||
return
|
||||
}
|
||||
*((*int32)(ptr)) = int32(val)
|
||||
} else {
|
||||
*((*int32)(ptr)) = iter.ReadInt32()
|
||||
}
|
||||
}})
|
||||
jsoniter.RegisterTypeDecoder("uint32", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(math.MaxUint32) || val < 0 {
|
||||
iter.ReportError("fuzzy decode uint32", "exceed range")
|
||||
return
|
||||
}
|
||||
*((*uint32)(ptr)) = uint32(val)
|
||||
} else {
|
||||
*((*uint32)(ptr)) = iter.ReadUint32()
|
||||
}
|
||||
}})
|
||||
jsoniter.RegisterTypeDecoder("int64", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(math.MaxInt64) || val < float64(math.MinInt64) {
|
||||
iter.ReportError("fuzzy decode int64", "exceed range")
|
||||
return
|
||||
}
|
||||
*((*int64)(ptr)) = int64(val)
|
||||
} else {
|
||||
*((*int64)(ptr)) = iter.ReadInt64()
|
||||
}
|
||||
}})
|
||||
jsoniter.RegisterTypeDecoder("uint64", &fuzzyIntegerDecoder{func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if isFloat {
|
||||
val := iter.ReadFloat64()
|
||||
if val > float64(math.MaxUint64) || val < 0 {
|
||||
iter.ReportError("fuzzy decode uint64", "exceed range")
|
||||
return
|
||||
}
|
||||
*((*uint64)(ptr)) = uint64(val)
|
||||
} else {
|
||||
*((*uint64)(ptr)) = iter.ReadUint64()
|
||||
}
|
||||
}})
|
||||
}
|
||||
|
||||
type tolerateEmptyArrayExtension struct {
|
||||
jsoniter.DummyExtension
|
||||
}
|
||||
|
||||
func (extension *tolerateEmptyArrayExtension) DecorateDecoder(typ reflect.Type, decoder jsoniter.ValDecoder) jsoniter.ValDecoder {
|
||||
if typ.Kind() == reflect.Struct || typ.Kind() == reflect.Map {
|
||||
return &tolerateEmptyArrayDecoder{decoder}
|
||||
}
|
||||
return decoder
|
||||
}
|
||||
|
||||
type tolerateEmptyArrayDecoder struct {
|
||||
valDecoder jsoniter.ValDecoder
|
||||
}
|
||||
|
||||
func (decoder *tolerateEmptyArrayDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
if iter.WhatIsNext() == jsoniter.ArrayValue {
|
||||
iter.Skip()
|
||||
newIter := iter.Pool().BorrowIterator([]byte("{}"))
|
||||
defer iter.Pool().ReturnIterator(newIter)
|
||||
decoder.valDecoder.Decode(ptr, newIter)
|
||||
} else {
|
||||
decoder.valDecoder.Decode(ptr, iter)
|
||||
}
|
||||
}
|
||||
|
||||
type fuzzyStringDecoder struct {
|
||||
}
|
||||
|
||||
func (decoder *fuzzyStringDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
valueType := iter.WhatIsNext()
|
||||
switch valueType {
|
||||
case jsoniter.NumberValue:
|
||||
var number json.Number
|
||||
iter.ReadVal(&number)
|
||||
*((*string)(ptr)) = string(number)
|
||||
case jsoniter.StringValue:
|
||||
*((*string)(ptr)) = iter.ReadString()
|
||||
default:
|
||||
iter.ReportError("fuzzyStringDecoder", "not number or string")
|
||||
}
|
||||
}
|
||||
|
||||
type fuzzyIntegerDecoder struct {
|
||||
fun func(isFloat bool, ptr unsafe.Pointer, iter *jsoniter.Iterator)
|
||||
}
|
||||
|
||||
func (decoder *fuzzyIntegerDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
valueType := iter.WhatIsNext()
|
||||
var str string
|
||||
switch valueType {
|
||||
case jsoniter.NumberValue:
|
||||
var number json.Number
|
||||
iter.ReadVal(&number)
|
||||
str = string(number)
|
||||
case jsoniter.StringValue:
|
||||
str = iter.ReadString()
|
||||
case jsoniter.BoolValue:
|
||||
if iter.ReadBool() {
|
||||
str = "1"
|
||||
} else {
|
||||
str = "0"
|
||||
}
|
||||
default:
|
||||
iter.ReportError("fuzzyIntegerDecoder", "not number or string")
|
||||
}
|
||||
newIter := iter.Pool().BorrowIterator([]byte(str))
|
||||
defer iter.Pool().ReturnIterator(newIter)
|
||||
isFloat := strings.IndexByte(str, '.') != -1
|
||||
decoder.fun(isFloat, ptr, newIter)
|
||||
if newIter.Error != nil && newIter.Error != io.EOF {
|
||||
iter.Error = newIter.Error
|
||||
}
|
||||
}
|
||||
|
||||
type fuzzyFloat32Decoder struct {
|
||||
}
|
||||
|
||||
func (decoder *fuzzyFloat32Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
valueType := iter.WhatIsNext()
|
||||
var str string
|
||||
switch valueType {
|
||||
case jsoniter.NumberValue:
|
||||
*((*float32)(ptr)) = iter.ReadFloat32()
|
||||
case jsoniter.StringValue:
|
||||
str = iter.ReadString()
|
||||
newIter := iter.Pool().BorrowIterator([]byte(str))
|
||||
defer iter.Pool().ReturnIterator(newIter)
|
||||
*((*float32)(ptr)) = newIter.ReadFloat32()
|
||||
if newIter.Error != nil && newIter.Error != io.EOF {
|
||||
iter.Error = newIter.Error
|
||||
}
|
||||
case jsoniter.BoolValue:
|
||||
// support bool to float32
|
||||
if iter.ReadBool() {
|
||||
*((*float32)(ptr)) = 1
|
||||
} else {
|
||||
*((*float32)(ptr)) = 0
|
||||
}
|
||||
default:
|
||||
iter.ReportError("fuzzyFloat32Decoder", "not number or string")
|
||||
}
|
||||
}
|
||||
|
||||
type fuzzyFloat64Decoder struct {
|
||||
}
|
||||
|
||||
func (decoder *fuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
valueType := iter.WhatIsNext()
|
||||
var str string
|
||||
switch valueType {
|
||||
case jsoniter.NumberValue:
|
||||
*((*float64)(ptr)) = iter.ReadFloat64()
|
||||
case jsoniter.StringValue:
|
||||
str = iter.ReadString()
|
||||
newIter := iter.Pool().BorrowIterator([]byte(str))
|
||||
defer iter.Pool().ReturnIterator(newIter)
|
||||
*((*float64)(ptr)) = newIter.ReadFloat64()
|
||||
if newIter.Error != nil && newIter.Error != io.EOF {
|
||||
iter.Error = newIter.Error
|
||||
}
|
||||
case jsoniter.BoolValue:
|
||||
// support bool to float64
|
||||
if iter.ReadBool() {
|
||||
*((*float64)(ptr)) = 1
|
||||
} else {
|
||||
*((*float64)(ptr)) = 0
|
||||
}
|
||||
default:
|
||||
iter.ReportError("fuzzyFloat32Decoder", "not number or string")
|
||||
}
|
||||
}
|
359
vendor/github.com/json-iterator/go/extra/fuzzy_decoder_test.go
generated
vendored
Normal file
359
vendor/github.com/json-iterator/go/extra/fuzzy_decoder_test.go
generated
vendored
Normal file
@ -0,0 +1,359 @@
|
||||
package extra
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/json-iterator/go"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterFuzzyDecoders()
|
||||
}
|
||||
|
||||
func Test_any_to_string(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val string
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal("100", val)
|
||||
should.Nil(jsoniter.UnmarshalFromString("10", &val))
|
||||
should.Equal("10", val)
|
||||
should.Nil(jsoniter.UnmarshalFromString("10.1", &val))
|
||||
should.Equal("10.1", val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal("10.1", val)
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
}
|
||||
func Test_any_to_int64(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val int64
|
||||
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(int64(100), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(int64(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(int64(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(int64(10), val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(int64(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(int64(1), val)
|
||||
|
||||
should.Nil(jsoniter.UnmarshalFromString(`-10`, &val))
|
||||
should.Equal(int64(-10), val)
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
|
||||
func Test_any_to_int(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val int
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(100, val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(10, val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(10, val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(10, val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(0, val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(1, val)
|
||||
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
|
||||
func Test_any_to_int16(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val int16
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(int16(100), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(int16(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(int16(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(int16(10), val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(int16(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(int16(1), val)
|
||||
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
|
||||
func Test_any_to_int32(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val int32
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(int32(100), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(int32(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(int32(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(int32(10), val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(int32(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(int32(1), val)
|
||||
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
|
||||
func Test_any_to_int8(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val int8
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(int8(100), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(int8(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(int8(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(int8(10), val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(int8(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(int8(1), val)
|
||||
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
|
||||
func Test_any_to_uint8(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val uint8
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(uint8(100), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(uint8(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(uint8(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(uint8(10), val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(uint8(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(uint8(1), val)
|
||||
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
|
||||
func Test_any_to_uint64(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val uint64
|
||||
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(uint64(100), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(uint64(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(uint64(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(uint64(10), val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(uint64(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(uint64(1), val)
|
||||
|
||||
// TODO fix?
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`-10`, &val))
|
||||
should.Equal(uint64(0), val)
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
func Test_any_to_uint32(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val uint32
|
||||
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(uint32(100), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(uint32(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(uint32(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(uint32(10), val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(uint32(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(uint32(1), val)
|
||||
|
||||
// TODO fix?
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`-10`, &val))
|
||||
should.Equal(uint32(0), val)
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
func Test_any_to_uint16(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val uint16
|
||||
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(uint16(100), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(uint16(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(uint16(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(uint16(10), val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(uint16(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(uint16(1), val)
|
||||
|
||||
// TODO fix?
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`-10`, &val))
|
||||
should.Equal(uint16(0), val)
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
func Test_any_to_uint(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val uint
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(uint(100), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(uint(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(uint(10), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(uint(10), val)
|
||||
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(uint(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(uint(1), val)
|
||||
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
// large float to int
|
||||
should.NotNil(jsoniter.UnmarshalFromString(`1234512345123451234512345.0`, &val))
|
||||
}
|
||||
|
||||
func Test_any_to_float32(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val float32
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(float32(100), val)
|
||||
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(float32(10.1), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(float32(10.1), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(float32(10), val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(float32(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(float32(1), val)
|
||||
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
}
|
||||
|
||||
func Test_any_to_float64(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val float64
|
||||
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"100"`, &val))
|
||||
should.Equal(float64(100), val)
|
||||
|
||||
should.Nil(jsoniter.UnmarshalFromString(`"10.1"`, &val))
|
||||
should.Equal(float64(10.1), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val))
|
||||
should.Equal(float64(10.1), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`10`, &val))
|
||||
should.Equal(float64(10), val)
|
||||
|
||||
// bool part
|
||||
should.Nil(jsoniter.UnmarshalFromString(`false`, &val))
|
||||
should.Equal(float64(0), val)
|
||||
should.Nil(jsoniter.UnmarshalFromString(`true`, &val))
|
||||
should.Equal(float64(1), val)
|
||||
|
||||
should.NotNil(jsoniter.UnmarshalFromString("{}", &val))
|
||||
should.NotNil(jsoniter.UnmarshalFromString("[]", &val))
|
||||
}
|
||||
|
||||
func Test_empty_array_as_map(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val map[string]interface{}
|
||||
should.Nil(jsoniter.UnmarshalFromString(`[]`, &val))
|
||||
should.Equal(map[string]interface{}{}, val)
|
||||
}
|
||||
|
||||
func Test_empty_array_as_object(t *testing.T) {
|
||||
should := require.New(t)
|
||||
var val struct{}
|
||||
should.Nil(jsoniter.UnmarshalFromString(`[]`, &val))
|
||||
should.Equal(struct{}{}, val)
|
||||
}
|
||||
|
||||
func Test_bad_case(t *testing.T) {
|
||||
var jsonstr = `
|
||||
{
|
||||
"extra_type": 181760,
|
||||
"combo_type": 0,
|
||||
"trigger_time_ms": 1498800398000,
|
||||
"_create_time": "2017-06-16 11:21:39",
|
||||
"_msg_type": 41000
|
||||
}
|
||||
`
|
||||
|
||||
type OrderEventRequestParams struct {
|
||||
ExtraType uint64 `json:"extra_type"`
|
||||
}
|
||||
|
||||
var a OrderEventRequestParams
|
||||
err := jsoniter.UnmarshalFromString(jsonstr, &a)
|
||||
should := require.New(t)
|
||||
should.Nil(err)
|
||||
}
|
41
vendor/github.com/json-iterator/go/extra/naming_strategy.go
generated
vendored
Normal file
41
vendor/github.com/json-iterator/go/extra/naming_strategy.go
generated
vendored
Normal file
@ -0,0 +1,41 @@
|
||||
package extra
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// SetNamingStrategy rename struct fields uniformly
|
||||
func SetNamingStrategy(translate func(string) string) {
|
||||
jsoniter.RegisterExtension(&namingStrategyExtension{jsoniter.DummyExtension{}, translate})
|
||||
}
|
||||
|
||||
type namingStrategyExtension struct {
|
||||
jsoniter.DummyExtension
|
||||
translate func(string) string
|
||||
}
|
||||
|
||||
func (extension *namingStrategyExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) {
|
||||
for _, binding := range structDescriptor.Fields {
|
||||
binding.ToNames = []string{extension.translate(binding.Field.Name)}
|
||||
binding.FromNames = []string{extension.translate(binding.Field.Name)}
|
||||
}
|
||||
}
|
||||
|
||||
// LowerCaseWithUnderscores one strategy to SetNamingStrategy for. It will change HelloWorld to hello_world.
|
||||
func LowerCaseWithUnderscores(name string) string {
|
||||
newName := []rune{}
|
||||
for i, c := range name {
|
||||
if i == 0 {
|
||||
newName = append(newName, unicode.ToLower(c))
|
||||
} else {
|
||||
if unicode.IsUpper(c) {
|
||||
newName = append(newName, '_')
|
||||
newName = append(newName, unicode.ToLower(c))
|
||||
} else {
|
||||
newName = append(newName, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
return string(newName)
|
||||
}
|
23
vendor/github.com/json-iterator/go/extra/naming_strategy_test.go
generated
vendored
Normal file
23
vendor/github.com/json-iterator/go/extra/naming_strategy_test.go
generated
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
package extra
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_lower_case_with_underscores(t *testing.T) {
|
||||
should := require.New(t)
|
||||
should.Equal("hello_world", LowerCaseWithUnderscores("helloWorld"))
|
||||
should.Equal("hello_world", LowerCaseWithUnderscores("HelloWorld"))
|
||||
SetNamingStrategy(LowerCaseWithUnderscores)
|
||||
output, err := jsoniter.Marshal(struct {
|
||||
UserName string
|
||||
FirstLanguage string
|
||||
}{
|
||||
UserName: "taowen",
|
||||
FirstLanguage: "Chinese",
|
||||
})
|
||||
should.Nil(err)
|
||||
should.Equal(`{"user_name":"taowen","first_language":"Chinese"}`, string(output))
|
||||
}
|
25
vendor/github.com/json-iterator/go/extra/privat_fields.go
generated
vendored
Normal file
25
vendor/github.com/json-iterator/go/extra/privat_fields.go
generated
vendored
Normal file
@ -0,0 +1,25 @@
|
||||
package extra
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// SupportPrivateFields include private fields when encoding/decoding
|
||||
func SupportPrivateFields() {
|
||||
jsoniter.RegisterExtension(&privateFieldsExtension{})
|
||||
}
|
||||
|
||||
type privateFieldsExtension struct {
|
||||
jsoniter.DummyExtension
|
||||
}
|
||||
|
||||
func (extension *privateFieldsExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) {
|
||||
for _, binding := range structDescriptor.Fields {
|
||||
isPrivate := unicode.IsLower(rune(binding.Field.Name[0]))
|
||||
if isPrivate {
|
||||
binding.FromNames = []string{binding.Field.Name}
|
||||
binding.ToNames = []string{binding.Field.Name}
|
||||
}
|
||||
}
|
||||
}
|
18
vendor/github.com/json-iterator/go/extra/private_fields_test.go
generated
vendored
Normal file
18
vendor/github.com/json-iterator/go/extra/private_fields_test.go
generated
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
package extra
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_private_fields(t *testing.T) {
|
||||
type TestObject struct {
|
||||
field1 string
|
||||
}
|
||||
SupportPrivateFields()
|
||||
should := require.New(t)
|
||||
obj := TestObject{}
|
||||
should.Nil(jsoniter.UnmarshalFromString(`{"field1":"Hello"}`, &obj))
|
||||
should.Equal("Hello", obj.field1)
|
||||
}
|
34
vendor/github.com/json-iterator/go/extra/time_as_int64_codec.go
generated
vendored
Normal file
34
vendor/github.com/json-iterator/go/extra/time_as_int64_codec.go
generated
vendored
Normal file
@ -0,0 +1,34 @@
|
||||
package extra
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// RegisterTimeAsInt64Codec encode/decode time since number of unit since epoch. the precision is the unit.
|
||||
func RegisterTimeAsInt64Codec(precision time.Duration) {
|
||||
jsoniter.RegisterTypeEncoder("time.Time", &timeAsInt64Codec{precision})
|
||||
jsoniter.RegisterTypeDecoder("time.Time", &timeAsInt64Codec{precision})
|
||||
}
|
||||
|
||||
type timeAsInt64Codec struct {
|
||||
precision time.Duration
|
||||
}
|
||||
|
||||
func (codec *timeAsInt64Codec) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||
nanoseconds := iter.ReadInt64() * codec.precision.Nanoseconds()
|
||||
*((*time.Time)(ptr)) = time.Unix(0, nanoseconds)
|
||||
}
|
||||
|
||||
func (codec *timeAsInt64Codec) IsEmpty(ptr unsafe.Pointer) bool {
|
||||
ts := *((*time.Time)(ptr))
|
||||
return ts.UnixNano() == 0
|
||||
}
|
||||
func (codec *timeAsInt64Codec) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
|
||||
ts := *((*time.Time)(ptr))
|
||||
stream.WriteInt64(ts.UnixNano() / codec.precision.Nanoseconds())
|
||||
}
|
||||
func (codec *timeAsInt64Codec) EncodeInterface(val interface{}, stream *jsoniter.Stream) {
|
||||
jsoniter.WriteToStream(val, stream, codec)
|
||||
}
|
31
vendor/github.com/json-iterator/go/extra/time_as_int64_codec_test.go
generated
vendored
Normal file
31
vendor/github.com/json-iterator/go/extra/time_as_int64_codec_test.go
generated
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
package extra
|
||||
|
||||
import (
|
||||
"github.com/json-iterator/go"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Test_time_as_int64(t *testing.T) {
|
||||
should := require.New(t)
|
||||
RegisterTimeAsInt64Codec(time.Nanosecond)
|
||||
output, err := jsoniter.Marshal(time.Unix(1497952257, 1002))
|
||||
should.Nil(err)
|
||||
should.Equal("1497952257000001002", string(output))
|
||||
var val time.Time
|
||||
should.Nil(jsoniter.Unmarshal(output, &val))
|
||||
should.Equal(int64(1497952257000001002), val.UnixNano())
|
||||
}
|
||||
|
||||
func Test_time_as_int64_keep_microsecond(t *testing.T) {
|
||||
t.Skip("conflict")
|
||||
should := require.New(t)
|
||||
RegisterTimeAsInt64Codec(time.Microsecond)
|
||||
output, err := jsoniter.Marshal(time.Unix(1, 1002))
|
||||
should.Nil(err)
|
||||
should.Equal("1000001", string(output))
|
||||
var val time.Time
|
||||
should.Nil(jsoniter.Unmarshal(output, &val))
|
||||
should.Equal(int64(1000001000), val.UnixNano())
|
||||
}
|
Reference in New Issue
Block a user