default_package.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package generator
  2. import (
  3. "go-common/app/tool/gengo/types"
  4. )
  5. // DefaultPackage contains a default implementation of Package.
  6. type DefaultPackage struct {
  7. // Short name of package, used in the "package xxxx" line.
  8. PackageName string
  9. // Import path of the package, and the location on disk of the package.
  10. PackagePath string
  11. // Emitted at the top of every file.
  12. HeaderText []byte
  13. // Emitted only for a "doc.go" file; appended to the HeaderText for
  14. // that file.
  15. PackageDocumentation []byte
  16. // If non-nil, will be called on "Generators"; otherwise, the static
  17. // list will be used. So you should set only one of these two fields.
  18. GeneratorFunc func(*Context) []Generator
  19. GeneratorList []Generator
  20. // Optional; filters the types exposed to the generators.
  21. FilterFunc func(*Context, *types.Type) bool
  22. }
  23. func (d *DefaultPackage) Name() string { return d.PackageName }
  24. func (d *DefaultPackage) Path() string { return d.PackagePath }
  25. func (d *DefaultPackage) Filter(c *Context, t *types.Type) bool {
  26. if d.FilterFunc != nil {
  27. return d.FilterFunc(c, t)
  28. }
  29. return true
  30. }
  31. func (d *DefaultPackage) Generators(c *Context) []Generator {
  32. if d.GeneratorFunc != nil {
  33. return d.GeneratorFunc(c)
  34. }
  35. return d.GeneratorList
  36. }
  37. func (d *DefaultPackage) Header(filename string) []byte {
  38. if filename == "doc.go" {
  39. return append(d.HeaderText, d.PackageDocumentation...)
  40. }
  41. return d.HeaderText
  42. }
  43. var (
  44. _ = Package(&DefaultPackage{})
  45. )