mirror of https://github.com/kubernetes/kops.git
codegen template improvments
* Include the header so we generate code that passes the header-check * Some tweaks to make golint happier
This commit is contained in:
parent
bda37d4921
commit
cc80e75e90
|
@ -28,7 +28,25 @@ type FitaskGenerator struct {
|
|||
|
||||
var _ codegen.Generator = &FitaskGenerator{}
|
||||
|
||||
const headerDef = `
|
||||
const fileHeaderDef = `/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
`
|
||||
|
||||
var preambleDef = `
|
||||
package {{.Package}}
|
||||
|
||||
import (
|
||||
|
@ -44,6 +62,7 @@ const perTypeDef = `
|
|||
// JSON marshalling boilerplate
|
||||
type real{{.Name}} {{.Name}}
|
||||
|
||||
// UnmarshalJSON implements conversion to JSON, supporitng an alternate specification of the object as a string
|
||||
func (o *{{.Name}}) UnmarshalJSON(data []byte) error {
|
||||
var jsonName string
|
||||
if err := json.Unmarshal(data, &jsonName); err == nil {
|
||||
|
@ -61,16 +80,19 @@ func (o *{{.Name}}) UnmarshalJSON(data []byte) error {
|
|||
|
||||
var _ fi.HasName = &{{.Name}}{}
|
||||
|
||||
func (e *{{.Name}}) GetName() *string {
|
||||
return e.Name
|
||||
// GetName returns the Name of the object, implementing fi.HasName
|
||||
func (o *{{.Name}}) GetName() *string {
|
||||
return o.Name
|
||||
}
|
||||
|
||||
func (e *{{.Name}}) SetName(name string) {
|
||||
e.Name = &name
|
||||
// SetName sets the Name of the object, implementing fi.SetName
|
||||
func (o *{{.Name}}) SetName(name string) {
|
||||
o.Name = &name
|
||||
}
|
||||
|
||||
func (e *{{.Name}}) String() string {
|
||||
return fi.TaskAsString(e)
|
||||
// String is the stringer function for the task, producing readable output using fi.TaskAsString
|
||||
func (o *{{.Name}}) String() string {
|
||||
return fi.TaskAsString(o)
|
||||
}
|
||||
`
|
||||
|
||||
|
@ -80,8 +102,14 @@ func (g *FitaskGenerator) Init(parser *codegen.GoParser) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (g *FitaskGenerator) WriteHeader(w io.Writer) error {
|
||||
t := template.Must(template.New("Header").Parse(headerDef))
|
||||
func (g *FitaskGenerator) WriteFileHeader(w io.Writer) error {
|
||||
t := template.Must(template.New("FileHeader").Parse(fileHeaderDef))
|
||||
|
||||
return t.Execute(w, g)
|
||||
}
|
||||
|
||||
func (g *FitaskGenerator) WritePreamble(w io.Writer) error {
|
||||
t := template.Must(template.New("Preamble").Parse(preambleDef))
|
||||
|
||||
return t.Execute(w, g)
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ import "io"
|
|||
|
||||
type Generator interface {
|
||||
Init(parser *GoParser) error
|
||||
WriteHeader(w io.Writer) error
|
||||
WriteFileHeader(w io.Writer) error
|
||||
WritePreamble(w io.Writer) error
|
||||
WriteType(w io.Writer, typeName string) error
|
||||
}
|
||||
|
|
|
@ -86,9 +86,14 @@ func RunGenerator(key string, generator Generator) {
|
|||
|
||||
var b bytes.Buffer
|
||||
|
||||
err = generator.WriteFileHeader(&b)
|
||||
if err != nil {
|
||||
log.Fatalf("error generating header: %v", err)
|
||||
}
|
||||
|
||||
fmt.Fprintf(&b, "// Code generated by \"%q %s\"; DO NOT EDIT\n\n", key, strings.Join(os.Args[1:], " "))
|
||||
|
||||
err = generator.WriteHeader(&b)
|
||||
err = generator.WritePreamble(&b)
|
||||
if err != nil {
|
||||
log.Fatalf("error generating header: %v", err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue