generator_schema.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package v1alpha1
  14. import (
  15. "fmt"
  16. "sync"
  17. )
  18. var builder map[string]Generator
  19. var buildlock sync.RWMutex
  20. func init() {
  21. builder = make(map[string]Generator)
  22. }
  23. // Register a generator type. Register panics if a
  24. // backend with the same generator is already registered.
  25. func Register(kind string, g Generator) {
  26. buildlock.Lock()
  27. defer buildlock.Unlock()
  28. _, exists := builder[kind]
  29. if exists {
  30. panic(fmt.Sprintf("kind %q already registered", kind))
  31. }
  32. builder[kind] = g
  33. }
  34. // ForceRegister adds to the schema, overwriting a generator if
  35. // already registered. Should only be used for testing.
  36. func ForceRegister(kind string, g Generator) {
  37. buildlock.Lock()
  38. builder[kind] = g
  39. buildlock.Unlock()
  40. }
  41. // GetGeneratorByName returns the provider implementation by name.
  42. func GetGeneratorByName(kind string) (Generator, bool) {
  43. buildlock.RLock()
  44. f, ok := builder[kind]
  45. buildlock.RUnlock()
  46. return f, ok
  47. }