mirror of
https://github.com/go-micro/go-micro.git
synced 2024-12-30 10:10:44 +02:00
remove generator and move to github.com/go-micro
This commit is contained in:
parent
ff1312438c
commit
97d59fbf78
@ -71,11 +71,7 @@ service.Run()
|
||||
|
||||
See the [examples](https://github.com/go-micro/examples) for detailed information on usage.
|
||||
|
||||
## Code Generation
|
||||
|
||||
See [cmd/protoc-gen-micro](https://github.com/micro/go-micro/tree/master/cmd/protoc-gen-micro) for protobuf code generation.
|
||||
|
||||
## Community
|
||||
## Projects
|
||||
|
||||
See [github.com/go-micro](https://github.com/go-micro) for community led projects.
|
||||
|
||||
@ -83,6 +79,7 @@ See [github.com/go-micro](https://github.com/go-micro) for community led project
|
||||
- [Plugins](https://github.com/go-micro/plugins)
|
||||
- [Examples](https://github.com/go-micro/examples)
|
||||
- [Dashboard](https://github.com/go-micro/dashboard)
|
||||
- [Generator](https://github.com/go-micro/generator)
|
||||
- [CLI](https://github.com/go-micro/cli)
|
||||
|
||||
## Changelog
|
||||
|
51
cmd/protoc-gen-micro/.github/workflows/tests.yml
vendored
51
cmd/protoc-gen-micro/.github/workflows/tests.yml
vendored
@ -1,51 +0,0 @@
|
||||
name: Run tests
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
|
||||
test:
|
||||
name: Test repo
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
|
||||
- name: Set up Go 1.13
|
||||
uses: actions/setup-go@v1
|
||||
with:
|
||||
go-version: 1.13
|
||||
id: go
|
||||
|
||||
- name: Check out code into the Go module directory
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- name: Get dependencies
|
||||
run: |
|
||||
go get -v -t -d ./...
|
||||
|
||||
- name: Run tests
|
||||
id: tests
|
||||
env:
|
||||
IN_TRAVIS_CI: yes
|
||||
run: go test -v ./...
|
||||
|
||||
- name: Notify of test failure
|
||||
if: failure()
|
||||
uses: rtCamp/action-slack-notify@v2.0.0
|
||||
env:
|
||||
SLACK_CHANNEL: build
|
||||
SLACK_COLOR: '#BF280A'
|
||||
SLACK_ICON: https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png
|
||||
SLACK_TITLE: Tests Failed
|
||||
SLACK_USERNAME: GitHub Actions
|
||||
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }}
|
||||
|
||||
- name: Notify of test success
|
||||
if: success()
|
||||
uses: rtCamp/action-slack-notify@v2.0.0
|
||||
env:
|
||||
SLACK_CHANNEL: build
|
||||
SLACK_COLOR: '#1FAD2B'
|
||||
SLACK_ICON: https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png
|
||||
SLACK_TITLE: Tests Passed
|
||||
SLACK_USERNAME: GitHub Actions
|
||||
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }}
|
||||
|
33
cmd/protoc-gen-micro/.gitignore
vendored
33
cmd/protoc-gen-micro/.gitignore
vendored
@ -1,33 +0,0 @@
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
_build
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
*.test
|
||||
*.prof
|
||||
|
||||
# ignore go build and test outputs
|
||||
coverage.txt
|
||||
coverage.out
|
||||
|
||||
# ignore locally built binaries
|
||||
protoc-gen-micro
|
||||
dist
|
@ -1,144 +0,0 @@
|
||||
# protoc-gen-micro
|
||||
|
||||
This is protobuf code generation for go-micro. We use protoc-gen-micro to reduce boilerplate code.
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
go install go-micro.dev/v4/cmd/protoc-gen-micro@v4
|
||||
```
|
||||
|
||||
Also required:
|
||||
|
||||
- [protoc](https://github.com/google/protobuf)
|
||||
- [protoc-gen-go](https://google.golang.org/protobuf)
|
||||
|
||||
## Usage
|
||||
|
||||
Define your service as `greeter.proto`
|
||||
|
||||
```
|
||||
syntax = "proto3";
|
||||
|
||||
package greeter;
|
||||
option go_package = "/proto;greeter";
|
||||
|
||||
service Greeter {
|
||||
rpc Hello(Request) returns (Response) {}
|
||||
}
|
||||
|
||||
message Request {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message Response {
|
||||
string msg = 1;
|
||||
}
|
||||
```
|
||||
|
||||
Generate the code
|
||||
|
||||
```
|
||||
protoc --proto_path=. --micro_out=. --go_out=. greeter.proto
|
||||
```
|
||||
|
||||
Your output result should be:
|
||||
|
||||
```
|
||||
./
|
||||
greeter.proto # original protobuf file
|
||||
greeter.pb.go # auto-generated by protoc-gen-go
|
||||
greeter.micro.go # auto-generated by protoc-gen-micro
|
||||
```
|
||||
|
||||
The micro generated code includes clients and handlers which reduce boiler plate code
|
||||
|
||||
### Server
|
||||
|
||||
Register the handler with your micro server
|
||||
|
||||
```go
|
||||
type Greeter struct{}
|
||||
|
||||
func (g *Greeter) Hello(ctx context.Context, req *proto.Request, rsp *proto.Response) error {
|
||||
rsp.Msg = "Hello " + req.Name
|
||||
return nil
|
||||
}
|
||||
|
||||
proto.RegisterGreeterHandler(service.Server(), &Greeter{})
|
||||
```
|
||||
|
||||
### Client
|
||||
|
||||
Create a service client with your micro client
|
||||
|
||||
```go
|
||||
client := proto.NewGreeterService("greeter", service.Client())
|
||||
```
|
||||
|
||||
### Errors
|
||||
|
||||
If you see an error about `protoc-gen-micro` not being found or executable, it's likely your environment may not be configured correctly. If you've already installed `protoc`, `protoc-gen-go`, and `protoc-gen-micro` ensure you've included `$GOPATH/bin` in your `PATH`.
|
||||
|
||||
Alternative specify the Go plugin paths as arguments to the `protoc` command
|
||||
|
||||
```
|
||||
protoc --plugin=protoc-gen-go=$GOPATH/bin/protoc-gen-go --plugin=protoc-gen-micro=$GOPATH/bin/protoc-gen-micro --proto_path=. --micro_out=. --go_out=. greeter.proto
|
||||
```
|
||||
|
||||
### Endpoint
|
||||
|
||||
Add a micro API endpoint which routes directly to an RPC method
|
||||
|
||||
Usage:
|
||||
|
||||
1. Clone `github.com/googleapis/googleapis` to use this feature as it requires http annotations.
|
||||
2. The protoc command must include `-I$GOPATH/src/github.com/googleapis/googleapis` for the annotations import.
|
||||
|
||||
```diff
|
||||
syntax = "proto3";
|
||||
|
||||
package greeter;
|
||||
option go_package = "/proto;greeter";
|
||||
|
||||
import "google/api/annotations.proto";
|
||||
|
||||
service Greeter {
|
||||
rpc Hello(Request) returns (Response) {
|
||||
option (google.api.http) = { post: "/hello"; body: "*"; };
|
||||
}
|
||||
}
|
||||
|
||||
message Request {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message Response {
|
||||
string msg = 1;
|
||||
}
|
||||
```
|
||||
|
||||
The proto generates a `RegisterGreeterHandler` function with a [api.Endpoint](https://godoc.org/go-micro.dev/v3/api#Endpoint).
|
||||
|
||||
```diff
|
||||
func RegisterGreeterHandler(s server.Server, hdlr GreeterHandler, opts ...server.HandlerOption) error {
|
||||
type greeter interface {
|
||||
Hello(ctx context.Context, in *Request, out *Response) error
|
||||
}
|
||||
type Greeter struct {
|
||||
greeter
|
||||
}
|
||||
h := &greeterHandler{hdlr}
|
||||
opts = append(opts, api.WithEndpoint(&api.Endpoint{
|
||||
Name: "Greeter.Hello",
|
||||
Path: []string{"/hello"},
|
||||
Method: []string{"POST"},
|
||||
Handler: "rpc",
|
||||
}))
|
||||
return s.Handle(s.NewHandler(&Greeter{h}, opts...))
|
||||
}
|
||||
```
|
||||
|
||||
## LICENSE
|
||||
|
||||
protoc-gen-micro is a liberal reuse of protoc-gen-go hence we maintain the original license
|
@ -1,227 +0,0 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.27.1
|
||||
// protoc v3.15.6
|
||||
// source: greeter.proto
|
||||
|
||||
package greeter
|
||||
|
||||
import (
|
||||
_ "google.golang.org/genproto/googleapis/api/annotations"
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
type Request struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
||||
Msg *string `protobuf:"bytes,2,opt,name=msg,proto3,oneof" json:"msg,omitempty"`
|
||||
}
|
||||
|
||||
func (x *Request) Reset() {
|
||||
*x = Request{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_greeter_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *Request) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*Request) ProtoMessage() {}
|
||||
|
||||
func (x *Request) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_greeter_proto_msgTypes[0]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use Request.ProtoReflect.Descriptor instead.
|
||||
func (*Request) Descriptor() ([]byte, []int) {
|
||||
return file_greeter_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *Request) GetName() string {
|
||||
if x != nil {
|
||||
return x.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *Request) GetMsg() string {
|
||||
if x != nil && x.Msg != nil {
|
||||
return *x.Msg
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Msg string `protobuf:"bytes,1,opt,name=msg,proto3" json:"msg,omitempty"`
|
||||
}
|
||||
|
||||
func (x *Response) Reset() {
|
||||
*x = Response{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_greeter_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *Response) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*Response) ProtoMessage() {}
|
||||
|
||||
func (x *Response) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_greeter_proto_msgTypes[1]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use Response.ProtoReflect.Descriptor instead.
|
||||
func (*Response) Descriptor() ([]byte, []int) {
|
||||
return file_greeter_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *Response) GetMsg() string {
|
||||
if x != nil {
|
||||
return x.Msg
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var File_greeter_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_greeter_proto_rawDesc = []byte{
|
||||
0x0a, 0x0d, 0x67, 0x72, 0x65, 0x65, 0x74, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a,
|
||||
0x1c, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x61, 0x6e, 0x6e, 0x6f,
|
||||
0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x3c, 0x0a,
|
||||
0x07, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65,
|
||||
0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x15, 0x0a, 0x03,
|
||||
0x6d, 0x73, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x03, 0x6d, 0x73, 0x67,
|
||||
0x88, 0x01, 0x01, 0x42, 0x06, 0x0a, 0x04, 0x5f, 0x6d, 0x73, 0x67, 0x22, 0x1c, 0x0a, 0x08, 0x52,
|
||||
0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x6d, 0x73, 0x67, 0x18, 0x01,
|
||||
0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6d, 0x73, 0x67, 0x32, 0x6e, 0x0a, 0x07, 0x47, 0x72, 0x65,
|
||||
0x65, 0x74, 0x65, 0x72, 0x12, 0x2f, 0x0a, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x12, 0x08, 0x2e,
|
||||
0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x09, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e,
|
||||
0x73, 0x65, 0x22, 0x11, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x0b, 0x22, 0x06, 0x2f, 0x68, 0x65, 0x6c,
|
||||
0x6c, 0x6f, 0x3a, 0x01, 0x2a, 0x12, 0x32, 0x0a, 0x06, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x12,
|
||||
0x08, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x09, 0x2e, 0x52, 0x65, 0x73, 0x70,
|
||||
0x6f, 0x6e, 0x73, 0x65, 0x22, 0x0f, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x09, 0x12, 0x07, 0x2f, 0x73,
|
||||
0x74, 0x72, 0x65, 0x61, 0x6d, 0x28, 0x01, 0x30, 0x01, 0x42, 0x0c, 0x5a, 0x0a, 0x2e, 0x2e, 0x2f,
|
||||
0x67, 0x72, 0x65, 0x65, 0x74, 0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
file_greeter_proto_rawDescOnce sync.Once
|
||||
file_greeter_proto_rawDescData = file_greeter_proto_rawDesc
|
||||
)
|
||||
|
||||
func file_greeter_proto_rawDescGZIP() []byte {
|
||||
file_greeter_proto_rawDescOnce.Do(func() {
|
||||
file_greeter_proto_rawDescData = protoimpl.X.CompressGZIP(file_greeter_proto_rawDescData)
|
||||
})
|
||||
return file_greeter_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_greeter_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
|
||||
var file_greeter_proto_goTypes = []interface{}{
|
||||
(*Request)(nil), // 0: Request
|
||||
(*Response)(nil), // 1: Response
|
||||
}
|
||||
var file_greeter_proto_depIdxs = []int32{
|
||||
0, // 0: Greeter.Hello:input_type -> Request
|
||||
0, // 1: Greeter.Stream:input_type -> Request
|
||||
1, // 2: Greeter.Hello:output_type -> Response
|
||||
1, // 3: Greeter.Stream:output_type -> Response
|
||||
2, // [2:4] is the sub-list for method output_type
|
||||
0, // [0:2] is the sub-list for method input_type
|
||||
0, // [0:0] is the sub-list for extension type_name
|
||||
0, // [0:0] is the sub-list for extension extendee
|
||||
0, // [0:0] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_greeter_proto_init() }
|
||||
func file_greeter_proto_init() {
|
||||
if File_greeter_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_greeter_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*Request); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_greeter_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*Response); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
file_greeter_proto_msgTypes[0].OneofWrappers = []interface{}{}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_greeter_proto_rawDesc,
|
||||
NumEnums: 0,
|
||||
NumMessages: 2,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_greeter_proto_goTypes,
|
||||
DependencyIndexes: file_greeter_proto_depIdxs,
|
||||
MessageInfos: file_greeter_proto_msgTypes,
|
||||
}.Build()
|
||||
File_greeter_proto = out.File
|
||||
file_greeter_proto_rawDesc = nil
|
||||
file_greeter_proto_goTypes = nil
|
||||
file_greeter_proto_depIdxs = nil
|
||||
}
|
@ -1,214 +0,0 @@
|
||||
// Code generated by protoc-gen-micro. DO NOT EDIT.
|
||||
// source: greeter.proto
|
||||
|
||||
package greeter
|
||||
|
||||
import (
|
||||
fmt "fmt"
|
||||
_ "google.golang.org/genproto/googleapis/api/annotations"
|
||||
proto "google.golang.org/protobuf/proto"
|
||||
math "math"
|
||||
)
|
||||
|
||||
import (
|
||||
context "context"
|
||||
api "go-micro.dev/v4/api"
|
||||
client "go-micro.dev/v4/client"
|
||||
server "go-micro.dev/v4/server"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ api.Endpoint
|
||||
var _ context.Context
|
||||
var _ client.Option
|
||||
var _ server.Option
|
||||
|
||||
// Api Endpoints for Greeter service
|
||||
|
||||
func NewGreeterEndpoints() []*api.Endpoint {
|
||||
return []*api.Endpoint{
|
||||
{
|
||||
Name: "Greeter.Hello",
|
||||
Path: []string{"/hello"},
|
||||
Method: []string{"POST"},
|
||||
Handler: "rpc",
|
||||
},
|
||||
{
|
||||
Name: "Greeter.Stream",
|
||||
Path: []string{"/stream"},
|
||||
Method: []string{"GET"},
|
||||
Stream: true,
|
||||
Handler: "rpc",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Client API for Greeter service
|
||||
|
||||
type GreeterService interface {
|
||||
Hello(ctx context.Context, in *Request, opts ...client.CallOption) (*Response, error)
|
||||
Stream(ctx context.Context, opts ...client.CallOption) (Greeter_StreamService, error)
|
||||
}
|
||||
|
||||
type greeterService struct {
|
||||
c client.Client
|
||||
name string
|
||||
}
|
||||
|
||||
func NewGreeterService(name string, c client.Client) GreeterService {
|
||||
return &greeterService{
|
||||
c: c,
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *greeterService) Hello(ctx context.Context, in *Request, opts ...client.CallOption) (*Response, error) {
|
||||
req := c.c.NewRequest(c.name, "Greeter.Hello", in)
|
||||
out := new(Response)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *greeterService) Stream(ctx context.Context, opts ...client.CallOption) (Greeter_StreamService, error) {
|
||||
req := c.c.NewRequest(c.name, "Greeter.Stream", &Request{})
|
||||
stream, err := c.c.Stream(ctx, req, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &greeterServiceStream{stream}, nil
|
||||
}
|
||||
|
||||
type Greeter_StreamService interface {
|
||||
Context() context.Context
|
||||
SendMsg(interface{}) error
|
||||
RecvMsg(interface{}) error
|
||||
Close() error
|
||||
Send(*Request) error
|
||||
Recv() (*Response, error)
|
||||
}
|
||||
|
||||
type greeterServiceStream struct {
|
||||
stream client.Stream
|
||||
}
|
||||
|
||||
func (x *greeterServiceStream) Close() error {
|
||||
return x.stream.Close()
|
||||
}
|
||||
|
||||
func (x *greeterServiceStream) Context() context.Context {
|
||||
return x.stream.Context()
|
||||
}
|
||||
|
||||
func (x *greeterServiceStream) SendMsg(m interface{}) error {
|
||||
return x.stream.Send(m)
|
||||
}
|
||||
|
||||
func (x *greeterServiceStream) RecvMsg(m interface{}) error {
|
||||
return x.stream.Recv(m)
|
||||
}
|
||||
|
||||
func (x *greeterServiceStream) Send(m *Request) error {
|
||||
return x.stream.Send(m)
|
||||
}
|
||||
|
||||
func (x *greeterServiceStream) Recv() (*Response, error) {
|
||||
m := new(Response)
|
||||
err := x.stream.Recv(m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Server API for Greeter service
|
||||
|
||||
type GreeterHandler interface {
|
||||
Hello(context.Context, *Request, *Response) error
|
||||
Stream(context.Context, Greeter_StreamStream) error
|
||||
}
|
||||
|
||||
func RegisterGreeterHandler(s server.Server, hdlr GreeterHandler, opts ...server.HandlerOption) error {
|
||||
type greeter interface {
|
||||
Hello(ctx context.Context, in *Request, out *Response) error
|
||||
Stream(ctx context.Context, stream server.Stream) error
|
||||
}
|
||||
type Greeter struct {
|
||||
greeter
|
||||
}
|
||||
h := &greeterHandler{hdlr}
|
||||
opts = append(opts, api.WithEndpoint(&api.Endpoint{
|
||||
Name: "Greeter.Hello",
|
||||
Path: []string{"/hello"},
|
||||
Method: []string{"POST"},
|
||||
Handler: "rpc",
|
||||
}))
|
||||
opts = append(opts, api.WithEndpoint(&api.Endpoint{
|
||||
Name: "Greeter.Stream",
|
||||
Path: []string{"/stream"},
|
||||
Method: []string{"GET"},
|
||||
Stream: true,
|
||||
Handler: "rpc",
|
||||
}))
|
||||
return s.Handle(s.NewHandler(&Greeter{h}, opts...))
|
||||
}
|
||||
|
||||
type greeterHandler struct {
|
||||
GreeterHandler
|
||||
}
|
||||
|
||||
func (h *greeterHandler) Hello(ctx context.Context, in *Request, out *Response) error {
|
||||
return h.GreeterHandler.Hello(ctx, in, out)
|
||||
}
|
||||
|
||||
func (h *greeterHandler) Stream(ctx context.Context, stream server.Stream) error {
|
||||
return h.GreeterHandler.Stream(ctx, &greeterStreamStream{stream})
|
||||
}
|
||||
|
||||
type Greeter_StreamStream interface {
|
||||
Context() context.Context
|
||||
SendMsg(interface{}) error
|
||||
RecvMsg(interface{}) error
|
||||
Close() error
|
||||
Send(*Response) error
|
||||
Recv() (*Request, error)
|
||||
}
|
||||
|
||||
type greeterStreamStream struct {
|
||||
stream server.Stream
|
||||
}
|
||||
|
||||
func (x *greeterStreamStream) Close() error {
|
||||
return x.stream.Close()
|
||||
}
|
||||
|
||||
func (x *greeterStreamStream) Context() context.Context {
|
||||
return x.stream.Context()
|
||||
}
|
||||
|
||||
func (x *greeterStreamStream) SendMsg(m interface{}) error {
|
||||
return x.stream.Send(m)
|
||||
}
|
||||
|
||||
func (x *greeterStreamStream) RecvMsg(m interface{}) error {
|
||||
return x.stream.Recv(m)
|
||||
}
|
||||
|
||||
func (x *greeterStreamStream) Send(m *Response) error {
|
||||
return x.stream.Send(m)
|
||||
}
|
||||
|
||||
func (x *greeterStreamStream) Recv() (*Request, error) {
|
||||
m := new(Request)
|
||||
if err := x.stream.Recv(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
syntax = "proto3";
|
||||
|
||||
option go_package = "../greeter";
|
||||
|
||||
import "google/api/annotations.proto";
|
||||
|
||||
service Greeter {
|
||||
rpc Hello(Request) returns (Response) {
|
||||
option (google.api.http) = { post: "/hello"; body: "*"; };
|
||||
}
|
||||
rpc Stream(stream Request) returns (stream Response) {
|
||||
option (google.api.http) = { get: "/stream"; };
|
||||
}
|
||||
}
|
||||
|
||||
message Request {
|
||||
string name = 1;
|
||||
optional string msg = 2;
|
||||
}
|
||||
|
||||
message Response {
|
||||
string msg = 1;
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
# Go support for Protocol Buffers - Google's data interchange format
|
||||
#
|
||||
# Copyright 2010 The Go Authors. All rights reserved.
|
||||
# https://github.com/golang/protobuf
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are
|
||||
# met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above
|
||||
# copyright notice, this list of conditions and the following disclaimer
|
||||
# in the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
# * Neither the name of Google Inc. nor the names of its
|
||||
# contributors may be used to endorse or promote products derived from
|
||||
# this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
include $(GOROOT)/src/Make.inc
|
||||
|
||||
TARG=github.com/golang/protobuf/protoc-gen-go/generator
|
||||
GOFILES=\
|
||||
generator.go\
|
||||
|
||||
DEPS=../descriptor ../plugin ../../proto
|
||||
|
||||
include $(GOROOT)/src/Make.pkg
|
File diff suppressed because it is too large
Load Diff
@ -1,135 +0,0 @@
|
||||
// Go support for Protocol Buffers - Google's data interchange format
|
||||
//
|
||||
// Copyright 2013 The Go Authors. All rights reserved.
|
||||
// https://github.com/golang/protobuf
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package generator
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
descriptor "google.golang.org/protobuf/types/descriptorpb"
|
||||
)
|
||||
|
||||
func TestCamelCase(t *testing.T) {
|
||||
tests := []struct {
|
||||
in, want string
|
||||
}{
|
||||
{"one", "One"},
|
||||
{"one_two", "OneTwo"},
|
||||
{"_my_field_name_2", "XMyFieldName_2"},
|
||||
{"Something_Capped", "Something_Capped"},
|
||||
{"my_Name", "My_Name"},
|
||||
{"OneTwo", "OneTwo"},
|
||||
{"_", "X"},
|
||||
{"_a_", "XA_"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
if got := CamelCase(tc.in); got != tc.want {
|
||||
t.Errorf("CamelCase(%q) = %q, want %q", tc.in, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoPackageOption(t *testing.T) {
|
||||
tests := []struct {
|
||||
in string
|
||||
impPath GoImportPath
|
||||
pkg GoPackageName
|
||||
ok bool
|
||||
}{
|
||||
{"", "", "", false},
|
||||
{"foo", "", "foo", true},
|
||||
{"github.com/golang/bar", "github.com/golang/bar", "bar", true},
|
||||
{"github.com/golang/bar;baz", "github.com/golang/bar", "baz", true},
|
||||
{"github.com/golang/string", "github.com/golang/string", "string", true},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
d := &FileDescriptor{
|
||||
FileDescriptorProto: &descriptor.FileDescriptorProto{
|
||||
Options: &descriptor.FileOptions{
|
||||
GoPackage: &tc.in,
|
||||
},
|
||||
},
|
||||
}
|
||||
impPath, pkg, ok := d.goPackageOption()
|
||||
if impPath != tc.impPath || pkg != tc.pkg || ok != tc.ok {
|
||||
t.Errorf("go_package = %q => (%q, %q, %t), want (%q, %q, %t)", tc.in,
|
||||
impPath, pkg, ok, tc.impPath, tc.pkg, tc.ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPackageNames(t *testing.T) {
|
||||
g := New()
|
||||
g.packageNames = make(map[GoImportPath]GoPackageName)
|
||||
g.usedPackageNames = make(map[GoPackageName]bool)
|
||||
for _, test := range []struct {
|
||||
importPath GoImportPath
|
||||
want GoPackageName
|
||||
}{
|
||||
{"github.com/golang/foo", "foo"},
|
||||
{"github.com/golang/second/package/named/foo", "foo1"},
|
||||
{"github.com/golang/third/package/named/foo", "foo2"},
|
||||
{"github.com/golang/conflicts/with/predeclared/ident/string", "string1"},
|
||||
} {
|
||||
if got := g.GoPackageName(test.importPath); got != test.want {
|
||||
t.Errorf("GoPackageName(%v) = %v, want %v", test.importPath, got, test.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnescape(t *testing.T) {
|
||||
tests := []struct {
|
||||
in string
|
||||
out string
|
||||
}{
|
||||
// successful cases, including all kinds of escapes
|
||||
{"", ""},
|
||||
{"foo bar baz frob nitz", "foo bar baz frob nitz"},
|
||||
{`\000\001\002\003\004\005\006\007`, string([]byte{0, 1, 2, 3, 4, 5, 6, 7})},
|
||||
{`\a\b\f\n\r\t\v\\\?\'\"`, string([]byte{'\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '?', '\'', '"'})},
|
||||
{`\x10\x20\x30\x40\x50\x60\x70\x80`, string([]byte{16, 32, 48, 64, 80, 96, 112, 128})},
|
||||
// variable length octal escapes
|
||||
{`\0\018\222\377\3\04\005\6\07`, string([]byte{0, 1, '8', 0222, 255, 3, 4, 5, 6, 7})},
|
||||
// malformed escape sequences left as is
|
||||
{"foo \\g bar", "foo \\g bar"},
|
||||
{"foo \\xg0 bar", "foo \\xg0 bar"},
|
||||
{"\\", "\\"},
|
||||
{"\\x", "\\x"},
|
||||
{"\\xf", "\\xf"},
|
||||
{"\\777", "\\777"}, // overflows byte
|
||||
}
|
||||
for _, tc := range tests {
|
||||
s := unescape(tc.in)
|
||||
if s != tc.out {
|
||||
t.Errorf("doUnescape(%q) = %q; should have been %q", tc.in, s, tc.out)
|
||||
}
|
||||
}
|
||||
}
|
@ -1,99 +0,0 @@
|
||||
// Go support for Protocol Buffers - Google's data interchange format
|
||||
//
|
||||
// Copyright 2010 The Go Authors. All rights reserved.
|
||||
// https://github.com/golang/protobuf
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
// protoc-gen-micro is a plugin for the Google protocol buffer compiler to generate
|
||||
// Go code. Run it by building this program and putting it in your path with
|
||||
// the name
|
||||
// protoc-gen-micro
|
||||
// That word 'micro' at the end becomes part of the option string set for the
|
||||
// protocol compiler, so once the protocol compiler (protoc) is installed
|
||||
// you can run
|
||||
// protoc --micro_out=output_directory --go_out=output_directory input_directory/file.proto
|
||||
// to generate go-micro code for the protocol defined by file.proto.
|
||||
// With that input, the output will be written to
|
||||
// output_directory/file.micro.go
|
||||
//
|
||||
// The generated code is documented in the package comment for
|
||||
// the library.
|
||||
//
|
||||
// See the README and documentation for protocol buffers to learn more:
|
||||
// https://developers.google.com/protocol-buffers/
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"go-micro.dev/v4/cmd/protoc-gen-micro/generator"
|
||||
_ "go-micro.dev/v4/cmd/protoc-gen-micro/plugin/micro"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Begin by allocating a generator. The request and response structures are stored there
|
||||
// so we can do error handling easily - the response structure contains the field to
|
||||
// report failure.
|
||||
g := generator.New()
|
||||
|
||||
data, err := io.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
g.Error(err, "reading input")
|
||||
}
|
||||
|
||||
if err := proto.Unmarshal(data, g.Request); err != nil {
|
||||
g.Error(err, "parsing input proto")
|
||||
}
|
||||
|
||||
if len(g.Request.FileToGenerate) == 0 {
|
||||
g.Fail("no files to generate")
|
||||
}
|
||||
|
||||
g.CommandLineParameters(g.Request.GetParameter())
|
||||
|
||||
// Create a wrapped version of the Descriptors and EnumDescriptors that
|
||||
// point to the file that defines them.
|
||||
g.WrapTypes()
|
||||
|
||||
g.SetPackageNames()
|
||||
g.BuildTypeNameMap()
|
||||
|
||||
g.GenerateAllFiles()
|
||||
|
||||
// Send back the results.
|
||||
data, err = proto.Marshal(g.Response)
|
||||
if err != nil {
|
||||
g.Error(err, "failed to marshal output proto")
|
||||
}
|
||||
_, err = os.Stdout.Write(data)
|
||||
if err != nil {
|
||||
g.Error(err, "failed to write output proto")
|
||||
}
|
||||
}
|
@ -1,560 +0,0 @@
|
||||
package micro
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"go-micro.dev/v4/cmd/protoc-gen-micro/generator"
|
||||
options "google.golang.org/genproto/googleapis/api/annotations"
|
||||
"google.golang.org/protobuf/proto"
|
||||
pb "google.golang.org/protobuf/types/descriptorpb"
|
||||
)
|
||||
|
||||
// Paths for packages used by code generated in this file,
|
||||
// relative to the import_prefix of the generator.Generator.
|
||||
const (
|
||||
apiPkgPath = "go-micro.dev/v4/api"
|
||||
contextPkgPath = "context"
|
||||
clientPkgPath = "go-micro.dev/v4/client"
|
||||
serverPkgPath = "go-micro.dev/v4/server"
|
||||
)
|
||||
|
||||
func init() {
|
||||
generator.RegisterPlugin(new(micro))
|
||||
}
|
||||
|
||||
// micro is an implementation of the Go protocol buffer compiler's
|
||||
// plugin architecture. It generates bindings for go-micro support.
|
||||
type micro struct {
|
||||
gen *generator.Generator
|
||||
}
|
||||
|
||||
// Name returns the name of this plugin, "micro".
|
||||
func (g *micro) Name() string {
|
||||
return "micro"
|
||||
}
|
||||
|
||||
// The names for packages imported in the generated code.
|
||||
// They may vary from the final path component of the import path
|
||||
// if the name is used by other packages.
|
||||
var (
|
||||
apiPkg string
|
||||
contextPkg string
|
||||
clientPkg string
|
||||
serverPkg string
|
||||
pkgImports map[generator.GoPackageName]bool
|
||||
)
|
||||
|
||||
// Init initializes the plugin.
|
||||
func (g *micro) Init(gen *generator.Generator) {
|
||||
g.gen = gen
|
||||
apiPkg = generator.RegisterUniquePackageName("api", nil)
|
||||
contextPkg = generator.RegisterUniquePackageName("context", nil)
|
||||
clientPkg = generator.RegisterUniquePackageName("client", nil)
|
||||
serverPkg = generator.RegisterUniquePackageName("server", nil)
|
||||
}
|
||||
|
||||
// Given a type name defined in a .proto, return its object.
|
||||
// Also record that we're using it, to guarantee the associated import.
|
||||
func (g *micro) objectNamed(name string) generator.Object {
|
||||
g.gen.RecordTypeUse(name)
|
||||
return g.gen.ObjectNamed(name)
|
||||
}
|
||||
|
||||
// Given a type name defined in a .proto, return its name as we will print it.
|
||||
func (g *micro) typeName(str string) string {
|
||||
return g.gen.TypeName(g.objectNamed(str))
|
||||
}
|
||||
|
||||
// P forwards to g.gen.P.
|
||||
func (g *micro) P(args ...interface{}) { g.gen.P(args...) }
|
||||
|
||||
// Generate generates code for the services in the given file.
|
||||
func (g *micro) Generate(file *generator.FileDescriptor) {
|
||||
if len(file.FileDescriptorProto.Service) == 0 {
|
||||
return
|
||||
}
|
||||
g.P("// Reference imports to suppress errors if they are not otherwise used.")
|
||||
g.P("var _ ", apiPkg, ".Endpoint")
|
||||
g.P("var _ ", contextPkg, ".Context")
|
||||
g.P("var _ ", clientPkg, ".Option")
|
||||
g.P("var _ ", serverPkg, ".Option")
|
||||
g.P()
|
||||
|
||||
for i, service := range file.FileDescriptorProto.Service {
|
||||
g.generateService(file, service, i)
|
||||
}
|
||||
}
|
||||
|
||||
// GenerateImports generates the import declaration for this file.
|
||||
func (g *micro) GenerateImports(file *generator.FileDescriptor, imports map[generator.GoImportPath]generator.GoPackageName) {
|
||||
if len(file.FileDescriptorProto.Service) == 0 {
|
||||
return
|
||||
}
|
||||
g.P("import (")
|
||||
g.P(apiPkg, " ", strconv.Quote(path.Join(g.gen.ImportPrefix, apiPkgPath)))
|
||||
g.P(contextPkg, " ", strconv.Quote(path.Join(g.gen.ImportPrefix, contextPkgPath)))
|
||||
g.P(clientPkg, " ", strconv.Quote(path.Join(g.gen.ImportPrefix, clientPkgPath)))
|
||||
g.P(serverPkg, " ", strconv.Quote(path.Join(g.gen.ImportPrefix, serverPkgPath)))
|
||||
g.P(")")
|
||||
g.P()
|
||||
|
||||
// We need to keep track of imported packages to make sure we don't produce
|
||||
// a name collision when generating types.
|
||||
pkgImports = make(map[generator.GoPackageName]bool)
|
||||
for _, name := range imports {
|
||||
pkgImports[name] = true
|
||||
}
|
||||
}
|
||||
|
||||
// reservedClientName records whether a client name is reserved on the client side.
|
||||
var reservedClientName = map[string]bool{
|
||||
// TODO: do we need any in go-micro?
|
||||
}
|
||||
|
||||
func unexport(s string) string {
|
||||
if len(s) == 0 {
|
||||
return ""
|
||||
}
|
||||
name := strings.ToLower(s[:1]) + s[1:]
|
||||
if pkgImports[generator.GoPackageName(name)] {
|
||||
return name + "_"
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
// generateService generates all the code for the named service.
|
||||
func (g *micro) generateService(file *generator.FileDescriptor, service *pb.ServiceDescriptorProto, index int) {
|
||||
path := fmt.Sprintf("6,%d", index) // 6 means service.
|
||||
|
||||
origServName := service.GetName()
|
||||
serviceName := strings.ToLower(service.GetName())
|
||||
pkg := file.GetPackage()
|
||||
if pkg != "" {
|
||||
serviceName = pkg
|
||||
}
|
||||
servName := generator.CamelCase(origServName)
|
||||
servAlias := servName + "Service"
|
||||
|
||||
// strip suffix
|
||||
if strings.HasSuffix(servAlias, "ServiceService") {
|
||||
servAlias = strings.TrimSuffix(servAlias, "Service")
|
||||
}
|
||||
|
||||
g.P()
|
||||
g.P("// Api Endpoints for ", servName, " service")
|
||||
g.P()
|
||||
|
||||
g.P("func New", servName, "Endpoints () []*", apiPkg, ".Endpoint {")
|
||||
g.P("return []*", apiPkg, ".Endpoint{")
|
||||
for _, method := range service.Method {
|
||||
if method.Options != nil && proto.HasExtension(method.Options, options.E_Http) {
|
||||
g.P("{")
|
||||
g.generateEndpoint(servName, method)
|
||||
g.P("},")
|
||||
}
|
||||
}
|
||||
g.P("}")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
g.P()
|
||||
g.P("// Client API for ", servName, " service")
|
||||
g.P()
|
||||
|
||||
// Client interface.
|
||||
g.P("type ", servAlias, " interface {")
|
||||
for i, method := range service.Method {
|
||||
g.gen.PrintComments(fmt.Sprintf("%s,2,%d", path, i)) // 2 means method in a service.
|
||||
g.P(g.generateClientSignature(servName, method))
|
||||
}
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
// Client structure.
|
||||
g.P("type ", unexport(servAlias), " struct {")
|
||||
g.P("c ", clientPkg, ".Client")
|
||||
g.P("name string")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
// NewClient factory.
|
||||
g.P("func New", servAlias, " (name string, c ", clientPkg, ".Client) ", servAlias, " {")
|
||||
/*
|
||||
g.P("if c == nil {")
|
||||
g.P("c = ", clientPkg, ".NewClient()")
|
||||
g.P("}")
|
||||
g.P("if len(name) == 0 {")
|
||||
g.P(`name = "`, serviceName, `"`)
|
||||
g.P("}")
|
||||
*/
|
||||
g.P("return &", unexport(servAlias), "{")
|
||||
g.P("c: c,")
|
||||
g.P("name: name,")
|
||||
g.P("}")
|
||||
g.P("}")
|
||||
g.P()
|
||||
var methodIndex, streamIndex int
|
||||
serviceDescVar := "_" + servName + "_serviceDesc"
|
||||
// Client method implementations.
|
||||
for _, method := range service.Method {
|
||||
var descExpr string
|
||||
if !method.GetServerStreaming() {
|
||||
// Unary RPC method
|
||||
descExpr = fmt.Sprintf("&%s.Methods[%d]", serviceDescVar, methodIndex)
|
||||
methodIndex++
|
||||
} else {
|
||||
// Streaming RPC method
|
||||
descExpr = fmt.Sprintf("&%s.Streams[%d]", serviceDescVar, streamIndex)
|
||||
streamIndex++
|
||||
}
|
||||
g.generateClientMethod(pkg, serviceName, servName, serviceDescVar, method, descExpr)
|
||||
}
|
||||
|
||||
g.P("// Server API for ", servName, " service")
|
||||
g.P()
|
||||
|
||||
// Server interface.
|
||||
serverType := servName + "Handler"
|
||||
g.P("type ", serverType, " interface {")
|
||||
for i, method := range service.Method {
|
||||
g.gen.PrintComments(fmt.Sprintf("%s,2,%d", path, i)) // 2 means method in a service.
|
||||
g.P(g.generateServerSignature(servName, method))
|
||||
}
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
// Server registration.
|
||||
g.P("func Register", servName, "Handler(s ", serverPkg, ".Server, hdlr ", serverType, ", opts ...", serverPkg, ".HandlerOption) error {")
|
||||
g.P("type ", unexport(servName), " interface {")
|
||||
|
||||
// generate interface methods
|
||||
for _, method := range service.Method {
|
||||
methName := generator.CamelCase(method.GetName())
|
||||
inType := g.typeName(method.GetInputType())
|
||||
outType := g.typeName(method.GetOutputType())
|
||||
|
||||
if !method.GetServerStreaming() && !method.GetClientStreaming() {
|
||||
g.P(methName, "(ctx ", contextPkg, ".Context, in *", inType, ", out *", outType, ") error")
|
||||
continue
|
||||
}
|
||||
g.P(methName, "(ctx ", contextPkg, ".Context, stream server.Stream) error")
|
||||
}
|
||||
g.P("}")
|
||||
g.P("type ", servName, " struct {")
|
||||
g.P(unexport(servName))
|
||||
g.P("}")
|
||||
g.P("h := &", unexport(servName), "Handler{hdlr}")
|
||||
for _, method := range service.Method {
|
||||
if method.Options != nil && proto.HasExtension(method.Options, options.E_Http) {
|
||||
g.P("opts = append(opts, ", apiPkg, ".WithEndpoint(&", apiPkg, ".Endpoint{")
|
||||
g.generateEndpoint(servName, method)
|
||||
g.P("}))")
|
||||
}
|
||||
}
|
||||
g.P("return s.Handle(s.NewHandler(&", servName, "{h}, opts...))")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
g.P("type ", unexport(servName), "Handler struct {")
|
||||
g.P(serverType)
|
||||
g.P("}")
|
||||
|
||||
// Server handler implementations.
|
||||
var handlerNames []string
|
||||
for _, method := range service.Method {
|
||||
hname := g.generateServerMethod(servName, method)
|
||||
handlerNames = append(handlerNames, hname)
|
||||
}
|
||||
}
|
||||
|
||||
// generateEndpoint creates the api endpoint
|
||||
func (g *micro) generateEndpoint(servName string, method *pb.MethodDescriptorProto) {
|
||||
if method.Options == nil || !proto.HasExtension(method.Options, options.E_Http) {
|
||||
return
|
||||
}
|
||||
// http rules
|
||||
r := proto.GetExtension(method.Options, options.E_Http)
|
||||
rule := r.(*options.HttpRule)
|
||||
var meth string
|
||||
var path string
|
||||
switch {
|
||||
case len(rule.GetDelete()) > 0:
|
||||
meth = "DELETE"
|
||||
path = rule.GetDelete()
|
||||
case len(rule.GetGet()) > 0:
|
||||
meth = "GET"
|
||||
path = rule.GetGet()
|
||||
case len(rule.GetPatch()) > 0:
|
||||
meth = "PATCH"
|
||||
path = rule.GetPatch()
|
||||
case len(rule.GetPost()) > 0:
|
||||
meth = "POST"
|
||||
path = rule.GetPost()
|
||||
case len(rule.GetPut()) > 0:
|
||||
meth = "PUT"
|
||||
path = rule.GetPut()
|
||||
}
|
||||
if len(meth) == 0 || len(path) == 0 {
|
||||
return
|
||||
}
|
||||
// TODO: process additional bindings
|
||||
g.P("Name:", fmt.Sprintf(`"%s.%s",`, servName, method.GetName()))
|
||||
g.P("Path:", fmt.Sprintf(`[]string{"%s"},`, path))
|
||||
g.P("Method:", fmt.Sprintf(`[]string{"%s"},`, meth))
|
||||
if method.GetServerStreaming() || method.GetClientStreaming() {
|
||||
g.P("Stream: true,")
|
||||
}
|
||||
g.P(`Handler: "rpc",`)
|
||||
}
|
||||
|
||||
// generateClientSignature returns the client-side signature for a method.
|
||||
func (g *micro) generateClientSignature(servName string, method *pb.MethodDescriptorProto) string {
|
||||
origMethName := method.GetName()
|
||||
methName := generator.CamelCase(origMethName)
|
||||
if reservedClientName[methName] {
|
||||
methName += "_"
|
||||
}
|
||||
reqArg := ", in *" + g.typeName(method.GetInputType())
|
||||
if method.GetClientStreaming() {
|
||||
reqArg = ""
|
||||
}
|
||||
respName := "*" + g.typeName(method.GetOutputType())
|
||||
if method.GetServerStreaming() || method.GetClientStreaming() {
|
||||
respName = servName + "_" + generator.CamelCase(origMethName) + "Service"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s(ctx %s.Context%s, opts ...%s.CallOption) (%s, error)", methName, contextPkg, reqArg, clientPkg, respName)
|
||||
}
|
||||
|
||||
func (g *micro) generateClientMethod(pkg, reqServ, servName, serviceDescVar string, method *pb.MethodDescriptorProto, descExpr string) {
|
||||
reqMethod := fmt.Sprintf("%s.%s", servName, method.GetName())
|
||||
useGrpc := g.gen.Param["use_grpc"]
|
||||
if useGrpc != "" {
|
||||
reqMethod = fmt.Sprintf("/%s.%s/%s", pkg, servName, method.GetName())
|
||||
}
|
||||
methName := generator.CamelCase(method.GetName())
|
||||
inType := g.typeName(method.GetInputType())
|
||||
outType := g.typeName(method.GetOutputType())
|
||||
|
||||
servAlias := servName + "Service"
|
||||
|
||||
// strip suffix
|
||||
if strings.HasSuffix(servAlias, "ServiceService") {
|
||||
servAlias = strings.TrimSuffix(servAlias, "Service")
|
||||
}
|
||||
|
||||
g.P("func (c *", unexport(servAlias), ") ", g.generateClientSignature(servName, method), "{")
|
||||
if !method.GetServerStreaming() && !method.GetClientStreaming() {
|
||||
g.P(`req := c.c.NewRequest(c.name, "`, reqMethod, `", in)`)
|
||||
g.P("out := new(", outType, ")")
|
||||
// TODO: Pass descExpr to Invoke.
|
||||
g.P("err := ", `c.c.Call(ctx, req, out, opts...)`)
|
||||
g.P("if err != nil { return nil, err }")
|
||||
g.P("return out, nil")
|
||||
g.P("}")
|
||||
g.P()
|
||||
return
|
||||
}
|
||||
streamType := unexport(servAlias) + methName
|
||||
g.P(`req := c.c.NewRequest(c.name, "`, reqMethod, `", &`, inType, `{})`)
|
||||
g.P("stream, err := c.c.Stream(ctx, req, opts...)")
|
||||
g.P("if err != nil { return nil, err }")
|
||||
|
||||
if !method.GetClientStreaming() {
|
||||
g.P("if err := stream.Send(in); err != nil { return nil, err }")
|
||||
// TODO: currently only grpc support CloseSend
|
||||
// g.P("if err := stream.CloseSend(); err != nil { return nil, err }")
|
||||
}
|
||||
|
||||
g.P("return &", streamType, "{stream}, nil")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
genSend := method.GetClientStreaming()
|
||||
genRecv := method.GetServerStreaming()
|
||||
|
||||
// Stream auxiliary types and methods.
|
||||
g.P("type ", servName, "_", methName, "Service interface {")
|
||||
g.P("Context() context.Context")
|
||||
g.P("SendMsg(interface{}) error")
|
||||
g.P("RecvMsg(interface{}) error")
|
||||
g.P("CloseSend() error")
|
||||
g.P("Close() error")
|
||||
|
||||
if genSend {
|
||||
g.P("Send(*", inType, ") error")
|
||||
}
|
||||
if genRecv {
|
||||
g.P("Recv() (*", outType, ", error)")
|
||||
}
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
g.P("type ", streamType, " struct {")
|
||||
g.P("stream ", clientPkg, ".Stream")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
g.P("func (x *", streamType, ") CloseSend() error {")
|
||||
g.P("return x.stream.CloseSend()")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
g.P("func (x *", streamType, ") Close() error {")
|
||||
g.P("return x.stream.Close()")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
g.P("func (x *", streamType, ") Context() context.Context {")
|
||||
g.P("return x.stream.Context()")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
g.P("func (x *", streamType, ") SendMsg(m interface{}) error {")
|
||||
g.P("return x.stream.Send(m)")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
g.P("func (x *", streamType, ") RecvMsg(m interface{}) error {")
|
||||
g.P("return x.stream.Recv(m)")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
if genSend {
|
||||
g.P("func (x *", streamType, ") Send(m *", inType, ") error {")
|
||||
g.P("return x.stream.Send(m)")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
}
|
||||
|
||||
if genRecv {
|
||||
g.P("func (x *", streamType, ") Recv() (*", outType, ", error) {")
|
||||
g.P("m := new(", outType, ")")
|
||||
g.P("err := x.stream.Recv(m)")
|
||||
g.P("if err != nil {")
|
||||
g.P("return nil, err")
|
||||
g.P("}")
|
||||
g.P("return m, nil")
|
||||
g.P("}")
|
||||
g.P()
|
||||
}
|
||||
}
|
||||
|
||||
// generateServerSignature returns the server-side signature for a method.
|
||||
func (g *micro) generateServerSignature(servName string, method *pb.MethodDescriptorProto) string {
|
||||
origMethName := method.GetName()
|
||||
methName := generator.CamelCase(origMethName)
|
||||
if reservedClientName[methName] {
|
||||
methName += "_"
|
||||
}
|
||||
|
||||
var reqArgs []string
|
||||
ret := "error"
|
||||
reqArgs = append(reqArgs, contextPkg+".Context")
|
||||
|
||||
if !method.GetClientStreaming() {
|
||||
reqArgs = append(reqArgs, "*"+g.typeName(method.GetInputType()))
|
||||
}
|
||||
if method.GetServerStreaming() || method.GetClientStreaming() {
|
||||
reqArgs = append(reqArgs, servName+"_"+generator.CamelCase(origMethName)+"Stream")
|
||||
}
|
||||
if !method.GetClientStreaming() && !method.GetServerStreaming() {
|
||||
reqArgs = append(reqArgs, "*"+g.typeName(method.GetOutputType()))
|
||||
}
|
||||
return methName + "(" + strings.Join(reqArgs, ", ") + ") " + ret
|
||||
}
|
||||
|
||||
func (g *micro) generateServerMethod(servName string, method *pb.MethodDescriptorProto) string {
|
||||
methName := generator.CamelCase(method.GetName())
|
||||
hname := fmt.Sprintf("_%s_%s_Handler", servName, methName)
|
||||
serveType := servName + "Handler"
|
||||
inType := g.typeName(method.GetInputType())
|
||||
outType := g.typeName(method.GetOutputType())
|
||||
|
||||
if !method.GetServerStreaming() && !method.GetClientStreaming() {
|
||||
g.P("func (h *", unexport(servName), "Handler) ", methName, "(ctx ", contextPkg, ".Context, in *", inType, ", out *", outType, ") error {")
|
||||
g.P("return h.", serveType, ".", methName, "(ctx, in, out)")
|
||||
g.P("}")
|
||||
g.P()
|
||||
return hname
|
||||
}
|
||||
streamType := unexport(servName) + methName + "Stream"
|
||||
g.P("func (h *", unexport(servName), "Handler) ", methName, "(ctx ", contextPkg, ".Context, stream server.Stream) error {")
|
||||
if !method.GetClientStreaming() {
|
||||
g.P("m := new(", inType, ")")
|
||||
g.P("if err := stream.Recv(m); err != nil { return err }")
|
||||
g.P("return h.", serveType, ".", methName, "(ctx, m, &", streamType, "{stream})")
|
||||
} else {
|
||||
g.P("return h.", serveType, ".", methName, "(ctx, &", streamType, "{stream})")
|
||||
}
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
genSend := method.GetServerStreaming()
|
||||
genRecv := method.GetClientStreaming()
|
||||
|
||||
// Stream auxiliary types and methods.
|
||||
g.P("type ", servName, "_", methName, "Stream interface {")
|
||||
g.P("Context() context.Context")
|
||||
g.P("SendMsg(interface{}) error")
|
||||
g.P("RecvMsg(interface{}) error")
|
||||
g.P("Close() error")
|
||||
|
||||
if genSend {
|
||||
g.P("Send(*", outType, ") error")
|
||||
}
|
||||
|
||||
if genRecv {
|
||||
g.P("Recv() (*", inType, ", error)")
|
||||
}
|
||||
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
g.P("type ", streamType, " struct {")
|
||||
g.P("stream ", serverPkg, ".Stream")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
g.P("func (x *", streamType, ") Close() error {")
|
||||
g.P("return x.stream.Close()")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
g.P("func (x *", streamType, ") Context() context.Context {")
|
||||
g.P("return x.stream.Context()")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
g.P("func (x *", streamType, ") SendMsg(m interface{}) error {")
|
||||
g.P("return x.stream.Send(m)")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
g.P("func (x *", streamType, ") RecvMsg(m interface{}) error {")
|
||||
g.P("return x.stream.Recv(m)")
|
||||
g.P("}")
|
||||
g.P()
|
||||
|
||||
if genSend {
|
||||
g.P("func (x *", streamType, ") Send(m *", outType, ") error {")
|
||||
g.P("return x.stream.Send(m)")
|
||||
g.P("}")
|
||||
g.P()
|
||||
}
|
||||
|
||||
if genRecv {
|
||||
g.P("func (x *", streamType, ") Recv() (*", inType, ", error) {")
|
||||
g.P("m := new(", inType, ")")
|
||||
g.P("if err := x.stream.Recv(m); err != nil { return nil, err }")
|
||||
g.P("return m, nil")
|
||||
g.P("}")
|
||||
g.P()
|
||||
}
|
||||
|
||||
return hname
|
||||
}
|
Loading…
Reference in New Issue
Block a user