2021-01-08 22:22:43 +03:00
|
|
|
// Code generated by protoc-gen-micro
|
|
|
|
// source: {{.File.Name}}
|
2021-02-02 07:59:50 +03:00
|
|
|
package {{goPkgLastElement .File | splitArray ";" | last | replace "." "_"}}
|
2021-01-08 22:22:43 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-14 14:27:41 +03:00
|
|
|
"fmt"
|
2021-01-08 22:22:43 +03:00
|
|
|
|
|
|
|
micro_client "github.com/unistack-org/micro/v3/client"
|
|
|
|
micro_server "github.com/unistack-org/micro/v3/server"
|
2021-01-19 04:07:16 +03:00
|
|
|
micro_client_http "github.com/unistack-org/micro-client-http/v3"
|
2021-01-08 22:22:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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}}
|
2021-02-02 09:29:56 +03:00
|
|
|
{{ if and (not .ClientStreaming) (.ServerStreaming) }}
|
2021-01-08 22:22:43 +03:00
|
|
|
func (c *{{$ServiceName | lowerFirst}}Service) {{.Name}}(ctx context.Context, req *{{$reqMethod}}, opts ...micro_client.CallOption) ({{$ServiceName}}_{{.Name}}Service, error) {
|
2021-02-02 09:29:56 +03:00
|
|
|
{{ else if .ClientStreaming }}
|
2021-01-08 22:22:43 +03:00
|
|
|
func (c *{{$ServiceName | lowerFirst}}Service) {{.Name}}(ctx context.Context, opts ...micro_client.CallOption) ({{$ServiceName}}_{{.Name}}Service, error) {
|
2021-02-02 09:29:56 +03:00
|
|
|
{{ else }}
|
2021-01-08 22:22:43 +03:00
|
|
|
func (c *{{$ServiceName | lowerFirst}}Service) {{.Name}}(ctx context.Context, req *{{$reqMethod}}, opts ...micro_client.CallOption) (*{{$rspMethod}}, error) {
|
|
|
|
{{- end }}
|
2021-01-10 03:33:10 +03:00
|
|
|
{{- if not (contains (json (openapiOption .)) "null") }}
|
2021-01-08 22:22:43 +03:00
|
|
|
{{- if (openapiOption .).Responses }}
|
|
|
|
errmap := make(map[string]interface{}, {{ len (openapiOption .).Responses}})
|
|
|
|
{{- range $k, $v := (openapiOption .).Responses }}
|
|
|
|
errmap["{{$k}}"] = &{{- (getMessageType $File $v.Schema.JsonSchema.Ref).Name }}{}
|
|
|
|
{{- end }}
|
|
|
|
{{- end }}
|
2021-02-02 09:29:56 +03:00
|
|
|
{{ end }}
|
2021-01-08 22:22:43 +03:00
|
|
|
nopts := append(opts,
|
|
|
|
micro_client_http.Method("{{httpVerb .}}"),
|
|
|
|
micro_client_http.Path("{{httpPath .}}"),
|
2021-01-19 12:43:37 +03:00
|
|
|
{{- if not (contains (httpBody .) "GET") }}
|
2021-01-19 04:15:19 +03:00
|
|
|
micro_client_http.Body("{{httpBody .}}"),
|
2021-01-19 12:43:37 +03:00
|
|
|
{{- end }}
|
2021-01-10 03:33:10 +03:00
|
|
|
{{- if not (contains (json (openapiOption .)) "null") }}
|
2021-01-08 22:22:43 +03:00
|
|
|
{{- if (openapiOption .).Responses }}
|
|
|
|
micro_client_http.ErrorMap(errmap),
|
|
|
|
{{- end }}
|
2021-01-10 03:33:10 +03:00
|
|
|
{{- end }}
|
2021-01-08 22:22:43 +03:00
|
|
|
)
|
|
|
|
{{- if or (.ServerStreaming) (.ClientStreaming)}}
|
|
|
|
stream, err := c.c.Stream(ctx, c.c.NewRequest(c.name, "{{$ServiceName}}.{{.Name}}", &{{$reqMethod}}{}), nopts...)
|
|
|
|
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, nopts...)
|
|
|
|
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}}
|
|
|
|
|
2021-02-02 09:29:56 +03:00
|
|
|
{{if or (.ServerStreaming) (.ClientStreaming)}}
|
2021-01-08 22:22:43 +03:00
|
|
|
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)
|
|
|
|
}
|
2021-02-02 09:29:56 +03:00
|
|
|
{{if .ServerStreaming}}
|
2021-01-08 22:22:43 +03:00
|
|
|
func (x *{{$ServiceName | lowerFirst}}{{.Name}}Stream) Send(m *{{$rspMethod}}) error {
|
|
|
|
return x.stream.Send(m)
|
|
|
|
}
|
2021-02-02 09:29:56 +03:00
|
|
|
{{end}}
|
|
|
|
|
2021-01-08 22:22:43 +03:00
|
|
|
{{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
|
|
|
|
}
|
2021-02-02 09:29:56 +03:00
|
|
|
{{end}}
|
2021-01-08 22:22:43 +03:00
|
|
|
|
2021-02-02 09:29:56 +03:00
|
|
|
{{end}}
|
|
|
|
{{end}}
|
2021-01-30 19:34:30 +03:00
|
|
|
|
|
|
|
{{- $errmsg := list }}
|
|
|
|
{{range .Service.Method}}
|
|
|
|
{{- if not (contains (json (openapiOption .)) "null") }}
|
|
|
|
{{- if (openapiOption .).Responses }}
|
|
|
|
{{ range $k, $v := (openapiOption .).Responses }}
|
|
|
|
{{- $msgtype := (getMessageType $File $v.Schema.JsonSchema.Ref) }}
|
|
|
|
{{- $errmsg = append $errmsg $msgtype.Name }}
|
|
|
|
{{- end }}
|
|
|
|
{{- end }}
|
|
|
|
{{- end }}
|
|
|
|
{{- end }}
|
|
|
|
|
|
|
|
|
|
|
|
{{range $k, $v := ($errmsg | uniq) }}
|
|
|
|
// Error method to satisfy error interface
|
|
|
|
func (e *{{- $v }}) Error() string {
|
|
|
|
return fmt.Sprintf("%#v", e)
|
|
|
|
}
|
|
|
|
{{- end }}
|