From a837603c6db1aeb226239391b9eb8db2ad3536b8 Mon Sep 17 00:00:00 2001 From: chen quan Date: Mon, 8 May 2023 11:35:10 +0800 Subject: [PATCH] chore: embedding template (#2810) --- cmd/protoc-gen-go-errors/errorsTemplate.tpl | 17 ++++ cmd/protoc-gen-go-errors/template.go | 22 +---- cmd/protoc-gen-go-http/httpTemplate.tpl | 93 +++++++++++++++++++ cmd/protoc-gen-go-http/template.go | 98 +-------------------- 4 files changed, 116 insertions(+), 114 deletions(-) create mode 100644 cmd/protoc-gen-go-errors/errorsTemplate.tpl create mode 100644 cmd/protoc-gen-go-http/httpTemplate.tpl diff --git a/cmd/protoc-gen-go-errors/errorsTemplate.tpl b/cmd/protoc-gen-go-errors/errorsTemplate.tpl new file mode 100644 index 000000000..b77a93905 --- /dev/null +++ b/cmd/protoc-gen-go-errors/errorsTemplate.tpl @@ -0,0 +1,17 @@ +{{ range .Errors }} + +{{ if .HasComment }}{{ .Comment }}{{ end -}} +func Is{{.CamelValue}}(err error) bool { + if err == nil { + return false + } + e := errors.FromError(err) + return e.Reason == {{ .Name }}_{{ .Value }}.String() && e.Code == {{ .HTTPCode }} +} + +{{ if .HasComment }}{{ .Comment }}{{ end -}} +func Error{{ .CamelValue }}(format string, args ...interface{}) *errors.Error { + return errors.New({{ .HTTPCode }}, {{ .Name }}_{{ .Value }}.String(), fmt.Sprintf(format, args...)) +} + +{{- end }} diff --git a/cmd/protoc-gen-go-errors/template.go b/cmd/protoc-gen-go-errors/template.go index b20793bc3..24850822f 100644 --- a/cmd/protoc-gen-go-errors/template.go +++ b/cmd/protoc-gen-go-errors/template.go @@ -2,28 +2,12 @@ package main import ( "bytes" + _ "embed" "text/template" ) -var errorsTemplate = ` -{{ range .Errors }} - -{{ if .HasComment }}{{ .Comment }}{{ end -}} -func Is{{.CamelValue}}(err error) bool { - if err == nil { - return false - } - e := errors.FromError(err) - return e.Reason == {{ .Name }}_{{ .Value }}.String() && e.Code == {{ .HTTPCode }} -} - -{{ if .HasComment }}{{ .Comment }}{{ end -}} -func Error{{ .CamelValue }}(format string, args ...interface{}) *errors.Error { - return errors.New({{ .HTTPCode }}, {{ .Name }}_{{ .Value }}.String(), fmt.Sprintf(format, args...)) -} - -{{- end }} -` +//go:embed errorsTemplate.tpl +var errorsTemplate string type errorInfo struct { Name string diff --git a/cmd/protoc-gen-go-http/httpTemplate.tpl b/cmd/protoc-gen-go-http/httpTemplate.tpl new file mode 100644 index 000000000..25dc0312c --- /dev/null +++ b/cmd/protoc-gen-go-http/httpTemplate.tpl @@ -0,0 +1,93 @@ +{{$svrType := .ServiceType}} +{{$svrName := .ServiceName}} + +{{- range .MethodSets}} +const Operation{{$svrType}}{{.OriginalName}} = "/{{$svrName}}/{{.OriginalName}}" +{{- end}} + +type {{.ServiceType}}HTTPServer interface { +{{- range .MethodSets}} + {{- if ne .Comment ""}} + {{.Comment}} + {{- end}} + {{.Name}}(context.Context, *{{.Request}}) (*{{.Reply}}, error) +{{- end}} +} + +func Register{{.ServiceType}}HTTPServer(s *http.Server, srv {{.ServiceType}}HTTPServer) { + r := s.Route("/") + {{- range .Methods}} + r.{{.Method}}("{{.Path}}", _{{$svrType}}_{{.Name}}{{.Num}}_HTTP_Handler(srv)) + {{- end}} +} + +{{range .Methods}} +func _{{$svrType}}_{{.Name}}{{.Num}}_HTTP_Handler(srv {{$svrType}}HTTPServer) func(ctx http.Context) error { + return func(ctx http.Context) error { + var in {{.Request}} + {{- if .HasBody}} + if err := ctx.Bind(&in{{.Body}}); err != nil { + return err + } + + {{- if not (eq .Body "")}} + if err := ctx.BindQuery(&in); err != nil { + return err + } + {{- end}} + {{- else}} + if err := ctx.BindQuery(&in{{.Body}}); err != nil { + return err + } + {{- end}} + {{- if .HasVars}} + if err := ctx.BindVars(&in); err != nil { + return err + } + {{- end}} + http.SetOperation(ctx,Operation{{$svrType}}{{.OriginalName}}) + h := ctx.Middleware(func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.{{.Name}}(ctx, req.(*{{.Request}})) + }) + out, err := h(ctx, &in) + if err != nil { + return err + } + reply := out.(*{{.Reply}}) + return ctx.Result(200, reply{{.ResponseBody}}) + } +} +{{end}} + +type {{.ServiceType}}HTTPClient interface { +{{- range .MethodSets}} + {{.Name}}(ctx context.Context, req *{{.Request}}, opts ...http.CallOption) (rsp *{{.Reply}}, err error) +{{- end}} +} + +type {{.ServiceType}}HTTPClientImpl struct{ + cc *http.Client +} + +func New{{.ServiceType}}HTTPClient (client *http.Client) {{.ServiceType}}HTTPClient { + return &{{.ServiceType}}HTTPClientImpl{client} +} + +{{range .MethodSets}} +func (c *{{$svrType}}HTTPClientImpl) {{.Name}}(ctx context.Context, in *{{.Request}}, opts ...http.CallOption) (*{{.Reply}}, error) { + var out {{.Reply}} + pattern := "{{.Path}}" + path := binding.EncodeURL(pattern, in, {{not .HasBody}}) + opts = append(opts, http.Operation(Operation{{$svrType}}{{.OriginalName}})) + opts = append(opts, http.PathTemplate(pattern)) + {{if .HasBody -}} + err := c.cc.Invoke(ctx, "{{.Method}}", path, in{{.Body}}, &out{{.ResponseBody}}, opts...) + {{else -}} + err := c.cc.Invoke(ctx, "{{.Method}}", path, nil, &out{{.ResponseBody}}, opts...) + {{end -}} + if err != nil { + return nil, err + } + return &out, err +} +{{end}} diff --git a/cmd/protoc-gen-go-http/template.go b/cmd/protoc-gen-go-http/template.go index 48f335755..9de8e7e17 100644 --- a/cmd/protoc-gen-go-http/template.go +++ b/cmd/protoc-gen-go-http/template.go @@ -2,105 +2,13 @@ package main import ( "bytes" + _ "embed" "strings" "text/template" ) -var httpTemplate = ` -{{$svrType := .ServiceType}} -{{$svrName := .ServiceName}} - -{{- range .MethodSets}} -const Operation{{$svrType}}{{.OriginalName}} = "/{{$svrName}}/{{.OriginalName}}" -{{- end}} - -type {{.ServiceType}}HTTPServer interface { -{{- range .MethodSets}} - {{- if ne .Comment ""}} - {{.Comment}} - {{- end}} - {{.Name}}(context.Context, *{{.Request}}) (*{{.Reply}}, error) -{{- end}} -} - -func Register{{.ServiceType}}HTTPServer(s *http.Server, srv {{.ServiceType}}HTTPServer) { - r := s.Route("/") - {{- range .Methods}} - r.{{.Method}}("{{.Path}}", _{{$svrType}}_{{.Name}}{{.Num}}_HTTP_Handler(srv)) - {{- end}} -} - -{{range .Methods}} -func _{{$svrType}}_{{.Name}}{{.Num}}_HTTP_Handler(srv {{$svrType}}HTTPServer) func(ctx http.Context) error { - return func(ctx http.Context) error { - var in {{.Request}} - {{- if .HasBody}} - if err := ctx.Bind(&in{{.Body}}); err != nil { - return err - } - - {{- if not (eq .Body "")}} - if err := ctx.BindQuery(&in); err != nil { - return err - } - {{- end}} - {{- else}} - if err := ctx.BindQuery(&in{{.Body}}); err != nil { - return err - } - {{- end}} - {{- if .HasVars}} - if err := ctx.BindVars(&in); err != nil { - return err - } - {{- end}} - http.SetOperation(ctx,Operation{{$svrType}}{{.OriginalName}}) - h := ctx.Middleware(func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.{{.Name}}(ctx, req.(*{{.Request}})) - }) - out, err := h(ctx, &in) - if err != nil { - return err - } - reply := out.(*{{.Reply}}) - return ctx.Result(200, reply{{.ResponseBody}}) - } -} -{{end}} - -type {{.ServiceType}}HTTPClient interface { -{{- range .MethodSets}} - {{.Name}}(ctx context.Context, req *{{.Request}}, opts ...http.CallOption) (rsp *{{.Reply}}, err error) -{{- end}} -} - -type {{.ServiceType}}HTTPClientImpl struct{ - cc *http.Client -} - -func New{{.ServiceType}}HTTPClient (client *http.Client) {{.ServiceType}}HTTPClient { - return &{{.ServiceType}}HTTPClientImpl{client} -} - -{{range .MethodSets}} -func (c *{{$svrType}}HTTPClientImpl) {{.Name}}(ctx context.Context, in *{{.Request}}, opts ...http.CallOption) (*{{.Reply}}, error) { - var out {{.Reply}} - pattern := "{{.Path}}" - path := binding.EncodeURL(pattern, in, {{not .HasBody}}) - opts = append(opts, http.Operation(Operation{{$svrType}}{{.OriginalName}})) - opts = append(opts, http.PathTemplate(pattern)) - {{if .HasBody -}} - err := c.cc.Invoke(ctx, "{{.Method}}", path, in{{.Body}}, &out{{.ResponseBody}}, opts...) - {{else -}} - err := c.cc.Invoke(ctx, "{{.Method}}", path, nil, &out{{.ResponseBody}}, opts...) - {{end -}} - if err != nil { - return nil, err - } - return &out, err -} -{{end}} -` +//go:embed httpTemplate.tpl +var httpTemplate string type serviceDesc struct { ServiceType string // Greeter