protoc-gen-go-micro/examples/go-kit/templates/{{.File.Package}}/gen/endpoints/endpoints.go.tmpl

87 lines
2.6 KiB
Cheetah
Raw Normal View History

2016-12-13 17:31:37 +03:00
package {{.File.Package}}_endpoints
{{$file := .File}}
import (
"fmt"
2016-12-13 17:31:37 +03:00
context "golang.org/x/net/context"
pb "{{cat .GoPWD "/" .DestinationDir | nospace | clean}}/pb"
2016-12-13 17:31:37 +03:00
"github.com/go-kit/kit/endpoint"
)
var _ = fmt.Errorf
2016-12-23 21:20:44 +03:00
type StreamEndpoint func(server interface{}, req interface{}) (err error)
2016-12-13 17:31:37 +03:00
type Endpoints struct {
{{range .Service.Method}}
2016-12-23 21:20:44 +03:00
{{if or (.ClientStreaming) (.ServerStreaming)}}
{{.Name}}Endpoint StreamEndpoint
{{else}}
{{.Name}}Endpoint endpoint.Endpoint
{{end}}
2016-12-13 17:31:37 +03:00
{{end}}
}
{{range .Service.Method}}
2016-12-23 21:20:44 +03:00
{{if .ServerStreaming}}
{{if .ClientStreaming}}
func (e *Endpoints){{.Name}}(server pb.{{$file.Package | title}}Service_{{.Name}}Server) error {
return fmt.Errorf("not implemented")
}
{{else}}
func (e *Endpoints){{.Name}}(in *pb.{{.Name}}Request, server pb.{{$file.Package | title}}Service_{{.Name}}Server) error {
return fmt.Errorf("not implemented")
}
{{end}}
{{else}}
{{if .ClientStreaming}}
func (e *Endpoints){{.Name}}(server pb.{{$file.Package | title}}Service_{{.Name}}Server) error {
return fmt.Errorf("not implemented")
}
{{else}}
func (e *Endpoints){{.Name}}(ctx context.Context, in *pb.{{.InputType | splitArray "." | last}}) (*pb.{{.OutputType | splitArray "." | last}}, error) {
out, err := e.{{.Name}}Endpoint(ctx, in)
if err != nil {
return &pb.{{.OutputType | splitArray "." | last}}{ErrMsg: err.Error()}, err
}
return out.(*pb.{{.OutputType | splitArray "." | last}}), err
}
{{end}}
{{end}}
2016-12-13 17:31:37 +03:00
{{end}}
{{range .Service.Method}}
2016-12-23 21:20:44 +03:00
{{if or (.ServerStreaming) (.ClientStreaming)}}
func Make{{.Name}}Endpoint(svc pb.{{$file.Package | title}}ServiceServer) StreamEndpoint {
return func(server interface{}, request interface{}) error {
{{if .ClientStreaming}}
return svc.{{.Name}}(server.(pb.{{$file.Package | title}}Service_{{.Name}}Server))
{{else}}
return svc.{{.Name}}(request.(*pb.{{.Name}}Request), server.(pb.{{$file.Package | title}}Service_{{.Name}}Server))
{{end}}
}
2016-12-13 17:31:37 +03:00
}
2016-12-23 21:20:44 +03:00
{{else}}
func Make{{.Name}}Endpoint(svc pb.{{$file.Package | title}}ServiceServer) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(*pb.{{.InputType | splitArray "." | last}})
rep, err := svc.{{.Name}}(ctx, req)
if err != nil {
return &pb.{{.OutputType | splitArray "." | last}}{ErrMsg: err.Error()}, err
}
return rep, nil
}
}
{{end}}
2016-12-13 17:31:37 +03:00
{{end}}
func MakeEndpoints(svc pb.{{.File.Package | title}}ServiceServer) Endpoints {
return Endpoints{
{{range .Service.Method}}
2016-12-23 21:20:44 +03:00
{{.Name}}Endpoint: Make{{.Name}}Endpoint(svc),
2016-12-13 17:31:37 +03:00
{{end}}
}
}