diff --git a/README.md b/README.md index 4998c0a..9e86053 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,6 @@ [![PkgGoDev](https://pkg.go.dev/badge/github.com/shockerli/cvt)](https://pkg.go.dev/github.com/shockerli/cvt) [![Go Report Card](https://goreportcard.com/badge/github.com/shockerli/cvt)](https://goreportcard.com/report/github.com/shockerli/cvt) [![Build Status](https://travis-ci.com/shockerli/cvt.svg?branch=master)](https://travis-ci.com/shockerli/cvt) -![GitHub top language](https://img.shields.io/github/languages/top/shockerli/cvt) [![codecov](https://codecov.io/gh/shockerli/cvt/branch/master/graph/badge.svg)](https://codecov.io/gh/shockerli/cvt) ![GitHub](https://img.shields.io/github/license/shockerli/cvt) [![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go) diff --git a/README_ZH.md b/README_ZH.md index 03d12e7..873d6ad 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -3,7 +3,6 @@ [![PkgGoDev](https://pkg.go.dev/badge/github.com/shockerli/cvt)](https://pkg.go.dev/github.com/shockerli/cvt) [![Go Report Card](https://goreportcard.com/badge/github.com/shockerli/cvt)](https://goreportcard.com/report/github.com/shockerli/cvt) [![Build Status](https://travis-ci.com/shockerli/cvt.svg?branch=master)](https://travis-ci.com/shockerli/cvt) -![GitHub top language](https://img.shields.io/github/languages/top/shockerli/cvt) [![codecov](https://codecov.io/gh/shockerli/cvt/branch/master/graph/badge.svg)](https://codecov.io/gh/shockerli/cvt) ![GitHub](https://img.shields.io/github/license/shockerli/cvt) [![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go) diff --git a/int_test.go b/int_test.go index a0d8214..c1be868 100644 --- a/int_test.go +++ b/int_test.go @@ -977,6 +977,8 @@ func TestUint64E(t *testing.T) { {"8", 8, false}, {"8.00", 8, false}, {"8.01", 8, false}, + {"00100", 100, false}, + {"00100.01", 100, false}, {[]byte("8"), 8, false}, {[]byte("8.00"), 8, false}, {[]byte("8.01"), 8, false}, @@ -1098,6 +1100,8 @@ func TestUint32E(t *testing.T) { {"8", 8, false}, {"8.00", 8, false}, {"8.01", 8, false}, + {"00100", 100, false}, + {"00100.01", 100, false}, {nil, 0, false}, {aliasTypeInt0, 0, false}, {&aliasTypeInt0, 0, false}, @@ -1212,6 +1216,8 @@ func TestUint16E(t *testing.T) { {"8", 8, false}, {"8.00", 8, false}, {"8.01", 8, false}, + {"00100", 100, false}, + {"00100.01", 100, false}, {nil, 0, false}, {aliasTypeInt0, 0, false}, {&aliasTypeInt0, 0, false}, @@ -1328,6 +1334,8 @@ func TestUint8E(t *testing.T) { {"8", 8, false}, {"8.00", 8, false}, {"8.01", 8, false}, + {"00100", 100, false}, + {"00100.01", 100, false}, {nil, 0, false}, {aliasTypeInt0, 0, false}, {&aliasTypeInt0, 0, false}, @@ -1444,6 +1452,8 @@ func TestUintE(t *testing.T) { {"8", 8, false}, {"8.00", 8, false}, {"8.01", 8, false}, + {"00100", 100, false}, + {"00100.01", 100, false}, {uint64(math.MaxUint32), uint(math.MaxUint32), false}, {uint32(math.MaxUint32), uint(math.MaxUint32), false}, {nil, 0, false}, @@ -1567,11 +1577,17 @@ func TestInt64E(t *testing.T) { {"8", 8, false}, {"8.00", 8, false}, {"8.01", 8, false}, + {"00100", 100, false}, + {"-00100", -100, false}, + {"00100.00", 100, false}, + {"-00100.01", -100, false}, {[]byte("-8"), -8, false}, {[]byte("-8.01"), -8, false}, {[]byte("8"), 8, false}, {[]byte("8.00"), 8, false}, {[]byte("8.01"), 8, false}, + {math.MaxInt64, math.MaxInt64, false}, + {math.MinInt64, math.MinInt64, false}, {uint32(math.MaxUint32), int64(math.MaxUint32), false}, {nil, 0, false}, {aliasTypeInt0, 0, false}, @@ -1689,12 +1705,17 @@ func TestInt32E(t *testing.T) { {"8", 8, false}, {"8.00", 8, false}, {"8.01", 8, false}, + {"00100", 100, false}, + {"-00100", -100, false}, + {"00100.00", 100, false}, + {"-00100.01", -100, false}, {[]byte("-8"), -8, false}, {[]byte("-8.01"), -8, false}, {[]byte("8"), 8, false}, {[]byte("8.00"), 8, false}, {[]byte("8.01"), 8, false}, {math.MaxInt32, int32(math.MaxInt32), false}, + {math.MinInt32, int32(math.MinInt32), false}, {nil, 0, false}, {aliasTypeInt0, 0, false}, {&aliasTypeInt0, 0, false}, @@ -1808,12 +1829,17 @@ func TestInt16E(t *testing.T) { {"8", 8, false}, {"8.00", 8, false}, {"8.01", 8, false}, + {"00100", 100, false}, + {"-00100", -100, false}, + {"00100.00", 100, false}, + {"-00100.01", -100, false}, {[]byte("-8"), -8, false}, {[]byte("-8.01"), -8, false}, {[]byte("8"), 8, false}, {[]byte("8.00"), 8, false}, {[]byte("8.01"), 8, false}, {math.MaxInt16, int16(math.MaxInt16), false}, + {math.MinInt16, int16(math.MinInt16), false}, {nil, 0, false}, {aliasTypeInt0, 0, false}, {&aliasTypeInt0, 0, false}, @@ -1929,12 +1955,17 @@ func TestInt8E(t *testing.T) { {"8", 8, false}, {"8.00", 8, false}, {"8.01", 8, false}, + {"00100", 100, false}, + {"-00100", -100, false}, + {"00100.00", 100, false}, + {"-00100.01", -100, false}, {[]byte("-8"), -8, false}, {[]byte("-8.01"), -8, false}, {[]byte("8"), 8, false}, {[]byte("8.00"), 8, false}, {[]byte("8.01"), 8, false}, {int8(math.MaxInt8), math.MaxInt8, false}, + {int8(math.MinInt8), math.MinInt8, false}, {nil, 0, false}, {aliasTypeInt0, 0, false}, {&aliasTypeInt0, 0, false}, @@ -2051,12 +2082,16 @@ func TestIntE(t *testing.T) { {"8", 8, false}, {"8.00", 8, false}, {"8.01", 8, false}, + {"00100", 100, false}, + {"-00100", -100, false}, + {"00100.00", 100, false}, + {"-00100.01", -100, false}, {[]byte("-8"), -8, false}, {[]byte("-8.01"), -8, false}, {[]byte("8"), 8, false}, {[]byte("8.00"), 8, false}, {[]byte("8.01"), 8, false}, - {int(math.MaxInt32), int(math.MaxInt32), false}, + {math.MaxInt32, math.MaxInt32, false}, {nil, 0, false}, {aliasTypeInt0, 0, false}, {&aliasTypeInt0, 0, false}, diff --git a/map.go b/map.go index 1eafb81..2e647b2 100644 --- a/map.go +++ b/map.go @@ -15,6 +15,19 @@ func StringMapE(val interface{}) (m map[string]interface{}, err error) { return nil, errUnsupportedTypeNil } + // direct type(for improve performance) + switch v := val.(type) { + case map[string]interface{}: + return v, nil + case []byte: + err = json.Unmarshal(v, &m) + return + case string: + err = json.Unmarshal([]byte(v), &m) + return + } + + // indirect type _, rv := indirect(val) switch rv.Kind() { case reflect.Map: @@ -23,6 +36,11 @@ func StringMapE(val interface{}) (m map[string]interface{}, err error) { } case reflect.Struct: m = struct2map(rv) + case reflect.Slice: + // []byte + if rv.Type().Elem().Kind() == reflect.Uint8 { + err = json.Unmarshal(rv.Bytes(), &m) + } case reflect.String: // JSON string of map err = json.Unmarshal([]byte(rv.String()), &m) diff --git a/map_test.go b/map_test.go index 2e669dc..c0f470f 100644 --- a/map_test.go +++ b/map_test.go @@ -12,10 +12,13 @@ func TestStringMapE(t *testing.T) { expect map[string]interface{} isErr bool }{ - // JSON String + // JSON {`{"name":"cvt","age":3.21}`, map[string]interface{}{"name": "cvt", "age": 3.21}, false}, {`{"name":"cvt","tag":"convert"}`, map[string]interface{}{"name": "cvt", "tag": "convert"}, false}, {`{"name":"cvt","build":true}`, map[string]interface{}{"name": "cvt", "build": true}, false}, + {[]byte(`{"name":"cvt","build":true}`), map[string]interface{}{"name": "cvt", "build": true}, false}, + {AliasTypeString(`{"name":"cvt","build":true}`), map[string]interface{}{"name": "cvt", "build": true}, false}, + {AliasTypeBytes(`{"name":"cvt","build":true}`), map[string]interface{}{"name": "cvt", "build": true}, false}, // Map {map[string]interface{}{}, map[string]interface{}{}, false},