default_package.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. // The location of the package on disk.
  24. Source string
  25. // Emitted at the top of every file.
  26. HeaderText []byte
  27. // Emitted only for a "doc.go" file; appended to the HeaderText for
  28. // that file.
  29. PackageDocumentation []byte
  30. // If non-nil, will be called on "Generators"; otherwise, the static
  31. // list will be used. So you should set only one of these two fields.
  32. GeneratorFunc func(*Context) []Generator
  33. GeneratorList []Generator
  34. // Optional; filters the types exposed to the generators.
  35. FilterFunc func(*Context, *types.Type) bool
  36. }
  37. func (d *DefaultPackage) Name() string { return d.PackageName }
  38. func (d *DefaultPackage) Path() string { return d.PackagePath }
  39. func (d *DefaultPackage) SourcePath() string { return d.Source }
  40. func (d *DefaultPackage) Filter(c *Context, t *types.Type) bool {
  41. if d.FilterFunc != nil {
  42. return d.FilterFunc(c, t)
  43. }
  44. return true
  45. }
  46. func (d *DefaultPackage) Generators(c *Context) []Generator {
  47. if d.GeneratorFunc != nil {
  48. return d.GeneratorFunc(c)
  49. }
  50. return d.GeneratorList
  51. }
  52. func (d *DefaultPackage) Header(filename string) []byte {
  53. if filename == "doc.go" {
  54. return append(d.HeaderText, d.PackageDocumentation...)
  55. }
  56. return d.HeaderText
  57. }
  58. var (
  59. _ = Package(&DefaultPackage{})
  60. )