allow to generate standalone code
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
97
http.go
97
http.go
@@ -1,21 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
openapi_options "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/options"
|
||||
"google.golang.org/protobuf/compiler/protogen"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type Error struct {
|
||||
packageName string
|
||||
types []string
|
||||
}
|
||||
|
||||
func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin) error {
|
||||
errors := make(map[string]struct{})
|
||||
|
||||
for _, file := range plugin.Files {
|
||||
if !file.Generate {
|
||||
continue
|
||||
@@ -25,7 +14,11 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin) erro
|
||||
}
|
||||
|
||||
gname := file.GeneratedFilenamePrefix + "_micro_" + component + ".pb.go"
|
||||
gfile := plugin.NewGeneratedFile(gname, file.GoImportPath)
|
||||
path := file.GoImportPath
|
||||
if g.standalone {
|
||||
path = "."
|
||||
}
|
||||
gfile := plugin.NewGeneratedFile(gname, path)
|
||||
|
||||
gfile.P("// Code generated by protoc-gen-micro")
|
||||
gfile.P("// source: ", *file.Proto.Name)
|
||||
@@ -44,88 +37,8 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin) erro
|
||||
generateServiceServer(gfile, service)
|
||||
generateServiceServerMethods(gfile, service)
|
||||
generateServiceRegister(gfile, service)
|
||||
if component == "http" {
|
||||
for k, v := range getErrors(service) {
|
||||
errors[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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("// Code generated by protoc-gen-micro")
|
||||
gfile.P("package ", err.packageName)
|
||||
gfile.P()
|
||||
|
||||
gfile.Import(fmtPackage)
|
||||
|
||||
for _, typ := range err.types {
|
||||
gfile.P("func (err *", typ[strings.LastIndex(typ, ".")+1:], ") Error() string {")
|
||||
gfile.P(`return `, fmtPackage.Ident("Sprintf"), `("%#v", err)`)
|
||||
gfile.P("}")
|
||||
gfile.P()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user