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

help #138

Open
chuckchann opened this issue May 31, 2023 · 3 comments
Open

help #138

chuckchann opened this issue May 31, 2023 · 3 comments
Labels

Comments

@chuckchann
Copy link

this is my code:

func main() {
data := map[string]interface{}{
"name": "Alice",
"age": 30,
"hobbies": []string{
"reading",
"swimming",
"running",
},
}

obj := objx.Map(data)
obj = obj.Set("hobbies[0]", "coding")

fmt.Println("after set -> ", obj)

}

my result is: after set -> map[age:30 hobbies:sssss name:Alice]

what I expect is: after set -> map[age:30 hobbies:[coding, swimming, running] name:Alice]

it seems that there is a bug in obj.Set function when I want to modify a value in the array?

@chuckchann
Copy link
Author

somebody help ?

@hanzei hanzei added the question label Jun 1, 2023
@geseq
Copy link
Collaborator

geseq commented Jun 4, 2023

what version are you using? I just added this test for a quick check on latest version and it passes fine


func TestSetArray(t *testing.T) {
	data := map[string]interface{}{
		"name": "Alice",
		"age":  30,
		"hobbies": []string{
			"reading",
			"swimming",
			"running",
		},
	}

	d := objx.Map(data)
	d = d.Set("hobbies[0]", "coding")
	assert.Equal(t, "coding", d.Get("hobbies[0]").String())
}

@chuckchann
Copy link
Author

what version are you using? I just added this test for a quick check on latest version and it passes fine


func TestSetArray(t *testing.T) {
	data := map[string]interface{}{
		"name": "Alice",
		"age":  30,
		"hobbies": []string{
			"reading",
			"swimming",
			"running",
		},
	}

	d := objx.Map(data)
	d = d.Set("hobbies[0]", "coding")
	assert.Equal(t, "coding", d.Get("hobbies[0]").String())
}

my version is v0.5.0
in your test case, the type of struct field( "hobbies") has been change to string, you can print the object "d", here is my print result:

"map[age:30 hobbies:coding name:Alice]"

that`s why your test case passes fine

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

No branches or pull requests

3 participants