2021-02-22 18:11:08 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-02-23 14:30:23 +03:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
openapi_options "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/options"
|
2021-02-22 18:11:08 +03:00
|
|
|
"google.golang.org/protobuf/compiler/protogen"
|
2021-02-23 14:30:23 +03:00
|
|
|
"google.golang.org/protobuf/proto"
|
2021-02-22 18:11:08 +03:00
|
|
|
)
|
|
|
|
|
2021-02-23 14:30:23 +03:00
|
|
|
type Error struct {
|
|
|
|
packageName string
|
|
|
|
types []string
|
|
|
|
}
|
|
|
|
|
2021-02-22 18:11:08 +03:00
|
|
|
func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin) error {
|
2021-02-23 14:30:23 +03:00
|
|
|
errors := make(map[string]struct{})
|
|
|
|
|
2021-02-22 18:11:08 +03:00
|
|
|
for _, file := range plugin.Files {
|
|
|
|
if !file.Generate {
|
|
|
|
continue
|
|
|
|
}
|
2021-02-23 03:04:56 +03:00
|
|
|
if len(file.Services) == 0 {
|
|
|
|
continue
|
2021-02-22 18:11:08 +03:00
|
|
|
}
|
|
|
|
|
2021-02-23 14:30:23 +03:00
|
|
|
gname := file.GeneratedFilenamePrefix + "_micro_" + component + ".pb.go"
|
2021-02-23 23:54:53 +03:00
|
|
|
gfile := plugin.NewGeneratedFile(gname, file.GoImportPath)
|
2021-02-23 03:04:56 +03:00
|
|
|
|
|
|
|
gfile.P("// Code generated by protoc-gen-micro")
|
|
|
|
gfile.P("// source: ", *file.Proto.Name)
|
|
|
|
gfile.P("package ", file.GoPackageName)
|
|
|
|
|
|
|
|
gfile.P()
|
|
|
|
gfile.P("import (")
|
|
|
|
gfile.P(`"context"`)
|
2021-02-23 14:30:23 +03:00
|
|
|
gfile.P()
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P(`micro_api "github.com/unistack-org/micro/v3/api"`)
|
2021-02-23 14:30:23 +03:00
|
|
|
gfile.P(`micro_client_http "github.com/unistack-org/micro-client-http/v3"`)
|
2021-02-23 03:04:56 +03:00
|
|
|
gfile.P(`micro_client "github.com/unistack-org/micro/v3/client"`)
|
|
|
|
gfile.P(`micro_server "github.com/unistack-org/micro/v3/server"`)
|
|
|
|
gfile.P(")")
|
|
|
|
gfile.P()
|
|
|
|
|
|
|
|
gfile.P("// Reference imports to suppress errors if they are not otherwise used.")
|
|
|
|
gfile.P("var (")
|
|
|
|
gfile.P("_ ", "micro_api.Endpoint")
|
|
|
|
gfile.P("_ ", "context.Context")
|
|
|
|
gfile.P(" _ ", "micro_client.Option")
|
|
|
|
gfile.P(" _ ", "micro_server.Option")
|
|
|
|
gfile.P(")")
|
|
|
|
gfile.P()
|
|
|
|
|
|
|
|
for _, service := range file.Services {
|
|
|
|
generateServiceClient(gfile, service)
|
|
|
|
generateServiceClientMethods(gfile, service, true)
|
|
|
|
generateServiceServer(gfile, service)
|
|
|
|
generateServiceServerMethods(gfile, service)
|
|
|
|
generateServiceRegister(gfile, service)
|
2021-02-23 14:30:23 +03:00
|
|
|
if component == "http" {
|
|
|
|
for k, v := range getErrors(service) {
|
|
|
|
errors[k] = v
|
|
|
|
}
|
|
|
|
}
|
2021-02-23 03:04:56 +03:00
|
|
|
}
|
2021-02-22 18:11:08 +03:00
|
|
|
}
|
|
|
|
|
2021-02-23 14:30:23 +03:00
|
|
|
files := make(map[string]*Error)
|
|
|
|
for _, file := range plugin.Files {
|
|
|
|
if !file.Generate {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err, ok := files[file.GeneratedFilenamePrefix]
|
|
|
|
if !ok {
|
|
|
|
err = &Error{packageName: string(file.GoPackageName)}
|
|
|
|
}
|
|
|
|
fok := false
|
|
|
|
for _, message := range file.Messages {
|
|
|
|
if _, ok := errors["."+string(message.Desc.FullName())]; ok {
|
|
|
|
fok = true
|
|
|
|
err.types = append(err.types, string(message.Desc.FullName()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if fok {
|
|
|
|
files[file.GeneratedFilenamePrefix] = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 23:54:53 +03:00
|
|
|
for _, file := range plugin.Files {
|
|
|
|
for efile, err := range files {
|
|
|
|
if file.GeneratedFilenamePrefix != efile {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
gfile := plugin.NewGeneratedFile(efile+"_micro_errors.pb.go", file.GoImportPath)
|
|
|
|
generateServiceErrors(gfile, err)
|
|
|
|
}
|
2021-02-23 14:30:23 +03:00
|
|
|
}
|
|
|
|
|
2021-02-22 18:11:08 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-02-23 14:30:23 +03:00
|
|
|
|
|
|
|
func getErrors(service *protogen.Service) map[string]struct{} {
|
|
|
|
errors := make(map[string]struct{})
|
|
|
|
|
|
|
|
for _, method := range service.Methods {
|
|
|
|
if method.Desc.Options() == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !proto.HasExtension(method.Desc.Options(), openapi_options.E_Openapiv2Operation) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := proto.GetExtension(method.Desc.Options(), openapi_options.E_Openapiv2Operation)
|
|
|
|
if opts == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
r := opts.(*openapi_options.Operation)
|
|
|
|
for _, response := range r.Responses {
|
|
|
|
if response.Schema == nil || response.Schema.JsonSchema == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
errors[response.Schema.JsonSchema.Ref] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateServiceErrors(gfile *protogen.GeneratedFile, err *Error) {
|
|
|
|
gfile.P("package ", err.packageName)
|
|
|
|
gfile.P("import (")
|
|
|
|
gfile.P(`"fmt"`)
|
|
|
|
gfile.P(")")
|
|
|
|
for _, typ := range err.types {
|
|
|
|
gfile.P("func (err *", typ[strings.LastIndex(typ, ".")+1:], ") Error() string {")
|
|
|
|
gfile.P(`return fmt.Sprintf("%#v", err)`)
|
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
|
|
|
}
|
|
|
|
}
|