blob: 944db48182bd42dce5a1595f6400cdba533f834e [file] [log] [blame]
sslobodrd046be82019-01-16 10:02:22 -05001/*
2Copyright 2015 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package runtime
18
19// SchemeBuilder collects functions that add things to a scheme. It's to allow
20// code to compile without explicitly referencing generated types. You should
21// declare one in each package that will have generated deep copy or conversion
22// functions.
23type SchemeBuilder []func(*Scheme) error
24
25// AddToScheme applies all the stored functions to the scheme. A non-nil error
26// indicates that one function failed and the attempt was abandoned.
27func (sb *SchemeBuilder) AddToScheme(s *Scheme) error {
28 for _, f := range *sb {
29 if err := f(s); err != nil {
30 return err
31 }
32 }
33 return nil
34}
35
36// Register adds a scheme setup function to the list.
37func (sb *SchemeBuilder) Register(funcs ...func(*Scheme) error) {
38 for _, f := range funcs {
39 *sb = append(*sb, f)
40 }
41}
42
43// NewSchemeBuilder calls Register for you.
44func NewSchemeBuilder(funcs ...func(*Scheme) error) SchemeBuilder {
45 var sb SchemeBuilder
46 sb.Register(funcs...)
47 return sb
48}