fix cmd proto server (#1098)

pull/1104/head
包子 4 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 = `
{{- /* delete empty line */ -}}
package service
import(
"context"
import (
"context"
pb "{{.Package}}"
pb "{{ .Package }}"
{{- if .GoogleEmpty }}
"google.golang.org/protobuf/types/known/emptypb"
{{- end }}
)
type {{.Service}}Service struct {
pb.Unimplemented{{.Service}}Server
type {{ .Service }}Service struct {
pb.Unimplemented{{ .Service }}Server
}
func New{{.Service}}Service() *{{.Service}}Service {
return &{{.Service}}Service{}
func New{{ .Service }}Service() *{{ .Service }}Service {
return &{{ .Service }}Service{}
}
{{- $s1 := "google.protobuf.Empty" }}
{{ range .Methods }}
func (s *{{.Service}}Service) {{.Name}}(ctx context.Context, req *pb.{{.Request}}) (*pb.{{.Reply}}, error) {
return &pb.{{.Reply}}{}, nil
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 {{ if eq .Reply $s1 }}&emptypb.Empty{}{{ else }}&pb.{{ .Reply }}{}{{ end }}, nil
}
{{- end }}
`
// Service is a proto service.
type Service struct {
Package string
Service string
Methods []*Method
Package string
Service string
Methods []*Method
GoogleEmpty bool
}
// Method is a proto method.
@ -45,6 +52,11 @@ type Method struct {
func (s *Service) execute() ([]byte, error) {
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)
if err != nil {
return nil, err

Loading…
Cancel
Save