2021-02-23 03:04:56 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-03-19 15:13:34 +03:00
|
|
|
"log"
|
2021-10-23 23:21:10 +03:00
|
|
|
"net/http"
|
2022-11-15 12:53:04 +03:00
|
|
|
"strconv"
|
2021-02-23 03:04:56 +03:00
|
|
|
"strings"
|
2022-11-15 12:53:04 +03:00
|
|
|
"time"
|
2021-02-23 03:04:56 +03:00
|
|
|
|
2023-05-04 00:22:05 +03:00
|
|
|
api_options "go.unistack.org/micro-proto/v4/api"
|
|
|
|
v2 "go.unistack.org/micro-proto/v4/openapiv2"
|
|
|
|
v3 "go.unistack.org/micro-proto/v4/openapiv3"
|
2021-02-23 03:04:56 +03:00
|
|
|
"google.golang.org/protobuf/compiler/protogen"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
2021-08-31 20:39:56 +03:00
|
|
|
var httpMethodMap = map[string]string{
|
2021-10-23 23:21:10 +03:00
|
|
|
http.MethodGet: "MethodGet",
|
|
|
|
http.MethodHead: "MethodHead",
|
|
|
|
http.MethodPost: "MethodPost",
|
|
|
|
http.MethodPut: "MethodPut",
|
|
|
|
http.MethodPatch: "MethodPatch",
|
|
|
|
http.MethodDelete: "MethodDelete",
|
|
|
|
http.MethodConnect: "MethodConnect",
|
|
|
|
http.MethodOptions: "MethodOptions",
|
|
|
|
http.MethodTrace: "MethodTrace",
|
2021-08-31 20:39:56 +03:00
|
|
|
}
|
2021-03-23 16:56:11 +03:00
|
|
|
|
2021-02-23 23:54:53 +03:00
|
|
|
func unexport(s string) string {
|
2021-02-23 03:04:56 +03:00
|
|
|
return strings.ToLower(s[:1]) + s[1:]
|
|
|
|
}
|
|
|
|
|
2023-07-31 00:54:44 +03:00
|
|
|
func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) {
|
2023-08-20 13:16:08 +03:00
|
|
|
serviceName := getServiceName(service)
|
2021-08-31 20:39:56 +03:00
|
|
|
// if rule, ok := getMicroApiService(service); ok {
|
2021-02-28 01:42:29 +03:00
|
|
|
// gfile.P("// client wrappers ", strings.Join(rule.ClientWrappers, ", "))
|
|
|
|
// }
|
2021-02-25 14:19:09 +03:00
|
|
|
gfile.P("type ", unexport(serviceName), "Client struct {")
|
2021-02-23 23:54:53 +03:00
|
|
|
gfile.P("c ", microClientPackage.Ident("Client"))
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("name string")
|
|
|
|
gfile.P("}")
|
|
|
|
|
2023-07-31 00:54:44 +03:00
|
|
|
if g.standalone {
|
|
|
|
gfile.P("func New", serviceName, "Client(name string, c ", microClientPackage.Ident("Client"), ") ", file.GoImportPath.Ident(serviceName), "Client {")
|
|
|
|
} else {
|
|
|
|
gfile.P("func New", serviceName, "Client(name string, c ", microClientPackage.Ident("Client"), ") ", serviceName, "Client {")
|
|
|
|
}
|
2021-02-25 14:19:09 +03:00
|
|
|
gfile.P("return &", unexport(serviceName), "Client{c: c, name: name}")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
|
|
|
}
|
|
|
|
|
2022-03-20 15:08:04 +03:00
|
|
|
func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protogen.Service, component string) {
|
2023-08-20 13:16:08 +03:00
|
|
|
serviceName := getServiceName(service)
|
2021-02-23 03:04:56 +03:00
|
|
|
for _, method := range service.Methods {
|
|
|
|
methodName := fmt.Sprintf("%s.%s", serviceName, method.GoName)
|
2022-03-20 15:08:04 +03:00
|
|
|
if component == "drpc" {
|
|
|
|
methodName = fmt.Sprintf("%s.%s", method.Parent.Desc.FullName(), method.Desc.Name())
|
|
|
|
}
|
2022-03-19 15:13:34 +03:00
|
|
|
g.generateClientFuncSignature(gfile, serviceName, method)
|
2021-02-23 03:04:56 +03:00
|
|
|
|
2022-03-20 15:08:04 +03:00
|
|
|
if component == "http" && method.Desc.Options() != nil {
|
2021-10-23 23:21:10 +03:00
|
|
|
if proto.HasExtension(method.Desc.Options(), v2.E_Openapiv2Operation) {
|
|
|
|
opts := proto.GetExtension(method.Desc.Options(), v2.E_Openapiv2Operation)
|
2021-02-23 03:04:56 +03:00
|
|
|
if opts != nil {
|
2021-10-23 23:21:10 +03:00
|
|
|
r := opts.(*v2.Operation)
|
2022-02-01 00:30:36 +03:00
|
|
|
if r.Responses == nil {
|
|
|
|
goto labelMethod
|
|
|
|
}
|
2021-08-16 23:34:32 +03:00
|
|
|
gfile.P("errmap := make(map[string]interface{}, ", len(r.Responses.ResponseCode), ")")
|
|
|
|
for _, rsp := range r.Responses.ResponseCode {
|
|
|
|
if schema := rsp.Value.GetJsonReference(); schema != nil {
|
2022-03-19 15:13:34 +03:00
|
|
|
xref := schema.XRef
|
|
|
|
if strings.HasPrefix(xref, "."+string(service.Desc.ParentFile().Package())+".") {
|
|
|
|
xref = strings.TrimPrefix(xref, "."+string(service.Desc.ParentFile().Package())+".")
|
2021-02-23 23:54:53 +03:00
|
|
|
}
|
2022-03-19 15:13:34 +03:00
|
|
|
if xref[0] == '.' {
|
|
|
|
xref = xref[1:]
|
2022-02-01 00:30:36 +03:00
|
|
|
}
|
2022-03-19 15:13:34 +03:00
|
|
|
switch xref {
|
2022-02-01 00:30:36 +03:00
|
|
|
case "micro.codec.Frame":
|
|
|
|
gfile.P(`errmap["`, rsp.Name, `"] = &`, microCodecPackage.Ident("Frame"), "{}")
|
|
|
|
case "micro.errors.Error":
|
|
|
|
gfile.P(`errmap["`, rsp.Name, `"] = &`, microErrorsPackage.Ident("Error"), "{}")
|
|
|
|
default:
|
2022-03-19 15:13:34 +03:00
|
|
|
ident, err := g.getGoIdentByXref(strings.TrimPrefix(schema.XRef, "."))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("cant find message by ref %s\n", schema.XRef)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
gfile.P(`errmap["`, rsp.Name, `"] = &`, gfile.QualifiedGoIdent(ident), "{}")
|
2022-02-01 00:30:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gfile.P("opts = append(opts,")
|
|
|
|
gfile.P(microClientHttpPackage.Ident("ErrorMap"), "(errmap),")
|
|
|
|
gfile.P(")")
|
|
|
|
}
|
|
|
|
if proto.HasExtension(method.Desc.Options(), v3.E_Openapiv3Operation) {
|
|
|
|
opts := proto.GetExtension(method.Desc.Options(), v3.E_Openapiv3Operation)
|
|
|
|
if opts != nil {
|
|
|
|
r := opts.(*v3.Operation)
|
|
|
|
if r.Responses == nil {
|
|
|
|
goto labelMethod
|
|
|
|
}
|
|
|
|
resps := r.Responses.ResponseOrReference
|
|
|
|
if r.Responses.GetDefault() != nil {
|
|
|
|
resps = append(resps, &v3.NamedResponseOrReference{Name: "default", Value: r.Responses.GetDefault()})
|
|
|
|
}
|
|
|
|
gfile.P("errmap := make(map[string]interface{}, ", len(resps), ")")
|
|
|
|
for _, rsp := range resps {
|
|
|
|
if schema := rsp.Value.GetReference(); schema != nil {
|
2022-03-19 15:13:34 +03:00
|
|
|
xref := schema.XRef
|
|
|
|
if strings.HasPrefix(xref, "."+string(service.Desc.ParentFile().Package())+".") {
|
|
|
|
xref = strings.TrimPrefix(xref, "."+string(service.Desc.ParentFile().Package())+".")
|
2022-02-01 00:30:36 +03:00
|
|
|
}
|
2022-03-19 15:13:34 +03:00
|
|
|
if xref[0] == '.' {
|
|
|
|
xref = xref[1:]
|
2022-02-01 00:30:36 +03:00
|
|
|
}
|
2022-03-19 15:13:34 +03:00
|
|
|
switch xref {
|
2022-02-01 00:30:36 +03:00
|
|
|
case "micro.codec.Frame":
|
2021-08-31 20:39:56 +03:00
|
|
|
gfile.P(`errmap["`, rsp.Name, `"] = &`, microCodecPackage.Ident("Frame"), "{}")
|
2022-02-01 00:30:36 +03:00
|
|
|
case "micro.errors.Error":
|
|
|
|
gfile.P(`errmap["`, rsp.Name, `"] = &`, microErrorsPackage.Ident("Error"), "{}")
|
|
|
|
default:
|
2022-03-19 15:13:34 +03:00
|
|
|
ident, err := g.getGoIdentByXref(strings.TrimPrefix(schema.XRef, "."))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("cant find message by ref %s\n", schema.XRef)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
gfile.P(`errmap["`, rsp.Name, `"] = &`, gfile.QualifiedGoIdent(ident), "{}")
|
2021-08-31 20:39:56 +03:00
|
|
|
}
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gfile.P("opts = append(opts,")
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P(microClientHttpPackage.Ident("ErrorMap"), "(errmap),")
|
2021-03-19 12:59:53 +03:00
|
|
|
gfile.P(")")
|
|
|
|
}
|
2021-10-23 23:21:10 +03:00
|
|
|
|
2022-02-01 00:30:36 +03:00
|
|
|
labelMethod:
|
2021-03-19 12:59:53 +03:00
|
|
|
if proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
|
|
|
|
gfile.P("opts = append(opts,")
|
|
|
|
endpoints, _ := generateEndpoints(method)
|
|
|
|
path, method, body := getEndpoint(endpoints[0])
|
2021-03-23 16:56:11 +03:00
|
|
|
if vmethod, ok := httpMethodMap[method]; ok {
|
|
|
|
gfile.P(microClientHttpPackage.Ident("Method"), `(`, httpPackage.Ident(vmethod), `),`)
|
|
|
|
} else {
|
|
|
|
gfile.P(microClientHttpPackage.Ident("Method"), `("`, method, `"),`)
|
|
|
|
}
|
2021-03-19 12:59:53 +03:00
|
|
|
gfile.P(microClientHttpPackage.Ident("Path"), `("`, path, `"),`)
|
2021-03-23 16:56:11 +03:00
|
|
|
if body != "" {
|
|
|
|
gfile.P(microClientHttpPackage.Ident("Body"), `("`, body, `"),`)
|
|
|
|
}
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P(")")
|
|
|
|
}
|
2021-10-23 23:21:10 +03:00
|
|
|
|
2021-10-24 00:52:39 +03:00
|
|
|
parameters := make(map[string]map[string]string)
|
2021-10-23 23:21:10 +03:00
|
|
|
// Build a list of header parameters.
|
2022-02-01 00:30:36 +03:00
|
|
|
e2opt := proto.GetExtension(method.Desc.Options(), v2.E_Openapiv2Operation)
|
|
|
|
if e2opt != nil && e2opt != v2.E_Openapiv2Operation.InterfaceOf(v2.E_Openapiv2Operation.Zero()) {
|
|
|
|
opt := e2opt.(*v2.Operation)
|
2021-10-24 00:52:39 +03:00
|
|
|
for _, paramOrRef := range opt.Parameters {
|
2022-02-01 00:30:36 +03:00
|
|
|
parameter := paramOrRef.GetParameter()
|
|
|
|
// NonBodyParameter()
|
|
|
|
if parameter == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
nonBodyParameter := parameter.GetNonBodyParameter()
|
|
|
|
if nonBodyParameter == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
headerParameter := nonBodyParameter.GetHeaderParameterSubSchema()
|
|
|
|
if headerParameter.In != "header" && headerParameter.In != "cookie" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
in, ok := parameters[headerParameter.In]
|
|
|
|
if !ok {
|
|
|
|
in = make(map[string]string)
|
|
|
|
parameters[headerParameter.In] = in
|
|
|
|
}
|
|
|
|
in[headerParameter.Name] = fmt.Sprintf("%v", headerParameter.Required)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e3opt := proto.GetExtension(method.Desc.Options(), v3.E_Openapiv3Operation)
|
|
|
|
if e3opt != nil && e3opt != v3.E_Openapiv3Operation.InterfaceOf(v3.E_Openapiv3Operation.Zero()) {
|
|
|
|
opt := e3opt.(*v3.Operation)
|
|
|
|
for _, paramOrRef := range opt.Parameters {
|
|
|
|
|
2021-10-24 00:52:39 +03:00
|
|
|
parameter := paramOrRef.GetParameter()
|
|
|
|
if parameter == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if parameter.In != "header" && parameter.In != "cookie" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
in, ok := parameters[parameter.In]
|
|
|
|
if !ok {
|
|
|
|
in = make(map[string]string)
|
|
|
|
parameters[parameter.In] = in
|
|
|
|
}
|
|
|
|
in[parameter.Name] = fmt.Sprintf("%v", parameter.Required)
|
|
|
|
}
|
2021-10-23 23:21:10 +03:00
|
|
|
}
|
|
|
|
|
2021-10-24 00:52:39 +03:00
|
|
|
if len(parameters) > 0 {
|
|
|
|
gfile.P("opts = append(opts,")
|
|
|
|
for pk, pv := range parameters {
|
|
|
|
params := make([]string, 0, len(pv)/2)
|
|
|
|
for k, v := range pv {
|
|
|
|
params = append(params, k, v)
|
|
|
|
}
|
|
|
|
gfile.P(microClientHttpPackage.Ident(strings.Title(pk)), `("`, strings.Join(params, `" ,"`), `"),`)
|
|
|
|
}
|
|
|
|
gfile.P(")")
|
|
|
|
}
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
2021-10-23 23:21:10 +03:00
|
|
|
|
2021-02-28 01:42:29 +03:00
|
|
|
if rule, ok := getMicroApiMethod(method); ok {
|
2022-11-15 12:53:04 +03:00
|
|
|
if rule.Timeout != "" {
|
|
|
|
td, err := time.ParseDuration(rule.Timeout)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("parse duration error %s\n", err.Error())
|
|
|
|
} else {
|
2023-02-14 00:22:35 +03:00
|
|
|
gfile.P("td := ", timePackage.Ident("Duration"), "(", td.Nanoseconds(), ")")
|
|
|
|
gfile.P("opts = append(opts, ", microClientPackage.Ident("WithRequestTimeout"), "(td))")
|
2022-11-15 12:53:04 +03:00
|
|
|
}
|
2021-02-28 01:42:29 +03:00
|
|
|
}
|
|
|
|
}
|
2021-02-23 03:04:56 +03:00
|
|
|
|
|
|
|
if !method.Desc.IsStreamingServer() && !method.Desc.IsStreamingClient() {
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("rsp := &", gfile.QualifiedGoIdent(method.Output.GoIdent), "{}")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P(`err := c.c.Call(ctx, c.c.NewRequest(c.name, "`, methodName, `", req), rsp, opts...)`)
|
|
|
|
gfile.P("if err != nil {")
|
|
|
|
gfile.P("return nil, err")
|
|
|
|
gfile.P("}")
|
|
|
|
gfile.P("return rsp, nil")
|
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
2021-02-23 23:54:53 +03:00
|
|
|
continue
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
|
|
|
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P(`stream, err := c.c.Stream(ctx, c.c.NewRequest(c.name, "`, methodName, `", &`, gfile.QualifiedGoIdent(method.Input.GoIdent), `{}), opts...)`)
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("if err != nil {")
|
|
|
|
gfile.P("return nil, err")
|
|
|
|
gfile.P("}")
|
|
|
|
|
|
|
|
if !method.Desc.IsStreamingClient() {
|
|
|
|
gfile.P("if err := stream.Send(req); err != nil {")
|
|
|
|
gfile.P("return nil, err")
|
|
|
|
gfile.P("}")
|
|
|
|
}
|
2021-02-25 14:19:09 +03:00
|
|
|
gfile.P("return &", unexport(serviceName), "Client", method.GoName, "{stream}, nil")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
|
|
|
|
|
|
|
if method.Desc.IsStreamingServer() || method.Desc.IsStreamingClient() {
|
2021-02-25 14:19:09 +03:00
|
|
|
gfile.P("type ", unexport(serviceName), "Client", method.GoName, " struct {")
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("stream ", microClientPackage.Ident("Stream"))
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("}")
|
|
|
|
}
|
|
|
|
|
|
|
|
if method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
|
2022-02-01 00:30:36 +03:00
|
|
|
gfile.P("func (s *", unexport(serviceName), "Client", method.GoName, ") CloseAndRecv() (*", gfile.QualifiedGoIdent(method.Output.GoIdent), ", error) {")
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("msg := &", gfile.QualifiedGoIdent(method.Output.GoIdent), "{}")
|
|
|
|
gfile.P("err := s.RecvMsg(msg)")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("if err == nil {")
|
|
|
|
gfile.P("err = s.Close()")
|
|
|
|
gfile.P("}")
|
|
|
|
gfile.P("if err != nil {")
|
|
|
|
gfile.P("return nil, err")
|
|
|
|
gfile.P("}")
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("return msg, nil")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("}")
|
|
|
|
}
|
|
|
|
|
|
|
|
gfile.P()
|
2021-02-25 14:19:09 +03:00
|
|
|
gfile.P("func (s *", unexport(serviceName), "Client", method.GoName, ") Close() error {")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("return s.stream.Close()")
|
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
2022-02-01 00:30:36 +03:00
|
|
|
gfile.P("func (s *", unexport(serviceName), "Client", method.GoName, ") CloseSend() error {")
|
|
|
|
gfile.P("return s.stream.CloseSend()")
|
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
2021-02-25 14:19:09 +03:00
|
|
|
gfile.P("func (s *", unexport(serviceName), "Client", method.GoName, ") Context() ", contextPackage.Ident("Context"), " {")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("return s.stream.Context()")
|
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
2021-02-25 14:19:09 +03:00
|
|
|
gfile.P("func (s *", unexport(serviceName), "Client", method.GoName, ") SendMsg(msg interface{}) error {")
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("return s.stream.Send(msg)")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
2021-02-25 14:19:09 +03:00
|
|
|
gfile.P("func (s *", unexport(serviceName), "Client", method.GoName, ") RecvMsg(msg interface{}) error {")
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("return s.stream.Recv(msg)")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
|
|
|
|
|
|
|
if method.Desc.IsStreamingClient() {
|
2022-11-15 12:53:04 +03:00
|
|
|
gfile.P("func (s *", unexport(serviceName), "Client", method.GoName, ") Header() ", microMetadataPackage.Ident("Metadata"), "{")
|
|
|
|
gfile.P("return s.stream.Response().Header()")
|
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
|
|
|
|
2021-02-25 14:19:09 +03:00
|
|
|
gfile.P("func (s *", unexport(serviceName), "Client", method.GoName, ") Send(msg *", gfile.QualifiedGoIdent(method.Input.GoIdent), ") error {")
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("return s.stream.Send(msg)")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
|
|
|
}
|
|
|
|
|
|
|
|
if method.Desc.IsStreamingServer() {
|
2021-02-25 14:19:09 +03:00
|
|
|
gfile.P("func (s *", unexport(serviceName), "Client", method.GoName, ") Recv() (*", gfile.QualifiedGoIdent(method.Output.GoIdent), ", error) {")
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("msg := &", gfile.QualifiedGoIdent(method.Output.GoIdent), "{}")
|
|
|
|
gfile.P("if err := s.stream.Recv(msg); err != nil {")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("return nil, err")
|
|
|
|
gfile.P("}")
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("return msg, nil")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-31 00:54:44 +03:00
|
|
|
func (g *Generator) generateServiceServer(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) {
|
2023-08-20 13:16:08 +03:00
|
|
|
serviceName := getServiceName(service)
|
2021-02-25 14:19:09 +03:00
|
|
|
gfile.P("type ", unexport(serviceName), "Server struct {")
|
2023-07-31 00:54:44 +03:00
|
|
|
if g.standalone {
|
|
|
|
gfile.P(file.GoImportPath.Ident(serviceName), "Server")
|
|
|
|
} else {
|
|
|
|
gfile.P(serviceName, "Server")
|
|
|
|
}
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
|
|
|
}
|
|
|
|
|
2022-03-19 15:13:34 +03:00
|
|
|
func (g *Generator) generateServiceServerMethods(gfile *protogen.GeneratedFile, service *protogen.Service) {
|
2023-08-20 13:16:08 +03:00
|
|
|
serviceName := getServiceName(service)
|
2021-02-23 03:04:56 +03:00
|
|
|
for _, method := range service.Methods {
|
2021-02-24 01:19:57 +03:00
|
|
|
generateServerFuncSignature(gfile, serviceName, method, true)
|
2021-02-28 00:38:57 +03:00
|
|
|
if rule, ok := getMicroApiMethod(method); ok {
|
2022-11-15 12:53:04 +03:00
|
|
|
if rule.Timeout != "" {
|
|
|
|
td, err := time.ParseDuration(rule.Timeout)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("parse duration error %s\n", err.Error())
|
|
|
|
} else {
|
|
|
|
gfile.P("var cancel ", contextPackage.Ident("CancelFunc"))
|
|
|
|
gfile.P("td := ", timePackage.Ident("Duration"), "(", td.Nanoseconds(), ")")
|
|
|
|
gfile.P("ctx, cancel = ", contextPackage.Ident("WithTimeout"), "(ctx, ", "td", ")")
|
|
|
|
gfile.P("defer cancel()")
|
|
|
|
}
|
2021-02-28 00:38:57 +03:00
|
|
|
}
|
|
|
|
}
|
2021-02-23 03:04:56 +03:00
|
|
|
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
|
|
|
|
if !method.Desc.IsStreamingClient() {
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("msg := &", gfile.QualifiedGoIdent(method.Input.GoIdent), "{}")
|
|
|
|
gfile.P("if err := stream.Recv(msg); err != nil {")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("return err")
|
|
|
|
gfile.P("}")
|
2021-02-25 14:19:09 +03:00
|
|
|
gfile.P("return h.", serviceName, "Server.", method.GoName, "(ctx, msg, &", unexport(serviceName), method.GoName, "Stream{stream})")
|
2021-02-23 03:04:56 +03:00
|
|
|
} else {
|
2021-02-25 14:19:09 +03:00
|
|
|
gfile.P("return h.", serviceName, "Server.", method.GoName, "(ctx, &", unexport(serviceName), method.GoName, "Stream{stream})")
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-24 00:52:39 +03:00
|
|
|
parameters := make(map[string]map[string]string)
|
|
|
|
// Build a list of header parameters.
|
2022-02-01 00:30:36 +03:00
|
|
|
e2opt := proto.GetExtension(method.Desc.Options(), v2.E_Openapiv2Operation)
|
|
|
|
if e2opt != nil && e2opt != v2.E_Openapiv2Operation.InterfaceOf(v2.E_Openapiv2Operation.Zero()) {
|
|
|
|
opt := e2opt.(*v2.Operation)
|
|
|
|
for _, paramOrRef := range opt.Parameters {
|
|
|
|
parameter := paramOrRef.GetParameter()
|
|
|
|
// NonBodyParameter()
|
|
|
|
if parameter == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
nonBodyParameter := parameter.GetNonBodyParameter()
|
|
|
|
if nonBodyParameter == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
headerParameter := nonBodyParameter.GetHeaderParameterSubSchema()
|
|
|
|
if headerParameter.In != "header" && headerParameter.In != "cookie" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
in, ok := parameters[headerParameter.In]
|
|
|
|
if !ok {
|
|
|
|
in = make(map[string]string)
|
|
|
|
parameters[headerParameter.In] = in
|
|
|
|
}
|
|
|
|
in[headerParameter.Name] = fmt.Sprintf("%v", headerParameter.Required)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e3opt := proto.GetExtension(method.Desc.Options(), v3.E_Openapiv3Operation)
|
|
|
|
if e3opt != nil && e3opt != v3.E_Openapiv3Operation.InterfaceOf(v3.E_Openapiv3Operation.Zero()) {
|
|
|
|
opt := e3opt.(*v3.Operation)
|
2021-10-24 00:52:39 +03:00
|
|
|
for _, paramOrRef := range opt.Parameters {
|
|
|
|
parameter := paramOrRef.GetParameter()
|
|
|
|
if parameter == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if parameter.In != "header" && parameter.In != "cookie" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
in, ok := parameters[parameter.In]
|
|
|
|
if !ok {
|
|
|
|
in = make(map[string]string)
|
|
|
|
parameters[parameter.In] = in
|
|
|
|
}
|
|
|
|
in[parameter.Name] = fmt.Sprintf("%v", parameter.Required)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(parameters) > 0 {
|
2021-10-27 15:29:48 +03:00
|
|
|
gfile.P(microServerHttpPackage.Ident("FillRequest"), `(ctx, req, `)
|
2021-10-24 00:52:39 +03:00
|
|
|
for pk, pv := range parameters {
|
|
|
|
params := make([]string, 0, len(pv)/2)
|
|
|
|
for k, v := range pv {
|
|
|
|
params = append(params, k, v)
|
|
|
|
}
|
|
|
|
gfile.P(microServerHttpPackage.Ident(strings.Title(pk)), `("`, strings.Join(params, `" ,"`), `"),`)
|
|
|
|
}
|
|
|
|
gfile.P(")")
|
|
|
|
}
|
2021-02-25 14:19:09 +03:00
|
|
|
gfile.P("return h.", serviceName, "Server.", method.GoName, "(ctx, req, rsp)")
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
|
|
|
|
|
|
|
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
|
2021-02-23 23:54:53 +03:00
|
|
|
gfile.P("type ", unexport(serviceName), method.GoName, "Stream struct {")
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("stream ", microServerPackage.Ident("Stream"))
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("}")
|
|
|
|
}
|
|
|
|
|
|
|
|
if method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("func (s *", unexport(serviceName), method.GoName, "Stream) SendAndClose(msg *", gfile.QualifiedGoIdent(method.Output.GoIdent), ") error {")
|
|
|
|
gfile.P("err := s.SendMsg(msg)")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("if err == nil {")
|
|
|
|
gfile.P("err = s.stream.Close()")
|
|
|
|
gfile.P("}")
|
|
|
|
gfile.P("return err")
|
|
|
|
gfile.P("}")
|
|
|
|
}
|
|
|
|
|
|
|
|
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
|
2021-02-23 23:54:53 +03:00
|
|
|
gfile.P("func (s *", unexport(serviceName), method.GoName, "Stream) Close() error {")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("return s.stream.Close()")
|
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
|
|
|
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("func (s *", unexport(serviceName), method.GoName, "Stream) Context() ", contextPackage.Ident("Context"), " {")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("return s.stream.Context()")
|
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
|
|
|
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("func (s *", unexport(serviceName), method.GoName, "Stream) SendMsg(msg interface{}) error {")
|
|
|
|
gfile.P("return s.stream.Send(msg)")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
|
|
|
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("func (s *", unexport(serviceName), method.GoName, "Stream) RecvMsg(msg interface{}) error {")
|
|
|
|
gfile.P("return s.stream.Recv(msg)")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
|
|
|
|
|
|
|
if method.Desc.IsStreamingServer() {
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("func (s *", unexport(serviceName), method.GoName, "Stream) Send(msg *", gfile.QualifiedGoIdent(method.Output.GoIdent), ") error {")
|
|
|
|
gfile.P("return s.stream.Send(msg)")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
|
|
|
}
|
|
|
|
|
|
|
|
if method.Desc.IsStreamingClient() {
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("func (s *", unexport(serviceName), method.GoName, "Stream) Recv() (*", gfile.QualifiedGoIdent(method.Input.GoIdent), ", error) {")
|
|
|
|
gfile.P("msg := &", gfile.QualifiedGoIdent(method.Input.GoIdent), "{}")
|
|
|
|
gfile.P("if err := s.stream.Recv(msg); err != nil {")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("return nil, err")
|
|
|
|
gfile.P("}")
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("return msg, nil")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-31 00:54:44 +03:00
|
|
|
func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service, component string) {
|
2023-08-20 13:16:08 +03:00
|
|
|
serviceName := getServiceName(service)
|
2023-07-31 00:54:44 +03:00
|
|
|
if g.standalone {
|
2023-08-12 19:52:46 +03:00
|
|
|
gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", file.GoImportPath.Ident(serviceName), "Server, opts ...", microOptionsPackage.Ident("Option"), ") error {")
|
2023-07-31 00:54:44 +03:00
|
|
|
} else {
|
2023-08-12 19:52:46 +03:00
|
|
|
gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", serviceName, "Server, opts ...", microOptionsPackage.Ident("Option"), ") error {")
|
2023-07-31 00:54:44 +03:00
|
|
|
}
|
2021-02-23 23:54:53 +03:00
|
|
|
gfile.P("type ", unexport(serviceName), " interface {")
|
2021-02-23 03:04:56 +03:00
|
|
|
for _, method := range service.Methods {
|
2021-02-24 01:19:57 +03:00
|
|
|
generateServerSignature(gfile, serviceName, method, true)
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
|
|
|
gfile.P("}")
|
|
|
|
gfile.P("type ", serviceName, " struct {")
|
2021-02-23 23:54:53 +03:00
|
|
|
gfile.P(unexport(serviceName))
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("}")
|
2021-02-25 14:19:09 +03:00
|
|
|
gfile.P("h := &", unexport(serviceName), "Server{sh}")
|
2023-08-12 19:52:46 +03:00
|
|
|
gfile.P("var nopts []", microOptionsPackage.Ident("Option"))
|
2023-03-15 01:10:59 +03:00
|
|
|
if component == "http" {
|
2023-07-31 00:54:44 +03:00
|
|
|
// if g.standalone {
|
|
|
|
// gfile.P("nopts = append(nopts, ", microServerHttpPackage.Ident("HandlerEndpoints"), "(", file.GoImportPath.Ident(serviceName), "ServerEndpoints))")
|
|
|
|
// } else {
|
2023-03-15 01:10:59 +03:00
|
|
|
gfile.P("nopts = append(nopts, ", microServerHttpPackage.Ident("HandlerEndpoints"), "(", serviceName, "ServerEndpoints))")
|
2023-07-31 00:54:44 +03:00
|
|
|
// }
|
2023-03-15 01:10:59 +03:00
|
|
|
}
|
2023-08-12 19:52:46 +03:00
|
|
|
gfile.P("return s.Handle(&", serviceName, "{h}, append(nopts, opts...)...)")
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("}")
|
|
|
|
}
|
|
|
|
|
2021-02-24 01:19:57 +03:00
|
|
|
func generateServerFuncSignature(gfile *protogen.GeneratedFile, serviceName string, method *protogen.Method, private bool) {
|
2021-02-23 23:54:53 +03:00
|
|
|
args := append([]interface{}{},
|
2021-02-25 14:19:09 +03:00
|
|
|
"func (h *", unexport(serviceName), "Server) ", method.GoName,
|
2021-02-23 23:54:53 +03:00
|
|
|
"(ctx ", contextPackage.Ident("Context"),
|
|
|
|
)
|
2021-02-24 01:19:57 +03:00
|
|
|
if private && (method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer()) {
|
2021-02-23 23:54:53 +03:00
|
|
|
args = append(args, ", stream ", microServerPackage.Ident("Stream"))
|
|
|
|
} else {
|
|
|
|
if !method.Desc.IsStreamingClient() {
|
|
|
|
args = append(args, ", req *", gfile.QualifiedGoIdent(method.Input.GoIdent))
|
|
|
|
}
|
|
|
|
if method.Desc.IsStreamingServer() || method.Desc.IsStreamingClient() {
|
|
|
|
args = append(args, ", stream ", serviceName, "_", method.GoName, "Stream")
|
|
|
|
}
|
|
|
|
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
|
|
|
|
args = append(args, ", rsp *", gfile.QualifiedGoIdent(method.Output.GoIdent))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args = append(args, ") error {")
|
|
|
|
gfile.P(args...)
|
|
|
|
}
|
2021-02-23 03:04:56 +03:00
|
|
|
|
2021-02-24 01:19:57 +03:00
|
|
|
func generateServerSignature(gfile *protogen.GeneratedFile, serviceName string, method *protogen.Method, private bool) {
|
2021-02-23 23:54:53 +03:00
|
|
|
args := append([]interface{}{},
|
|
|
|
method.GoName,
|
|
|
|
"(ctx ", contextPackage.Ident("Context"),
|
|
|
|
)
|
2021-02-24 01:19:57 +03:00
|
|
|
if private && (method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer()) {
|
2021-02-23 23:54:53 +03:00
|
|
|
args = append(args, ", stream ", microServerPackage.Ident("Stream"))
|
|
|
|
} else {
|
|
|
|
if !method.Desc.IsStreamingClient() {
|
|
|
|
args = append(args, ", req *", gfile.QualifiedGoIdent(method.Input.GoIdent))
|
|
|
|
}
|
|
|
|
if method.Desc.IsStreamingServer() || method.Desc.IsStreamingClient() {
|
|
|
|
args = append(args, ", stream ", serviceName, "_", method.GoName, "Stream")
|
|
|
|
}
|
|
|
|
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
|
|
|
|
args = append(args, ", rsp *", gfile.QualifiedGoIdent(method.Output.GoIdent))
|
|
|
|
}
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
2021-02-23 23:54:53 +03:00
|
|
|
args = append(args, ") error")
|
|
|
|
gfile.P(args...)
|
|
|
|
}
|
|
|
|
|
2022-03-19 15:13:34 +03:00
|
|
|
func (g *Generator) generateClientFuncSignature(gfile *protogen.GeneratedFile, serviceName string, method *protogen.Method) {
|
2021-02-23 23:54:53 +03:00
|
|
|
args := append([]interface{}{},
|
|
|
|
"func (c *",
|
|
|
|
unexport(serviceName),
|
2021-02-25 14:19:09 +03:00
|
|
|
"Client) ",
|
2021-02-23 23:54:53 +03:00
|
|
|
method.GoName,
|
|
|
|
"(ctx ", contextPackage.Ident("Context"), ", ",
|
|
|
|
)
|
|
|
|
if !method.Desc.IsStreamingClient() {
|
|
|
|
args = append(args, "req *", gfile.QualifiedGoIdent(method.Input.GoIdent), ", ")
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
2023-08-12 19:52:46 +03:00
|
|
|
args = append(args, "opts ...", microOptionsPackage.Ident("Option"), ") (")
|
2021-02-23 03:04:56 +03:00
|
|
|
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
|
2021-02-23 23:54:53 +03:00
|
|
|
args = append(args, "*", gfile.QualifiedGoIdent(method.Output.GoIdent))
|
|
|
|
} else {
|
2023-12-26 00:55:09 +03:00
|
|
|
args = append(args, gfile.QualifiedGoIdent(protogen.GoIdent{GoName: serviceName + "_" + method.GoName + "Client", GoImportPath: method.Output.GoIdent.GoImportPath}))
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
2021-02-23 23:54:53 +03:00
|
|
|
args = append(args, ", error) {")
|
|
|
|
gfile.P(args...)
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
|
|
|
|
2021-02-23 23:54:53 +03:00
|
|
|
func generateClientSignature(gfile *protogen.GeneratedFile, serviceName string, method *protogen.Method) {
|
|
|
|
args := append([]interface{}{},
|
|
|
|
method.GoName,
|
|
|
|
"(ctx ", contextPackage.Ident("Context"), ", ",
|
|
|
|
)
|
|
|
|
if !method.Desc.IsStreamingClient() {
|
|
|
|
args = append(args, "req *", gfile.QualifiedGoIdent(method.Input.GoIdent), ", ")
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
2023-08-12 19:52:46 +03:00
|
|
|
args = append(args, "opts ...", microOptionsPackage.Ident("Option"), ") (")
|
2021-02-23 23:54:53 +03:00
|
|
|
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
|
|
|
|
args = append(args, "*", gfile.QualifiedGoIdent(method.Output.GoIdent))
|
|
|
|
} else {
|
2021-02-25 14:19:09 +03:00
|
|
|
args = append(args, serviceName, "_", method.GoName, "Client")
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
2021-02-23 23:54:53 +03:00
|
|
|
args = append(args, ", error)")
|
|
|
|
gfile.P(args...)
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
|
|
|
|
2022-03-19 15:13:34 +03:00
|
|
|
func (g *Generator) generateServiceClientInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
|
2023-08-20 13:16:08 +03:00
|
|
|
serviceName := getServiceName(service)
|
2021-02-25 14:19:09 +03:00
|
|
|
gfile.P("type ", serviceName, "Client interface {")
|
2021-02-23 03:04:56 +03:00
|
|
|
for _, method := range service.Methods {
|
2021-02-23 23:54:53 +03:00
|
|
|
generateClientSignature(gfile, serviceName, method)
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
|
|
|
}
|
|
|
|
|
2022-03-19 15:13:34 +03:00
|
|
|
func (g *Generator) generateServiceServerInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
|
2023-08-20 13:16:08 +03:00
|
|
|
serviceName := getServiceName(service)
|
2021-02-25 14:19:09 +03:00
|
|
|
gfile.P("type ", serviceName, "Server interface {")
|
2021-02-23 03:04:56 +03:00
|
|
|
for _, method := range service.Methods {
|
2021-02-24 01:19:57 +03:00
|
|
|
generateServerSignature(gfile, serviceName, method, false)
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
|
|
|
}
|
|
|
|
|
2022-03-19 15:13:34 +03:00
|
|
|
func (g *Generator) generateServiceClientStreamInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
|
2023-08-20 13:16:08 +03:00
|
|
|
serviceName := getServiceName(service)
|
2021-02-23 03:04:56 +03:00
|
|
|
for _, method := range service.Methods {
|
|
|
|
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
methodName := method.GoName
|
2021-02-25 14:19:09 +03:00
|
|
|
gfile.P("type ", serviceName, "_", methodName, "Client interface {")
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("Context() ", contextPackage.Ident("Context"))
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("SendMsg(msg interface{}) error")
|
|
|
|
gfile.P("RecvMsg(msg interface{}) error")
|
|
|
|
if method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
|
2022-02-01 00:30:36 +03:00
|
|
|
gfile.P("CloseAndRecv() (*", gfile.QualifiedGoIdent(method.Output.GoIdent), ", error)")
|
2023-05-11 10:36:39 +03:00
|
|
|
gfile.P("CloseSend() error")
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
|
|
|
gfile.P("Close() error")
|
|
|
|
if method.Desc.IsStreamingClient() {
|
2022-11-15 12:53:04 +03:00
|
|
|
gfile.P("Header() ", microMetadataPackage.Ident("Metadata"))
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("Send(msg *", gfile.QualifiedGoIdent(method.Input.GoIdent), ") error")
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
|
|
|
if method.Desc.IsStreamingServer() {
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("Recv() (*", gfile.QualifiedGoIdent(method.Output.GoIdent), ", error)")
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
2021-02-23 18:45:03 +03:00
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 15:13:34 +03:00
|
|
|
func (g *Generator) generateServiceServerStreamInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
|
2023-08-20 13:16:08 +03:00
|
|
|
serviceName := getServiceName(service)
|
2021-02-23 03:04:56 +03:00
|
|
|
for _, method := range service.Methods {
|
|
|
|
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
methodName := method.GoName
|
|
|
|
gfile.P("type ", serviceName, "_", methodName, "Stream interface {")
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("Context() ", contextPackage.Ident("Context"))
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("SendMsg(msg interface{}) error")
|
|
|
|
gfile.P("RecvMsg(msg interface{}) error")
|
|
|
|
if method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("SendAndClose(msg *", gfile.QualifiedGoIdent(method.Output.GoIdent), ") error")
|
2023-12-26 00:55:09 +03:00
|
|
|
// gfile.P("CloseSend() error")
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
|
|
|
gfile.P("Close() error")
|
|
|
|
if method.Desc.IsStreamingClient() {
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("Recv() (*", gfile.QualifiedGoIdent(method.Input.GoIdent), ", error)")
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
|
|
|
if method.Desc.IsStreamingServer() {
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("Send(msg *", gfile.QualifiedGoIdent(method.Output.GoIdent), ") error")
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
2021-02-23 18:45:03 +03:00
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateEndpoints(method *protogen.Method) ([]*api_options.HttpRule, bool) {
|
|
|
|
if method.Desc.Options() == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
r := proto.GetExtension(method.Desc.Options(), api_options.E_Http)
|
|
|
|
if r == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
rule := r.(*api_options.HttpRule)
|
|
|
|
rules := []*api_options.HttpRule{rule}
|
|
|
|
rules = append(rules, rule.GetAdditionalBindings()...)
|
|
|
|
|
|
|
|
return rules, method.Desc.IsStreamingServer() || method.Desc.IsStreamingClient()
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:38:57 +03:00
|
|
|
func getMicroApiMethod(method *protogen.Method) (*api_options.MicroMethod, bool) {
|
|
|
|
if method.Desc.Options() == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !proto.HasExtension(method.Desc.Options(), api_options.E_MicroMethod) {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
r := proto.GetExtension(method.Desc.Options(), api_options.E_MicroMethod)
|
|
|
|
if r == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
rule := r.(*api_options.MicroMethod)
|
|
|
|
return rule, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMicroApiService(service *protogen.Service) (*api_options.MicroService, bool) {
|
|
|
|
if service.Desc.Options() == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !proto.HasExtension(service.Desc.Options(), api_options.E_MicroService) {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
r := proto.GetExtension(service.Desc.Options(), api_options.E_MicroService)
|
|
|
|
if r == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
rule := r.(*api_options.MicroService)
|
|
|
|
return rule, true
|
|
|
|
}
|
|
|
|
|
2021-02-23 03:04:56 +03:00
|
|
|
func getEndpoint(rule *api_options.HttpRule) (string, string, string) {
|
|
|
|
var meth string
|
|
|
|
var path string
|
|
|
|
var body string
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case len(rule.GetDelete()) > 0:
|
|
|
|
meth = "DELETE"
|
|
|
|
path = rule.GetDelete()
|
|
|
|
case len(rule.GetGet()) > 0:
|
|
|
|
meth = "GET"
|
|
|
|
path = rule.GetGet()
|
|
|
|
case len(rule.GetPatch()) > 0:
|
|
|
|
meth = "PATCH"
|
|
|
|
path = rule.GetPatch()
|
|
|
|
case len(rule.GetPost()) > 0:
|
|
|
|
meth = "POST"
|
|
|
|
path = rule.GetPost()
|
|
|
|
case len(rule.GetPut()) > 0:
|
|
|
|
meth = "PUT"
|
|
|
|
path = rule.GetPut()
|
|
|
|
case rule.GetCustom() != nil:
|
|
|
|
crule := rule.GetCustom()
|
|
|
|
meth = crule.Kind
|
|
|
|
path = crule.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
body = rule.GetBody()
|
|
|
|
return path, meth, body
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateEndpoint(gfile *protogen.GeneratedFile, serviceName string, methodName string, rule *api_options.HttpRule, streaming bool) {
|
|
|
|
path, meth, body := getEndpoint(rule)
|
|
|
|
gfile.P("Name:", fmt.Sprintf(`"%s.%s",`, serviceName, methodName))
|
|
|
|
gfile.P("Path:", fmt.Sprintf(`[]string{"%s"},`, path))
|
2021-03-26 15:00:55 +03:00
|
|
|
//if vmethod, ok := httpMethodMap[meth]; ok {
|
|
|
|
// gfile.P("Method:", `[]string{`, httpPackage.Ident(vmethod), `},`)
|
|
|
|
//} else {
|
|
|
|
gfile.P("Method:", fmt.Sprintf(`[]string{"%s"},`, meth))
|
|
|
|
// }
|
2021-03-23 16:56:11 +03:00
|
|
|
if len(rule.GetGet()) == 0 && body != "" {
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P("Body:", fmt.Sprintf(`"%s",`, body))
|
|
|
|
}
|
|
|
|
if streaming {
|
|
|
|
gfile.P("Stream: true,")
|
|
|
|
}
|
|
|
|
gfile.P(`Handler: "rpc",`)
|
|
|
|
}
|
2022-03-19 15:13:34 +03:00
|
|
|
|
|
|
|
func (g *Generator) getGoIdentByXref(xref string) (protogen.GoIdent, error) {
|
|
|
|
idx := strings.LastIndex(xref, ".")
|
|
|
|
pkg := xref[:idx]
|
|
|
|
msg := xref[idx+1:]
|
|
|
|
for _, file := range g.plugin.Files {
|
|
|
|
if strings.Compare(pkg, *(file.Proto.Package)) != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ident, err := getGoIdentByMessage(file.Messages, msg); err == nil {
|
|
|
|
return ident, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return protogen.GoIdent{}, fmt.Errorf("not found")
|
|
|
|
}
|
|
|
|
|
2023-10-17 23:43:18 +03:00
|
|
|
func (g *Generator) getMessageByXref(xref string) (*protogen.Message, error) {
|
|
|
|
idx := strings.LastIndex(xref, ".")
|
|
|
|
pkg := xref[:idx]
|
|
|
|
msg := xref[idx+1:]
|
|
|
|
for _, file := range g.plugin.Files {
|
|
|
|
if strings.Compare(pkg, *(file.Proto.Package)) != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if pmsg, err := getProtoMessage(file.Messages, msg); err == nil {
|
|
|
|
return pmsg, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getProtoMessage(messages []*protogen.Message, msg string) (*protogen.Message, error) {
|
|
|
|
for _, message := range messages {
|
|
|
|
if strings.Compare(msg, message.GoIdent.GoName) == 0 {
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
if len(message.Messages) > 0 {
|
|
|
|
if pmsg, err := getProtoMessage(message.Messages, msg); err == nil {
|
|
|
|
return pmsg, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("not found")
|
|
|
|
}
|
|
|
|
|
2022-03-19 15:13:34 +03:00
|
|
|
func getGoIdentByMessage(messages []*protogen.Message, msg string) (protogen.GoIdent, error) {
|
|
|
|
for _, message := range messages {
|
|
|
|
if strings.Compare(msg, message.GoIdent.GoName) == 0 {
|
|
|
|
return message.GoIdent, nil
|
|
|
|
}
|
|
|
|
if len(message.Messages) > 0 {
|
|
|
|
if ident, err := getGoIdentByMessage(message.Messages, msg); err == nil {
|
|
|
|
return ident, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return protogen.GoIdent{}, fmt.Errorf("not found")
|
|
|
|
}
|
2022-11-15 12:53:04 +03:00
|
|
|
|
|
|
|
func (g *Generator) generateServiceDesc(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) {
|
2023-08-20 13:16:08 +03:00
|
|
|
serviceName := getServiceName(service)
|
2022-11-15 12:53:04 +03:00
|
|
|
|
|
|
|
gfile.P("// ", serviceName, "_ServiceDesc", " is the ", grpcPackage.Ident("ServiceDesc"), " for ", serviceName, " service.")
|
|
|
|
gfile.P("// It's only intended for direct use with ", grpcPackage.Ident("RegisterService"), ",")
|
|
|
|
gfile.P("// and not to be introspected or modified (even as a copy)")
|
|
|
|
gfile.P("var ", serviceName, "_ServiceDesc", " = ", grpcPackage.Ident("ServiceDesc"), " {")
|
|
|
|
gfile.P("ServiceName: ", strconv.Quote(string(service.Desc.FullName())), ",")
|
|
|
|
gfile.P("HandlerType: (*", serviceName, "Server)(nil),")
|
|
|
|
gfile.P("Methods: []", grpcPackage.Ident("MethodDesc"), "{")
|
|
|
|
for _, method := range service.Methods {
|
|
|
|
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
gfile.P("{")
|
|
|
|
gfile.P("MethodName: ", strconv.Quote(string(method.Desc.Name())), ",")
|
|
|
|
gfile.P("Handler: ", method.GoName, ",")
|
|
|
|
gfile.P("},")
|
|
|
|
}
|
|
|
|
gfile.P("},")
|
|
|
|
gfile.P("Streams: []", grpcPackage.Ident("StreamDesc"), "{")
|
|
|
|
for _, method := range service.Methods {
|
|
|
|
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
gfile.P("{")
|
|
|
|
gfile.P("StreamName: ", strconv.Quote(string(method.Desc.Name())), ",")
|
|
|
|
gfile.P("Handler: ", method.GoName, ",")
|
|
|
|
if method.Desc.IsStreamingServer() {
|
|
|
|
gfile.P("ServerStreams: true,")
|
|
|
|
}
|
|
|
|
if method.Desc.IsStreamingClient() {
|
|
|
|
gfile.P("ClientStreams: true,")
|
|
|
|
}
|
|
|
|
gfile.P("},")
|
|
|
|
}
|
|
|
|
gfile.P("},")
|
|
|
|
gfile.P("Metadata: \"", file.Desc.Path(), "\",")
|
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
|
|
|
}
|
2023-02-14 00:22:35 +03:00
|
|
|
|
2023-02-22 00:05:54 +03:00
|
|
|
func (g *Generator) generateServiceName(gfile *protogen.GeneratedFile, service *protogen.Service) {
|
2023-08-20 13:16:08 +03:00
|
|
|
serviceName := getServiceName(service)
|
2023-02-14 00:22:35 +03:00
|
|
|
gfile.P("var (")
|
|
|
|
gfile.P(serviceName, "Name", "=", `"`, serviceName, `"`)
|
|
|
|
gfile.P(")")
|
|
|
|
}
|
2023-02-22 00:05:54 +03:00
|
|
|
|
2023-07-30 23:21:32 +03:00
|
|
|
func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.Service, component string) {
|
|
|
|
if component != "http" {
|
|
|
|
return
|
|
|
|
}
|
2023-08-20 13:16:08 +03:00
|
|
|
serviceName := getServiceName(service)
|
2023-02-22 00:05:54 +03:00
|
|
|
|
|
|
|
gfile.P("var (")
|
|
|
|
gfile.P(serviceName, "ServerEndpoints = []", microServerHttpPackage.Ident("EndpointMetadata"), "{")
|
|
|
|
|
|
|
|
for _, method := range service.Methods {
|
|
|
|
if proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
|
|
|
|
if endpoints, streaming := generateEndpoints(method); endpoints != nil {
|
|
|
|
for _, ep := range endpoints {
|
|
|
|
epath, emethod, ebody := getEndpoint(ep)
|
|
|
|
gfile.P("{")
|
|
|
|
gfile.P(`Name: "`, serviceName+"."+method.GoName, `",`)
|
|
|
|
gfile.P(`Path: "`, epath, `",`)
|
|
|
|
gfile.P(`Method: "`, emethod, `",`)
|
|
|
|
gfile.P(`Body: "`, ebody, `",`)
|
|
|
|
gfile.P(`Stream: `, streaming, `,`)
|
|
|
|
gfile.P("},")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gfile.P("}")
|
|
|
|
gfile.P(")")
|
|
|
|
}
|
2023-08-20 13:16:08 +03:00
|
|
|
|
|
|
|
func getServiceName(s *protogen.Service) string {
|
|
|
|
if strings.HasSuffix(s.GoName, "Service") {
|
|
|
|
return s.GoName
|
|
|
|
}
|
|
|
|
return s.GoName + "Service"
|
|
|
|
}
|
2023-10-17 23:43:18 +03:00
|
|
|
|
|
|
|
func (g *Generator) writeErrors(plugin *protogen.Plugin) error {
|
|
|
|
errorsMap := make(map[string]struct{})
|
|
|
|
|
|
|
|
for _, file := range plugin.Files {
|
|
|
|
for _, service := range file.Services {
|
|
|
|
for _, method := range service.Methods {
|
|
|
|
if method.Desc.Options() != nil {
|
|
|
|
if proto.HasExtension(method.Desc.Options(), v2.E_Openapiv2Operation) {
|
|
|
|
opts := proto.GetExtension(method.Desc.Options(), v2.E_Openapiv2Operation)
|
|
|
|
if opts != nil {
|
|
|
|
r := opts.(*v2.Operation)
|
|
|
|
if r.Responses == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rsp := range r.Responses.ResponseCode {
|
|
|
|
if schema := rsp.Value.GetJsonReference(); schema != nil {
|
|
|
|
xref := schema.XRef
|
|
|
|
if xref[0] == '.' {
|
|
|
|
xref = xref[1:]
|
|
|
|
}
|
|
|
|
errorsMap[xref] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if proto.HasExtension(method.Desc.Options(), v3.E_Openapiv3Operation) {
|
|
|
|
opts := proto.GetExtension(method.Desc.Options(), v3.E_Openapiv3Operation)
|
|
|
|
if opts != nil {
|
|
|
|
r := opts.(*v3.Operation)
|
|
|
|
if r.Responses == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
resps := r.Responses.ResponseOrReference
|
|
|
|
if r.Responses.GetDefault() != nil {
|
|
|
|
resps = append(resps, &v3.NamedResponseOrReference{Name: "default", Value: r.Responses.GetDefault()})
|
|
|
|
}
|
|
|
|
for _, rsp := range resps {
|
|
|
|
if schema := rsp.Value.GetReference(); schema != nil {
|
|
|
|
xref := schema.XRef
|
|
|
|
if xref[0] == '.' {
|
|
|
|
xref = xref[1:]
|
|
|
|
}
|
|
|
|
errorsMap[xref] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var gfile *protogen.GeneratedFile
|
2023-10-18 01:01:30 +03:00
|
|
|
var importPath protogen.GoImportPath
|
|
|
|
|
2023-10-17 23:43:18 +03:00
|
|
|
if len(errorsMap) > 0 {
|
2023-10-18 01:01:30 +03:00
|
|
|
|
2023-10-17 23:43:18 +03:00
|
|
|
var packageName string
|
|
|
|
|
|
|
|
for _, file := range plugin.Files {
|
|
|
|
if !file.Generate {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(file.Services) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
packageName = string(file.GoPackageName)
|
2023-10-18 01:01:30 +03:00
|
|
|
importPath = file.GoImportPath
|
2023-10-17 23:43:18 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:01:30 +03:00
|
|
|
if g.standalone {
|
|
|
|
importPath = "."
|
|
|
|
}
|
|
|
|
|
|
|
|
gfile = plugin.NewGeneratedFile("micro_errors.pb.go", importPath)
|
|
|
|
|
2023-10-17 23:43:18 +03:00
|
|
|
gfile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.")
|
|
|
|
gfile.P("// protoc-gen-go-micro version: " + versionComment)
|
|
|
|
gfile.P()
|
|
|
|
gfile.P("package ", packageName)
|
|
|
|
gfile.P()
|
|
|
|
|
|
|
|
gfile.Import(protojsonPackage)
|
|
|
|
|
|
|
|
gfile.P("var (")
|
|
|
|
gfile.P("marshaler = ", protojsonPackage.Ident("MarshalOptions"), "{}")
|
|
|
|
gfile.P(")")
|
|
|
|
}
|
|
|
|
|
|
|
|
for xref := range errorsMap {
|
|
|
|
msg, err := g.getMessageByXref(xref)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-18 00:37:00 +03:00
|
|
|
for _, field := range msg.Fields {
|
|
|
|
if field.GoName == "Error" {
|
|
|
|
return fmt.Errorf("failed generate Error() string interface for %s message %s already have Error field", field.Location.SourceFile, msg.Desc.Name())
|
|
|
|
}
|
|
|
|
}
|
2023-10-17 23:43:18 +03:00
|
|
|
gfile.P(`func (m *`, msg.GoIdent.GoName, `) Error() string {`)
|
|
|
|
gfile.P(`buf, _ := marshaler.Marshal(m)`)
|
|
|
|
gfile.P("return string(buf)")
|
|
|
|
gfile.P(`}`)
|
|
|
|
// log.Printf("xref %#+v %v\n", msg.GoIdent.GoName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|