protoc-gen-go-micro/templates/{{trimSuffix ".proto" .File.Name}}_micro.pb.go.tmpl

145 lines
4.7 KiB
Cheetah
Raw Normal View History

// Code generated by protoc-gen-micro
// source: {{.File.Name}}
package {{goPkgLastElement .File | splitArray ";" | last}}
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" }}
// New{{$ServiceName}}Endpoints provides api endpoints metdata for {{$ServiceName}} service
func New{{$ServiceName}}Endpoints() []*micro_api.Endpoint {
var endpoints []*micro_api.Endpoint
{{- range .Service.Method}}
{{- if not (contains (json (httpOption .)) "null") }}
{{- if ne (httpVerb .) "" }}
endpoint := &micro_api.Endpoint{
Name: "{{$ServiceName}}.{{.Name}}",
Path: []string{"{{httpPath .}}"},
Method: []string{"{{httpVerb .}}"},
{{- if ne (httpBody .) "" }}
Body: "{{httpBody .}}",
{{- end}}
{{- if or (.ClientStreaming) (.ServerStreaming)}}
Stream: true,
{{- end}}
Handler: "rpc",
}
{{- range httpPathsAdditionalBindings . }}
endpoint.Path = append(endpoint.Path, "{{.}}")
{{- end}}
endpoints = append(endpoints, endpoint)
{{- 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}}