| 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 |
"k8s.io/gengo/types" |
| 21 |
) |
| 22 |
|
| 23 |
// DefaultPackage contains a default implementation of Package. |
| 24 |
type DefaultPackage struct { |
| 25 |
// Short name of package, used in the "package xxxx" line. |
| 26 |
PackageName string |
| 27 |
// Import path of the package, and the location on disk of the package. |
| 28 |
PackagePath string |
| 29 |
// The location of the package on disk. |
| 30 |
Source string |
| 31 |
|
| 32 |
// Emitted at the top of every file. |
| 33 |
HeaderText []byte |
| 34 |
|
| 35 |
// Emitted only for a "doc.go" file; appended to the HeaderText for |
| 36 |
// that file. |
| 37 |
PackageDocumentation []byte |
| 38 |
|
| 39 |
// If non-nil, will be called on "Generators"; otherwise, the static |
| 40 |
// list will be used. So you should set only one of these two fields. |
| 41 |
GeneratorFunc func(*Context) []Generator |
| 42 |
GeneratorList []Generator |
| 43 |
|
| 44 |
// Optional; filters the types exposed to the generators. |
| 45 |
FilterFunc func(*Context, *types.Type) bool |
| 46 |
} |
| 47 |
|
| 48 |
func (d *DefaultPackage) Name() string { return d.PackageName } |
| 49 |
func (d *DefaultPackage) Path() string { return d.PackagePath } |
| 50 |
func (d *DefaultPackage) SourcePath() string { return d.Source } |
| 51 |
|
| 52 |
func (d *DefaultPackage) Filter(c *Context, t *types.Type) bool { |
| 53 |
if d.FilterFunc != nil { |
| 54 |
return d.FilterFunc(c, t) |
| 55 |
} |
| 56 |
return true |
| 57 |
} |
| 58 |
|
| 59 |
func (d *DefaultPackage) Generators(c *Context) []Generator { |
| 60 |
if d.GeneratorFunc != nil { |
| 61 |
return d.GeneratorFunc(c) |
| 62 |
} |
| 63 |
return d.GeneratorList |
| 64 |
} |
| 65 |
|
| 66 |
func (d *DefaultPackage) Header(filename string) []byte { |
| 67 |
if filename == "doc.go" { |
| 68 |
return append(d.HeaderText, d.PackageDocumentation...) |
| 69 |
} |
| 70 |
return d.HeaderText |
| 71 |
} |
| 72 |
|
| 73 |
var ( |
| 74 |
_ = Package(&DefaultPackage{}) |
| 75 |
) |