default_package.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 generator
  14. import (
  15. "k8s.io/gengo/types"
  16. )
  17. // DefaultPackage contains a default implementation of Package.
  18. type DefaultPackage struct {
  19. // Short name of package, used in the "package xxxx" line.
  20. PackageName string
  21. // Import path of the package, and the location on disk of the package.
  22. PackagePath string
  23. // Emitted at the top of every file.
  24. HeaderText []byte
  25. // Emitted only for a "doc.go" file; appended to the HeaderText for
  26. // that file.
  27. PackageDocumentation []byte
  28. // If non-nil, will be called on "Generators"; otherwise, the static
  29. // list will be used. So you should set only one of these two fields.
  30. GeneratorFunc func(*Context) []Generator
  31. GeneratorList []Generator
  32. // Optional; filters the types exposed to the generators.
  33. FilterFunc func(*Context, *types.Type) bool
  34. }
  35. func (d *DefaultPackage) Name() string { return d.PackageName }
  36. func (d *DefaultPackage) Path() string { return d.PackagePath }
  37. func (d *DefaultPackage) Filter(c *Context, t *types.Type) bool {
  38. if d.FilterFunc != nil {
  39. return d.FilterFunc(c, t)
  40. }
  41. return true
  42. }
  43. func (d *DefaultPackage) Generators(c *Context) []Generator {
  44. if d.GeneratorFunc != nil {
  45. return d.GeneratorFunc(c)
  46. }
  47. return d.GeneratorList
  48. }
  49. func (d *DefaultPackage) Header(filename string) []byte {
  50. if filename == "doc.go" {
  51. return append(d.HeaderText, d.PackageDocumentation...)
  52. }
  53. return d.HeaderText
  54. }
  55. var (
  56. _ = Package(&DefaultPackage{})
  57. )