Debian Codegraph @

v2/generator/simple_target.go

raw
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/v2/types"
21 )
22
23 // SimpleTarget is implements Target in terms of static configuration.
24 // The package name, path, and dir are required to be non-empty.
25 type SimpleTarget struct {
26 // PkgName is the name of the resulting package (as in "package xxxx").
27 // Required.
28 PkgName string
29 // PkgPath is the canonical Go import-path of the resulting package (as in
30 // "import example.com/xxxx/yyyy"). Required.
31 PkgPath string
32 // PkgDir is the location of the resulting package on disk (which may not
33 // exist yet). It may be absolute or relative to CWD. Required.
34 PkgDir string
35
36 // HeaderComment is emitted at the top of every output file. Optional.
37 HeaderComment []byte
38
39 // PkgDocComment is emitted after the header comment for a "doc.go" file.
40 // Optional.
41 PkgDocComment []byte
42
43 // FilterFunc will be called to implement Target.Filter. Optional.
44 FilterFunc func(*Context, *types.Type) bool
45
46 // GeneratorsFunc will be called to implement Target.Generators. Optional.
47 GeneratorsFunc func(*Context) []Generator
48 }
49
50 func (st SimpleTarget) Name() string { return st.PkgName }
51 func (st SimpleTarget) Path() string { return st.PkgPath }
52 func (st SimpleTarget) Dir() string { return st.PkgDir }
53
54 func (st SimpleTarget) Filter(c *Context, t *types.Type) bool {
55 if st.FilterFunc != nil {
56 return st.FilterFunc(c, t)
57 }
58 return true
59 }
60
61 func (st SimpleTarget) Generators(c *Context) []Generator {
62 if st.GeneratorsFunc != nil {
63 return st.GeneratorsFunc(c)
64 }
65 return nil
66 }
67
68 func (st SimpleTarget) Header(filename string) []byte {
69 if filename == "doc.go" {
70 return append(st.HeaderComment, st.PkgDocComment...)
71 }
72 return st.HeaderComment
73 }
74
75 var (
76 _ = Target(SimpleTarget{})
77 )