I have the below interface that implements a simpler Active Record Like implementation for my persistent layer.
type DBInterface interface {
FindAll(collection []byte) map[string]string
FindOne(collection []byte, id int) map[string]string
Destroy(collection []byte, id int) bool
Update(collection []byte, obj map[string]string ) map[string]string
Create(collection []byte, obj map[string]string) map[string]string
}
The application has different collection's that it talks to and different corresponding models. I need to be able to pass in a dynamic Struct , instead of a map for the value obj ( ie. Update , Create Signatures )
I can't seem to understand how to use reflection to resolve the Struct , any guidance would help.
More details on what I am trying explain :
Consider the below snippet from mgo example from https://labix.org/mgo
err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
&Person{"Cla", "+55 53 8402 8510"})
When we insert data to the collection , we do a &Person I want to be able to pass in this bit &Person{"Ale", "+55 53 8116 9639"} but the method receiving the would only know it in the Run time. Because it could be a Person , Car , Book etc Struct depending on the func calling the method
Declare your obj type as interface{}
Update(collection []byte, obj interface{} ) map[string]string
Now you can pass Person,Book,Car etc to this function as obj.
Use a type switch inside Update function for each actual struct
switch t := obj.(type){
case Car://Handle Car type
case Perosn:
case Book:
}
Structs needs to be decided at compile time.No dynamic types in Go.Even interfaces are static types.