-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranger.go
181 lines (147 loc) · 4.36 KB
/
ranger.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
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: MPL-2.0
package generator
import (
"bufio"
"bytes"
"html/template"
_ "embed"
pgs "github.com/lyft/protoc-gen-star"
pgsgo "github.com/lyft/protoc-gen-star/lang/go"
)
var (
//go:embed templates/gofile.template
embeddedTemplateGofile string
//go:embed templates/service.template
embeddedTemplateService string
TMP_GOFILE_FILE *template.Template
TMP_SERVICE_DEFINITION *template.Template
)
func noescape(str string) template.HTML {
return template.HTML(str)
}
func init() {
var fn = template.FuncMap{
"noescape": noescape,
"gotype": goMessageType,
}
// load file content
TMP_GOFILE_FILE = template.New("file")
TMP_GOFILE_FILE.Funcs(fn)
tmpl, err := TMP_GOFILE_FILE.Parse(embeddedTemplateGofile)
if err != nil {
panic(err)
}
TMP_GOFILE_FILE = tmpl
// load service content
TMP_SERVICE_DEFINITION = template.New("service")
TMP_SERVICE_DEFINITION.Funcs(fn)
tmpl, err = TMP_SERVICE_DEFINITION.Parse(embeddedTemplateService)
if err != nil {
panic(err)
}
TMP_SERVICE_DEFINITION = tmpl
}
func New() *rangerc {
return &rangerc{&pgs.ModuleBase{}}
}
type rangerc struct {
*pgs.ModuleBase
}
func (f *rangerc) Name() string {
return "ranger"
}
func (fc *rangerc) Execute(targets map[string]pgs.File, pkgs map[string]pgs.Package) []pgs.Artifact {
for _, f := range targets {
ctx := fc.Push(f.Name().String())
goctx := pgsgo.InitContext(ctx.Parameters())
pkgname := goctx.PackageName(f).String()
fc.Debugf("processing file %s with pkg %s", f.Name(), pkgname)
// do not create a file if no service is part of the definition
if len(f.Services()) == 0 {
continue
}
pkg := f.Package()
servicesRendered := ""
imports := make(map[string]bool)
// iterate over each service
for i, service := range f.Services() {
// collect import paths for each protobuf method
for _, m := range service.Methods() {
fc.Debugf("signature %s, %s, %s", m.Name(), m.Input().Name(), m.Output().Name())
if pkg != m.Input().Package() {
inputPkg := goctx.ImportPath(m.Input()).String()
fc.Debugf("found additional import in input: %s", inputPkg)
imports[inputPkg] = true
}
if pkg != m.Output().Package() {
outputPkg := goctx.ImportPath(m.Output()).String()
fc.Debugf("found additional import in output: %s", outputPkg)
imports[outputPkg] = true
}
}
// render service
service, err := fc.renderService(goServiceRenderOpts{
Pkg: pkg,
Service: service,
}, i)
fc.CheckErr(err, "unable to render ", service, " to proto")
servicesRendered += service
}
// generate complete file, services are included
fileContent, err := fc.renderFile(goFileRenderOpts{
Version: "version",
Source: f.Name().String(),
Package: pkgname,
Service: servicesRendered,
Imports: imports,
})
fc.CheckErr(err, "unable to convert ", f.Name().String(), " to proto")
fp := pgs.FilePath(ctx.OutputPath())
fc.AddGeneratorFile(fp.SetBase(f.Name().String()).SetExt(".ranger.go").String(), fileContent)
fc.Pop()
}
fc.Debugf("processing ranger generator completed")
return fc.Artifacts()
}
// those are the input options for the gofile.templace
type goFileRenderOpts struct {
Version string
Source string
Package string
Service string
Imports map[string]bool
}
func (fc *rangerc) renderFile(renderOpts goFileRenderOpts) (string, error) {
return fc.render(TMP_GOFILE_FILE, renderOpts)
}
// those are the input options for the service.templace
type goServiceRenderOpts struct {
Pkg pgs.Package
Service pgs.Service
}
func (fc *rangerc) renderService(renderOpts goServiceRenderOpts, index int) (string, error) {
return fc.render(TMP_SERVICE_DEFINITION, renderOpts)
}
// render a given go template
func (fc *rangerc) render(tmplate *template.Template, data interface{}) (string, error) {
var buf bytes.Buffer
writer := bufio.NewWriter(&buf)
err := tmplate.Execute(writer, data)
if err != nil {
return "", err
}
writer.Flush()
return buf.String(), nil
}
// checks if the pkg is identical of the message type pkg. if the packages
// are not identical, it adds the
func goMessageType(pkg pgs.Package, m pgs.Message) string {
goctx := pgsgo.InitContext(pgs.Parameters{})
typeName := pgsgo.PGGUpperCamelCase(m.Name()).String()
// if the method
if pkg == m.Package() {
return typeName
}
return goctx.PackageName(m.Package()).String() + "." + typeName
}