forked from moul/protoc-gen-gotemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
197 lines (178 loc) · 5.54 KB
/
encoder.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"bytes"
"log"
"os"
"path/filepath"
"strings"
"text/template"
"time"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/golang/protobuf/protoc-gen-go/plugin"
pgghelpers "github.com/moul/protoc-gen-gotemplate/helpers"
)
type GenericTemplateBasedEncoder struct {
templateDir string
service *descriptor.ServiceDescriptorProto
file *descriptor.FileDescriptorProto
enum []*descriptor.EnumDescriptorProto
debug bool
destinationDir string
}
type Ast struct {
BuildDate time.Time `json:"build-date"`
BuildHostname string `json:"build-hostname"`
BuildUser string `json:"build-user"`
GoPWD string `json:"go-pwd,omitempty"`
PWD string `json:"pwd"`
Debug bool `json:"debug"`
DestinationDir string `json:"destination-dir"`
File *descriptor.FileDescriptorProto `json:"file"`
RawFilename string `json:"raw-filename"`
Filename string `json:"filename"`
TemplateDir string `json:"template-dir"`
Service *descriptor.ServiceDescriptorProto `json:"service"`
Enum []*descriptor.EnumDescriptorProto `json:"enum"`
}
func NewGenericServiceTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto, debug bool, destinationDir string) (e *GenericTemplateBasedEncoder) {
e = &GenericTemplateBasedEncoder{
service: service,
file: file,
templateDir: templateDir,
debug: debug,
destinationDir: destinationDir,
enum: file.GetEnumType(),
}
if debug {
log.Printf("new encoder: file=%q service=%q template-dir=%q", file.GetName(), service.GetName(), templateDir)
}
return
}
func NewGenericTemplateBasedEncoder(templateDir string, file *descriptor.FileDescriptorProto, debug bool, destinationDir string) (e *GenericTemplateBasedEncoder) {
e = &GenericTemplateBasedEncoder{
service: nil,
file: file,
templateDir: templateDir,
enum: file.GetEnumType(),
debug: debug,
destinationDir: destinationDir,
}
if debug {
log.Printf("new encoder: file=%q template-dir=%q", file.GetName(), templateDir)
}
return
}
func (e *GenericTemplateBasedEncoder) templates() ([]string, error) {
filenames := []string{}
err := filepath.Walk(e.templateDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if filepath.Ext(path) != ".tmpl" {
return nil
}
rel, err := filepath.Rel(e.templateDir, path)
if err != nil {
return err
}
if e.debug {
log.Printf("new template: %q", rel)
}
filenames = append(filenames, rel)
return nil
})
return filenames, err
}
func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, error) {
// prepare the ast passed to the template engine
hostname, _ := os.Hostname()
pwd, _ := os.Getwd()
goPwd := ""
if os.Getenv("GOPATH") != "" {
goPwd, _ = filepath.Rel(os.Getenv("GOPATH")+"/src", pwd)
if strings.Contains(goPwd, "../") {
goPwd = ""
}
}
ast := Ast{
BuildDate: time.Now(),
BuildHostname: hostname,
BuildUser: os.Getenv("USER"),
PWD: pwd,
GoPWD: goPwd,
File: e.file,
TemplateDir: e.templateDir,
DestinationDir: e.destinationDir,
RawFilename: templateFilename,
Filename: "",
Service: e.service,
Enum: e.enum,
}
buffer := new(bytes.Buffer)
tmpl, err := template.New("").Funcs(pgghelpers.ProtoHelpersFuncMap).Parse(templateFilename)
if err != nil {
return nil, err
}
if err := tmpl.Execute(buffer, ast); err != nil {
return nil, err
}
ast.Filename = buffer.String()
return &ast, nil
}
func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (string, string, error) {
// initialize template engine
fullPath := filepath.Join(e.templateDir, templateFilename)
templateName := filepath.Base(fullPath)
tmpl, err := template.New(templateName).Funcs(pgghelpers.ProtoHelpersFuncMap).ParseFiles(fullPath)
if err != nil {
return "", "", err
}
ast, err := e.genAst(templateFilename)
if err != nil {
return "", "", err
}
// generate the content
buffer := new(bytes.Buffer)
if err := tmpl.Execute(buffer, ast); err != nil {
return "", "", err
}
return buffer.String(), ast.Filename, nil
}
func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse_File {
templates, err := e.templates()
if err != nil {
log.Fatalf("cannot get templates from %q: %v", e.templateDir, err)
}
length := len(templates)
files := make([]*plugin_go.CodeGeneratorResponse_File, 0, length)
errChan := make(chan error, length)
resultChan := make(chan *plugin_go.CodeGeneratorResponse_File, length)
for _, templateFilename := range templates {
go func(tmpl string) {
content, translatedFilename, err := e.buildContent(tmpl)
if err != nil {
errChan <- err
return
}
filename := translatedFilename[:len(translatedFilename)-len(".tmpl")]
resultChan <- &plugin_go.CodeGeneratorResponse_File{
Content: &content,
Name: &filename,
}
}(templateFilename)
}
for i := 0; i < length; i++ {
select {
case f := <-resultChan:
files = append(files, f)
case err = <-errChan:
}
}
if err != nil {
panic(err)
}
return files
}