protoc-gen-go-micro/examples/go-kit/templates/{{.File.Package}}/gen/endpoints/endpoints.go.tmpl
2017-03-16 16:15:34 +01:00

90 lines
2.7 KiB
Cheetah

package {{.File.Package}}_endpoints
{{$file := .File}}
import (
context "context"
"fmt"
oldcontext "golang.org/x/net/context"
pb "{{cat .GoPWD "/" .DestinationDir | nospace | clean}}/pb"
"github.com/go-kit/kit/endpoint"
)
var _ = endpoint.Chain
var _ = fmt.Errorf
var _ = context.Background
type StreamEndpoint func(server interface{}, req interface{}) (err error)
type Endpoints struct {
{{range .Service.Method}}
{{if or (.ClientStreaming) (.ServerStreaming)}}
{{.Name}}Endpoint StreamEndpoint
{{else}}
{{.Name}}Endpoint endpoint.Endpoint
{{end}}
{{end}}
}
{{range .Service.Method}}
{{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 oldcontext.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}}
{{end}}
{{range .Service.Method}}
{{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}}
}
}
{{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}}
{{end}}
func MakeEndpoints(svc pb.{{.File.Package | title}}ServiceServer) Endpoints {
return Endpoints{
{{range .Service.Method}}
{{.Name}}Endpoint: Make{{.Name}}Endpoint(svc),
{{end}}
}
}