main.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. // deepcopy-gen is a tool for auto-generating DeepCopy functions.
  14. //
  15. // Given a list of input directories, it will generate DeepCopy and
  16. // DeepCopyInto methods that efficiently perform a full deep-copy of each type.
  17. // If these methods already exist (are predefined by the developer), they are
  18. // used instead of generating new ones. Generated code will use standard value
  19. // assignment whenever possible. If that is not possible it will try to call
  20. // its own generated copy function for the type. Failing that, it will fall
  21. // back on `conversion.Cloner.DeepCopy(val)` to make the copy. The resulting
  22. // file will be stored in the same directory as the processed source package.
  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
  27. // below. They must be added to the interfaces themselves manually, e.g.
  28. //
  29. // type Object interface {
  30. // ...
  31. // DeepCopyObject() Object
  32. // }
  33. //
  34. // Generation is governed by comment tags in the source. Any package may
  35. // request DeepCopy generation by including a comment in the file-comments of
  36. // one file, of the form:
  37. //
  38. // // +k8s:deepcopy-gen=package
  39. //
  40. // DeepCopy functions can be generated for individual types, rather than the
  41. // entire package by specifying a comment on the type definition of the form:
  42. //
  43. // // +k8s:deepcopy-gen=true
  44. //
  45. // When generating for a whole package, individual types may opt out of
  46. // DeepCopy generation by specifying a comment on the type definition of the
  47. // form:
  48. //
  49. // // +k8s:deepcopy-gen=false
  50. //
  51. // Additional DeepCopyInterfaceName methods can be generated by specifying a
  52. // comment on the type definition of the form:
  53. //
  54. // // +k8s:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.Object,k8s.io/kubernetes/runtime.List
  55. //
  56. // This leads to the generation of DeepCopyObject and DeepCopyList with the given
  57. // interfaces as return types. We say that the tagged type implements deepcopy for the
  58. // interfaces.
  59. //
  60. // The deepcopy funcs for interfaces using "+k8s:deepcopy-gen:interfaces" use the pointer
  61. // of the type as receiver. For those special cases where the non-pointer object should
  62. // implement the interface, this can be done with:
  63. //
  64. // // +k8s:deepcopy-gen:nonpointer-interfaces=true
  65. package main
  66. import (
  67. "flag"
  68. "github.com/spf13/pflag"
  69. "k8s.io/code-generator/cmd/deepcopy-gen/args"
  70. "k8s.io/code-generator/cmd/deepcopy-gen/generators"
  71. "k8s.io/gengo/v2"
  72. "k8s.io/gengo/v2/generator"
  73. "k8s.io/klog/v2"
  74. )
  75. func main() {
  76. klog.InitFlags(nil)
  77. args := args.New()
  78. args.AddFlags(pflag.CommandLine)
  79. flag.Set("logtostderr", "true")
  80. pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
  81. pflag.Parse()
  82. if err := args.Validate(); err != nil {
  83. klog.Fatalf("Error: %v", err)
  84. }
  85. myTargets := func(context *generator.Context) []generator.Target {
  86. return generators.GetTargets(context, args)
  87. }
  88. // Run it.
  89. if err := gengo.Execute(
  90. generators.NameSystems(),
  91. generators.DefaultNameSystem(),
  92. myTargets,
  93. gengo.StdBuildTag,
  94. pflag.Args(),
  95. ); err != nil {
  96. klog.Fatalf("Error: %v", err)
  97. }
  98. klog.V(2).Info("Completed successfully.")
  99. }