Skip to content

Commit

Permalink
Merge pull request #1037 from qazwsxedckll/dev
Browse files Browse the repository at this point in the history
feat: generate errors even if no services
  • Loading branch information
rogeralsing authored Mar 8, 2024
2 parents fb0ab3e + f9650ee commit ef91a6a
Show file tree
Hide file tree
Showing 18 changed files with 483 additions and 720 deletions.
8 changes: 4 additions & 4 deletions protobuf/protoc-gen-go-grain/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.PHONY: testdata
testdata:
protoc --go_out=. --go_opt=paths=source_relative --plugin=protoc-gen-go-grain=protoc-gen-go-grain.sh --go-grain_out=. --go-grain_opt=paths=source_relative -I../../ -I. testdata/hello/*.proto
protoc --go_out=. --go_opt=paths=source_relative --plugin=protoc-gen-go-grain=protoc-gen-go-grain.sh --go-grain_out=. --go-grain_opt=paths=source_relative -I../../ -I. testdata/reenter/*.proto
protoc --go_out=. --go_opt=paths=source_relative --plugin=protoc-gen-go-grain=protoc-gen-go-grain.sh --go-grain_out=. --go-grain_opt=paths=source_relative -I../../ -I. testdata/multi-services/*.proto
protoc --go_out=. --go_opt=paths=source_relative --plugin=protoc-gen-go-grain=protoc-gen-go-grain.sh --go-grain_out=. --go-grain_opt=paths=source_relative -I../../ -I. testdata/error/*.proto
protoc --go_out=. --go_opt=paths=source_relative --plugin=protoc-gen-go-grain=protoc-gen-go-grain.sh --go-grain_out=. --go-grain_opt=paths=source_relative -I../../ -I. test/hello/*.proto
protoc --go_out=. --go_opt=paths=source_relative --plugin=protoc-gen-go-grain=protoc-gen-go-grain.sh --go-grain_out=. --go-grain_opt=paths=source_relative -I../../ -I. test/reenter/*.proto
protoc --go_out=. --go_opt=paths=source_relative --plugin=protoc-gen-go-grain=protoc-gen-go-grain.sh --go-grain_out=. --go-grain_opt=paths=source_relative -I../../ -I. test/multi-services/*.proto
protoc --go_out=. --go_opt=paths=source_relative --plugin=protoc-gen-go-grain=protoc-gen-go-grain.sh --go-grain_out=. --go-grain_opt=paths=source_relative -I../../ -I. test/error/*.proto

.PHONY: options
options:
Expand Down
28 changes: 15 additions & 13 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,35 +61,36 @@ 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)

if len(file.Services) == 0 {
return
}

g.QualifiedGoIdent(actorPackage.Ident(""))
g.QualifiedGoIdent(clusterPackage.Ident(""))
g.QualifiedGoIdent(protoPackage.Ident(""))
g.QualifiedGoIdent(fmtPackage.Ident(""))
g.QualifiedGoIdent(timePackage.Ident(""))
g.QualifiedGoIdent(slogPackage.Ident(""))

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

if len(file.Services) == 0 {
return
}

g.QualifiedGoIdent(actorPackage.Ident(""))
g.QualifiedGoIdent(protoPackage.Ident(""))
g.QualifiedGoIdent(timePackage.Ident(""))
g.QualifiedGoIdent(slogPackage.Ident(""))

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
132 changes: 132 additions & 0 deletions protobuf/protoc-gen-go-grain/test/error/error.pb.go

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

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) {}
}
36 changes: 36 additions & 0 deletions protobuf/protoc-gen-go-grain/test/error/error_grain.pb.go

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

Loading

0 comments on commit ef91a6a

Please sign in to comment.