Access pointer value of a struct inside a function - go

I want to pass a struct object to a function & be able to access its pointer value from that function. I am not able to understand why the following is resulting in error.
func GetStructFieldPointers(u interface{}, jsonFields []string) []interface{} {
structVal := reflect.ValueOf(&u).Elem()
structType := reflect.TypeOf(u)
numberOfFields := structVal.NumField() // getting error here reflect:
// call of reflect.Value.NumField
// on interface Value
numberOfJSONFields := len(jsonFields)
res := make([]interface{}, numberOfJSONFields)
fmt.Println(jsonFields)
for fieldIndex, field := range jsonFields {
for i := 0; i < numberOfFields; i++ {
if structType.Field(i).Tag.Get("json") == field {
valueField := structVal.Field(i)
res[fieldIndex] = valueField.Addr().Interface()
}
}
}
return res
}
type User struct {
Id int `json:"id"`
Name string `json:"name"`
Address string `json:"address"`
}
user := User{}
res := GetStructFieldPointers(user, []string{"id", "name"})
To make this work, I had to make structType as a parameter like following:
func GetStructFieldPointers(u interface{}, structType reflect.Type, jsonFields []string) []interface{} {
structVal := reflect.ValueOf(u).Elem()
// structType := reflect.TypeOf(u)
numberOfFields := structVal.NumField()
numberOfJSONFields := len(jsonFields)
res := make([]interface{}, numberOfJSONFields)
fmt.Println(jsonFields)
for fieldIndex, field := range jsonFields {
for i := 0; i < numberOfFields; i++ {
if structType.Field(i).Tag.Get("json") == field {
valueField := structVal.Field(i)
res[fieldIndex] = valueField.Addr().Interface()
}
}
}
return res
}
user := User{}
res := GetStructFieldPointers(&user, reflect.TypeOf(user), []string{"id", "name"})
I like to know how to pass User{} as a parameter & use in both reflect.ValueOf & reflect.TypeOf calls.

