Guest User

Untitled

a guest
Jan 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. ```golang
  2. package cmd
  3.  
  4. import (
  5. "fmt"
  6. "io"
  7. "path/filepath"
  8. "reflect"
  9. "strings"
  10.  
  11. "k8s.io/gengo/parser"
  12. "github.com/sirupsen/logrus"
  13. "k8s.io/gengo/generator"
  14. "k8s.io/gengo/namer"
  15. "k8s.io/gengo/types"
  16. )
  17.  
  18. func generate(inDir, outDir string) error {
  19. b := parser.New()
  20. if err := b.AddDirRecursive(inDir); err != nil {
  21. return err
  22. }
  23.  
  24. ctx, err := generator.NewContext(
  25. b,
  26. namer.NameSystems{
  27. "public": namer.NewPublicNamer(1),
  28. "raw": namer.NewRawNamer("", nil),
  29. //"defaultfn": defaultFnNamer(),
  30. //"objectdefaultfn": objectDefaultFnNamer(),
  31. },
  32. "public",
  33. )
  34. if err != nil {
  35. return err
  36. }
  37.  
  38. pkgs := generatePackages(ctx)
  39. if err := ctx.ExecutePackages(outDir, pkgs); err != nil {
  40. return err
  41. }
  42. return nil
  43. }
  44.  
  45. func generatePackages(context *generator.Context) generator.Packages {
  46. packages := generator.Packages{}
  47. for _, i := range context.Inputs {
  48. pkg := context.Universe[i]
  49. if pkg == nil {
  50. // If the input had no Go files, for example.
  51. continue
  52. }
  53. logrus.Trace("pkg.Path ", pkg.Path)
  54. name := strings.Split(filepath.Base(pkg.Path), ".")[0]
  55. logrus.Trace(name)
  56. packages = append(packages,
  57. &generator.DefaultPackage{
  58. PackageName: name,
  59. //PackagePath: path,
  60. //HeaderText: header,
  61. GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) {
  62. return []generator.Generator{
  63. newGeneratorTemplate(name),
  64. }
  65. },
  66. FilterFunc: func(c *generator.Context, t *types.Type) bool {
  67. return t.Name.Package == pkg.Path
  68. },
  69. })
  70. }
  71. return packages
  72. }
  73.  
  74. type generatorTemplate struct {
  75. generator.DefaultGen
  76. }
  77.  
  78. func newGeneratorTemplate(name string) generator.Generator {
  79. return &generatorTemplate{
  80. DefaultGen: generator.DefaultGen{
  81. OptionalName: name + ".tmpl",
  82. },
  83. }
  84. }
  85.  
  86. //func (d DefaultGen) Name() string { return d.OptionalName }
  87. //func (d DefaultGen) Filter(*Context, *types.Type) bool { return true }
  88. //func (d DefaultGen) Namers(*Context) namer.NameSystems { return nil }
  89. //func (d DefaultGen) Imports(*Context) []string { return []string{} }
  90. //func (d DefaultGen) PackageVars(*Context) []string { return []string{} }
  91. //func (d DefaultGen) PackageConsts(*Context) []string { return []string{} }
  92. //func (d DefaultGen) GenerateType(*Context, *types.Type, io.Writer) error { return nil }
  93. //func (d DefaultGen) Filename() string { return d.OptionalName + ".go" }
  94. //func (d DefaultGen) FileType() string { return GolangFileType }
  95. //func (d DefaultGen) Finalize(*Context, io.Writer) error { return nil }
  96.  
  97. // The name of this generator. Will be included in generated comments.
  98. func (g *generatorTemplate) Name() string {
  99. logrus.Trace("generatorTemplate.Name ")
  100. return "template"
  101. }
  102.  
  103. // Filter should return true if this generator cares about this type.
  104. // (otherwise, GenerateType will not be called.)
  105. //
  106. // Filter is called before any of the generator's other functions;
  107. // subsequent calls will get a context with only the types that passed
  108. // this filter.
  109. func (g *generatorTemplate) Filter(ctx *generator.Context, t *types.Type) bool {
  110. logrus.Trace("generatorTemplate.Filter ", t)
  111. if t.Kind == types.Struct {
  112. return true
  113. }
  114. return false
  115. }
  116.  
  117. // If this generator needs special namers, return them here. These will
  118. // override the original namers in the context if there is a collision.
  119. // You may return nil if you don't need special names. These names will
  120. // be available in the context passed to the rest of the generator's
  121. // functions.
  122. //
  123. // A use case for this is to return a namer that tracks imports.
  124. func (g *generatorTemplate) Namers(ctx *generator.Context) namer.NameSystems {
  125. logrus.Trace("generatorTemplate.Namers")
  126. return nil
  127. }
  128.  
  129. // Init should write an init function, and any other content that's not
  130. // generated per-type. (It's not intended for generator specific
  131. // initialization! Do that when your Package constructs the
  132. // Generators.)
  133. func (g *generatorTemplate) Init(*generator.Context, io.Writer) error {
  134. logrus.Trace("generatorTemplate.Init")
  135. return nil
  136. }
  137.  
  138. // Finalize should write finish up functions, and any other content that's not
  139. // generated per-type.
  140. func (g *generatorTemplate) Finalize(*generator.Context, io.Writer) error {
  141. logrus.Trace("generatorTemplate.Finalize")
  142. return nil
  143. }
  144.  
  145. // PackageVars should emit an array of variable lines. They will be
  146. // placed in a var ( ... ) block. There's no need to include a leading
  147. // \t or trailing \n.
  148. func (g *generatorTemplate) PackageVars(*generator.Context) []string {
  149. logrus.Trace("generatorTemplate.PackageVars")
  150. return nil
  151. }
  152.  
  153. // PackageConsts should emit an array of constant lines. They will be
  154. // placed in a const ( ... ) block. There's no need to include a leading
  155. // \t or trailing \n.
  156. func (g *generatorTemplate) PackageConsts(*generator.Context) []string {
  157. logrus.Trace("generatorTemplate.PackageConsts")
  158. return nil
  159. }
  160.  
  161. // GenerateType should emit the code for a particular type.
  162. func (g *generatorTemplate) GenerateType(ctx *generator.Context, t *types.Type, w io.Writer) error {
  163. logrus.Trace("generatorTemplate.GenerateType ", t, reflect.TypeOf(w))
  164. names := strings.Split(t.Name.String(), ".")
  165. if _, err := fmt.Fprintf(w, "type %s struct {\n", names[len(names)-1]); err != nil {
  166. return err
  167. }
  168. for _, m := range t.Members {
  169. logrus.Trace(m.String())
  170. if _, err := fmt.Fprintln(w, m.String()); err != nil {
  171. return err
  172. }
  173. }
  174. if _, err := fmt.Fprintf(w, "}"); err != nil {
  175. return err
  176. }
  177. return nil
  178. }
  179.  
  180. // Imports should return a list of necessary imports. They will be
  181. // formatted correctly. You do not need to include quotation marks,
  182. // return only the package name; alternatively, you can also return
  183. // imports in the format `name "path/to/pkg"`. Imports will be called
  184. // after Init, PackageVars, PackageConsts, and GenerateType, to allow
  185. // you to keep track of what imports you actually need.
  186. func (g *generatorTemplate) Imports(*generator.Context) []string {
  187. logrus.Trace("generatorTemplate.Imports")
  188. return nil
  189. }
  190.  
  191. // Preferred file name of this generator, not including a path. It is
  192. // allowed for multiple generators to use the same filename, but it's
  193. // up to you to make sure they don't have colliding import names.
  194. // TODO: provide per-file import tracking, removing the requirement
  195. // that generators coordinate..
  196. func (g *generatorTemplate) Filename() string {
  197. logrus.Trace("generatorTemplate.Filename")
  198. return g.OptionalName + ".go"
  199. }
  200. ```
Add Comment
Please, Sign In to add comment