From eafca7ca0b822d6554facdeed2fa720e195b719d Mon Sep 17 00:00:00 2001 From: R Landau Date: Thu, 6 Jun 2024 16:40:36 -0400 Subject: [PATCH 1/2] exploit json/encoding to wrap inbound data in map[string]interface{} --- gabs.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gabs.go b/gabs.go index b846954..01fbaff 100644 --- a/gabs.go +++ b/gabs.go @@ -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. From 1acefbd2e781f63c81f2c1c94f4cac4a68370020 Mon Sep 17 00:00:00 2001 From: R Landau Date: Thu, 6 Jun 2024 16:40:54 -0400 Subject: [PATCH 2/2] simple tests for Wrap == New --- gabs_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/gabs_test.go b/gabs_test.go index f8429e5..2badfb5 100644 --- a/gabs_test.go +++ b/gabs_test.go @@ -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++ {