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"
|
|
|
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
{{- $ServiceName := .Service.Name | trimSuffix "Service" }}
|
2021-01-30 19:34:30 +03:00
|
|
|
{{- $epnum := 0 }}
|
|
|
|
{{- range .Service.Method}}
|
|
|
|
{{- if not (contains (json (httpOption .)) "null") }}
|
|
|
|
{{- if ne (httpVerb .) "" }}
|
|
|
|
{{- $epnum = add $epnum 1 }}
|
|
|
|
{{- range httpPathsAdditionalBindings . }}
|
|
|
|
{{- $epnum = add $epnum 1 }}
|
|
|
|
{{- end }}
|
|
|
|
{{- end }}
|
|
|
|
{{- end }}
|
|
|
|
{{- end }}
|
2021-01-08 22:22:43 +03:00
|
|
|
|
|
|
|
// New{{$ServiceName}}Endpoints provides api endpoints metdata for {{$ServiceName}} service
|
|
|
|
func New{{$ServiceName}}Endpoints() []*micro_api.Endpoint {
|
2021-01-30 19:34:30 +03:00
|
|
|
endpoints := make([]*micro_api.Endpoint, 0, {{ $epnum }})
|
|
|
|
{{- if ne $epnum 0 }}
|
|
|
|
var endpoint *micro_api.Endpoint
|
|
|
|
{{- end }}
|
2021-01-08 22:22:43 +03:00
|
|
|
{{- range .Service.Method}}
|
2021-01-12 13:39:43 +03:00
|
|
|
{{- if not (contains (json (httpOption .)) "null") }}
|
2021-01-30 19:34:30 +03:00
|
|
|
{{- $httpOption := (httpOption .) }}
|
|
|
|
{{- if ne $httpOption.Method "" }}
|
|
|
|
endpoint = µ_api.Endpoint{
|
2021-01-08 22:22:43 +03:00
|
|
|
Name: "{{$ServiceName}}.{{.Name}}",
|
2021-01-30 19:34:30 +03:00
|
|
|
Path: []string{"{{$httpOption.Path}}"},
|
|
|
|
Method: []string{"{{$httpOption.Method}}"},
|
|
|
|
Body: "{{$httpOption.Body}}",
|
|
|
|
{{- if or (.ClientStreaming) (.ServerStreaming)}}
|
|
|
|
Stream: true,
|
2021-01-08 22:22:43 +03:00
|
|
|
{{- end}}
|
2021-01-30 19:34:30 +03:00
|
|
|
Handler: "rpc",
|
|
|
|
}
|
|
|
|
endpoints = append(endpoints, endpoint)
|
|
|
|
{{- range $index, $element := $httpOption.Additional }}
|
|
|
|
endpoint = µ_api.Endpoint{
|
|
|
|
Name: "{{$ServiceName}}.{{.Name}}",
|
|
|
|
Path: []string{"{{$element.Path}}"},
|
|
|
|
Method: []string{"{{$element.Method}}"},
|
|
|
|
Body: "{{$element.Body}}",
|
2021-01-08 22:22:43 +03:00
|
|
|
{{- if or (.ClientStreaming) (.ServerStreaming)}}
|
|
|
|
Stream: true,
|
|
|
|
{{- end}}
|
|
|
|
Handler: "rpc",
|
|
|
|
}
|
|
|
|
endpoints = append(endpoints, endpoint)
|
|
|
|
{{- end}}
|
|
|
|
{{- end}}
|
2021-01-12 13:39:43 +03:00
|
|
|
{{- end}}
|
2021-01-30 19:34:30 +03:00
|
|
|
{{- end}}
|
2021-01-08 22:22:43 +03:00
|
|
|
return endpoints
|
|
|
|
}
|
|
|
|
|
|
|
|
// {{$ServiceName}}Service interface
|
|
|
|
type {{$ServiceName}}Service interface {
|
|
|
|
{{- range .Service.Method}}
|
|
|
|
{{- $reqMethod := .InputType | splitArray "." | last }}
|
|
|
|
{{- $rspMethod := .OutputType | splitArray "." | last }}
|
|
|
|
{{- if or (.ServerStreaming) (.ClientStreaming)}}
|
|
|
|
{{- if and (.ServerStreaming) (not .ClientStreaming)}}
|
|
|
|
{{.Name}}(context.Context, *{{$reqMethod}}, ...micro_client.CallOption) ({{$ServiceName}}_{{.Name}}Service, error)
|
|
|
|
{{- else}}
|
|
|
|
{{.Name}}(context.Context, ...micro_client.CallOption) ({{$ServiceName}}_{{.Name}}Service, error)
|
|
|
|
{{- end}}
|
|
|
|
{{- else}}
|
|
|
|
{{.Name}}(context.Context, *{{$reqMethod}}, ...micro_client.CallOption) (*{{$rspMethod}}, error)
|
|
|
|
{{- end}}
|
|
|
|
{{- end}}
|
|
|
|
}
|
|
|
|
|
|
|
|
{{range .Service.Method}}
|
|
|
|
{{- $reqMethod := .InputType | splitArray "." | last}}
|
|
|
|
{{- $rspMethod := .OutputType | splitArray "." | last}}
|
|
|
|
{{if or (.ServerStreaming) (.ClientStreaming)}}
|
|
|
|
type {{$ServiceName}}_{{.Name}}Service interface {
|
|
|
|
Context() context.Context
|
|
|
|
SendMsg(interface{}) error
|
|
|
|
RecvMsg(interface{}) error
|
|
|
|
{{- if and (.ClientStreaming) (not .ServerStreaming)}}
|
|
|
|
RecvAndClose() (*{{$rspMethod}}, error)
|
|
|
|
{{- end}}
|
|
|
|
Close() error
|
|
|
|
{{- if .ClientStreaming}}
|
|
|
|
Send(*{{$reqMethod}}) error
|
|
|
|
{{- end}}
|
|
|
|
{{- if .ServerStreaming}}
|
|
|
|
Recv() (*{{$rspMethod}}, error)
|
|
|
|
{{- end}}
|
|
|
|
}
|
|
|
|
{{- end}}
|
|
|
|
{{- end}}
|
|
|
|
// Micro server stuff
|
|
|
|
|
|
|
|
// {{$ServiceName}}Handler server handler
|
|
|
|
type {{$ServiceName}}Handler interface {
|
|
|
|
{{- range .Service.Method}}
|
|
|
|
{{- $reqMethod := .InputType | splitArray "." | last}}
|
|
|
|
{{- $rspMethod := .OutputType | splitArray "." | last}}
|
|
|
|
{{- if or (.ServerStreaming) (.ClientStreaming)}}
|
|
|
|
{{- if not .ClientStreaming}}
|
|
|
|
{{.Name}}(context.Context, *{{$reqMethod}}, {{$ServiceName}}_{{.Name}}Stream) error
|
|
|
|
{{- else}}
|
|
|
|
{{.Name}}(context.Context, {{$ServiceName}}_{{.Name}}Stream) error
|
|
|
|
{{- end}}
|
|
|
|
{{- else}}
|
|
|
|
{{.Name}}(context.Context, *{{$reqMethod}}, *{{$rspMethod}}) error
|
|
|
|
{{- 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...))
|
|
|
|
}
|
|
|
|
|
|
|
|
{{- range .Service.Method}}
|
|
|
|
{{- $reqMethod := .InputType | splitArray "." | last}}
|
|
|
|
{{- $rspMethod := .OutputType | splitArray "." | last}}
|
|
|
|
{{if or (.ServerStreaming) (.ClientStreaming)}}
|
|
|
|
type {{$ServiceName}}_{{.Name}}Stream interface {
|
|
|
|
Context() context.Context
|
|
|
|
SendMsg(interface{}) error
|
|
|
|
RecvMsg(interface{}) error
|
|
|
|
{{- if and (.ClientStreaming) (not .ServerStreaming)}}
|
|
|
|
SendAndClose(*{{$rspMethod}}) error
|
|
|
|
{{- end}}
|
|
|
|
Close() error
|
|
|
|
{{- if .ServerStreaming}}
|
|
|
|
Send(*{{$rspMethod}}) error
|
|
|
|
{{- end}}
|
|
|
|
{{- if .ClientStreaming}}
|
|
|
|
Recv() (*{{$reqMethod}}, error)
|
|
|
|
{{- end}}
|
|
|
|
}
|
|
|
|
{{- end}}
|
|
|
|
{{- end}}
|