-
Notifications
You must be signed in to change notification settings - Fork 83
Implement a new code generator that utilizes google.golang.org/protobuf #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| Copyright The containerd Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "google.golang.org/protobuf/compiler/protogen" | ||
| "strings" | ||
| ) | ||
|
|
||
| // generator is a Go code generator that uses ttrpc.Server and ttrpc.Client. | ||
| // Unlike the original gogo version, this doesn't generate serializers for message types and | ||
| // let protoc-gen-go handle them. | ||
| type generator struct { | ||
| out *protogen.GeneratedFile | ||
| contextT string | ||
| serverT string | ||
| clientT string | ||
| methodT string | ||
| } | ||
|
|
||
| func newGenerator(out *protogen.GeneratedFile) *generator { | ||
| gen := generator{out: out} | ||
| gen.contextT = out.QualifiedGoIdent(protogen.GoIdent{GoImportPath: "context", GoName: "Context"}) | ||
| gen.serverT = out.QualifiedGoIdent(protogen.GoIdent{GoImportPath: "github.com/containerd/ttrpc", GoName: "Server"}) | ||
| gen.clientT = out.QualifiedGoIdent(protogen.GoIdent{GoImportPath: "github.com/containerd/ttrpc", GoName: "Client"}) | ||
| gen.methodT = out.QualifiedGoIdent(protogen.GoIdent{GoImportPath: "github.com/containerd/ttrpc", GoName: "Method"}) | ||
| return &gen | ||
| } | ||
|
|
||
| func generate(plugin *protogen.Plugin, input *protogen.File) error { | ||
| file := plugin.NewGeneratedFile(input.GeneratedFilenamePrefix+"_ttrpc.pb.go", input.GoImportPath) | ||
| file.P("// Code generated by protoc-gen-go-ttrpc. DO NOT EDIT.") | ||
| file.P("// source: ", input.Desc.Path()) | ||
| file.P("package ", input.GoPackageName) | ||
|
|
||
| gen := newGenerator(file) | ||
| for _, service := range input.Services { | ||
| gen.genService(service) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (gen *generator) genService(service *protogen.Service) { | ||
| fullName := service.Desc.FullName() | ||
| p := gen.out | ||
|
|
||
| serviceName := service.GoName + "Service" | ||
| p.P("type ", serviceName, " interface{") | ||
| for _, method := range service.Methods { | ||
| p.P(method.GoName, | ||
| "(ctx ", gen.contextT, ",", | ||
| "req *", method.Input.GoIdent, ")", | ||
| "(*", method.Output.GoIdent, ", error)") | ||
| } | ||
| p.P("}") | ||
|
|
||
| // registration method | ||
| p.P("func Register", serviceName, "(srv *", gen.serverT, ", svc ", serviceName, "){") | ||
| p.P(`srv.Register("`, fullName, `", map[string]`, gen.methodT, "{") | ||
| for _, method := range service.Methods { | ||
| p.P(`"`, method.GoName, `": func(ctx `, gen.contextT, ", unmarshal func(interface{}) error)(interface{}, error){") | ||
| p.P("var req ", method.Input.GoIdent) | ||
| p.P("if err := unmarshal(&req); err != nil {") | ||
| p.P("return nil, err") | ||
| p.P("}") | ||
| p.P("return svc.", method.GoName, "(ctx, &req)") | ||
| p.P("},") | ||
| } | ||
| p.P("})") | ||
| p.P("}") | ||
|
|
||
| clientType := service.GoName + "Client" | ||
| clientStructType := strings.ToLower(clientType[:1]) + clientType[1:] | ||
| p.P("type ", clientStructType, " struct{") | ||
| p.P("client *", gen.clientT) | ||
| p.P("}") | ||
| p.P("func New", clientType, "(client *", gen.clientT, ")", serviceName, "{") | ||
| p.P("return &", clientStructType, "{") | ||
| p.P("client:client,") | ||
| p.P("}") | ||
| p.P("}") | ||
|
|
||
| for _, method := range service.Methods { | ||
| p.P("func (c *", clientStructType, ")", method.GoName, "(", | ||
| "ctx ", gen.contextT, ",", | ||
| "req *", method.Input.GoIdent, ")", | ||
| "(*", method.Output.GoIdent, ", error){") | ||
| p.P("var resp ", method.Output.GoIdent) | ||
| p.P(`if err := c.client.Call(ctx, "`, fullName, `", "`, method.Desc.Name(), `", req, &resp); err != nil {`) | ||
| p.P("return nil, err") | ||
| p.P("}") | ||
| p.P("return &resp, nil") | ||
| p.P("}") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
| Copyright The containerd Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "google.golang.org/protobuf/compiler/protogen" | ||
| ) | ||
|
|
||
| func main() { | ||
| protogen.Options{}.Run(func(gen *protogen.Plugin) error { | ||
| for _, f := range gen.Files { | ||
| if !f.Generate { | ||
| continue | ||
| } | ||
| if err := generate(gen, f); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does this get used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The vtcodec type is needed to invoke vtprotobuf's marshal/unmarshal code.