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

Container.Set does not take effect on parent node if param "hierarchy" is empty #118

Closed
Asterism12 opened this issue Apr 15, 2022 · 5 comments

Comments

@Asterism12
Copy link

Asterism12 commented Apr 15, 2022

func test() {
	bs := []byte(`{"name":{"first":"jim","last":"raynor"}}`)

	data, _ := gabs.ParseJSON(bs)
	_, _ = data.Set("bond", "name", "last")
	fmt.Println(data.String())
	// output : {"name":{"first":"jim","last":"bond"}}

	data1, _ := gabs.ParseJSON(bs)
	data11 := data1.S("name")
	_, _ = data11.Set("bond", "last")
	fmt.Println(data1.String())
	// output : {"name":{"first":"jim","last":"bond"}}

	data2, _ := gabs.ParseJSON(bs)
	data22 := data2.S("name", "last")
	_, _ = data22.Set("bond")
	fmt.Println(data2.String())
	// output : {"name":{"first":"jim","last":"raynor"}}
}
  • env
    go 1.17

  • What happened ?
    data2 has not been changed.

  • What is expected ?
    data2 has been changed as same as data and data1.

@Asterism12 Asterism12 changed the title Container.Set doesn't work if hierarchy is empty Container.Set does not take effect on parent node if param "hierarchy" is empty Apr 15, 2022
@Asterism12
Copy link
Author

func test2() {
	bs := []byte(`["james","raynor"]`)
	data, _ := gabs.ParseJSON(bs)
	ds := data.Children()
	for i := range ds {
		nv, _ := ds[i].Set("bond")
		ds[i] = nv
	}
	fmt.Println(data.String())
	// output : ["james","raynor"]
}

It is troublesome to iterate arrays.

@mihaitodor
Copy link
Contributor

mihaitodor commented Apr 17, 2022

Hey, thanks for trying Gabs! I think this relates to #91.

For your first example, data22.Set("bond") doesn't change data2, so you'll need to do data2.Set(data22, "name", "last") before printing data2.

For your second example, try this:

func test2() {
	bs := []byte(`["james","raynor"]`)
	data, _ := gabs.ParseJSON(bs)
	ds := data.Children()
	for i := range ds {
		nv, _ := ds[i].Set("bond")
		_, _ = data.SetIndex(nv.Data(), i)
	}
	fmt.Println(data.String())
	// output : ["bond","bond"]
}

Hope this helps.

@Asterism12
Copy link
Author

Thank you a lot for response! Your advice to the second example is helpful.

For the first example, what confused to me is data11.Set("bond", "last") will change value of data1, but data22.Set("bond") will not change value of data2.

@mihaitodor
Copy link
Contributor

mihaitodor commented Apr 18, 2022

You're welcome! Yeah, I agree, there are some inconsistencies in the API as mentioned in the issue I linked to. Not sure if there are any plans to address them for the time being.

@Asterism12
Copy link
Author

I got it. Thanks!

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