Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inner map can be pointer to map #49

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion simplejson.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package simplejson
import (
"encoding/json"
"errors"
"fmt"
"log"
)

Expand Down Expand Up @@ -33,6 +34,13 @@ func New() *Json {
}
}

// NewFromMap returns a pointer to a new `Json` object initialized by `data`
func NewFromMap(data map[string]interface{}) *Json {
return &Json{
data: data,
}
}

// Interface returns the underlying data
func (j *Json) Interface() interface{} {
return j.data
Expand Down Expand Up @@ -176,7 +184,13 @@ func (j *Json) Map() (map[string]interface{}, error) {
if m, ok := (j.data).(map[string]interface{}); ok {
return m, nil
}
return nil, errors.New("type assertion to map[string]interface{} failed")
if m, ok := (j.data).(*map[string]interface{}); ok {
return *m, nil
}
if m, ok := (j.data).(*Json); ok {
return m.MustMap(), nil
}
return nil, errors.New(fmt.Sprintf("type assertion to map[string]interface{} failed %T", j.data))
}

// Array type asserts to an `array`
Expand Down
4 changes: 2 additions & 2 deletions simplejson_go10.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (j *Json) Int() (int, error) {
case uint, uint8, uint16, uint32, uint64:
return int(reflect.ValueOf(j.data).Uint()), nil
}
return 0, errors.New("invalid value type")
return 0, errors.New("invalid value type %T", j.data)
}

// Int64 coerces into an int64
Expand All @@ -58,7 +58,7 @@ func (j *Json) Int64() (int64, error) {
case uint, uint8, uint16, uint32, uint64:
return int64(reflect.ValueOf(j.data).Uint()), nil
}
return 0, errors.New("invalid value type")
return 0, errors.New("invalid value type %T", j.data)
}

// Uint64 coerces into an uint64
Expand Down
3 changes: 2 additions & 1 deletion simplejson_go10_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ package simplejson

import (
"bytes"
"github.com/bmizerany/assert"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewFromReader(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion simplejson_go11_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ package simplejson
import (
"bytes"
"encoding/json"
"github.com/bmizerany/assert"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewFromReader(t *testing.T) {
Expand Down
9 changes: 8 additions & 1 deletion simplejson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"testing"

"github.com/bmizerany/assert"
"github.com/stretchr/testify/assert"
)

func TestSimplejson(t *testing.T) {
Expand Down Expand Up @@ -246,3 +246,10 @@ func TestPathWillOverwriteExisting(t *testing.T) {
assert.Equal(t, nil, err)
assert.Equal(t, "bar", s)
}

func TestInnerMapCanBePointer(t *testing.T) {
inner := map[string]interface{}{"key": "value"}
data := map[string]interface{}{"key": &inner}
js := NewFromMap(data)
assert.Equal(t, "value", js.Get("key").Get("key").MustString())
}