| 1 |
/* |
| 2 |
Copyright 2015 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
// deepcopy-gen is a tool for auto-generating DeepCopy functions. |
| 18 |
// |
| 19 |
// Given a list of input directories, it will generate DeepCopy and DeepCopyInto |
| 20 |
// methods that efficiently perform a full deep-copy of each type. If these |
| 21 |
// already exist (are predefined by the developer), they are used instead of |
| 22 |
// generating new ones. |
| 23 |
// |
| 24 |
// If interfaces are referenced in types, it is expected that corresponding |
| 25 |
// DeepCopyInterfaceName methods exist, e.g. DeepCopyObject for runtime.Object. |
| 26 |
// These can be predefined by the developer or generated through tags, see below. |
| 27 |
// They must be added to the interfaces themselves manually, e.g. |
| 28 |
// type Object interface { |
| 29 |
// ... |
| 30 |
// DeepCopyObject() Object |
| 31 |
// } |
| 32 |
// |
| 33 |
// All generation is governed by comment tags in the source. Any package may |
| 34 |
// request DeepCopy generation by including a comment in the file-comments of |
| 35 |
// a doc.go file, of the form: |
| 36 |
// // +k8s:deepcopy-gen=package |
| 37 |
// |
| 38 |
// DeepCopy functions can be generated for individual types, rather than the |
| 39 |
// entire package by specifying a comment on the type definition of the form: |
| 40 |
// // +k8s:deepcopy-gen=true |
| 41 |
// |
| 42 |
// When generating for a whole package, individual types may opt out of |
| 43 |
// DeepCopy generation by specifying a comment on the type definition of the form: |
| 44 |
// // +k8s:deepcopy-gen=false |
| 45 |
// |
| 46 |
// Additional DeepCopyInterfaceName methods can be generated by specifying a |
| 47 |
// comment on the type definition of the form: |
| 48 |
// // +k8s:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.Object,k8s.io/kubernetes/runtime.List |
| 49 |
// This leads to the generation of DeepCopyObject and DeepCopyList with the given |
| 50 |
// interfaces as return types. We say that the tagged type implements deepcopy for the |
| 51 |
// interfaces. |
| 52 |
// |
| 53 |
// The deepcopy funcs for interfaces using "+k8s:deepcopy-gen:interfaces" use the pointer |
| 54 |
// of the type as receiver. For those special cases where the non-pointer object should |
| 55 |
// implement the interface, this can be done with: |
| 56 |
// // +k8s:deepcopy-gen:nonpointer-interfaces=true |
| 57 |
package main |
| 58 |
|
| 59 |
import ( |
| 60 |
"k8s.io/gengo/args" |
| 61 |
"k8s.io/gengo/examples/deepcopy-gen/generators" |
| 62 |
|
| 63 |
"github.com/spf13/pflag" |
| 64 |
"k8s.io/klog/v2" |
| 65 |
) |
| 66 |
|
| 67 |
func main() { |
| 68 |
klog.InitFlags(nil) |
| 69 |
arguments := args.Default() |
| 70 |
|
| 71 |
// Override defaults. |
| 72 |
arguments.OutputFileBaseName = "deepcopy_generated" |
| 73 |
|
| 74 |
// Custom args. |
| 75 |
customArgs := &generators.CustomArgs{} |
| 76 |
pflag.CommandLine.StringSliceVar(&customArgs.BoundingDirs, "bounding-dirs", customArgs.BoundingDirs, |
| 77 |
"Comma-separated list of import paths which bound the types for which deep-copies will be generated.") |
| 78 |
arguments.CustomArgs = customArgs |
| 79 |
|
| 80 |
// Run it. |
| 81 |
if err := arguments.Execute( |
| 82 |
generators.NameSystems(), |
| 83 |
generators.DefaultNameSystem(), |
| 84 |
generators.Packages, |
| 85 |
); err != nil { |
| 86 |
klog.Fatalf("Error: %v", err) |
| 87 |
} |
| 88 |
klog.V(2).Info("Completed successfully.") |
| 89 |
} |