Skip to content
This repository has been archived by the owner on Oct 11, 2018. It is now read-only.

Field.Set: use of reflect.Indirect #16

Closed
pikanezi opened this issue Sep 8, 2014 · 5 comments
Closed

Field.Set: use of reflect.Indirect #16

pikanezi opened this issue Sep 8, 2014 · 5 comments

Comments

@pikanezi
Copy link

pikanezi commented Sep 8, 2014

Hello,

Thanks for this useful package, but I have a question: why did you use reflect.Indirect when setting a field instead of using the value of the field itself? When setting pointers, it seems that it just does not work when using the value that the field points to.

@fatih
Copy link
Owner

fatih commented Sep 9, 2014

Hm but reflect.Indirect is returning the value itself if it's not a pointer. And I need the value to set the field. See: http://golang.org/src/pkg/reflect/value.go?s=66983:67011#L2249

Here are the tests fro set https://github.com/fatih/structs/blob/master/field_test.go#L46

Could you add a test case that invalidates this test so I can be sure what the real problem is?

@pikanezi
Copy link
Author

pikanezi commented Sep 9, 2014

I've forked your repository and added a test that fails.
https://github.com/pikanezi/structs

Basically:

type Bar struct {
    A string
}

type Foo struct {
    B *Bar
}


foo := &Foo{
    B: nil,
}
structInfo := structs.New(foo)
bar := &Bar {
    A: "helloWorld",
}

err := structInfo.Field("B").Set(bar); err != nil {
    fmt.Println(err) // Prints "field is not settable"
}

@fatih
Copy link
Owner

fatih commented Sep 9, 2014

Thanks! I'll look it into asap.

fatih added a commit that referenced this issue Sep 9, 2014
@fatih
Copy link
Owner

fatih commented Sep 9, 2014

Thanks for the case. I've added it to the test cases and fix the problem. As you said earlier there is no need to take the address as that would disallow to set it. 👍

@pikanezi
Copy link
Author

Glad I could help :)

@fatih fatih closed this as completed Sep 10, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants