From cf009293e58c41406fdb62e2f31d907da421edb6 Mon Sep 17 00:00:00 2001 From: HominSu Date: Sun, 9 Apr 2023 10:59:57 +0800 Subject: [PATCH] feat(cmd/protoc-gen-go-errors): support optional args generation 1. updated Details message to use the Format message instead of a plain string. 2. added the with_args field to the Format message, which controls whether code with arguments should be generated. Signed-off-by: HominSu --- cmd/protoc-gen-go-errors/errors.go | 18 ++- cmd/protoc-gen-go-errors/errors/errors.pb.go | 156 ++++++++++++++----- cmd/protoc-gen-go-errors/errors/errors.proto | 7 +- cmd/protoc-gen-go-errors/template.go | 7 +- third_party/errors/errors.proto | 7 +- 5 files changed, 149 insertions(+), 46 deletions(-) diff --git a/cmd/protoc-gen-go-errors/errors.go b/cmd/protoc-gen-go-errors/errors.go index 883e236ca..48e3393c2 100644 --- a/cmd/protoc-gen-go-errors/errors.go +++ b/cmd/protoc-gen-go-errors/errors.go @@ -116,13 +116,27 @@ func genErrorsReason(_ *protogen.Plugin, _ *protogen.File, g *protogen.Generated } func getDetails(d *errors.Details) *errorDetails { + var format *errorFormat + hasFormat := d.Format != nil + if hasFormat { + format = getFormat(d.Format) + } + details := &errorDetails{ - Format: d.GetFormat(), - HasFormat: d.Format != nil, + Format: format, + HasFormat: hasFormat, } return details } +func getFormat(f *errors.Format) *errorFormat { + format := &errorFormat{ + Str: f.Str, + WithArgs: f.WithArgs, + } + return format +} + func case2Camel(name string) string { if !strings.Contains(name, "_") { if name == strings.ToUpper(name) { diff --git a/cmd/protoc-gen-go-errors/errors/errors.pb.go b/cmd/protoc-gen-go-errors/errors/errors.pb.go index b95599c61..51132ed6a 100644 --- a/cmd/protoc-gen-go-errors/errors/errors.pb.go +++ b/cmd/protoc-gen-go-errors/errors/errors.pb.go @@ -97,7 +97,7 @@ type Details struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Format *string `protobuf:"bytes,1,opt,name=format,proto3,oneof" json:"format,omitempty"` + Format *Format `protobuf:"bytes,1,opt,name=format,proto3,oneof" json:"format,omitempty"` } func (x *Details) Reset() { @@ -132,13 +132,68 @@ func (*Details) Descriptor() ([]byte, []int) { return file_errors_proto_rawDescGZIP(), []int{1} } -func (x *Details) GetFormat() string { - if x != nil && x.Format != nil { - return *x.Format +func (x *Details) GetFormat() *Format { + if x != nil { + return x.Format + } + return nil +} + +type Format struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Str string `protobuf:"bytes,1,opt,name=str,proto3" json:"str,omitempty"` + WithArgs bool `protobuf:"varint,2,opt,name=with_args,json=withArgs,proto3" json:"with_args,omitempty"` +} + +func (x *Format) Reset() { + *x = Format{} + if protoimpl.UnsafeEnabled { + mi := &file_errors_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Format) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Format) ProtoMessage() {} + +func (x *Format) ProtoReflect() protoreflect.Message { + mi := &file_errors_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Format.ProtoReflect.Descriptor instead. +func (*Format) Descriptor() ([]byte, []int) { + return file_errors_proto_rawDescGZIP(), []int{2} +} + +func (x *Format) GetStr() string { + if x != nil { + return x.Str } return "" } +func (x *Format) GetWithArgs() bool { + if x != nil { + return x.WithArgs + } + return false +} + var file_errors_proto_extTypes = []protoimpl.ExtensionInfo{ { ExtendedType: (*descriptorpb.EnumOptions)(nil), @@ -198,29 +253,34 @@ var file_errors_proto_rawDesc = []byte{ 0x61, 0x1a, 0x3b, 0x0a, 0x0d, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0x31, - 0x0a, 0x07, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x73, 0x12, 0x1b, 0x0a, 0x06, 0x66, 0x6f, 0x72, - 0x6d, 0x61, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x06, 0x66, 0x6f, 0x72, + 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x22, 0x41, + 0x0a, 0x07, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x73, 0x12, 0x2b, 0x0a, 0x06, 0x66, 0x6f, 0x72, + 0x6d, 0x61, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x65, 0x72, 0x72, 0x6f, + 0x72, 0x73, 0x2e, 0x46, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x48, 0x00, 0x52, 0x06, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x88, 0x01, 0x01, 0x42, 0x09, 0x0a, 0x07, 0x5f, 0x66, 0x6f, 0x72, 0x6d, 0x61, - 0x74, 0x3a, 0x40, 0x0a, 0x0c, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x5f, 0x63, 0x6f, 0x64, - 0x65, 0x12, 0x1c, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, - 0xd4, 0x08, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0b, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x43, - 0x6f, 0x64, 0x65, 0x3a, 0x36, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x21, 0x2e, 0x67, 0x6f, - 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6e, - 0x75, 0x6d, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xd5, - 0x08, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x3a, 0x50, 0x0a, 0x07, 0x64, - 0x65, 0x74, 0x61, 0x69, 0x6c, 0x73, 0x12, 0x21, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x56, 0x61, 0x6c, - 0x75, 0x65, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xd6, 0x08, 0x20, 0x01, 0x28, 0x0b, - 0x32, 0x0f, 0x2e, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x73, 0x2e, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, - 0x73, 0x52, 0x07, 0x64, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x73, 0x88, 0x01, 0x01, 0x42, 0x59, 0x0a, - 0x18, 0x63, 0x6f, 0x6d, 0x2e, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x6b, 0x72, 0x61, 0x74, - 0x6f, 0x73, 0x2e, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x73, 0x50, 0x01, 0x5a, 0x2c, 0x67, 0x69, 0x74, - 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x6f, 0x2d, 0x6b, 0x72, 0x61, 0x74, 0x6f, - 0x73, 0x2f, 0x6b, 0x72, 0x61, 0x74, 0x6f, 0x73, 0x2f, 0x76, 0x32, 0x2f, 0x65, 0x72, 0x72, 0x6f, - 0x72, 0x73, 0x3b, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x73, 0xa2, 0x02, 0x0c, 0x4b, 0x72, 0x61, 0x74, - 0x6f, 0x73, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x74, 0x22, 0x37, 0x0a, 0x06, 0x46, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x73, + 0x74, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x73, 0x74, 0x72, 0x12, 0x1b, 0x0a, + 0x09, 0x77, 0x69, 0x74, 0x68, 0x5f, 0x61, 0x72, 0x67, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, + 0x52, 0x08, 0x77, 0x69, 0x74, 0x68, 0x41, 0x72, 0x67, 0x73, 0x3a, 0x40, 0x0a, 0x0c, 0x64, 0x65, + 0x66, 0x61, 0x75, 0x6c, 0x74, 0x5f, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x1c, 0x2e, 0x67, 0x6f, 0x6f, + 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6e, 0x75, + 0x6d, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xd4, 0x08, 0x20, 0x01, 0x28, 0x05, 0x52, + 0x0b, 0x64, 0x65, 0x66, 0x61, 0x75, 0x6c, 0x74, 0x43, 0x6f, 0x64, 0x65, 0x3a, 0x36, 0x0a, 0x04, + 0x63, 0x6f, 0x64, 0x65, 0x12, 0x21, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x56, 0x61, 0x6c, 0x75, 0x65, + 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xd5, 0x08, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, + 0x63, 0x6f, 0x64, 0x65, 0x3a, 0x50, 0x0a, 0x07, 0x64, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x73, 0x12, + 0x21, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, + 0x66, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x4f, 0x70, 0x74, 0x69, 0x6f, + 0x6e, 0x73, 0x18, 0xd6, 0x08, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x65, 0x72, 0x72, 0x6f, + 0x72, 0x73, 0x2e, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x73, 0x52, 0x07, 0x64, 0x65, 0x74, 0x61, + 0x69, 0x6c, 0x73, 0x88, 0x01, 0x01, 0x42, 0x59, 0x0a, 0x18, 0x63, 0x6f, 0x6d, 0x2e, 0x67, 0x69, + 0x74, 0x68, 0x75, 0x62, 0x2e, 0x6b, 0x72, 0x61, 0x74, 0x6f, 0x73, 0x2e, 0x65, 0x72, 0x72, 0x6f, + 0x72, 0x73, 0x50, 0x01, 0x5a, 0x2c, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, + 0x2f, 0x67, 0x6f, 0x2d, 0x6b, 0x72, 0x61, 0x74, 0x6f, 0x73, 0x2f, 0x6b, 0x72, 0x61, 0x74, 0x6f, + 0x73, 0x2f, 0x76, 0x32, 0x2f, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x73, 0x3b, 0x65, 0x72, 0x72, 0x6f, + 0x72, 0x73, 0xa2, 0x02, 0x0c, 0x4b, 0x72, 0x61, 0x74, 0x6f, 0x73, 0x45, 0x72, 0x72, 0x6f, 0x72, + 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -235,25 +295,27 @@ func file_errors_proto_rawDescGZIP() []byte { return file_errors_proto_rawDescData } -var file_errors_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_errors_proto_msgTypes = make([]protoimpl.MessageInfo, 4) var file_errors_proto_goTypes = []interface{}{ (*Error)(nil), // 0: errors.Error (*Details)(nil), // 1: errors.Details - nil, // 2: errors.Error.MetadataEntry - (*descriptorpb.EnumOptions)(nil), // 3: google.protobuf.EnumOptions - (*descriptorpb.EnumValueOptions)(nil), // 4: google.protobuf.EnumValueOptions + (*Format)(nil), // 2: errors.Format + nil, // 3: errors.Error.MetadataEntry + (*descriptorpb.EnumOptions)(nil), // 4: google.protobuf.EnumOptions + (*descriptorpb.EnumValueOptions)(nil), // 5: google.protobuf.EnumValueOptions } var file_errors_proto_depIdxs = []int32{ - 2, // 0: errors.Error.metadata:type_name -> errors.Error.MetadataEntry - 3, // 1: errors.default_code:extendee -> google.protobuf.EnumOptions - 4, // 2: errors.code:extendee -> google.protobuf.EnumValueOptions - 4, // 3: errors.details:extendee -> google.protobuf.EnumValueOptions - 1, // 4: errors.details:type_name -> errors.Details - 5, // [5:5] is the sub-list for method output_type - 5, // [5:5] is the sub-list for method input_type - 4, // [4:5] is the sub-list for extension type_name - 1, // [1:4] is the sub-list for extension extendee - 0, // [0:1] is the sub-list for field type_name + 3, // 0: errors.Error.metadata:type_name -> errors.Error.MetadataEntry + 2, // 1: errors.Details.format:type_name -> errors.Format + 4, // 2: errors.default_code:extendee -> google.protobuf.EnumOptions + 5, // 3: errors.code:extendee -> google.protobuf.EnumValueOptions + 5, // 4: errors.details:extendee -> google.protobuf.EnumValueOptions + 1, // 5: errors.details:type_name -> errors.Details + 6, // [6:6] is the sub-list for method output_type + 6, // [6:6] is the sub-list for method input_type + 5, // [5:6] is the sub-list for extension type_name + 2, // [2:5] is the sub-list for extension extendee + 0, // [0:2] is the sub-list for field type_name } func init() { file_errors_proto_init() } @@ -286,6 +348,18 @@ func file_errors_proto_init() { return nil } } + file_errors_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Format); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } } file_errors_proto_msgTypes[1].OneofWrappers = []interface{}{} type x struct{} @@ -294,7 +368,7 @@ func file_errors_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_errors_proto_rawDesc, NumEnums: 0, - NumMessages: 3, + NumMessages: 4, NumExtensions: 3, NumServices: 0, }, diff --git a/cmd/protoc-gen-go-errors/errors/errors.proto b/cmd/protoc-gen-go-errors/errors/errors.proto index 9efa5548b..067dfc6ce 100644 --- a/cmd/protoc-gen-go-errors/errors/errors.proto +++ b/cmd/protoc-gen-go-errors/errors/errors.proto @@ -26,5 +26,10 @@ extend google.protobuf.EnumValueOptions { } message Details { - optional string format = 1; + optional Format format = 1; +} + +message Format { + string str = 1; + bool with_args = 2; } diff --git a/cmd/protoc-gen-go-errors/template.go b/cmd/protoc-gen-go-errors/template.go index a2e6340a0..70aa940af 100644 --- a/cmd/protoc-gen-go-errors/template.go +++ b/cmd/protoc-gen-go-errors/template.go @@ -21,10 +21,15 @@ type errorInfo struct { } type errorDetails struct { - Format string + Format *errorFormat HasFormat bool } +type errorFormat struct { + Str string + WithArgs bool +} + type errorWrapper struct { Errors []*errorInfo } diff --git a/third_party/errors/errors.proto b/third_party/errors/errors.proto index 49bc4bc9f..ed2bca7b0 100644 --- a/third_party/errors/errors.proto +++ b/third_party/errors/errors.proto @@ -19,5 +19,10 @@ extend google.protobuf.EnumValueOptions { } message Details { - optional string format = 1; + optional Format format = 1; +} + +message Format { + string str = 1; + bool with_args = 2; }