protoc-gen-go-micro/templates/e3t0cmltU3VmZml4ICIucHJvdG8iIC5GaWxlLk5hbWV9fV9taWNyby5wYi5nby50bXBs

167 lines
5.5 KiB
Plaintext
Raw Permalink Normal View History

// 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"
)
{{- $ServiceName := .Service.Name | trimSuffix "Service" }}
{{- $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 }}
// New{{$ServiceName}}Endpoints provides api endpoints metdata for {{$ServiceName}} service
func New{{$ServiceName}}Endpoints() []*micro_api.Endpoint {
endpoints := make([]*micro_api.Endpoint, 0, {{ $epnum }})
{{- if ne $epnum 0 }}
var endpoint *micro_api.Endpoint
{{- end }}
{{- range .Service.Method}}
{{- if not (contains (json (httpOption .)) "null") }}
{{- $httpOption := (httpOption .) }}
{{- if ne $httpOption.Method "" }}
endpoint = &micro_api.Endpoint{
Name: "{{$ServiceName}}.{{.Name}}",
Path: []string{"{{$httpOption.Path}}"},
Method: []string{"{{$httpOption.Method}}"},
Body: "{{$httpOption.Body}}",
{{- if or (.ClientStreaming) (.ServerStreaming)}}
Stream: true,
{{- end}}
Handler: "rpc",
}
endpoints = append(endpoints, endpoint)
{{- range $index, $element := $httpOption.Additional }}
endpoint = &micro_api.Endpoint{
Name: "{{$ServiceName}}.{{.Name}}",
Path: []string{"{{$element.Path}}"},
Method: []string{"{{$element.Method}}"},
Body: "{{$element.Body}}",
{{- if or (.ClientStreaming) (.ServerStreaming)}}
Stream: true,
{{- end}}
Handler: "rpc",
}
endpoints = append(endpoints, endpoint)
{{- end}}
{{- end}}
{{- end}}
{{- end}}
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}}