default_generator.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package generator
  2. import (
  3. "io"
  4. "go-common/app/tool/gengo/namer"
  5. "go-common/app/tool/gengo/types"
  6. )
  7. // consts
  8. const (
  9. GolangFileType = "golang"
  10. )
  11. // DefaultGen implements a do-nothing Generator.
  12. //
  13. // It can be used to implement static content files.
  14. type DefaultGen struct {
  15. // OptionalName, if present, will be used for the generator's name, and
  16. // the filename (with ".go" appended).
  17. OptionalName string
  18. // OptionalBody, if present, will be used as the return from the "Init"
  19. // method. This causes it to be static content for the entire file if
  20. // no other generator touches the file.
  21. OptionalBody []byte
  22. }
  23. func (d DefaultGen) Name() string { return d.OptionalName }
  24. func (d DefaultGen) Filter(*Context, *types.Type) bool { return true }
  25. func (d DefaultGen) Namers(*Context) namer.NameSystems { return nil }
  26. func (d DefaultGen) Imports(*Context) []string { return []string{} }
  27. func (d DefaultGen) PackageVars(*Context) []string { return []string{} }
  28. func (d DefaultGen) PackageConsts(*Context) []string { return []string{} }
  29. func (d DefaultGen) GenerateType(*Context, *types.Type, io.Writer) error { return nil }
  30. func (d DefaultGen) Filename() string { return d.OptionalName + ".go" }
  31. func (d DefaultGen) FileType() string { return GolangFileType }
  32. func (d DefaultGen) Finalize(*Context, io.Writer) error { return nil }
  33. func (d DefaultGen) Init(c *Context, w io.Writer) error {
  34. _, err := w.Write(d.OptionalBody)
  35. return err
  36. }
  37. var (
  38. _ = Generator(DefaultGen{})
  39. )