scheme_builder.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. Copyright 2015 The Kubernetes 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. http://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 runtime
  14. // SchemeBuilder collects functions that add things to a scheme. It's to allow
  15. // code to compile without explicitly referencing generated types. You should
  16. // declare one in each package that will have generated deep copy or conversion
  17. // functions.
  18. type SchemeBuilder []func(*Scheme) error
  19. // AddToScheme applies all the stored functions to the scheme. A non-nil error
  20. // indicates that one function failed and the attempt was abandoned.
  21. func (sb *SchemeBuilder) AddToScheme(s *Scheme) error {
  22. for _, f := range *sb {
  23. if err := f(s); err != nil {
  24. return err
  25. }
  26. }
  27. return nil
  28. }
  29. // Register adds a scheme setup function to the list.
  30. func (sb *SchemeBuilder) Register(funcs ...func(*Scheme) error) {
  31. for _, f := range funcs {
  32. *sb = append(*sb, f)
  33. }
  34. }
  35. // NewSchemeBuilder calls Register for you.
  36. func NewSchemeBuilder(funcs ...func(*Scheme) error) SchemeBuilder {
  37. var sb SchemeBuilder
  38. sb.Register(funcs...)
  39. return sb
  40. }