package sdk import ( "sync" ) type Reference struct { Category Category Type string } type Registration interface { RegisterAt(registry *TypeRegistry) } type RegistrationFunc func(registry *TypeRegistry) func (f RegistrationFunc) RegisterAt(registry *TypeRegistry) { f(registry) } func NewTypeRegistry() *TypeRegistry { return &TypeRegistry{ registrations: make(map[Reference]Factory), } } type TypeRegistry struct { lock sync.Mutex registrations map[Reference]Factory } func (r *TypeRegistry) List() (refs []Reference) { refs = make([]Reference, 0, len(r.registrations)) for k := range r.registrations { refs = append(refs, k) } return refs } func (r *TypeRegistry) Add(cat Category, moduleName string, factory Factory) { r.lock.Lock() defer r.lock.Unlock() r.registrations[specOf(cat, moduleName)] = factory } func (r *TypeRegistry) Get(cat Category, moduleName string) Module { f, ok := r.registrations[specOf(cat, moduleName)] if !ok { return nil } return f.Create() } func specOf(cat Category, moduleName string) Reference { return Reference{ Category: cat, Type: moduleName, } }