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

50 lines
1.2 KiB
Cheetah
Raw Normal View History

2016-12-13 17:31:37 +03:00
package {{.File.Package}}_endpoints
{{$file := .File}}
import (
"fmt"
context "golang.org/x/net/context"
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/{{.File.Package}}/gen/pb"
2016-12-13 17:31:37 +03:00
"github.com/go-kit/kit/endpoint"
)
var _ = fmt.Errorf
type Endpoints struct {
{{range .Service.Method}}
{{.Name}}Endpoint endpoint.Endpoint
{{end}}
}
{{range .Service.Method}}
func (e *Endpoints){{.Name}}(ctx context.Context, in *pb.{{.Name}}Request) (*pb.{{.Name}}Response, error) {
out, err := e.{{.Name}}Endpoint(ctx, in)
if err != nil {
return &pb.{{.Name}}Response{ErrMsg: err.Error()}, err
}
2016-12-13 18:03:16 +03:00
return out.(*pb.{{.Name}}Response), err
2016-12-13 17:31:37 +03:00
}
{{end}}
{{range .Service.Method}}
func Make{{.Name}}Endpoint(svc pb.{{$file.Package | title}}ServiceServer) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(*pb.{{.Name}}Request)
rep, err := svc.{{.Name}}(ctx, req)
if err != nil {
2016-12-13 18:03:16 +03:00
return &pb.{{.Name}}Response{ErrMsg: err.Error()}, err
2016-12-13 17:31:37 +03:00
}
return rep, nil
}
}
{{end}}
func MakeEndpoints(svc pb.{{.File.Package | title}}ServiceServer) Endpoints {
return Endpoints{
{{range .Service.Method}}
{{.Name}}Endpoint: Make{{.Name}}Endpoint(svc),
{{end}}
}
}