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

Appending to an object in an array does not work #98

Open
geoynomous opened this issue Apr 12, 2021 · 4 comments
Open

Appending to an object in an array does not work #98

geoynomous opened this issue Apr 12, 2021 · 4 comments

Comments

@geoynomous
Copy link

    // Create JSON with "outer" array field
    jsonObj := gabs.New()
    jsonObj.Array("outer")

    // Create JSON with "inner" array field
    innerObj := gabs.New()
    innerObj.ArrayOfSize(0, "inner")

    // Append to inner
    jsonObj.ArrayAppend(innerObj, "outer");

    // Create data JSON
    data := gabs.New()
    data.Set(1, "value")

    // Create a jsonpath
    jsonPath := "outer.0.inner"

    // Count array size of path
    counterInner, _ := jsonObj.ArrayCountP(jsonPath)
    log.Print("#Count BEFORE: ", counterInner)

    // Appent to path (inner)
    jsonObj.ArrayAppendP(data, jsonPath)

    // Count again
    counterInner, _ = jsonObj.ArrayCountP(jsonPath)
    log.Print("#Count AFTER : ", counterInner)
    // BOOM! Nothing is added

It is similar to #91 - please fix it!

@geoynomous
Copy link
Author

Here is a fix!

diff --git a/gabs.go b/gabs.go
index c75e8de..7c649e6 100644
--- a/gabs.go
+++ b/gabs.go
@@ -143,6 +143,8 @@ func (g *Container) searchStrict(allowWildcard bool, hierarchy ...string) (*Cont
                        if !ok {
                                return nil, fmt.Errorf("failed to resolve path segment '%v': key '%v' was not found", target, pathSeg)
                        }
+               } else if g2, ok := object.(*Container); ok {
+                       return g2.searchStrict(false, hierarchy[target:]...)
                } else if marray, ok := object.([]interface{}); ok {
                        if allowWildcard && pathSeg == "*" {
                                tmpArray := []interface{}{}
@@ -308,6 +310,8 @@ func (g *Container) Set(value interface{}, hierarchy ...string) (*Container, err
                                mmap[pathSeg] = map[string]interface{}{}
                                object = mmap[pathSeg]
                        }
+               } else if g2, ok := object.(*Container); ok {
+                       return g2.Set(value, hierarchy[target:]...)
                } else if marray, ok := object.([]interface{}); ok {
                        if pathSeg == "-" {
                                if target < 1 {

@geoynomous
Copy link
Author

is this read?

@Jeffail
Copy link
Owner

Jeffail commented Apr 21, 2021

Hey @geoynomous, sorry, I've not been very attentive to this repo. The line jsonObj.ArrayAppend(innerObj, "outer") needs to insert the underlying array rather than a gabs container, so it should be jsonObj.ArrayAppend(innerObj.Data(), "outer"), and the same goes for jsonObj.ArrayAppendP(data, jsonPath), which should be jsonObj.ArrayAppendP(data.Data(), jsonPath).

You will likely also need to rearrange some of this so that mutations of arrays are performed before inserting the array into a parent object, since arrays are reference types but the reference might change during append operations.

@geoynomous
Copy link
Author

Thanks - that works .. but still I think it's a bug - as the execution finishes without an error reported, but no effect takes places. So either create an error - or apply the fix from above - or if it's a gabs container given call .Data() on it and proceed with that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants