I'm having trouble calling a method in my recursive reflection function. Here it is:
func setPropertiesFromFlags(v reflect.Value, viper *viper.Viper) {
t := v.Type()
method := v.MethodByName("Parse")
fmt.Println(method)
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
switch field.Type.Kind() {
case reflect.Struct:
setPropertiesFromFlags(v.Field(i), viper)
case reflect.String:
v.Field(i).SetString(viper.GetString(field.Tag.Get("name")))
}
}
I'm calling the function with:
// Config struct passed to all services
type Config struct {
common.Config
common.ServerConfig
common.AuthConfig
}
// Parse the thing already!
func (c *Config) Parse() {
fmt.Println("RUN THIS THING")
}
int main() {
setPropertiesFromFlags(reflect.ValueOf(c).Elem(), viper)
}
What I'm hoping for is to get my parse method in the place where I'm printing method and run .Call() against it. Instead it's printing out: <invalid reflect.Value>
which I cannot call against.
I suppose I'm having trouble wrapping my head around the return values of each method. I know I have to use ValueOf to pull the value but it seems any permutation I try I'm getting the methods from the reflection class itself :-p sigh
Aucun commentaire:
Enregistrer un commentaire