Skip to content

Commit

Permalink
fix: do not generate xerror when no enum found (#704)
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq authored Feb 6, 2023
1 parent b05007c commit 41319a9
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 26 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ init:
go install github.com/go-swagger/go-swagger/cmd/swagger@v0.30.4
cd ./cmd/protoc-gen-go-echo && go install .
cd ./cmd/protoc-gen-go-gin && go install .
cd ./cmd/protoc-gen-go-xerror && go install .

# update buf mod
update:
Expand Down
5 changes: 5 additions & 0 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ plugins:
opt:
- paths=source_relative

- name: go-xerror
out: proto
opt:
- paths=source_relative

- name: go-grpc
out: proto
opt:
Expand Down
10 changes: 6 additions & 4 deletions cmd/protoc-gen-go-xerror/xerror.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ const (

// generateFile generates a _error.pb.go file.
func generateFile(gen *protogen.Plugin, file *protogen.File) *protogen.GeneratedFile {
if len(file.Services) == 0 {
errorInfos := genErrors(gen, file)
if len(errorInfos.Errors) == 0 {
return nil
}

filename := file.GeneratedFilenamePrefix + "_xerror.pb.go"
g := gen.NewGeneratedFile(filename, file.GoImportPath)
g.P("// Code generated by github.com/douyu/jupiter/cmd/protoc-gen-go-xerror. DO NOT EDIT.")
Expand All @@ -35,12 +37,12 @@ func generateFile(gen *protogen.Plugin, file *protogen.File) *protogen.Generated
g.P("var _ = new(", xerrorPkg.Ident("Err"), ")")
g.P()

genErrors(gen, file, g)
g.P(errorInfos.execute())

return g
}

func genErrors(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile) {
func genErrors(gen *protogen.Plugin, file *protogen.File) *errorInfos {
errorInfos := new(errorInfos)
for _, enum := range file.Enums {
for _, enumValue := range enum.Values {
Expand All @@ -56,7 +58,7 @@ func genErrors(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedF
}
}

g.P(errorInfos.execute())
return errorInfos
}

func protocVersion(gen *protogen.Plugin) string {
Expand Down
10 changes: 5 additions & 5 deletions proto/error/v1/error.pb.go

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

2 changes: 1 addition & 1 deletion proto/error/v1/error.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package error.v1;

import "google/protobuf/descriptor.proto";

option go_package = "github.com/douyu/jupiter-layout/api/error/v1;errorv1";
option go_package = "github.com/douyu/jupiter/proto/error/v1;errorv1";
option java_multiple_files = true;
option java_outer_classname = "ErrorProtoV1";
option java_package = "com.douyu.error.v1";
Expand Down
142 changes: 142 additions & 0 deletions proto/helloworld/v1/error.pb.go

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

18 changes: 18 additions & 0 deletions proto/helloworld/v1/error.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";

package helloworld.v1;

import "error/v1/error.proto";

option go_package = "github.com/douyu/jupiter/proto/helloworld/v1;helloworldv1";
option java_multiple_files = true;
option java_outer_classname = "HelloWorldProtoV1";
option java_package = "com.douyu.helloworld.v1";

// Error codes for the helloworld.v1 package.
enum Error {
// ERROR_UNSPECIFIED is the invalid code.
ERROR_UNSPECIFIED = 0;
// ERROR_NAME_INVALID is the error code for invalid name.
ERROR_NAME_INVALID = 1000 [(error.v1.msg) = "name is invalid"];
}
15 changes: 15 additions & 0 deletions proto/helloworld/v1/error.swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"swagger": "2.0",
"info": {
"title": "helloworld/v1/error.proto",
"version": "version not set"
},
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"paths": {},
"definitions": {}
}

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

28 changes: 14 additions & 14 deletions proto/helloworld/v1/helloworld.pb.go

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

0 comments on commit 41319a9

Please sign in to comment.