Skip to content

Commit

Permalink
feat: generate errors even if no services
Browse files Browse the repository at this point in the history
  • Loading branch information
qazwsxedckll committed Mar 8, 2024
1 parent fb0ab3e commit 1426311
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 273 deletions.
23 changes: 12 additions & 11 deletions protobuf/protoc-gen-go-grain/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ var (
)

func generateFile(gen *protogen.Plugin, file *protogen.File) {
if len(file.Services) == 0 {
if len(file.Services) == 0 && len(file.Enums) == 0 {
return
}

filename := file.GeneratedFilenamePrefix + "_grain.pb.go"
g := gen.NewGeneratedFile(filename, file.GoImportPath)

generateHeader(gen, g, file)
generateContent(gen, g, file)
generateContent(g, file)
}

func generateHeader(gen *protogen.Plugin, g *protogen.GeneratedFile, file *protogen.File) {
Expand All @@ -60,9 +61,15 @@ func generateHeader(gen *protogen.Plugin, g *protogen.GeneratedFile, file *proto
g.P()
}

func generateContent(gen *protogen.Plugin, g *protogen.GeneratedFile, file *protogen.File) {
func generateContent(g *protogen.GeneratedFile, file *protogen.File) {
g.P("package ", file.GoPackageName)

for _, enum := range file.Enums {
if enum.Desc.Name() == "ErrorReason" {
generateErrorReasons(g, enum)
}
}

if len(file.Services) == 0 {
return
}
Expand All @@ -74,21 +81,15 @@ func generateContent(gen *protogen.Plugin, g *protogen.GeneratedFile, file *prot
g.QualifiedGoIdent(timePackage.Ident(""))
g.QualifiedGoIdent(slogPackage.Ident(""))

for _, enum := range file.Enums {
if enum.Desc.Name() == "ErrorReason" {
generateErrorReasons(g, enum)
}
}

for _, service := range file.Services {
generateService(service, file, g)
generateService(service, g)
g.P()
}

generateRespond(g)
}

func generateService(service *protogen.Service, file *protogen.File, g *protogen.GeneratedFile) {
func generateService(service *protogen.Service, g *protogen.GeneratedFile) {
if service.Desc.Options().(*descriptorpb.ServiceOptions).GetDeprecated() {
g.P("//")
g.P(deprecationComment)
Expand Down
105 changes: 14 additions & 91 deletions protobuf/protoc-gen-go-grain/testdata/error/hello.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions protobuf/protoc-gen-go-grain/testdata/error/hello.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,9 @@ syntax = "proto3";

package hello;

import "google/protobuf/empty.proto";

option go_package = "github.com/asynkron/protoactor-go/protoc-gen-go-grain/testdata/hello";

enum ErrorReason {
USER_NOT_FOUND = 0;
CONTENT_MISSING = 1;
}

message SayHelloResponse {
string message = 1;
}

service Hello {
rpc SayHello (google.protobuf.Empty) returns (SayHelloResponse) {}
}
Loading

0 comments on commit 1426311

Please sign in to comment.