Compare commits

...

3 Commits

Author SHA1 Message Date
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 154 additions and 14 deletions

View File

@@ -63,8 +63,8 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
g.reflection = *flagReflection
plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
genClient := true
genServer := true
var genClient bool
var genServer bool
var genNone bool
if strings.Contains(g.components, "server") {
@@ -113,6 +113,11 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
}
if err = g.writeErrors(plugin); err != nil {
plugin.Error(err)
return err
}
if err = g.astGenerate(plugin); err != nil {
plugin.Error(err)
return err

158
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) {
serviceName := service.GoName
serviceName := getServiceName(service)
// if rule, ok := getMicroApiService(service); ok {
// 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) {
serviceName := service.GoName
serviceName := getServiceName(service)
for _, method := range service.Methods {
methodName := fmt.Sprintf("%s.%s", serviceName, method.GoName)
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) {
serviceName := service.GoName
serviceName := getServiceName(service)
gfile.P("type ", unexport(serviceName), "Server struct {")
if g.standalone {
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) {
serviceName := service.GoName
serviceName := getServiceName(service)
for _, method := range service.Methods {
generateServerFuncSignature(gfile, serviceName, method, true)
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) {
serviceName := service.GoName
serviceName := getServiceName(service)
if g.standalone {
gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", file.GoImportPath.Ident(serviceName), "Server, opts ...", microOptionsPackage.Ident("Option"), ") error {")
} else {
@@ -597,7 +597,7 @@ func generateClientSignature(gfile *protogen.GeneratedFile, serviceName string,
}
func (g *Generator) generateServiceClientInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName
serviceName := getServiceName(service)
gfile.P("type ", serviceName, "Client interface {")
for _, method := range service.Methods {
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) {
serviceName := service.GoName
serviceName := getServiceName(service)
gfile.P("type ", serviceName, "Server interface {")
for _, method := range service.Methods {
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) {
serviceName := service.GoName
serviceName := getServiceName(service)
for _, method := range service.Methods {
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
continue
@@ -645,7 +645,7 @@ func (g *Generator) generateServiceClientStreamInterface(gfile *protogen.Generat
}
func (g *Generator) generateServiceServerStreamInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName
serviceName := getServiceName(service)
for _, method := range service.Methods {
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
continue
@@ -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 {
@@ -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) {
serviceName := service.GoName
serviceName := getServiceName(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"), ",")
@@ -849,7 +878,7 @@ func (g *Generator) generateServiceDesc(gfile *protogen.GeneratedFile, file *pro
}
func (g *Generator) generateServiceName(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName
serviceName := getServiceName(service)
gfile.P("var (")
gfile.P(serviceName, "Name", "=", `"`, serviceName, `"`)
gfile.P(")")
@@ -859,7 +888,7 @@ func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, serv
if component != "http" {
return
}
serviceName := service.GoName
serviceName := getServiceName(service)
gfile.P("var (")
gfile.P(serviceName, "ServerEndpoints = []", microServerHttpPackage.Ident("EndpointMetadata"), "{")
@@ -884,3 +913,108 @@ func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, serv
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
if len(errorsMap) > 0 {
gfile = plugin.NewGeneratedFile("micro_errors.pb.go", ".")
var packageName string
for _, file := range plugin.Files {
if !file.Generate {
continue
}
if len(file.Services) == 0 {
continue
}
packageName = string(file.GoPackageName)
break
}
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
}
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"