31 lines
519 B
Go
31 lines
519 B
Go
package resource
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
var (
|
|
ResourceTypes *Types = NewTypes()
|
|
)
|
|
|
|
type TypeFactory func() Resource
|
|
|
|
type Types struct {
|
|
registry map[string]TypeFactory
|
|
}
|
|
|
|
func NewTypes() *Types {
|
|
return &Types{ registry: make(map[string]TypeFactory) }
|
|
}
|
|
|
|
func (t *Types) Register(name string, factory TypeFactory) {
|
|
t.registry[name] = factory
|
|
}
|
|
|
|
func (t *Types) New(name string) (Resource, error) {
|
|
if r,ok := t.registry[name]; ok {
|
|
return r(), nil
|
|
}
|
|
return nil, fmt.Errorf("Unknown type: %s", name)
|
|
}
|