Debian Codegraph @

v2/generator/go_generator.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 "io"
21
22 "k8s.io/gengo/v2/namer"
23 "k8s.io/gengo/v2/types"
24 )
25
26 const (
27 GoFileType = "go"
28 )
29
30 // GoGenerator implements a do-nothing Generator for Go files. It can be
31 // used as a base for custom Generators, which embed it and then define the
32 // methods they need to specialize.
33 type GoGenerator struct {
34 // OutputFilename is used as the Generator's name, and filename.
35 OutputFilename string
36
37 // Body, if present, will be used as the return from the "Init" method.
38 // This causes it to be static content for the entire file if no other
39 // generator touches the file.
40 OptionalBody []byte
41 }
42
43 func (gg GoGenerator) Name() string { return gg.OutputFilename }
44 func (gg GoGenerator) Filter(*Context, *types.Type) bool { return true }
45 func (gg GoGenerator) Namers(*Context) namer.NameSystems { return nil }
46 func (gg GoGenerator) Imports(*Context) []string { return []string{} }
47 func (gg GoGenerator) PackageVars(*Context) []string { return []string{} }
48 func (gg GoGenerator) PackageConsts(*Context) []string { return []string{} }
49 func (gg GoGenerator) GenerateType(*Context, *types.Type, io.Writer) error { return nil }
50 func (gg GoGenerator) Filename() string { return gg.OutputFilename }
51 func (gg GoGenerator) FileType() string { return GoFileType }
52 func (gg GoGenerator) Finalize(*Context, io.Writer) error { return nil }
53
54 func (gg GoGenerator) Init(c *Context, w io.Writer) error {
55 _, err := w.Write(gg.OptionalBody)
56 return err
57 }
58
59 var (
60 _ = Generator(GoGenerator{})
61 )