Skip to content

Commit 5c55f00

Browse files
authored
Generate protobuf type-conversion functions (#118)
1 parent 72a0dc3 commit 5c55f00

8 files changed

Lines changed: 584 additions & 22 deletions

File tree

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,16 @@ generate-rpcwrappers:
7979
rm -rf $(GENRPCWRAPPERS_DIR)/*_gen.go
8080
cd $(GENRPCWRAPPERS_DIR); go run . -service frontend -license_file ../../../LICENSE
8181
cp $(GENRPCWRAPPERS_DIR)/lazy_client_gen.go client/frontend/lazy_client_gen.go
82+
mkdir -p proto/compat
83+
cp $(GENRPCWRAPPERS_DIR)/conversion_gen.go proto/compat/frontend_conversion_gen.go
8284

8385
rm -rf ./cmd/tools/genrpcwrappers/*_gen.go
8486
cd $(GENRPCWRAPPERS_DIR); go run . -service admin -license_file ../../../LICENSE
8587
cp ./cmd/tools/genrpcwrappers/lazy_client_gen.go client/admin/lazy_client_gen.go
88+
cp $(GENRPCWRAPPERS_DIR)/conversion_gen.go proto/compat/admin_conversion_gen.go
8689

8790
rm -rf ./cmd/tools/genrpcwrappers/*_gen.go
88-
89-
go fmt ./client/...
91+
go fmt ./client/... ./proto/compat/...
9092

9193
test: generate-test-certs
9294
go test $(TEST_ARG) ./...

client/admin/lazy_client_gen.go

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/frontend/lazy_client_gen.go

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/tools/genrpcwrappers/extra.go

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,51 @@
11
package main
22

3-
import "io"
3+
import (
4+
"io"
5+
)
46

57
var (
68
lazyClientMap = map[string]string{
79
"admin": "GetAdminClient",
810
"frontend": "GetWorkflowServiceClient",
911
}
12+
13+
ignoreMethodsNotIn122 = map[string]bool{
14+
// adminservice methods
15+
"AddTasks": true,
16+
"CancelDLQJob": true,
17+
"DeepHealthCheck": true,
18+
"DescribeDLQJob": true,
19+
"DescribeTaskQueuePartition": true,
20+
"ForceUnloadTaskQueuePartition": true,
21+
"GenerateLastHistoryReplicationTasks": true,
22+
"GetDLQTasks": true,
23+
"GetWorkflowExecutionRawHistory": true,
24+
"ImportWorkflowExecution": true,
25+
"ListQueues": true,
26+
"MergeDLQTasks": true,
27+
"PurgeDLQTasks": true,
28+
"SyncWorkflowState": true,
29+
30+
// workflowservice
31+
"DescribeDeployment": true,
32+
"ExecuteMultiOperation": true,
33+
"GetCurrentDeployment": true,
34+
"GetDeploymentReachability": true,
35+
"GetWorkerVersioningRules": true,
36+
"ListDeployments": true,
37+
"PauseActivityById": true,
38+
"PollNexusTaskQueue": true,
39+
"ResetActivityById": true,
40+
"RespondNexusTaskCompleted": true,
41+
"RespondNexusTaskFailed": true,
42+
"SetCurrentDeployment": true,
43+
"ShutdownWorker": true,
44+
"UnpauseActivityById": true,
45+
"UpdateActivityOptionsById": true,
46+
"UpdateWorkerVersioningRules": true,
47+
"UpdateWorkflowExecutionOptions": true,
48+
}
1049
)
1150

1251
func init() {
@@ -59,3 +98,43 @@ func (c *lazyClient) {{.Method}}(
5998
}
6099
`)
61100
}
101+
102+
func generate122ConversionCode(w io.Writer, service service) {
103+
writeTemplatedCode(w, service, `
104+
package compat
105+
import (
106+
svc "{{.ServicePackagePath}}"
107+
svc122 "{{.ServicePackagePath122}}"
108+
"github.com/temporalio/s2s-proxy/common"
109+
)
110+
111+
// {{.ServiceNameTitle}}ConvertTo122 accepts a protobuf type and returns
112+
// the corresponding gogo-based protobuf type from Temporal v1.22.
113+
func {{.ServiceNameTitle}}ConvertTo122(vAny any) (common.Marshaler, bool) {
114+
switch vAny.(type) {
115+
`)
116+
117+
writeTemplatedMethods122(w, service, "conversion", `
118+
case *svc.{{.Method}}Response:
119+
return &svc122.{{.Method}}Response{}, true
120+
case *svc.{{.Method}}Request:
121+
return &svc122.{{.Method}}Request{}, true
122+
`)
123+
124+
writeTemplatedCode(w, service, `}
125+
return nil, false
126+
}
127+
`)
128+
}
129+
130+
func writeTemplatedMethods122(w io.Writer, service service, impl string, text string) {
131+
sType := service.clientType.Elem()
132+
for n := 0; n < sType.NumMethod(); n++ {
133+
m := sType.Method(n)
134+
if ignoreMethodsNotIn122[m.Name] {
135+
continue
136+
}
137+
138+
writeTemplatedMethod(w, service, impl, m, text)
139+
}
140+
}

cmd/tools/genrpcwrappers/main.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,13 @@ func panicIfErr(err error) {
156156
}
157157

158158
func writeTemplatedCode(w io.Writer, service service, text string) {
159+
pkgPath := service.clientType.Elem().PkgPath()
160+
titleCase := strings.ToUpper(service.name[:1]) + service.name[1:]
159161
panicIfErr(template.Must(template.New("code").Parse(text)).Execute(w, map[string]string{
160-
"ServiceName": service.name,
161-
"ServicePackagePath": service.clientType.Elem().PkgPath(),
162+
"ServiceName": service.name,
163+
"ServicePackagePath": pkgPath,
164+
"ServiceNameTitle": titleCase,
165+
"ServicePackagePath122": strings.Replace(pkgPath, "go.temporal.io", "github.com/temporalio/s2s-proxy/proto/1_22", 1),
162166
}))
163167
}
164168

@@ -409,21 +413,13 @@ func makeGetMatchingClient(reqType reflect.Type) string {
409413
panic("I don't know how to get a client from a " + t.String())
410414
}
411415

412-
func writeTemplatedMethod(w io.Writer, service service, impl string, m reflect.Method, text string) {
413-
key := fmt.Sprintf("%s.%s.%s", impl, service.name, m.Name)
414-
if ignoreMethod[key] {
415-
return
416-
}
417-
418-
mt := m.Type // should look like: func(context.Context, request reqType, opts []grpc.CallOption) (respType, error)
419-
if !mt.IsVariadic() ||
420-
mt.NumIn() != 3 ||
421-
mt.NumOut() != 2 ||
422-
mt.In(0).String() != "context.Context" ||
423-
mt.Out(1).String() != "error" {
424-
panic(key + " doesn't look like a grpc handler method")
425-
}
416+
func methodKey(impl string, svc service, m reflect.Method) string {
417+
return fmt.Sprintf("%s.%s.%s", impl, svc.name, m.Name)
418+
}
426419

420+
func writeTemplatedMethod(w io.Writer, service service, impl string, m reflect.Method, text string) {
421+
key := methodKey(impl, service, m)
422+
mt := m.Type
427423
reqType := mt.In(1)
428424
respType := mt.Out(0)
429425

@@ -460,7 +456,22 @@ func writeTemplatedMethod(w io.Writer, service service, impl string, m reflect.M
460456
func writeTemplatedMethods(w io.Writer, service service, impl string, text string) {
461457
sType := service.clientType.Elem()
462458
for n := 0; n < sType.NumMethod(); n++ {
463-
writeTemplatedMethod(w, service, impl, sType.Method(n), text)
459+
m := sType.Method(n)
460+
key := methodKey(impl, service, m)
461+
if ignoreMethod[key] {
462+
continue
463+
}
464+
465+
// should look like: func(context.Context, request reqType, opts []grpc.CallOption) (respType, error)
466+
mt := m.Type
467+
if !mt.IsVariadic() ||
468+
mt.NumIn() != 3 ||
469+
mt.NumOut() != 2 ||
470+
mt.In(0).String() != "context.Context" ||
471+
mt.Out(1).String() != "error" {
472+
panic(key + " doesn't look like a grpc handler method")
473+
}
474+
writeTemplatedMethod(w, service, impl, m, text)
464475
}
465476
}
466477

@@ -675,4 +686,6 @@ func main() {
675686
if svc.name == "admin" {
676687
callWithFile(generateACLServer, svc, "acl_server", "")
677688
}
689+
690+
callWithFile(generate122ConversionCode, svc, "conversion", "")
678691
}

common/marshaler.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package common
2+
3+
type Marshaler interface {
4+
Marshal() ([]byte, error)
5+
Unmarshal([]byte) error
6+
}

proto/compat/admin_conversion_gen.go

Lines changed: 162 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)