-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathheader.go
51 lines (45 loc) Β· 1.09 KB
/
header.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package codegen
import (
"goa.design/goa/pkg"
)
// Header returns a Go source file header section template.
func Header(title, pack string, imports []*ImportSpec) *SectionTemplate {
return &SectionTemplate{
Name: "source-header",
Source: headerT,
Data: map[string]interface{}{
"Title": title,
"ToolVersion": pkg.Version(),
"Pkg": pack,
"Imports": imports,
},
}
}
// AddImport adds imports to a section template that was generated with
// Header.
func AddImport(section *SectionTemplate, imprts ...*ImportSpec) {
if len(imprts) == 0 {
return
}
var specs []*ImportSpec
if data, ok := section.Data.(map[string]interface{}); ok {
if imports, ok := data["Imports"]; ok {
specs = imports.([]*ImportSpec)
}
data["Imports"] = append(specs, imprts...)
}
}
const (
headerT = `{{if .Title}}// Code generated by goa {{.ToolVersion}}, DO NOT EDIT.
//
// {{.Title}}
//
// Command:
{{comment commandLine}}
{{end}}package {{.Pkg}}
{{if .Imports}}import {{if gt (len .Imports) 1}}(
{{end}}{{range .Imports}} {{.Code}}
{{end}}{{if gt (len .Imports) 1}})
{{end}}
{{end}}`
)