From 8b4b31fd7d25d81ad5dd0348847327c434052775 Mon Sep 17 00:00:00 2001 From: sevennt Date: Mon, 6 Sep 2021 18:43:49 +0800 Subject: [PATCH] feat: add -file flag to protoc-gen-go-errors --- cmd/protoc-gen-go-errors/main.go | 5 ++ examples/grpc/errors/error.sh | 2 +- .../grpc/errors/{myerr.pb.go => errors.pb.go} | 74 +++++++++---------- .../grpc/errors/{myerr.proto => errors.proto} | 0 ...myerr_errors.pb.go => errors_errors.pb.go} | 0 5 files changed, 43 insertions(+), 38 deletions(-) rename examples/grpc/errors/{myerr.pb.go => errors.pb.go} (50%) rename examples/grpc/errors/{myerr.proto => errors.proto} (100%) rename examples/grpc/errors/{myerr_errors.pb.go => errors_errors.pb.go} (100%) diff --git a/cmd/protoc-gen-go-errors/main.go b/cmd/protoc-gen-go-errors/main.go index e4f806ea..16b4234b 100644 --- a/cmd/protoc-gen-go-errors/main.go +++ b/cmd/protoc-gen-go-errors/main.go @@ -25,6 +25,7 @@ package main import ( "flag" "fmt" + "path/filepath" "google.golang.org/protobuf/compiler/protogen" "google.golang.org/protobuf/types/pluginpb" @@ -41,12 +42,16 @@ func main() { return } var flags flag.FlagSet + file := flags.String("file", "", "specified proto files (default value: errors.proto)") protogen.Options{ParamFunc: flags.Set}.Run(func(gen *protogen.Plugin) error { gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL) for _, f := range gen.Files { if !f.Generate { continue } + if filepath.Base(f.Proto.GetName()) != *file { + continue + } generateFile(gen, f) } return nil diff --git a/examples/grpc/errors/error.sh b/examples/grpc/errors/error.sh index e93f542f..1ecb5e64 100755 --- a/examples/grpc/errors/error.sh +++ b/examples/grpc/errors/error.sh @@ -4,4 +4,4 @@ rm -rf ./protoc-gen-go-errors go build -o ./protoc-gen-go-errors ../../../cmd/protoc-gen-go-errors export PATH=$PATH:$(pwd)/ -protoc --proto_path=. --go_out=paths=source_relative:. --go-errors_out=paths=source_relative:. ./myerr.proto +protoc --proto_path=. --go_out=paths=source_relative:. --go-errors_out=paths=source_relative,file=errors.proto:. ./errors.proto diff --git a/examples/grpc/errors/myerr.pb.go b/examples/grpc/errors/errors.pb.go similarity index 50% rename from examples/grpc/errors/myerr.pb.go rename to examples/grpc/errors/errors.pb.go index 07c2eaa0..38e1b5cb 100644 --- a/examples/grpc/errors/myerr.pb.go +++ b/examples/grpc/errors/errors.pb.go @@ -2,7 +2,7 @@ // versions: // protoc-gen-go v1.26.0 // protoc v3.17.3 -// source: myerr.proto +// source: errors.proto package bizv1 @@ -60,11 +60,11 @@ func (x Err) String() string { } func (Err) Descriptor() protoreflect.EnumDescriptor { - return file_myerr_proto_enumTypes[0].Descriptor() + return file_errors_proto_enumTypes[0].Descriptor() } func (Err) Type() protoreflect.EnumType { - return &file_myerr_proto_enumTypes[0] + return &file_errors_proto_enumTypes[0] } func (x Err) Number() protoreflect.EnumNumber { @@ -73,41 +73,41 @@ func (x Err) Number() protoreflect.EnumNumber { // Deprecated: Use Err.Descriptor instead. func (Err) EnumDescriptor() ([]byte, []int) { - return file_myerr_proto_rawDescGZIP(), []int{0} + return file_errors_proto_rawDescGZIP(), []int{0} } -var File_myerr_proto protoreflect.FileDescriptor - -var file_myerr_proto_rawDesc = []byte{ - 0x0a, 0x0b, 0x6d, 0x79, 0x65, 0x72, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, 0x62, - 0x69, 0x7a, 0x2e, 0x76, 0x31, 0x2a, 0x47, 0x0a, 0x03, 0x45, 0x72, 0x72, 0x12, 0x0f, 0x0a, 0x0b, - 0x45, 0x52, 0x52, 0x5f, 0x49, 0x4e, 0x56, 0x41, 0x4c, 0x49, 0x44, 0x10, 0x00, 0x12, 0x16, 0x0a, - 0x12, 0x45, 0x52, 0x52, 0x5f, 0x55, 0x53, 0x45, 0x52, 0x5f, 0x4e, 0x4f, 0x54, 0x5f, 0x46, 0x4f, - 0x55, 0x4e, 0x44, 0x10, 0x01, 0x12, 0x17, 0x0a, 0x13, 0x45, 0x52, 0x52, 0x5f, 0x43, 0x4f, 0x4e, - 0x54, 0x45, 0x4e, 0x54, 0x5f, 0x4d, 0x49, 0x53, 0x53, 0x49, 0x4e, 0x47, 0x10, 0x02, 0x42, 0x2a, - 0x0a, 0x0e, 0x63, 0x6f, 0x6d, 0x2e, 0x65, 0x67, 0x6f, 0x2e, 0x62, 0x69, 0x7a, 0x2e, 0x76, 0x31, - 0x42, 0x08, 0x42, 0x69, 0x7a, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x0c, 0x62, 0x69, - 0x7a, 0x2f, 0x76, 0x31, 0x3b, 0x62, 0x69, 0x7a, 0x76, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x33, +var File_errors_proto protoreflect.FileDescriptor + +var file_errors_proto_rawDesc = []byte{ + 0x0a, 0x0c, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, + 0x62, 0x69, 0x7a, 0x2e, 0x76, 0x31, 0x2a, 0x47, 0x0a, 0x03, 0x45, 0x72, 0x72, 0x12, 0x0f, 0x0a, + 0x0b, 0x45, 0x52, 0x52, 0x5f, 0x49, 0x4e, 0x56, 0x41, 0x4c, 0x49, 0x44, 0x10, 0x00, 0x12, 0x16, + 0x0a, 0x12, 0x45, 0x52, 0x52, 0x5f, 0x55, 0x53, 0x45, 0x52, 0x5f, 0x4e, 0x4f, 0x54, 0x5f, 0x46, + 0x4f, 0x55, 0x4e, 0x44, 0x10, 0x01, 0x12, 0x17, 0x0a, 0x13, 0x45, 0x52, 0x52, 0x5f, 0x43, 0x4f, + 0x4e, 0x54, 0x45, 0x4e, 0x54, 0x5f, 0x4d, 0x49, 0x53, 0x53, 0x49, 0x4e, 0x47, 0x10, 0x02, 0x42, + 0x2a, 0x0a, 0x0e, 0x63, 0x6f, 0x6d, 0x2e, 0x65, 0x67, 0x6f, 0x2e, 0x62, 0x69, 0x7a, 0x2e, 0x76, + 0x31, 0x42, 0x08, 0x42, 0x69, 0x7a, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x0c, 0x62, + 0x69, 0x7a, 0x2f, 0x76, 0x31, 0x3b, 0x62, 0x69, 0x7a, 0x76, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x33, } var ( - file_myerr_proto_rawDescOnce sync.Once - file_myerr_proto_rawDescData = file_myerr_proto_rawDesc + file_errors_proto_rawDescOnce sync.Once + file_errors_proto_rawDescData = file_errors_proto_rawDesc ) -func file_myerr_proto_rawDescGZIP() []byte { - file_myerr_proto_rawDescOnce.Do(func() { - file_myerr_proto_rawDescData = protoimpl.X.CompressGZIP(file_myerr_proto_rawDescData) +func file_errors_proto_rawDescGZIP() []byte { + file_errors_proto_rawDescOnce.Do(func() { + file_errors_proto_rawDescData = protoimpl.X.CompressGZIP(file_errors_proto_rawDescData) }) - return file_myerr_proto_rawDescData + return file_errors_proto_rawDescData } -var file_myerr_proto_enumTypes = make([]protoimpl.EnumInfo, 1) -var file_myerr_proto_goTypes = []interface{}{ +var file_errors_proto_enumTypes = make([]protoimpl.EnumInfo, 1) +var file_errors_proto_goTypes = []interface{}{ (Err)(0), // 0: biz.v1.Err } -var file_myerr_proto_depIdxs = []int32{ +var file_errors_proto_depIdxs = []int32{ 0, // [0:0] is the sub-list for method output_type 0, // [0:0] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name @@ -115,27 +115,27 @@ var file_myerr_proto_depIdxs = []int32{ 0, // [0:0] is the sub-list for field type_name } -func init() { file_myerr_proto_init() } -func file_myerr_proto_init() { - if File_myerr_proto != nil { +func init() { file_errors_proto_init() } +func file_errors_proto_init() { + if File_errors_proto != nil { return } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), - RawDescriptor: file_myerr_proto_rawDesc, + RawDescriptor: file_errors_proto_rawDesc, NumEnums: 1, NumMessages: 0, NumExtensions: 0, NumServices: 0, }, - GoTypes: file_myerr_proto_goTypes, - DependencyIndexes: file_myerr_proto_depIdxs, - EnumInfos: file_myerr_proto_enumTypes, + GoTypes: file_errors_proto_goTypes, + DependencyIndexes: file_errors_proto_depIdxs, + EnumInfos: file_errors_proto_enumTypes, }.Build() - File_myerr_proto = out.File - file_myerr_proto_rawDesc = nil - file_myerr_proto_goTypes = nil - file_myerr_proto_depIdxs = nil + File_errors_proto = out.File + file_errors_proto_rawDesc = nil + file_errors_proto_goTypes = nil + file_errors_proto_depIdxs = nil } diff --git a/examples/grpc/errors/myerr.proto b/examples/grpc/errors/errors.proto similarity index 100% rename from examples/grpc/errors/myerr.proto rename to examples/grpc/errors/errors.proto diff --git a/examples/grpc/errors/myerr_errors.pb.go b/examples/grpc/errors/errors_errors.pb.go similarity index 100% rename from examples/grpc/errors/myerr_errors.pb.go rename to examples/grpc/errors/errors_errors.pb.go