Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```golang
- package cmd
- import (
- "fmt"
- "io"
- "path/filepath"
- "reflect"
- "strings"
- "k8s.io/gengo/parser"
- "github.com/sirupsen/logrus"
- "k8s.io/gengo/generator"
- "k8s.io/gengo/namer"
- "k8s.io/gengo/types"
- )
- func generate(inDir, outDir string) error {
- b := parser.New()
- if err := b.AddDirRecursive(inDir); err != nil {
- return err
- }
- ctx, err := generator.NewContext(
- b,
- namer.NameSystems{
- "public": namer.NewPublicNamer(1),
- "raw": namer.NewRawNamer("", nil),
- //"defaultfn": defaultFnNamer(),
- //"objectdefaultfn": objectDefaultFnNamer(),
- },
- "public",
- )
- if err != nil {
- return err
- }
- pkgs := generatePackages(ctx)
- if err := ctx.ExecutePackages(outDir, pkgs); err != nil {
- return err
- }
- return nil
- }
- func generatePackages(context *generator.Context) generator.Packages {
- packages := generator.Packages{}
- for _, i := range context.Inputs {
- pkg := context.Universe[i]
- if pkg == nil {
- // If the input had no Go files, for example.
- continue
- }
- logrus.Trace("pkg.Path ", pkg.Path)
- name := strings.Split(filepath.Base(pkg.Path), ".")[0]
- logrus.Trace(name)
- packages = append(packages,
- &generator.DefaultPackage{
- PackageName: name,
- //PackagePath: path,
- //HeaderText: header,
- GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) {
- return []generator.Generator{
- newGeneratorTemplate(name),
- }
- },
- FilterFunc: func(c *generator.Context, t *types.Type) bool {
- return t.Name.Package == pkg.Path
- },
- })
- }
- return packages
- }
- type generatorTemplate struct {
- generator.DefaultGen
- }
- func newGeneratorTemplate(name string) generator.Generator {
- return &generatorTemplate{
- DefaultGen: generator.DefaultGen{
- OptionalName: name + ".tmpl",
- },
- }
- }
- //func (d DefaultGen) Name() string { return d.OptionalName }
- //func (d DefaultGen) Filter(*Context, *types.Type) bool { return true }
- //func (d DefaultGen) Namers(*Context) namer.NameSystems { return nil }
- //func (d DefaultGen) Imports(*Context) []string { return []string{} }
- //func (d DefaultGen) PackageVars(*Context) []string { return []string{} }
- //func (d DefaultGen) PackageConsts(*Context) []string { return []string{} }
- //func (d DefaultGen) GenerateType(*Context, *types.Type, io.Writer) error { return nil }
- //func (d DefaultGen) Filename() string { return d.OptionalName + ".go" }
- //func (d DefaultGen) FileType() string { return GolangFileType }
- //func (d DefaultGen) Finalize(*Context, io.Writer) error { return nil }
- // The name of this generator. Will be included in generated comments.
- func (g *generatorTemplate) Name() string {
- logrus.Trace("generatorTemplate.Name ")
- return "template"
- }
- // Filter should return true if this generator cares about this type.
- // (otherwise, GenerateType will not be called.)
- //
- // Filter is called before any of the generator's other functions;
- // subsequent calls will get a context with only the types that passed
- // this filter.
- func (g *generatorTemplate) Filter(ctx *generator.Context, t *types.Type) bool {
- logrus.Trace("generatorTemplate.Filter ", t)
- if t.Kind == types.Struct {
- return true
- }
- return false
- }
- // If this generator needs special namers, return them here. These will
- // override the original namers in the context if there is a collision.
- // You may return nil if you don't need special names. These names will
- // be available in the context passed to the rest of the generator's
- // functions.
- //
- // A use case for this is to return a namer that tracks imports.
- func (g *generatorTemplate) Namers(ctx *generator.Context) namer.NameSystems {
- logrus.Trace("generatorTemplate.Namers")
- return nil
- }
- // Init should write an init function, and any other content that's not
- // generated per-type. (It's not intended for generator specific
- // initialization! Do that when your Package constructs the
- // Generators.)
- func (g *generatorTemplate) Init(*generator.Context, io.Writer) error {
- logrus.Trace("generatorTemplate.Init")
- return nil
- }
- // Finalize should write finish up functions, and any other content that's not
- // generated per-type.
- func (g *generatorTemplate) Finalize(*generator.Context, io.Writer) error {
- logrus.Trace("generatorTemplate.Finalize")
- return nil
- }
- // PackageVars should emit an array of variable lines. They will be
- // placed in a var ( ... ) block. There's no need to include a leading
- // \t or trailing \n.
- func (g *generatorTemplate) PackageVars(*generator.Context) []string {
- logrus.Trace("generatorTemplate.PackageVars")
- return nil
- }
- // PackageConsts should emit an array of constant lines. They will be
- // placed in a const ( ... ) block. There's no need to include a leading
- // \t or trailing \n.
- func (g *generatorTemplate) PackageConsts(*generator.Context) []string {
- logrus.Trace("generatorTemplate.PackageConsts")
- return nil
- }
- // GenerateType should emit the code for a particular type.
- func (g *generatorTemplate) GenerateType(ctx *generator.Context, t *types.Type, w io.Writer) error {
- logrus.Trace("generatorTemplate.GenerateType ", t, reflect.TypeOf(w))
- names := strings.Split(t.Name.String(), ".")
- if _, err := fmt.Fprintf(w, "type %s struct {\n", names[len(names)-1]); err != nil {
- return err
- }
- for _, m := range t.Members {
- logrus.Trace(m.String())
- if _, err := fmt.Fprintln(w, m.String()); err != nil {
- return err
- }
- }
- if _, err := fmt.Fprintf(w, "}"); err != nil {
- return err
- }
- return nil
- }
- // Imports should return a list of necessary imports. They will be
- // formatted correctly. You do not need to include quotation marks,
- // return only the package name; alternatively, you can also return
- // imports in the format `name "path/to/pkg"`. Imports will be called
- // after Init, PackageVars, PackageConsts, and GenerateType, to allow
- // you to keep track of what imports you actually need.
- func (g *generatorTemplate) Imports(*generator.Context) []string {
- logrus.Trace("generatorTemplate.Imports")
- return nil
- }
- // Preferred file name of this generator, not including a path. It is
- // allowed for multiple generators to use the same filename, but it's
- // up to you to make sure they don't have colliding import names.
- // TODO: provide per-file import tracking, removing the requirement
- // that generators coordinate..
- func (g *generatorTemplate) Filename() string {
- logrus.Trace("generatorTemplate.Filename")
- return g.OptionalName + ".go"
- }
- ```
Add Comment
Please, Sign In to add comment