registry.go 647 B

123456789101112131415161718192021222324252627
  1. package apiv1
  2. import (
  3. "context"
  4. "sync"
  5. )
  6. var registry = new(sync.Map)
  7. // KeyManagerNewFunc is the type that represents the method to initialize a new
  8. // KeyManager.
  9. type KeyManagerNewFunc func(ctx context.Context, opts Options) (KeyManager, error)
  10. // Register adds to the registry a method to create a KeyManager of type t.
  11. func Register(t Type, fn KeyManagerNewFunc) {
  12. registry.Store(t, fn)
  13. }
  14. // LoadKeyManagerNewFunc returns the function initialize a KayManager.
  15. func LoadKeyManagerNewFunc(t Type) (KeyManagerNewFunc, bool) {
  16. v, ok := registry.Load(t)
  17. if !ok {
  18. return nil, false
  19. }
  20. fn, ok := v.(KeyManagerNewFunc)
  21. return fn, ok
  22. }