gabs icon indicating copy to clipboard operation
gabs copied to clipboard

Inconsistency Between .Wrap() and .New() Containers Causes Delete() to fail

Open rflandau opened this issue 1 year ago • 0 comments

There appears to be an under-the-hood inconsistency between a Wrap()-derived container and a New()-derived container. This causes both .Delete() and .DeleteP() to fail when called on Wrap()-derived containers.

Clock the following samples:

New

jObj := gabs.New()

jObj.Set(1, "A")
jObj.SetP(2, "B")

if err := jObj.Delete("B"); err != nil {
	panic(err)
}

fmt.Println(jObj.String())

This works as intended, returning {"A":1}.

Wrap

type t struct {
	A int
	B int
}

wrap := gabs.Wrap(t{A: 1, B: 2})

if err := wrap.Delete("B"); err != nil {
	panic(err)
}

fmt.Println(wrap)

This, however, panics, returning a 'not an object or array' error.


The difference occurs at gabs.go:471. The New snippet has an actual map to operate on, containing map[string]interface {} {"A": 1, "B": 2} beneath the hood. The Wrap container, however, is just data{A: 1, B: 2} and therefore fails the map type assertion.

I currently have a PR in the works to fix this.

rflandau avatar Jun 06 '24 20:06 rflandau