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

53 lines
1.3 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
type Endpoints struct {
{{range .Service.Method}}
{{.Name}}Endpoint endpoint.Endpoint
{{end}}
}
{{range .Service.Method}}
/*{{. | prettyjson}}*/
func (e *Endpoints){{.Name}}(ctx context.Context, in *pb.{{.InputType | splitArray "." | last}}) (*pb.{{.OutputType | splitArray "." | last}}, error) {
2016-12-13 17:31:37 +03:00
out, err := e.{{.Name}}Endpoint(ctx, in)
if err != nil {
return &pb.{{.OutputType | splitArray "." | last}}{ErrMsg: err.Error()}, err
2016-12-13 17:31:37 +03:00
}
return out.(*pb.{{.OutputType | splitArray "." | last}}), 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.{{.InputType | splitArray "." | last}})
2016-12-13 17:31:37 +03:00
rep, err := svc.{{.Name}}(ctx, req)
if err != nil {
return &pb.{{.OutputType | splitArray "." | last}}{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}}
}
}