-1

I need Go to implicitly resolve my struct type, in order to do generic replacement of some attribute.

//must replace the attribute with attValue
func SetAttribute(object interface{}, attributeName string, attValue interface{}, objectType reflect.Type) interface{} {

    /// works perfectly, but function SetAttribute needs to know Customer type to do the convertion
    convertedObject := object.(Customer) // <-- Need to hard code a cast :(

    // doesn't works... raise panic!
    //convertedObject := object 


    value := reflect.ValueOf(&convertedObject).Elem()
    field := value.FieldByName(attributeName)
    valueForAtt := reflect.ValueOf(attValue)
    field.Set(valueForAtt)

    return value.Interface()
}

Please check out full example in the Go playground... http://play.golang.org/p/jxxSB5FKEy

blackgreen
  • 34,072
  • 23
  • 111
  • 129
DLopes
  • 567
  • 2
  • 6
  • 13

1 Answers1

1

convertedObject is the value of what is in the object interface. Taking the address of that has no effect on the original customer. (and converted is probably a poor prefix for the name, since that is generated from a "type assertion", not a "type conversion")

If you use object directly, it panics, because you're then taking the address of the interface, not the customer.

You need to pass the address of the customer you want to modify to the function:

SetAttribute(&customer, "Local", addressNew, reflect.TypeOf(Customer{}))

You can also have your SetAttribute check if it's a pointer first:

if reflect.ValueOf(object).Kind() != reflect.Ptr {
    panic("need a pointer")
}

value := reflect.ValueOf(object).Elem() 
field := value.FieldByName(attributeName)
valueForAtt := reflect.ValueOf(attValue)
field.Set(valueForAtt)
return value.Interface()
JimB
  • 104,193
  • 13
  • 262
  • 255
  • Sorry, man. The problem becomes more trickier in the real implementation... Look at these new question: http://stackoverflow.com/questions/30585938/generic-programming-in-go-avoiding-hard-coded-type-assertion – DLopes Jun 02 '15 at 01:38