|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "strings" |
| 21 | + |
| 22 | + "google.golang.org/protobuf/compiler/protogen" |
| 23 | +) |
| 24 | + |
| 25 | +// generator is a Go code generator that uses ttrpc.Server and ttrpc.Client. |
| 26 | +// Unlike the original gogo version, this doesn't generate serializers for message types and |
| 27 | +// let protoc-gen-go handle them. |
| 28 | +type generator struct { |
| 29 | + out *protogen.GeneratedFile |
| 30 | + |
| 31 | + ident struct { |
| 32 | + context string |
| 33 | + server string |
| 34 | + client string |
| 35 | + method string |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func newGenerator(out *protogen.GeneratedFile) *generator { |
| 40 | + gen := generator{out: out} |
| 41 | + gen.ident.context = out.QualifiedGoIdent(protogen.GoIdent{ |
| 42 | + GoImportPath: "context", |
| 43 | + GoName: "Context", |
| 44 | + }) |
| 45 | + gen.ident.server = out.QualifiedGoIdent(protogen.GoIdent{ |
| 46 | + GoImportPath: "github.com/containerd/ttrpc", |
| 47 | + GoName: "Server", |
| 48 | + }) |
| 49 | + gen.ident.client = out.QualifiedGoIdent(protogen.GoIdent{ |
| 50 | + GoImportPath: "github.com/containerd/ttrpc", |
| 51 | + GoName: "Client", |
| 52 | + }) |
| 53 | + gen.ident.method = out.QualifiedGoIdent(protogen.GoIdent{ |
| 54 | + GoImportPath: "github.com/containerd/ttrpc", |
| 55 | + GoName: "Method", |
| 56 | + }) |
| 57 | + return &gen |
| 58 | +} |
| 59 | + |
| 60 | +func generate(plugin *protogen.Plugin, input *protogen.File) error { |
| 61 | + file := plugin.NewGeneratedFile(input.GeneratedFilenamePrefix+"_ttrpc.pb.go", input.GoImportPath) |
| 62 | + file.P("// Code generated by protoc-gen-go-ttrpc. DO NOT EDIT.") |
| 63 | + file.P("// source: ", input.Desc.Path()) |
| 64 | + file.P("package ", input.GoPackageName) |
| 65 | + |
| 66 | + gen := newGenerator(file) |
| 67 | + for _, service := range input.Services { |
| 68 | + gen.genService(service) |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func (gen *generator) genService(service *protogen.Service) { |
| 74 | + fullName := service.Desc.FullName() |
| 75 | + p := gen.out |
| 76 | + |
| 77 | + serviceName := service.GoName + "Service" |
| 78 | + p.P("type ", serviceName, " interface{") |
| 79 | + for _, method := range service.Methods { |
| 80 | + p.P(method.GoName, |
| 81 | + "(ctx ", gen.ident.context, ",", |
| 82 | + "req *", method.Input.GoIdent, ")", |
| 83 | + "(*", method.Output.GoIdent, ", error)") |
| 84 | + } |
| 85 | + p.P("}") |
| 86 | + |
| 87 | + // registration method |
| 88 | + p.P("func Register", serviceName, "(srv *", gen.ident.server, ", svc ", serviceName, "){") |
| 89 | + p.P(`srv.Register("`, fullName, `", map[string]`, gen.ident.method, "{") |
| 90 | + for _, method := range service.Methods { |
| 91 | + p.P(`"`, method.GoName, `": func(ctx `, gen.ident.context, ", unmarshal func(interface{}) error)(interface{}, error){") |
| 92 | + p.P("var req ", method.Input.GoIdent) |
| 93 | + p.P("if err := unmarshal(&req); err != nil {") |
| 94 | + p.P("return nil, err") |
| 95 | + p.P("}") |
| 96 | + p.P("return svc.", method.GoName, "(ctx, &req)") |
| 97 | + p.P("},") |
| 98 | + } |
| 99 | + p.P("})") |
| 100 | + p.P("}") |
| 101 | + |
| 102 | + clientType := service.GoName + "Client" |
| 103 | + clientStructType := strings.ToLower(clientType[:1]) + clientType[1:] |
| 104 | + p.P("type ", clientStructType, " struct{") |
| 105 | + p.P("client *", gen.ident.client) |
| 106 | + p.P("}") |
| 107 | + p.P("func New", clientType, "(client *", gen.ident.client, ")", serviceName, "{") |
| 108 | + p.P("return &", clientStructType, "{") |
| 109 | + p.P("client:client,") |
| 110 | + p.P("}") |
| 111 | + p.P("}") |
| 112 | + |
| 113 | + for _, method := range service.Methods { |
| 114 | + p.P("func (c *", clientStructType, ")", method.GoName, "(", |
| 115 | + "ctx ", gen.ident.context, ",", |
| 116 | + "req *", method.Input.GoIdent, ")", |
| 117 | + "(*", method.Output.GoIdent, ", error){") |
| 118 | + p.P("var resp ", method.Output.GoIdent) |
| 119 | + p.P(`if err := c.client.Call(ctx, "`, fullName, `", "`, method.Desc.Name(), `", req, &resp); err != nil {`) |
| 120 | + p.P("return nil, err") |
| 121 | + p.P("}") |
| 122 | + p.P("return &resp, nil") |
| 123 | + p.P("}") |
| 124 | + } |
| 125 | +} |
0 commit comments