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()
|
|
|
|
|
2021-02-23 23:58:24 +03:00
|
|
|
gfile.Import(contextPackage)
|
|
|
|
gfile.Import(microApiPackage)
|
|
|
|
gfile.Import(microClientPackage)
|
|
|
|
gfile.Import(microClientHttpPackage)
|
|
|
|
gfile.Import(microServerPackage)
|
2021-02-23 03:04:56 +03:00
|
|
|
|
|
|
|
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) {
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P("// Code generated by protoc-gen-micro")
|
2021-02-23 14:30:23 +03:00
|
|
|
gfile.P("package ", err.packageName)
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P()
|
|
|
|
|
|
|
|
gfile.Import(fmtPackage)
|
|
|
|
|
2021-02-23 14:30:23 +03:00
|
|
|
for _, typ := range err.types {
|
|
|
|
gfile.P("func (err *", typ[strings.LastIndex(typ, ".")+1:], ") Error() string {")
|
2021-02-24 01:19:57 +03:00
|
|
|
gfile.P(`return `, fmtPackage.Ident("Sprintf"), `("%#v", err)`)
|
2021-02-23 14:30:23 +03:00
|
|
|
gfile.P("}")
|
|
|
|
gfile.P()
|
|
|
|
}
|
|
|
|
}
|