jx/internal/resource/types.go
2024-03-20 09:15:27 -07:00

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)
}