Compare commits

..

7 Commits

Author SHA1 Message Date
858bf4716c Merge pull request 'fix gen' (#90) from ff into master
Reviewed-on: #90
2023-12-26 00:56:33 +03:00
0c6589a630 fix gen
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-12-26 00:55:57 +03:00
6152d866d6 error generate in standalone mode
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-10-18 01:01:30 +03:00
5565534f79 check error field in proto message
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-10-18 00:37:58 +03:00
6228457361 generate error interface
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-10-17 23:43:18 +03:00
190a1903f4 Merge pull request 'append Service only in generated go files' (#87) from namingfix into master
Reviewed-on: #87
2023-08-20 13:16:24 +03:00
d658731441 append Service only in generated go files
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-08-20 13:16:08 +03:00
3 changed files with 168 additions and 16 deletions

View File

@@ -63,8 +63,8 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
g.reflection = *flagReflection g.reflection = *flagReflection
plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL) plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
genClient := true var genClient bool
genServer := true var genServer bool
var genNone bool var genNone bool
if strings.Contains(g.components, "server") { if strings.Contains(g.components, "server") {
@@ -90,6 +90,9 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
continue continue
case "micro": case "micro":
err = g.microGenerate(component, plugin, genClient, genServer) err = g.microGenerate(component, plugin, genClient, genServer)
if err == nil {
err = g.writeErrors(plugin)
}
case "http": case "http":
err = g.httpGenerate(component, plugin, genClient, genServer) err = g.httpGenerate(component, plugin, genClient, genServer)
case "grpc", "drpc", "rpc": case "grpc", "drpc", "rpc":

176
util.go
View File

@@ -32,7 +32,7 @@ func unexport(s string) string {
} }
func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) { func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) {
serviceName := service.GoName serviceName := getServiceName(service)
// if rule, ok := getMicroApiService(service); ok { // if rule, ok := getMicroApiService(service); ok {
// gfile.P("// client wrappers ", strings.Join(rule.ClientWrappers, ", ")) // gfile.P("// client wrappers ", strings.Join(rule.ClientWrappers, ", "))
// } // }
@@ -52,7 +52,7 @@ func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, file *p
} }
func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protogen.Service, component string) { func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protogen.Service, component string) {
serviceName := service.GoName serviceName := getServiceName(service)
for _, method := range service.Methods { for _, method := range service.Methods {
methodName := fmt.Sprintf("%s.%s", serviceName, method.GoName) methodName := fmt.Sprintf("%s.%s", serviceName, method.GoName)
if component == "drpc" { if component == "drpc" {
@@ -324,7 +324,7 @@ func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile,
} }
func (g *Generator) generateServiceServer(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) { func (g *Generator) generateServiceServer(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) {
serviceName := service.GoName serviceName := getServiceName(service)
gfile.P("type ", unexport(serviceName), "Server struct {") gfile.P("type ", unexport(serviceName), "Server struct {")
if g.standalone { if g.standalone {
gfile.P(file.GoImportPath.Ident(serviceName), "Server") gfile.P(file.GoImportPath.Ident(serviceName), "Server")
@@ -336,7 +336,7 @@ func (g *Generator) generateServiceServer(gfile *protogen.GeneratedFile, file *p
} }
func (g *Generator) generateServiceServerMethods(gfile *protogen.GeneratedFile, service *protogen.Service) { func (g *Generator) generateServiceServerMethods(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName serviceName := getServiceName(service)
for _, method := range service.Methods { for _, method := range service.Methods {
generateServerFuncSignature(gfile, serviceName, method, true) generateServerFuncSignature(gfile, serviceName, method, true)
if rule, ok := getMicroApiMethod(method); ok { if rule, ok := getMicroApiMethod(method); ok {
@@ -486,7 +486,7 @@ func (g *Generator) generateServiceServerMethods(gfile *protogen.GeneratedFile,
} }
func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service, component string) { func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service, component string) {
serviceName := service.GoName serviceName := getServiceName(service)
if g.standalone { if g.standalone {
gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", file.GoImportPath.Ident(serviceName), "Server, opts ...", microOptionsPackage.Ident("Option"), ") error {") gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", file.GoImportPath.Ident(serviceName), "Server, opts ...", microOptionsPackage.Ident("Option"), ") error {")
} else { } else {
@@ -572,7 +572,7 @@ func (g *Generator) generateClientFuncSignature(gfile *protogen.GeneratedFile, s
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() { if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
args = append(args, "*", gfile.QualifiedGoIdent(method.Output.GoIdent)) args = append(args, "*", gfile.QualifiedGoIdent(method.Output.GoIdent))
} else { } else {
args = append(args, serviceName, "_", method.GoName, "Client") args = append(args, gfile.QualifiedGoIdent(protogen.GoIdent{GoName: serviceName + "_" + method.GoName + "Client", GoImportPath: method.Output.GoIdent.GoImportPath}))
} }
args = append(args, ", error) {") args = append(args, ", error) {")
gfile.P(args...) gfile.P(args...)
@@ -597,7 +597,7 @@ func generateClientSignature(gfile *protogen.GeneratedFile, serviceName string,
} }
func (g *Generator) generateServiceClientInterface(gfile *protogen.GeneratedFile, service *protogen.Service) { func (g *Generator) generateServiceClientInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName serviceName := getServiceName(service)
gfile.P("type ", serviceName, "Client interface {") gfile.P("type ", serviceName, "Client interface {")
for _, method := range service.Methods { for _, method := range service.Methods {
generateClientSignature(gfile, serviceName, method) generateClientSignature(gfile, serviceName, method)
@@ -607,7 +607,7 @@ func (g *Generator) generateServiceClientInterface(gfile *protogen.GeneratedFile
} }
func (g *Generator) generateServiceServerInterface(gfile *protogen.GeneratedFile, service *protogen.Service) { func (g *Generator) generateServiceServerInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName serviceName := getServiceName(service)
gfile.P("type ", serviceName, "Server interface {") gfile.P("type ", serviceName, "Server interface {")
for _, method := range service.Methods { for _, method := range service.Methods {
generateServerSignature(gfile, serviceName, method, false) generateServerSignature(gfile, serviceName, method, false)
@@ -617,7 +617,7 @@ func (g *Generator) generateServiceServerInterface(gfile *protogen.GeneratedFile
} }
func (g *Generator) generateServiceClientStreamInterface(gfile *protogen.GeneratedFile, service *protogen.Service) { func (g *Generator) generateServiceClientStreamInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName serviceName := getServiceName(service)
for _, method := range service.Methods { for _, method := range service.Methods {
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() { if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
continue continue
@@ -645,7 +645,7 @@ func (g *Generator) generateServiceClientStreamInterface(gfile *protogen.Generat
} }
func (g *Generator) generateServiceServerStreamInterface(gfile *protogen.GeneratedFile, service *protogen.Service) { func (g *Generator) generateServiceServerStreamInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName serviceName := getServiceName(service)
for _, method := range service.Methods { for _, method := range service.Methods {
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() { if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
continue continue
@@ -657,7 +657,7 @@ func (g *Generator) generateServiceServerStreamInterface(gfile *protogen.Generat
gfile.P("RecvMsg(msg interface{}) error") gfile.P("RecvMsg(msg interface{}) error")
if method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() { if method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
gfile.P("SendAndClose(msg *", gfile.QualifiedGoIdent(method.Output.GoIdent), ") error") gfile.P("SendAndClose(msg *", gfile.QualifiedGoIdent(method.Output.GoIdent), ") error")
gfile.P("CloseSend() error") // gfile.P("CloseSend() error")
} }
gfile.P("Close() error") gfile.P("Close() error")
if method.Desc.IsStreamingClient() { if method.Desc.IsStreamingClient() {
@@ -792,6 +792,35 @@ func (g *Generator) getGoIdentByXref(xref string) (protogen.GoIdent, error) {
return protogen.GoIdent{}, fmt.Errorf("not found") return protogen.GoIdent{}, fmt.Errorf("not found")
} }
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")
}
func getGoIdentByMessage(messages []*protogen.Message, msg string) (protogen.GoIdent, error) { func getGoIdentByMessage(messages []*protogen.Message, msg string) (protogen.GoIdent, error) {
for _, message := range messages { for _, message := range messages {
if strings.Compare(msg, message.GoIdent.GoName) == 0 { if strings.Compare(msg, message.GoIdent.GoName) == 0 {
@@ -807,7 +836,7 @@ func getGoIdentByMessage(messages []*protogen.Message, msg string) (protogen.GoI
} }
func (g *Generator) generateServiceDesc(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) { func (g *Generator) generateServiceDesc(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) {
serviceName := service.GoName serviceName := getServiceName(service)
gfile.P("// ", serviceName, "_ServiceDesc", " is the ", grpcPackage.Ident("ServiceDesc"), " for ", serviceName, " service.") 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("// It's only intended for direct use with ", grpcPackage.Ident("RegisterService"), ",")
@@ -849,7 +878,7 @@ func (g *Generator) generateServiceDesc(gfile *protogen.GeneratedFile, file *pro
} }
func (g *Generator) generateServiceName(gfile *protogen.GeneratedFile, service *protogen.Service) { func (g *Generator) generateServiceName(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName serviceName := getServiceName(service)
gfile.P("var (") gfile.P("var (")
gfile.P(serviceName, "Name", "=", `"`, serviceName, `"`) gfile.P(serviceName, "Name", "=", `"`, serviceName, `"`)
gfile.P(")") gfile.P(")")
@@ -859,7 +888,7 @@ func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, serv
if component != "http" { if component != "http" {
return return
} }
serviceName := service.GoName serviceName := getServiceName(service)
gfile.P("var (") gfile.P("var (")
gfile.P(serviceName, "ServerEndpoints = []", microServerHttpPackage.Ident("EndpointMetadata"), "{") gfile.P(serviceName, "ServerEndpoints = []", microServerHttpPackage.Ident("EndpointMetadata"), "{")
@@ -884,3 +913,122 @@ func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, serv
gfile.P("}") gfile.P("}")
gfile.P(")") gfile.P(")")
} }
func getServiceName(s *protogen.Service) string {
if strings.HasSuffix(s.GoName, "Service") {
return s.GoName
}
return s.GoName + "Service"
}
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
var importPath protogen.GoImportPath
if len(errorsMap) > 0 {
var packageName string
for _, file := range plugin.Files {
if !file.Generate {
continue
}
if len(file.Services) == 0 {
continue
}
packageName = string(file.GoPackageName)
importPath = file.GoImportPath
break
}
if g.standalone {
importPath = "."
}
gfile = plugin.NewGeneratedFile("micro_errors.pb.go", importPath)
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
}
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())
}
}
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
}

View File

@@ -20,6 +20,7 @@ var (
microErrorsPackage = protogen.GoImportPath("go.unistack.org/micro/v4/errors") microErrorsPackage = protogen.GoImportPath("go.unistack.org/micro/v4/errors")
microOptionsPackage = protogen.GoImportPath("go.unistack.org/micro/v4/options") microOptionsPackage = protogen.GoImportPath("go.unistack.org/micro/v4/options")
grpcPackage = protogen.GoImportPath("google.golang.org/grpc") grpcPackage = protogen.GoImportPath("google.golang.org/grpc")
protojsonPackage = protogen.GoImportPath("google.golang.org/protobuf/encoding/protojson")
timePackage = protogen.GoImportPath("time") timePackage = protogen.GoImportPath("time")
deprecationComment = "// Deprecated: Do not use." deprecationComment = "// Deprecated: Do not use."
versionComment = "v4.0.2" versionComment = "v4.0.2"