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

Match Wrap container to comparable New container #142

Open
wants to merge 2 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
6 changes: 5 additions & 1 deletion gabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,11 @@ func New() *Container {
// Wrap an already unmarshalled JSON object (or a new map[string]interface{})
// into a *Container.
func Wrap(root interface{}) *Container {
return &Container{root}
b, _ := json.Marshal(root)
var m map[string]interface{}
json.Unmarshal(b, &m)

return &Container{m}
}

// ParseJSON unmarshals a JSON byte slice into a *Container.
Expand Down
44 changes: 44 additions & 0 deletions gabs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,50 @@ func TestFlattenIncludeEmpty(t *testing.T) {
}
}

func TestWrapAgainstNew(t *testing.T) {
exp := New()

exp.Set(1, "A")
exp.SetP(2, "B")
exp.SetP("3", "Alpha.Beta")
exp.SetP(3.14, "Alpha.Gamma")

if err := exp.Delete("B"); err != nil {
t.Error(err)
}
if err := exp.DeleteP("Alpha.Beta"); err != nil {
t.Error(err)
}

type toWrap struct {
A int
B int
Alpha struct {
Beta string
Gamma *float64
}
}

gamma := 3.14

act := Wrap(toWrap{A: 1, B: 2, Alpha: struct {
Beta string
Gamma *float64
}{Beta: "3", Gamma: &gamma}})

if err := act.Delete("B"); err != nil {
panic(err)
}
if err := act.DeleteP("Alpha.Beta"); err != nil {
t.Error(err)
}

if exp.String() != act.String() {
t.Errorf("Wrong result: %v != %v", act, exp)
}

}

func BenchmarkChildren(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
Expand Down