216 lines
8.7 KiB
Plaintext
216 lines
8.7 KiB
Plaintext
// Code generated by protoc-gen-micro
|
|
// source: {{.File.Name}}
|
|
package {{goPkgLastElement .File | splitArray ";" | last | replace "." "_"}}
|
|
|
|
import (
|
|
"context"
|
|
|
|
micro_api "github.com/unistack-org/micro/v3/api"
|
|
micro_client "github.com/unistack-org/micro/v3/client"
|
|
micro_server "github.com/unistack-org/micro/v3/server"
|
|
)
|
|
|
|
var (
|
|
_ micro_server.Option
|
|
_ micro_client.Option
|
|
)
|
|
|
|
{{- $File := .File }}
|
|
{{- $ServiceName := .Service.Name | trimSuffix "Service" }}
|
|
|
|
type {{$ServiceName | lowerFirst}}Service struct {
|
|
c micro_client.Client
|
|
name string
|
|
}
|
|
|
|
// Micro client stuff
|
|
|
|
// New{{$ServiceName}}Service create new service client
|
|
func New{{$ServiceName}}Service(name string, c micro_client.Client) {{$ServiceName}}Service {
|
|
return &{{$ServiceName | lowerFirst}}Service{c: c, name: name}
|
|
}
|
|
|
|
{{range .Service.Method}}
|
|
{{- $reqMethod := .InputType | splitArray "." | last}}
|
|
{{- $rspMethod := .OutputType | splitArray "." | last}}
|
|
{{- if and (not .ClientStreaming) (.ServerStreaming) -}}
|
|
func (c *{{$ServiceName | lowerFirst}}Service) {{.Name}}(ctx context.Context, req *{{$reqMethod}}, opts ...micro_client.CallOption) ({{$ServiceName}}_{{.Name}}Service, error) {
|
|
{{- else if .ClientStreaming -}}
|
|
func (c *{{$ServiceName | lowerFirst}}Service) {{.Name}}(ctx context.Context, opts ...micro_client.CallOption) ({{$ServiceName}}_{{.Name}}Service, error) {
|
|
{{- else -}}
|
|
func (c *{{$ServiceName | lowerFirst}}Service) {{.Name}}(ctx context.Context, req *{{$reqMethod}}, opts ...micro_client.CallOption) (*{{$rspMethod}}, error) {
|
|
{{- end -}}
|
|
{{- if or (.ServerStreaming) (.ClientStreaming)}}
|
|
stream, err := c.c.Stream(ctx, c.c.NewRequest(c.name, "{{$ServiceName}}.{{.Name}}", &{{$reqMethod}}{}), opts...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
{{- if not .ClientStreaming }}
|
|
if err := stream.Send(req); err != nil {
|
|
return nil, err
|
|
}
|
|
{{- end}}
|
|
return &{{$ServiceName | lowerFirst}}Service{{.Name}}{stream}, nil
|
|
{{- else}}
|
|
rsp := &{{$rspMethod}}{}
|
|
err := c.c.Call(ctx, c.c.NewRequest(c.name, "{{$ServiceName}}.{{.Name}}", req), rsp, opts...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return rsp, nil
|
|
{{- end}}
|
|
}
|
|
|
|
{{if or (.ServerStreaming) (.ClientStreaming)}}
|
|
type {{$ServiceName | lowerFirst}}Service{{.Name}} struct {
|
|
stream micro_client.Stream
|
|
}
|
|
{{if and (.ClientStreaming) (not .ServerStreaming)}}
|
|
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) RecvAndClose() (*{{$rspMethod}}, error) {
|
|
m := &{{$rspMethod}}{}
|
|
err := x.RecvMsg(m)
|
|
if err == nil {
|
|
err = x.Close()
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return m, nil
|
|
}
|
|
{{- end}}
|
|
|
|
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) Close() error {
|
|
return x.stream.Close()
|
|
}
|
|
|
|
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) Context() context.Context {
|
|
return x.stream.Context()
|
|
}
|
|
|
|
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) SendMsg(m interface{}) error {
|
|
return x.stream.Send(m)
|
|
}
|
|
|
|
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) RecvMsg(m interface{}) error {
|
|
return x.stream.Recv(m)
|
|
}
|
|
|
|
{{ if .ClientStreaming -}}
|
|
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) Send(m *{{$reqMethod}}) error {
|
|
return x.stream.Send(m)
|
|
}
|
|
{{end}}
|
|
|
|
{{ if .ServerStreaming -}}
|
|
func (x *{{$ServiceName | lowerFirst}}Service{{.Name}}) Recv() (*{{$rspMethod}}, error) {
|
|
m := &{{$rspMethod}}{}
|
|
if err := x.stream.Recv(m); err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
{{ end }}
|
|
|
|
{{- end}}
|
|
{{- end}}
|
|
|
|
// Micro server stuff
|
|
|
|
type {{$ServiceName | lowerFirst}}Handler struct {
|
|
{{$ServiceName}}Handler
|
|
}
|
|
{{range .Service.Method }}
|
|
{{- $reqMethod := .InputType | splitArray "." | last}}
|
|
{{- $rspMethod := .OutputType | splitArray "." | last}}
|
|
{{- if or (.ServerStreaming) (.ClientStreaming)}}
|
|
func (h *{{$ServiceName | lowerFirst}}Handler) {{.Name}}(ctx context.Context, stream micro_server.Stream) error {
|
|
{{- if not .ClientStreaming}}
|
|
m := &{{$reqMethod}}{}
|
|
if err := stream.Recv(m); err != nil {
|
|
return err
|
|
}
|
|
return h.{{$ServiceName}}Handler.{{.Name}}(ctx, m, &{{$ServiceName | lowerFirst}}{{.Name}}Stream{stream})
|
|
{{- else}}
|
|
return h.{{$ServiceName}}Handler.{{.Name}}(ctx, &{{$ServiceName | lowerFirst}}{{.Name}}Stream{stream})
|
|
{{- end}}
|
|
}
|
|
{{- else}}
|
|
func (h *{{$ServiceName | lowerFirst}}Handler) {{.Name}}(ctx context.Context, req *{{$reqMethod}}, rsp *{{$rspMethod}}) error {
|
|
return h.{{$ServiceName}}Handler.{{.Name}}(ctx, req, rsp)
|
|
}
|
|
{{- end}}
|
|
{{if or (.ServerStreaming) (.ClientStreaming)}}
|
|
type {{$ServiceName | lowerFirst}}{{.Name}}Stream struct {
|
|
stream micro_server.Stream
|
|
}
|
|
{{if and (.ClientStreaming) (not .ServerStreaming)}}
|
|
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) SendAndClose(m *{{$rspMethod}}) error {
|
|
err := x.SendMsg(m)
|
|
if err == nil {
|
|
err = x.stream.Close()
|
|
}
|
|
return err
|
|
}
|
|
{{- end}}
|
|
|
|
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) Close() error {
|
|
return x.stream.Close()
|
|
}
|
|
|
|
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) Context() context.Context {
|
|
return x.stream.Context()
|
|
}
|
|
|
|
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) SendMsg(m interface{}) error {
|
|
return x.stream.Send(m)
|
|
}
|
|
|
|
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) RecvMsg(m interface{}) error {
|
|
return x.stream.Recv(m)
|
|
}
|
|
|
|
{{- if .ServerStreaming}}
|
|
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) Send(m *{{$rspMethod}}) error {
|
|
return x.stream.Send(m)
|
|
}
|
|
{{end}}
|
|
|
|
{{if .ClientStreaming}}
|
|
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) Recv() (*{{$reqMethod}}, error) {
|
|
m := &{{$reqMethod}}{}
|
|
if err := x.stream.Recv(m); err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
{{end}}
|
|
|
|
{{end}}
|
|
{{end}}
|
|
|
|
// Register{{$ServiceName}}Handler registers server handler
|
|
func Register{{$ServiceName}}Handler(s micro_server.Server, sh {{$ServiceName}}Handler, opts ...micro_server.HandlerOption) error {
|
|
type {{$ServiceName | lowerFirst}} interface {
|
|
{{- range .Service.Method}}
|
|
{{- $reqMethod := .InputType | splitArray "." | last}}
|
|
{{- $rspMethod := .OutputType | splitArray "." | last}}
|
|
{{- if or (.ServerStreaming) (.ClientStreaming)}}
|
|
{{.Name}}(context.Context, micro_server.Stream) error
|
|
{{- else}}
|
|
{{.Name}}(context.Context, *{{$reqMethod}}, *{{$rspMethod}}) error
|
|
{{- end}}
|
|
{{- end}}
|
|
}
|
|
type {{$ServiceName}} struct {
|
|
{{$ServiceName | lowerFirst}}
|
|
}
|
|
h := &{{$ServiceName | lowerFirst}}Handler{sh}
|
|
for _, endpoint := range New{{$ServiceName}}Endpoints() {
|
|
opts = append(opts, micro_api.WithEndpoint(endpoint))
|
|
}
|
|
return s.Handle(s.NewHandler(&{{$ServiceName}}{h}, opts...))
|
|
}
|
|
|