default_generator.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. "io"
  16. "k8s.io/gengo/namer"
  17. "k8s.io/gengo/types"
  18. )
  19. const (
  20. GolangFileType = "golang"
  21. )
  22. // DefaultGen implements a do-nothing Generator.
  23. //
  24. // It can be used to implement static content files.
  25. type DefaultGen struct {
  26. // OptionalName, if present, will be used for the generator's name, and
  27. // the filename (with ".go" appended).
  28. OptionalName string
  29. // OptionalBody, if present, will be used as the return from the "Init"
  30. // method. This causes it to be static content for the entire file if
  31. // no other generator touches the file.
  32. OptionalBody []byte
  33. }
  34. func (d DefaultGen) Name() string { return d.OptionalName }
  35. func (d DefaultGen) Filter(*Context, *types.Type) bool { return true }
  36. func (d DefaultGen) Namers(*Context) namer.NameSystems { return nil }
  37. func (d DefaultGen) Imports(*Context) []string { return []string{} }
  38. func (d DefaultGen) PackageVars(*Context) []string { return []string{} }
  39. func (d DefaultGen) PackageConsts(*Context) []string { return []string{} }
  40. func (d DefaultGen) GenerateType(*Context, *types.Type, io.Writer) error { return nil }
  41. func (d DefaultGen) Filename() string { return d.OptionalName + ".go" }
  42. func (d DefaultGen) FileType() string { return GolangFileType }
  43. func (d DefaultGen) Finalize(*Context, io.Writer) error { return nil }
  44. func (d DefaultGen) Init(c *Context, w io.Writer) error {
  45. _, err := w.Write(d.OptionalBody)
  46. return err
  47. }
  48. var (
  49. _ = Generator(DefaultGen{})
  50. )