The output of this program is map[], but I want map[Id:true name:true]
I'm trying to dry up some of my SQL CRUD code and thought it would be nice to embed a persistence struct that handles reading and writing to the database. In the example below, the persistence struct would be Inner and my model would be Outer. Thanks!
http://play.golang.org/p/fsPqJ-6aLI
package main
import (
"fmt"
"reflect"
)
type Inner struct {
}
type Outer struct {
Inner
Id int
name string
}
func (i *Inner) Fields() map[string]bool {
typ := reflect.TypeOf(*i)
attrs := make(map[string]bool)
if typ.Kind() != reflect.Struct {
fmt.Printf("%v type can't have attributes inspected
", typ.Kind())
return attrs
}
// loop through the struct's fields and set the map
for i := 0; i < typ.NumField(); i++ {
p := typ.Field(i)
if !p.Anonymous {
v := reflect.ValueOf(p.Type)
v = v.Elem()
attrs[p.Name] = v.CanSet()
}
}
return attrs
}
func main() {
val := Outer{}
fmt.Println(val.Fields()) // prints map[], but I want map[Id:true name:true]
}
You can't. You're specifically calling a method on Inner
, which has no knowledge of where it's embedded. Embedding isn't inheritance, it's simple automatic delegation.
You probably want to look in the direction of wrapping these in a common persistence interface, or even a generic function that can handle persisting your data types.
Now, if you really want to try this, you can get access to the outer struct through the pointer address, but you will need to know that outer type you want to access, which means that you can't get it via reflection.
outer := (*Outer)(unsafe.Pointer(i))
typ := reflect.TypeOf(*outer)
It seems like you can do this: if you create an interface and pass the object in question as an arg to the function, reflect gets the correct Outer type of the object:
package main
import (
"fmt"
"reflect"
)
type InType interface {
Fields(obj InType) map[string]bool
}
type Inner struct {
}
type Outer struct {
Inner
Id int
name string
}
func (i *Inner) Fields(obj InType) map[string]bool {
typ := reflect.TypeOf(obj).Elem()
attrs := make(map[string]bool)
if typ.Kind() != reflect.Struct {
fmt.Printf("%v type can't have attributes inspected
", typ.Kind())
return attrs
}
// loop through the struct's fields and set the map
for i := 0; i < typ.NumField(); i++ {
p := typ.Field(i)
if !p.Anonymous {
v := reflect.ValueOf(p.Type)
v = v.Elem()
attrs[p.Name] = v.CanSet()
}
}
return attrs
}
func main() {
val := Outer{}
fmt.Println(val.Fields(&val)) // prints map[Id:true name:true]
}