/
main.go
204 lines (184 loc) · 4.34 KB
/
main.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
198
199
200
201
202
203
204
package main
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"go/format"
"io"
"os"
"path/filepath"
"strings"
"text/template"
"time"
"github.com/cloudwego/thriftgo/generator/golang/extension/meta"
"github.com/cloudwego/thriftgo/parser"
"github.com/cloudwego/thriftgo/plugin"
"github.com/cloudwego/thriftgo/semantic"
)
var _ json.Marshaler
func main() {
input, err := io.ReadAll(os.Stdin)
if err != nil {
println("Failed to read plugin input:", err.Error())
os.Exit(1)
}
request, err := plugin.UnmarshalRequest(input)
if err != nil {
println("Failed to unmarshal plugin request:", err.Error())
os.Exit(1)
}
if request.Language != "go" {
println("Unsupported language:", request.Language)
os.Exit(1)
}
err = semantic.ResolveSymbols(request.AST)
if err != nil {
println("Failed to resolve symbols:", err.Error())
os.Exit(1)
}
debugln(func() {
requestJSON, err := json.MarshalIndent(request, "", " ")
if err != nil {
panic(err)
}
println(string(requestJSON))
})
gen := &generator{
ast: request.AST,
}
gen.parsePluginParameters(request.PluginParameters)
idlBytes, err := gen.encodeIDLToBytes()
if err != nil {
println("Failed to encode IDL:", err.Error())
os.Exit(1)
}
buf := &bytes.Buffer{}
tpl, err := template.New("").Parse(implTpl)
if err != nil {
println("Failed to parse plugin template:", err.Error())
os.Exit(1)
}
const genTimeLayout = "20060102150405Z"
err = tpl.Execute(buf, map[string]interface{}{
"PkgName": gen.getPkgName(),
"GenTime": time.Now().In(time.UTC).Format(genTimeLayout),
"Args": gen.args,
"IDLBytes": idlBytes,
})
if err != nil {
println("Failed to execute plugin template:", err.Error())
os.Exit(1)
}
code := buf.Bytes()
code, err = format.Source(code)
if err != nil {
println("Failed to format generated code:", err.Error())
os.Exit(1)
}
outputFile := gen.getOutputFile(request.OutputPath)
descCode := &plugin.Generated{
Content: string(code),
Name: &outputFile,
InsertionPoint: nil,
}
response := &plugin.Response{
Error: nil,
Contents: []*plugin.Generated{descCode},
Warnings: nil,
}
os.Exit(exit(response))
}
func exit(response *plugin.Response) int {
data, err := plugin.MarshalResponse(response)
if err != nil {
println("Failed to marshal response:", err.Error())
return 1
}
_, err = os.Stdout.Write(data)
if err != nil {
println("Error at writing response out:", err.Error())
return 1
}
return 0
}
type generator struct {
ast *parser.Thrift
args struct {
IsCombineService bool
AutoSetup bool
}
}
func (p *generator) parsePluginParameters(params []string) {
for _, arg := range params {
kv := strings.SplitN(arg, "=", 2)
var k, v = kv[0], ""
if len(kv) > 1 {
v = kv[1]
}
println("options: " + k + "=" + v)
switch k {
case "combine_service":
p.args.IsCombineService = true
case "auto_setup":
p.args.AutoSetup = true
}
}
}
func (p *generator) encodeIDLToBytes() (string, error) {
ast := p.ast
deleteName2CategoryInfo(ast)
buf, err := meta.Marshal(ast)
if err != nil {
return "", fmt.Errorf("cannot marshal parser.Thrift: %w", err)
}
gzBuf := bytes.NewBuffer(nil)
gzw := gzip.NewWriter(gzBuf)
_, err = gzw.Write(buf)
if err != nil {
return "", fmt.Errorf("cannot compress IDL bytes: %w", err)
}
err = gzw.Close()
if err != nil {
return "", fmt.Errorf("cannot compress IDL bytes: %w", err)
}
strb := strings.Builder{}
for i, x := range gzBuf.Bytes() {
if i > 0 && i%16 == 0 {
strb.WriteByte('\n')
}
strb.WriteString(fmt.Sprintf("0x%02x,", x))
}
return strb.String(), nil
}
func deleteName2CategoryInfo(ast *parser.Thrift) {
if ast == nil {
return
}
ast.Name2Category = nil
for _, inc := range ast.Includes {
deleteName2CategoryInfo(inc.Reference)
}
}
func (p *generator) getPkgName() string {
ast := p.ast
lastSvc := ast.Services[len(ast.Services)-1]
pkgName := strings.ToLower(lastSvc.Name)
if p.args.IsCombineService {
pkgName = "combineservice"
}
return pkgName
}
func (p *generator) getOutputFile(outputPath string) string {
ast := p.ast
var namespace string
for _, ns := range ast.Namespaces {
if ns.Language == "go" {
namespace = strings.ReplaceAll(ns.Name, ".", "/")
}
}
pkgName := p.getPkgName
filename := "plugin-reflect-gen.go"
descFile := filepath.Join(outputPath, namespace, pkgName(), filename)
return descFile
}