| 1 |
/* |
| 2 |
Copyright 2015 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package generator |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"io" |
| 22 |
|
| 23 |
"k8s.io/gengo/namer" |
| 24 |
"k8s.io/gengo/parser" |
| 25 |
"k8s.io/gengo/types" |
| 26 |
) |
| 27 |
|
| 28 |
// Package contains the contract for generating a package. |
| 29 |
type Package interface { |
| 30 |
// Name returns the package short name. |
| 31 |
Name() string |
| 32 |
// Path returns the package import path. |
| 33 |
Path() string |
| 34 |
// SourcePath returns the location of the package on disk. |
| 35 |
SourcePath() string |
| 36 |
|
| 37 |
// Filter should return true if this package cares about this type. |
| 38 |
// Otherwise, this type will be omitted from the type ordering for |
| 39 |
// this package. |
| 40 |
Filter(*Context, *types.Type) bool |
| 41 |
|
| 42 |
// Header should return a header for the file, including comment markers. |
| 43 |
// Useful for copyright notices and doc strings. Include an |
| 44 |
// autogeneration notice! Do not include the "package x" line. |
| 45 |
Header(filename string) []byte |
| 46 |
|
| 47 |
// Generators returns the list of generators for this package. It is |
| 48 |
// allowed for more than one generator to write to the same file. |
| 49 |
// A Context is passed in case the list of generators depends on the |
| 50 |
// input types. |
| 51 |
Generators(*Context) []Generator |
| 52 |
} |
| 53 |
|
| 54 |
type File struct { |
| 55 |
Name string |
| 56 |
FileType string |
| 57 |
PackageName string |
| 58 |
Header []byte |
| 59 |
PackagePath string |
| 60 |
PackageSourcePath string |
| 61 |
Imports map[string]struct{} |
| 62 |
Vars bytes.Buffer |
| 63 |
Consts bytes.Buffer |
| 64 |
Body bytes.Buffer |
| 65 |
} |
| 66 |
|
| 67 |
type FileType interface { |
| 68 |
AssembleFile(f *File, path string) error |
| 69 |
VerifyFile(f *File, path string) error |
| 70 |
} |
| 71 |
|
| 72 |
// Packages is a list of packages to generate. |
| 73 |
type Packages []Package |
| 74 |
|
| 75 |
// Generator is the contract for anything that wants to do auto-generation. |
| 76 |
// It's expected that the io.Writers passed to the below functions will be |
| 77 |
// ErrorTrackers; this allows implementations to not check for io errors, |
| 78 |
// making more readable code. |
| 79 |
// |
| 80 |
// The call order for the functions that take a Context is: |
| 81 |
// 1. Filter() // Subsequent calls see only types that pass this. |
| 82 |
// 2. Namers() // Subsequent calls see the namers provided by this. |
| 83 |
// 3. PackageVars() |
| 84 |
// 4. PackageConsts() |
| 85 |
// 5. Init() |
| 86 |
// 6. GenerateType() // Called N times, once per type in the context's Order. |
| 87 |
// 7. Imports() |
| 88 |
// |
| 89 |
// You may have multiple generators for the same file. |
| 90 |
type Generator interface { |
| 91 |
// The name of this generator. Will be included in generated comments. |
| 92 |
Name() string |
| 93 |
|
| 94 |
// Filter should return true if this generator cares about this type. |
| 95 |
// (otherwise, GenerateType will not be called.) |
| 96 |
// |
| 97 |
// Filter is called before any of the generator's other functions; |
| 98 |
// subsequent calls will get a context with only the types that passed |
| 99 |
// this filter. |
| 100 |
Filter(*Context, *types.Type) bool |
| 101 |
|
| 102 |
// If this generator needs special namers, return them here. These will |
| 103 |
// override the original namers in the context if there is a collision. |
| 104 |
// You may return nil if you don't need special names. These names will |
| 105 |
// be available in the context passed to the rest of the generator's |
| 106 |
// functions. |
| 107 |
// |
| 108 |
// A use case for this is to return a namer that tracks imports. |
| 109 |
Namers(*Context) namer.NameSystems |
| 110 |
|
| 111 |
// Init should write an init function, and any other content that's not |
| 112 |
// generated per-type. (It's not intended for generator specific |
| 113 |
// initialization! Do that when your Package constructs the |
| 114 |
// Generators.) |
| 115 |
Init(*Context, io.Writer) error |
| 116 |
|
| 117 |
// Finalize should write finish up functions, and any other content that's not |
| 118 |
// generated per-type. |
| 119 |
Finalize(*Context, io.Writer) error |
| 120 |
|
| 121 |
// PackageVars should emit an array of variable lines. They will be |
| 122 |
// placed in a var ( ... ) block. There's no need to include a leading |
| 123 |
// \t or trailing \n. |
| 124 |
PackageVars(*Context) []string |
| 125 |
|
| 126 |
// PackageConsts should emit an array of constant lines. They will be |
| 127 |
// placed in a const ( ... ) block. There's no need to include a leading |
| 128 |
// \t or trailing \n. |
| 129 |
PackageConsts(*Context) []string |
| 130 |
|
| 131 |
// GenerateType should emit the code for a particular type. |
| 132 |
GenerateType(*Context, *types.Type, io.Writer) error |
| 133 |
|
| 134 |
// Imports should return a list of necessary imports. They will be |
| 135 |
// formatted correctly. You do not need to include quotation marks, |
| 136 |
// return only the package name; alternatively, you can also return |
| 137 |
// imports in the format `name "path/to/pkg"`. Imports will be called |
| 138 |
// after Init, PackageVars, PackageConsts, and GenerateType, to allow |
| 139 |
// you to keep track of what imports you actually need. |
| 140 |
Imports(*Context) []string |
| 141 |
|
| 142 |
// Preferred file name of this generator, not including a path. It is |
| 143 |
// allowed for multiple generators to use the same filename, but it's |
| 144 |
// up to you to make sure they don't have colliding import names. |
| 145 |
// TODO: provide per-file import tracking, removing the requirement |
| 146 |
// that generators coordinate.. |
| 147 |
Filename() string |
| 148 |
|
| 149 |
// A registered file type in the context to generate this file with. If |
| 150 |
// the FileType is not found in the context, execution will stop. |
| 151 |
FileType() string |
| 152 |
} |
| 153 |
|
| 154 |
// Context is global context for individual generators to consume. |
| 155 |
type Context struct { |
| 156 |
// A map from the naming system to the names for that system. E.g., you |
| 157 |
// might have public names and several private naming systems. |
| 158 |
Namers namer.NameSystems |
| 159 |
|
| 160 |
// All the types, in case you want to look up something. |
| 161 |
Universe types.Universe |
| 162 |
|
| 163 |
// Incoming imports, i.e. packages importing the given package. |
| 164 |
incomingImports map[string][]string |
| 165 |
|
| 166 |
// Incoming transitive imports, i.e. the transitive closure of IncomingImports |
| 167 |
incomingTransitiveImports map[string][]string |
| 168 |
|
| 169 |
// All the user-specified packages. This is after recursive expansion. |
| 170 |
Inputs []string |
| 171 |
|
| 172 |
// The canonical ordering of the types (will be filtered by both the |
| 173 |
// Package's and Generator's Filter methods). |
| 174 |
Order []*types.Type |
| 175 |
|
| 176 |
// A set of types this context can process. If this is empty or nil, |
| 177 |
// the default "golang" filetype will be provided. |
| 178 |
FileTypes map[string]FileType |
| 179 |
|
| 180 |
// If true, Execute* calls will just verify that the existing output is |
| 181 |
// correct. (You may set this after calling NewContext.) |
| 182 |
Verify bool |
| 183 |
|
| 184 |
// Allows generators to add packages at runtime. |
| 185 |
builder *parser.Builder |
| 186 |
|
| 187 |
// If specified, trim the prefix from a package's path before writing files. |
| 188 |
TrimPathPrefix string |
| 189 |
} |
| 190 |
|
| 191 |
// NewContext generates a context from the given builder, naming systems, and |
| 192 |
// the naming system you wish to construct the canonical ordering from. |
| 193 |
func NewContext(b *parser.Builder, nameSystems namer.NameSystems, canonicalOrderName string) (*Context, error) { |
| 194 |
universe, err := b.FindTypes() |
| 195 |
if err != nil { |
| 196 |
return nil, err |
| 197 |
} |
| 198 |
|
| 199 |
c := &Context{ |
| 200 |
Namers: namer.NameSystems{}, |
| 201 |
Universe: universe, |
| 202 |
Inputs: b.FindPackages(), |
| 203 |
FileTypes: map[string]FileType{ |
| 204 |
GolangFileType: NewGolangFile(), |
| 205 |
}, |
| 206 |
builder: b, |
| 207 |
} |
| 208 |
|
| 209 |
for name, systemNamer := range nameSystems { |
| 210 |
c.Namers[name] = systemNamer |
| 211 |
if name == canonicalOrderName { |
| 212 |
orderer := namer.Orderer{Namer: systemNamer} |
| 213 |
c.Order = orderer.OrderUniverse(universe) |
| 214 |
} |
| 215 |
} |
| 216 |
return c, nil |
| 217 |
} |
| 218 |
|
| 219 |
// IncomingImports returns the incoming imports for each package. The map is lazily computed. |
| 220 |
func (ctxt *Context) IncomingImports() map[string][]string { |
| 221 |
if ctxt.incomingImports == nil { |
| 222 |
incoming := map[string][]string{} |
| 223 |
for _, pkg := range ctxt.Universe { |
| 224 |
for imp := range pkg.Imports { |
| 225 |
incoming[imp] = append(incoming[imp], pkg.Path) |
| 226 |
} |
| 227 |
} |
| 228 |
ctxt.incomingImports = incoming |
| 229 |
} |
| 230 |
return ctxt.incomingImports |
| 231 |
} |
| 232 |
|
| 233 |
// TransitiveIncomingImports returns the transitive closure of the incoming imports for each package. |
| 234 |
// The map is lazily computed. |
| 235 |
func (ctxt *Context) TransitiveIncomingImports() map[string][]string { |
| 236 |
if ctxt.incomingTransitiveImports == nil { |
| 237 |
ctxt.incomingTransitiveImports = transitiveClosure(ctxt.IncomingImports()) |
| 238 |
} |
| 239 |
return ctxt.incomingTransitiveImports |
| 240 |
} |
| 241 |
|
| 242 |
// AddDir adds a Go package to the context. The specified path must be a single |
| 243 |
// go package import path. GOPATH, GOROOT, and the location of your go binary |
| 244 |
// (`which go`) will all be searched, in the normal Go fashion. |
| 245 |
// Deprecated. Please use AddDirectory. |
| 246 |
func (ctxt *Context) AddDir(path string) error { |
| 247 |
ctxt.incomingImports = nil |
| 248 |
ctxt.incomingTransitiveImports = nil |
| 249 |
return ctxt.builder.AddDirTo(path, &ctxt.Universe) |
| 250 |
} |
| 251 |
|
| 252 |
// AddDirectory adds a Go package to the context. The specified path must be a |
| 253 |
// single go package import path. GOPATH, GOROOT, and the location of your go |
| 254 |
// binary (`which go`) will all be searched, in the normal Go fashion. |
| 255 |
func (ctxt *Context) AddDirectory(path string) (*types.Package, error) { |
| 256 |
ctxt.incomingImports = nil |
| 257 |
ctxt.incomingTransitiveImports = nil |
| 258 |
return ctxt.builder.AddDirectoryTo(path, &ctxt.Universe) |
| 259 |
} |