objx icon indicating copy to clipboard operation
objx copied to clipboard

help

Open chuckchann opened this issue 2 years ago • 3 comments

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 avatar May 31 '23 13:05 chuckchann

somebody help ?

chuckchann avatar Jun 01 '23 06:06 chuckchann

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())
}

geseq avatar Jun 04 '23 08:06 geseq

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

chuckchann avatar Jun 05 '23 02:06 chuckchann