Skip to content

Commit

Permalink
Merge pull request #5 from shockerli/feat-new-func
Browse files Browse the repository at this point in the history
`StringMapE` support JSON for []byte
  • Loading branch information
shockerli authored Jan 5, 2022
2 parents 44e7e8c + 6f83711 commit 0ba7150
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 36 additions & 1 deletion int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down
18 changes: 18 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit 0ba7150

Please sign in to comment.