I'm trying to dynamically create structs based on a string.
In the below example reflect.TypeOf &c and &c1 are different because I return interface{} from makeInstance. TypeOf c and c1 are the same.
My question is how do I change the way I handle the output of makeInstance so it creates an object identical to c1 but will still allow me to create objects identical to b1 also?
type Car struct {
Make int `json:"make"`
Model int `json:"model"`
}
type Bus struct {
Seats int `json:"seats"`
Route int `json:"route"`
}
var typeRegistry = make(map[string]reflect.Type)
func init() {
typeRegistry["Car"] = reflect.TypeOf(Car{})
typeRegistry["Bus"] = reflect.TypeOf(Bus{})
}
func makeInstance(name string) interface{} {
v := reflect.New(typeRegistry[name]).Elem()
return v.Interface()
}
func main() {
c := makeInstance("Car")
b := makeInstance("Bus")
var b1 Bus
var c1 Car
fmt.Println(reflect.TypeOf(&c))
fmt.Println(reflect.TypeOf(&c1))
fmt.Println(reflect.TypeOf(c))
fmt.Println(reflect.TypeOf(c1))
Edit:
My overall outcome is to have a program that reads a json config file that will list the types of objects I want to go off and hit a rest api and collect e.g.
{
"auth":[{
"username": "admin",
"password": "admin"
}],
"transport":[
{
"vehicle":["car", "bus"]
}]
}
When looping through vehicle it would hit an api and perform a query and return data. I would then want to create an array of the which ever vehicle is included in the config and unmarshal the json response into it. I'm trying to create the objects dynamically so I could avoid having to check if vehicle = car, if vehicle = bus etc as I will eventually have many types of vehicles but may not always have every vehicle and it seems long winded.