-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathmain.go
More file actions
217 lines (201 loc) · 6.16 KB
/
main.go
File metadata and controls
217 lines (201 loc) · 6.16 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"os"
"strconv"
)
type operationInfo struct {
FieldName string
OperationName string
}
type serviceInfo struct {
VarName string
ServiceName string
Operations []operationInfo
}
func main() {
var inputPath string
var outputPath string
flag.StringVar(&inputPath, "input", "", "input generated proto-model service file")
flag.StringVar(&outputPath, "output", "", "output registry file")
flag.Parse()
if inputPath == "" || outputPath == "" {
fmt.Fprintln(os.Stderr, "both -input and -output are required")
os.Exit(2)
}
services, err := parseServices(inputPath)
if err != nil {
fmt.Fprintf(os.Stderr, "parse services: %v\n", err)
os.Exit(1)
}
if len(services) == 0 {
fmt.Fprintln(os.Stderr, "no nexus services found")
os.Exit(1)
}
source, err := renderRegistry(services)
if err != nil {
fmt.Fprintf(os.Stderr, "render registry: %v\n", err)
os.Exit(1)
}
if err := os.WriteFile(outputPath, source, 0o644); err != nil {
fmt.Fprintf(os.Stderr, "write registry: %v\n", err)
os.Exit(1)
}
}
func parseServices(path string) ([]serviceInfo, error) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, path, nil, parser.SkipObjectResolution)
if err != nil {
return nil, err
}
var services []serviceInfo
for _, decl := range file.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok || genDecl.Tok != token.VAR {
continue
}
for _, spec := range genDecl.Specs {
valueSpec, ok := spec.(*ast.ValueSpec)
if !ok || len(valueSpec.Names) != 1 || len(valueSpec.Values) != 1 {
continue
}
compositeLit, ok := valueSpec.Values[0].(*ast.CompositeLit)
if !ok {
continue
}
structType, ok := valueSpec.Type.(*ast.StructType)
if !ok {
structType, ok = compositeLit.Type.(*ast.StructType)
if !ok {
continue
}
}
serviceName, ok := compositeServiceName(compositeLit)
if !ok {
continue
}
var operations []operationInfo
for _, field := range structType.Fields.List {
for _, name := range field.Names {
if name.Name == "ServiceName" {
continue
}
operationName, ok := compositeOperationName(compositeLit, name.Name)
if !ok {
return nil, fmt.Errorf("service %s field %s missing operation literal", valueSpec.Names[0].Name, name.Name)
}
operations = append(operations, operationInfo{
FieldName: name.Name,
OperationName: operationName,
})
}
}
if len(operations) == 0 {
continue
}
services = append(services, serviceInfo{
VarName: valueSpec.Names[0].Name,
ServiceName: serviceName,
Operations: operations,
})
}
}
return services, nil
}
func compositeServiceName(lit *ast.CompositeLit) (string, bool) {
for _, elt := range lit.Elts {
kv, ok := elt.(*ast.KeyValueExpr)
if !ok {
continue
}
key, ok := kv.Key.(*ast.Ident)
if !ok || key.Name != "ServiceName" {
continue
}
return stringLiteral(kv.Value)
}
return "", false
}
func compositeOperationName(lit *ast.CompositeLit, fieldName string) (string, bool) {
for _, elt := range lit.Elts {
kv, ok := elt.(*ast.KeyValueExpr)
if !ok {
continue
}
key, ok := kv.Key.(*ast.Ident)
if !ok || key.Name != fieldName {
continue
}
call, ok := kv.Value.(*ast.CallExpr)
if !ok || len(call.Args) == 0 {
return "", false
}
return stringLiteral(call.Args[len(call.Args)-1])
}
return "", false
}
func stringLiteral(expr ast.Expr) (string, bool) {
lit, ok := expr.(*ast.BasicLit)
if !ok || lit.Kind != token.STRING {
return "", false
}
value, err := strconv.Unquote(lit.Value)
if err != nil {
return "", false
}
return value, true
}
func renderRegistry(services []serviceInfo) ([]byte, error) {
var buf bytes.Buffer
buf.WriteString("// Code generated by systemnexusgen. DO NOT EDIT.\n\n")
buf.WriteString("package workflowservicenexus\n\n")
buf.WriteString("import (\n")
buf.WriteString("\t\"reflect\"\n\n")
buf.WriteString("\t\"google.golang.org/protobuf/proto\"\n")
buf.WriteString(")\n\n")
buf.WriteString("type TemporalNexusOperationKey struct {\n")
buf.WriteString("\tServiceName string\n")
buf.WriteString("\tOperationName string\n")
buf.WriteString("}\n\n")
buf.WriteString("type TemporalNexusOperationReference interface {\n")
buf.WriteString("\tName() string\n")
buf.WriteString("\tInputType() reflect.Type\n")
buf.WriteString("\tOutputType() reflect.Type\n")
buf.WriteString("}\n\n")
buf.WriteString("var TemporalNexusOperationRegistry = map[TemporalNexusOperationKey]TemporalNexusOperationReference{\n")
for _, service := range services {
for _, operation := range service.Operations {
fmt.Fprintf(&buf, "\t{ServiceName: %q, OperationName: %q}: %s.%s,\n", service.ServiceName, operation.OperationName, service.VarName, operation.FieldName)
}
}
buf.WriteString("}\n\n")
buf.WriteString("func GetTemporalNexusOperation(serviceName, operationName string) TemporalNexusOperationReference {\n")
buf.WriteString("\treturn TemporalNexusOperationRegistry[TemporalNexusOperationKey{ServiceName: serviceName, OperationName: operationName}]\n")
buf.WriteString("}\n\n")
buf.WriteString("func IsTemporalNexusOperation(serviceName, operationName string) bool {\n")
buf.WriteString("\treturn GetTemporalNexusOperation(serviceName, operationName) != nil\n")
buf.WriteString("}\n\n")
buf.WriteString("func NewTemporalNexusOperationInputIfSystemOperation(serviceName, operationName string) proto.Message {\n")
buf.WriteString("\top := GetTemporalNexusOperation(serviceName, operationName)\n")
buf.WriteString("\tif op == nil {\n")
buf.WriteString("\t\treturn nil\n")
buf.WriteString("\t}\n")
buf.WriteString("\tinputType := op.InputType()\n")
buf.WriteString("\tif inputType == nil {\n")
buf.WriteString("\t\treturn nil\n")
buf.WriteString("\t}\n")
buf.WriteString("\tif inputType.Kind() == reflect.Ptr {\n")
buf.WriteString("\t\tmsg, _ := reflect.New(inputType.Elem()).Interface().(proto.Message)\n")
buf.WriteString("\t\treturn msg\n")
buf.WriteString("\t}\n")
buf.WriteString("\tmsg, _ := reflect.New(inputType).Interface().(proto.Message)\n")
buf.WriteString("\treturn msg\n")
buf.WriteString("}\n")
return format.Source(buf.Bytes())
}