Fix unexported Unmarshal

This commit is contained in:
Adam Scarr 2017-08-11 00:10:58 +10:00
parent 60dd11deee
commit c160e565ba
3 changed files with 10 additions and 9 deletions

View File

@ -36,6 +36,7 @@ func init() {
_value = Any(_null, _true, _false, _string, _number, _array, _object) _value = Any(_null, _true, _false, _string, _number, _array, _object)
} }
func unmarshal(input string) (interface{}, error) { // Unmarshall json string into map[string]interface{} or []interface{}
func Unmarshal(input string) (interface{}, error) {
return Run(_value, input, ASCIIWhitespace) return Run(_value, input, ASCIIWhitespace)
} }

View File

@ -11,31 +11,31 @@ import (
func TestUnmarshal(t *testing.T) { func TestUnmarshal(t *testing.T) {
t.Run("basic types", func(t *testing.T) { t.Run("basic types", func(t *testing.T) {
result, err := unmarshal(`true`) result, err := Unmarshal(`true`)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, true, result) require.Equal(t, true, result)
result, err = unmarshal(`false`) result, err = Unmarshal(`false`)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, false, result) require.Equal(t, false, result)
result, err = unmarshal(`null`) result, err = Unmarshal(`null`)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, nil, result) require.Equal(t, nil, result)
result, err = unmarshal(`"true"`) result, err = Unmarshal(`"true"`)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, "true", result) require.Equal(t, "true", result)
}) })
t.Run("array", func(t *testing.T) { t.Run("array", func(t *testing.T) {
result, err := unmarshal(`[true, null, false, -1.23e+4]`) result, err := Unmarshal(`[true, null, false, -1.23e+4]`)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, []interface{}{true, nil, false, -1.23e+4}, result) require.Equal(t, []interface{}{true, nil, false, -1.23e+4}, result)
}) })
t.Run("object", func(t *testing.T) { t.Run("object", func(t *testing.T) {
result, err := unmarshal(`{"true":true, "false":false, "null": null, "number": 404} `) result, err := Unmarshal(`{"true":true, "false":false, "null": null, "number": 404} `)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, map[string]interface{}{"true": true, "false": false, "null": nil, "number": int64(404)}, result) require.Equal(t, map[string]interface{}{"true": true, "false": false, "null": nil, "number": int64(404)}, result)
}) })
@ -54,7 +54,7 @@ func BenchmarkUnmarshalParsec(b *testing.B) {
func BenchmarkUnmarshalParsify(b *testing.B) { func BenchmarkUnmarshalParsify(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_, err := unmarshal(benchmarkString) _, err := Unmarshal(benchmarkString)
require.NoError(b, err) require.NoError(b, err)
} }
goparsify.DumpDebugStats() goparsify.DumpDebugStats()

View File

@ -48,7 +48,7 @@ func main() {
} }
for i := 0; i < max; i++ { for i := 0; i < max; i++ {
_, err := json.unmarshal(benchmarkString) _, err := json.Unmarshal(benchmarkString)
if err != nil { if err != nil {
panic(err) panic(err)
} }