On this line: structVal := reflect.ValueOf(&u).Elem() you're taking the address of an interface (the argument of your func) and not an address to the interface's underlying value, and then you're passing the pointer to ValueOf, so the .Elem() call returns the "elem value" to which the pointer points to, which is the interface, not struct.
If you know for a fact that the passed in value is a struct and not a pointer, all you need is this: structVal := reflect.ValueOf(u).
If a pointer was passed to your func, eg GetStructFieldPointers(&u, ... then this is what you want: structVal := reflect.ValueOf(u).Elem().
But also you can handle both cases by checking the value's kind.
rv := reflect.ValueOf(u)
if rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
if rv.Kind() == reflect.Struct {
fmt.Println(rv.NumField())
}
https://play.golang.org/p/9F9LNnwEaH
Update:
Took a better look at your code... If you want to be able to get the addresses of your struct's fields, you need to pass a pointer to the struct as the argument, or else those fields are going to be unaddressable.
https://play.golang.org/p/RaA2rau3s-

Related

Generically modify struct fields using reflection in golang

Below is a method which uses reflect package to modify fields of a struct ,this works for a specific struct type
func modify(obj Car) interface{} {
ty := reflect.TypeOf(obj)
for i := 0; i < ty.NumField(); i++ {
rval := reflect.Indirect(reflect.ValueOf(&obj))
field := rval.Field(i)
fieldType := field.Kind()
switch fieldType {
case reflect.String:
field.SetString("")
case reflect.Int:
field.SetInt(0)
case reflect.Ptr:
field.Set(reflect.ValueOf(nil))
}
}
return obj
}
modifying the signature to
func modify(obj interface{}) interface{} {
results in
panic: reflect: call of reflect.Value.Field on interface Value
at line
field := rval.Field(i)
https://go.dev/play/p/pGfKtIg5RUp
It works with the signature
func modify(obj Car) interface{} {
https://go.dev/play/p/31Oh6WLmlGP
Why is the compile time type modifying the behaviour ?
The goal here is to mask certain fields based on struct tags .It could wrap an endpoint and the input and output to the method being wrapped could be struct or pointer so in above case both calls should work
modify(car)
modify(&car)
This is how it works for both value and pointer types
func modify(obj interface{}) interface{} {
rv := reflect.ValueOf(obj)
trv := reflect.TypeOf(obj)
value := reflect.New(rv.Type())
if rv.Kind() == reflect.Pointer {
rv = reflect.ValueOf(obj).Elem()
trv = reflect.TypeOf(obj).Elem()
value = reflect.New(rv.Type())
}
for i := 0; i < rv.NumField(); i++ {
field := rv.Field(i)
fieldType := field.Kind()
v := value.Elem().Field(i)
tag, _ := trv.Field(i).Tag.Lookup("es")
if len(tag) != 0 {
switch fieldType {
case reflect.String:
v.SetString(tag)
case reflect.Int:
v.SetInt(0)
case reflect.Ptr:
v.Set(reflect.ValueOf(nil))
}
} else {
v.Set(field)
}
}
return value
}
https://go.dev/play/p/C1pqw_UbPcG

Obtain structure info

The program is:
package main
import (
"fmt"
"reflect"
)
type Request struct {
Method string
Resource string //path
Protocol string
}
type s struct {
ID int
Title string
Request Request
Price float64
Interface interface{}
Exists bool
Many []string
}
func main() {
s := s{}
iterateStruct(s)
}
func iterateStruct(s interface{}) {
e := reflect.ValueOf(s)
for i := 0; i < e.NumField(); i++ {
varName := e.Type().Field(i).Name
varKind := e.Field(i).Kind()
fmt.Println(e.Type().Field(i).Name)
if varKind == reflect.Struct {
//iterateStruct( <what should be here?>)
}
varType := e.Type().Field(i).Type
varValue := e.Field(i).Interface()
fmt.Printf("%v %v %v %v\n", varName, varKind, varType, varValue)
}
}
Using recursion I'd like to get the same information for Request, that is a structure part of a structure.
What would I need to pass as a parameter? I tried various ways but I have to reckon it's a lot of trial and error for me.
Try this:
if varKind == reflect.Struct {
iterateStruct(e.Field(i).Interface())
}
e.Field(i) returns the Value for the struct field. Interface{} will return the underlying value, so you can call iterateStruct using that.
Here's an example that handles fields with pointers to structs, interfaces containing struct value, etc.. As a bonus, this example indents nested structs.
func iterate(v reflect.Value, indent string) {
v = reflect.Indirect(v)
if v.Kind() != reflect.Struct {
return
}
indent += " "
for i := 0; i < v.NumField(); i++ {
varName := v.Type().Field(i).Name
varKind := v.Field(i).Kind()
varType := v.Type().Field(i).Type
varValue := v.Field(i).Interface()
fmt.Printf("%s%v %v %v %v\n", indent, varName, varKind, varType, varValue)
iterate(v.Field(i), indent)
}
}
Call it like this:
iterate(reflect.ValueOf(s), "")
https://go.dev/play/p/y1CzbKAUvD_w

Get value of pointer of a struct field using reflect

package main
import (
"fmt"
"reflect"
)
type PetDetails struct {
Name *string
}
type Student struct {
Fname string
Lname string
City string
Mobile *int
Pet *PetDetails
}
func main() {
i := 7777777777
petName := "Groot"
s := Student{"Chetan", "Tulsyan", "Bangalore", &i, &PetDetails{&petName}}
v := reflect.ValueOf(s)
typeOfS := v.Type()
for i := 0; i< v.NumField(); i++ {
fmt.Printf("Field: %s\tValue: %v\n", typeOfS.Field(i).Name, v.Field(i).Interface())
}
}
I am trying to convert these struct into map[string]string as I need the map for update query of mongoDB. After converting my struct to BSON, instead of querying { "pet.name": "Groot" } it becomes { "pet": { "name": "Groot" } } which deletes other fields inside the embedded document pet. I'm not sure how to override BSON marshals as I'm using mongodb driver, not mgo
I would like to get value of Mobile pointer and Name of the Pet, but all I get is the address
How can I get the value, e.g. 7777 and Groot ? Thanks
You can use Elem to dereference the pointer types.
x := 5
ptr := reflect.ValueOf(&x)
value := ptr.Elem()
ptr.Type().Name() // *int
ptr.Type().Kind() // reflect.Ptr
ptr.Interface() // [pointer to x]
ptr.Set(4) // panic
value.Type().Name() // int
value.Type().Kind() // reflect.Int
value.Interface() // 5
value.Set(4) // this works
For example, to retrieve the mobile number in your example you should change the loop in main to:
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
value := field.Interface()
// If a pointer type dereference with Elem
if field.Kind() == reflect.Ptr {
value = field.Elem().Interface()
}
fmt.Printf("Field: %s\tValue: %v\n", typeOfS.Field(i).Name, value)
}

How to copy a non-pointer value to a pointer indirected value via reflection

I want the Set method below to set the APtr field of a passed in B struct to a value that gets passed in by value, i.e. without a pointer indirection.
For that to work via go reflection, I will probably have to copy that value to a new location that I have the address of? Either way, how can I get this to work? What I have is a working version for non-pointers values.
type A struct {
AnInt int
}
type B struct {
AnA A
APtr *A
}
func Set(strukt interface{}, fieldName string, newFieldValue interface{}) {
struktValueElem := reflect.ValueOf(strukt).Elem()
field := struktValueElem.FieldByName(fieldName)
newFieldValueValue := reflect.ValueOf(newFieldValue)
if field.Kind() == reflect.Ptr {
// ?? implement me
} else { // not a pointer? more straightforward:
field.Set(newFieldValueValue)
}
}
func main() {
aB := B{}
anA := A{4}
Set(&aB, "AnA", anA) // works
Set(&aB, "APtr", anA) // implement me
}
Playground: https://play.golang.org/p/6tcmbXxBcIm
func Set(strukt interface{}, fieldName string, newFieldValue interface{}) {
struktValueElem := reflect.ValueOf(strukt).Elem()
field := struktValueElem.FieldByName(fieldName)
newFieldValueValue := reflect.ValueOf(newFieldValue)
if field.Kind() == reflect.Ptr {
rt := field.Type() // type *A
rt = rt.Elem() // type A
rv := reflect.New(rt) // value *A
el := rv.Elem() // value A (addressable)
el.Set(newFieldValueValue) // el is addressable and has the same type as newFieldValueValue (A), Set can be used
field.Set(rv) // field is addressable and has the same type as rv (*A), Set can be used
} else { // not a pointer? more straightforward:
field.Set(newFieldValueValue)
}
}
https://play.golang.org/p/jgEK_rKbgO9
https://play.golang.org/p/B6vOONQ-RXO (compact)

Check if a slice contains a struct with a given field value

Trying to check if a struct in some slice contains a value of a given field so i wrote this
func main() {
//test
Objs := []Obj{{1,"xxx"},{2,"yyy"},{3,"zzz"}}
res := containsStructFieldValue(Objs,"X",1)
fmt.Println(res)
}
type Obj struct {
X int
Y string
}
func containsStructFieldValue(slice []Obj ,fieldName string,fieldValueToCheck interface {}) bool{
for _,s := range slice{
r := reflect.ValueOf(s)
f := r.FieldByName(fieldName)
if f.IsValid(){
if f.Interface() == fieldValueToCheck{
return true //a field with the given value exists
}
}
}
return false
}
i need it to work for any given struct type but when i tried slice []interface as the parameter i found out that its not possible, any idea on how to make the above method work for any struct type?
You can use reflect to range over an interface{}, for instance:
func containsStructFieldValue(slice interface{} ,fieldName string,fieldValueToCheck interface {}) bool{
rangeOnMe := reflect.ValueOf(slice)
for i := 0; i < rangeOnMe.Len(); i++ {
s := rangeOnMe.Index(i)
f := s.FieldByName(fieldName)
if f.IsValid(){
if f.Interface() == fieldValueToCheck {
return true
}
}
}
}
Note that I did not check that slice is indeed a slice... If not, this code will panic. You can use reflect.Kind to check this if you want to avoid this behaviour.

Resources