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. 16
      cmd/kratos/internal/proto/server/template.go

@ -6,12 +6,16 @@ import (
)
var serviceTemplate = `
{{- /* delete empty line */ -}}
package service
import (
"context"
pb "{{ .Package }}"
{{- if .GoogleEmpty }}
"google.golang.org/protobuf/types/known/emptypb"
{{- end }}
)
type {{ .Service }}Service struct {
@ -21,9 +25,11 @@ type {{.Service}}Service struct {
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 }}
`
@ -33,6 +39,7 @@ type Service struct {
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