fix cmd proto server (#1098)

pull/1104/head
包子 3 years ago committed by GitHub
parent 5f41ea0f04
commit 8cf377cf9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 36
      cmd/kratos/internal/proto/server/template.go

@ -6,33 +6,40 @@ import (
) )
var serviceTemplate = ` var serviceTemplate = `
{{- /* delete empty line */ -}}
package service package service
import( import (
"context" "context"
pb "{{.Package}}" pb "{{ .Package }}"
{{- if .GoogleEmpty }}
"google.golang.org/protobuf/types/known/emptypb"
{{- end }}
) )
type {{.Service}}Service struct { type {{ .Service }}Service struct {
pb.Unimplemented{{.Service}}Server pb.Unimplemented{{ .Service }}Server
} }
func New{{.Service}}Service() *{{.Service}}Service { func New{{ .Service }}Service() *{{ .Service }}Service {
return &{{.Service}}Service{} return &{{ .Service }}Service{}
} }
{{- $s1 := "google.protobuf.Empty" }}
{{ range .Methods }} {{ range .Methods }}
func (s *{{.Service}}Service) {{.Name}}(ctx context.Context, req *pb.{{.Request}}) (*pb.{{.Reply}}, error) { func (s *{{ .Service }}Service) {{ .Name }}(ctx context.Context, req {{ if eq .Reply $s1 }}*emptypb.Empty{{ else }}*pb.{{ .Request }}{{ end }}) ({{ if eq .Reply $s1 }}*emptypb.Empty{{ else }}*pb.{{ .Reply }}{{ end }}, error) {
return &pb.{{.Reply}}{}, nil return {{ if eq .Reply $s1 }}&emptypb.Empty{}{{ else }}&pb.{{ .Reply }}{}{{ end }}, nil
} }
{{- end }} {{- end }}
` `
// Service is a proto service. // Service is a proto service.
type Service struct { type Service struct {
Package string Package string
Service string Service string
Methods []*Method Methods []*Method
GoogleEmpty bool
} }
// Method is a proto method. // Method is a proto method.
@ -45,6 +52,11 @@ type Method struct {
func (s *Service) execute() ([]byte, error) { func (s *Service) execute() ([]byte, error) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
for _, method := range s.Methods {
if method.Request == "google.protobuf.Empty" {
s.GoogleEmpty = true
}
}
tmpl, err := template.New("service").Parse(serviceTemplate) tmpl, err := template.New("service").Parse(serviceTemplate)
if err != nil { if err != nil {
return nil, err return nil, err

Loading…
Cancel
Save