Compare commits

...

5 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
3 changed files with 147 additions and 2 deletions

View File

@ -90,6 +90,9 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
continue
case "micro":
err = g.microGenerate(component, plugin, genClient, genServer)
if err == nil {
err = g.writeErrors(plugin)
}
case "http":
err = g.httpGenerate(component, plugin, genClient, genServer)
case "grpc", "drpc", "rpc":

145
util.go
View File

@ -572,7 +572,7 @@ func (g *Generator) generateClientFuncSignature(gfile *protogen.GeneratedFile, s
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
args = append(args, "*", gfile.QualifiedGoIdent(method.Output.GoIdent))
} 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) {")
gfile.P(args...)
@ -657,7 +657,7 @@ func (g *Generator) generateServiceServerStreamInterface(gfile *protogen.Generat
gfile.P("RecvMsg(msg interface{}) error")
if method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
gfile.P("SendAndClose(msg *", gfile.QualifiedGoIdent(method.Output.GoIdent), ") error")
gfile.P("CloseSend() error")
// gfile.P("CloseSend() error")
}
gfile.P("Close() error")
if method.Desc.IsStreamingClient() {
@ -792,6 +792,35 @@ func (g *Generator) getGoIdentByXref(xref string) (protogen.GoIdent, error) {
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) {
for _, message := range messages {
if strings.Compare(msg, message.GoIdent.GoName) == 0 {
@ -891,3 +920,115 @@ func getServiceName(s *protogen.Service) string {
}
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")
microOptionsPackage = protogen.GoImportPath("go.unistack.org/micro/v4/options")
grpcPackage = protogen.GoImportPath("google.golang.org/grpc")
protojsonPackage = protogen.GoImportPath("google.golang.org/protobuf/encoding/protojson")
timePackage = protogen.GoImportPath("time")
deprecationComment = "// Deprecated: Do not use."
versionComment = "v4.0.2"