From 81c648b7ba56d259a6a34ee1c41a2b6909a8e6f9 Mon Sep 17 00:00:00 2001 From: thinkgo Date: Tue, 26 Dec 2023 15:05:18 +0800 Subject: [PATCH] feat: add annotation --- cmd/protoc-gen-saber-enum/enum.tpl | 4 +- cmd/protoc-gen-saber-enum/main.go | 2 +- cmd/protoc-gen-saber-seaql/main.go | 2 +- example/enums/mapper.example.pb.go | 21 ++- example/enums/nested.mapping.pb.go | 130 +++++++++++++----- example/enums/nested.pb.go | 180 +++++++++++++++++++++---- example/enums/nested.proto | 25 ++++ example/enums/non_nested.mapping.pb.go | 68 +++++++--- example/enums/non_nested.pb.go | 87 +++++++++++- example/enums/non_nested.proto | 20 +++ example/seaql/create_table.sql | 8 +- example/seaql/dict.sql | 8 +- example/seaql/dict_item.sql | 4 +- example/seaql/enums.proto | 6 +- example/seaql/seaql.proto | 64 +++++---- internal/protoenum/enum.go | 91 ++++++++----- internal/protoutil/annotation.go | 55 ++++++++ internal/protoutil/annotation_test.go | 134 ++++++++++++++++++ internal/protoutil/comment.go | 63 +++++++++ 19 files changed, 804 insertions(+), 168 deletions(-) create mode 100644 internal/protoutil/annotation.go create mode 100644 internal/protoutil/annotation_test.go create mode 100644 internal/protoutil/comment.go diff --git a/cmd/protoc-gen-saber-enum/enum.tpl b/cmd/protoc-gen-saber-enum/enum.tpl index 3318bf1..ab1021a 100644 --- a/cmd/protoc-gen-saber-enum/enum.tpl +++ b/cmd/protoc-gen-saber-enum/enum.tpl @@ -47,7 +47,7 @@ func (x {{$enumName}}) IntoNumberString() string { } // MappingDescriptor mapping description. -// {{$e.Comment}} +{{$e.Comment}} func (x {{$enumName}}) MappingDescriptor() string { return __{{$enumName}}Mapping_Desc[x] } @@ -58,7 +58,7 @@ func ({{$enumName}}) EnumCount() int { } // Get{{$enumName}}Value get mapping value -// {{$e.Comment}} +{{$e.Comment}} func Get{{$enumName}}Value(s string) int { return int(__{{$enumName}}Mapping_Value[s]) } diff --git a/cmd/protoc-gen-saber-enum/main.go b/cmd/protoc-gen-saber-enum/main.go index 1ddab85..3693b2b 100644 --- a/cmd/protoc-gen-saber-enum/main.go +++ b/cmd/protoc-gen-saber-enum/main.go @@ -7,7 +7,7 @@ import ( "google.golang.org/protobuf/compiler/protogen" ) -const version = "v0.0.8" +const version = "v1.0.0" var args = &struct { ShowVersion bool // 显示版本 diff --git a/cmd/protoc-gen-saber-seaql/main.go b/cmd/protoc-gen-saber-seaql/main.go index 9298f1b..68f2e4c 100644 --- a/cmd/protoc-gen-saber-seaql/main.go +++ b/cmd/protoc-gen-saber-seaql/main.go @@ -7,7 +7,7 @@ import ( "google.golang.org/protobuf/compiler/protogen" ) -const version = "v0.0.5" +const version = "v1.0.0" var args = &struct { ShowVersion bool // 显示当前版本 diff --git a/example/enums/mapper.example.pb.go b/example/enums/mapper.example.pb.go index e3a430d..6c86c39 100644 --- a/example/enums/mapper.example.pb.go +++ b/example/enums/mapper.example.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-saber-enum. DO NOT EDIT. // versions: -// - protoc-gen-saber-enum v0.0.6 +// - protoc-gen-saber-enum v1.0.0 // - protoc v4.24.0 // source: nested.proto,non_nested.proto @@ -16,10 +16,22 @@ func EnumComment() []map[int32]string { int32(Nested_Status_Right): "nested4", int32(Nested_Status_End): "end", }, + { + int32(Nested_StatusAnnote_Unspecified): "\"unspecified\", aaaa", + int32(Nested_StatusAnnote_Up): "nested1", + int32(Nested_StatusAnnote_Down): "nested2", + int32(Nested_StatusAnnote_Left): "nested3", + int32(Nested_StatusAnnote_Right): "", + int32(Nested_StatusAnnote_End): "end", + }, { int32(Nested_Nested1_Type_Disable): "禁用", int32(Nested_Nested1_Type_Enable): "启用", }, + { + int32(Nested_Nested1_TypeAnnote_Disable): "禁用", + int32(Nested_Nested1_TypeAnnote_Enable): "启用", + }, { int32(NonNestedStatus_NonNestedStatus_Unspecified): "未定义", int32(NonNestedStatus_NonNestedStatus_Up): "打开", @@ -27,5 +39,12 @@ func EnumComment() []map[int32]string { int32(NonNestedStatus_NonNestedStatus_Left): "左", int32(NonNestedStatus_NonNestedStatus_Right): "右", }, + { + int32(NonNestedStatusAnnote_NonNestedStatusAnnote_Unspecified): "未定义", + int32(NonNestedStatusAnnote_NonNestedStatusAnnote_Up): "打开", + int32(NonNestedStatusAnnote_NonNestedStatusAnnote_Down): "关闭", + int32(NonNestedStatusAnnote_NonNestedStatusAnnote_Left): "左", + int32(NonNestedStatusAnnote_NonNestedStatusAnnote_Right): "右", + }, } } diff --git a/example/enums/nested.mapping.pb.go b/example/enums/nested.mapping.pb.go index a466c72..b587396 100644 --- a/example/enums/nested.mapping.pb.go +++ b/example/enums/nested.mapping.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-saber-enum. DO NOT EDIT. // versions: -// - protoc-gen-saber-enum v0.0.6 +// - protoc-gen-saber-enum v1.0.0 // - protoc v4.24.0 // source: nested.proto @@ -41,7 +41,8 @@ func (x Nested_Status) IntoNumberString() string { } // MappingDescriptor mapping description. -// Status 状态值, [0:\"unspecified\", aaaa,1:nested1,2:nested2,3:nested3,4:nested4,999:end] +// Status 状态值 +// [0:\"unspecified\", aaaa,1:nested1,2:nested2,3:nested3,4:nested4,999:end] func (x Nested_Status) MappingDescriptor() string { return __Nested_StatusMapping_Desc[x] } @@ -51,40 +52,62 @@ func (Nested_Status) EnumCount() int { return 6 } -// Value_Status_Unspecified \"unspecified\", aaaa -func (Nested_Status) Value_Unspecified() Nested_Status { - return Nested_Status_Unspecified +// GetNested_StatusValue get mapping value +// Status 状态值 +// [0:\"unspecified\", aaaa,1:nested1,2:nested2,3:nested3,4:nested4,999:end] +func GetNested_StatusValue(s string) int { + return int(__Nested_StatusMapping_Value[s]) } -// Value_Status_Up nested1 -func (Nested_Status) Value_Up() Nested_Status { - return Nested_Status_Up -} +// Enum value mapping for Nested_StatusAnnote. +var ( + __Nested_StatusAnnoteMapping_Desc = map[Nested_StatusAnnote]string{ + 0: "\"unspecified\", aaaa", + 1: "nested1", + 2: "nested2", + 3: "nested3", + 4: "", + 999: "end", + } + __Nested_StatusAnnoteMapping_Value = map[string]Nested_StatusAnnote{ + "\"unspecified\", aaaa": 0, + "nested1": 1, + "nested2": 2, + "nested3": 3, + "": 4, + "end": 999, + } +) -// Value_Status_Down nested2 -func (Nested_Status) Value_Down() Nested_Status { - return Nested_Status_Down +// IntoNumber returns the enum value as an integer. +func (x Nested_StatusAnnote) IntoNumber() int32 { + return int32(x) } -// Value_Status_Left nested3 -func (Nested_Status) Value_Left() Nested_Status { - return Nested_Status_Left +// IntoNumberString returns the enum value as an integer string. +func (x Nested_StatusAnnote) IntoNumberString() string { + return strconv.FormatInt(int64(x), 10) } -// Value_Status_Right nested4 -func (Nested_Status) Value_Right() Nested_Status { - return Nested_Status_Right +// MappingDescriptor mapping description. +// StatusAnnote 状态值 +// #[Enum] +// [0:\"unspecified\", aaaa,1:nested1,2:nested2,3:nested3,4:,999:end] +func (x Nested_StatusAnnote) MappingDescriptor() string { + return __Nested_StatusAnnoteMapping_Desc[x] } -// Value_Status_End end -func (Nested_Status) Value_End() Nested_Status { - return Nested_Status_End +// EnumCount the number of enum values. +func (Nested_StatusAnnote) EnumCount() int { + return 6 } -// GetNested_StatusValue get mapping value -// Status 状态值, [0:\"unspecified\", aaaa,1:nested1,2:nested2,3:nested3,4:nested4,999:end] -func GetNested_StatusValue(s string) int { - return int(__Nested_StatusMapping_Value[s]) +// GetNested_StatusAnnoteValue get mapping value +// StatusAnnote 状态值 +// #[Enum] +// [0:\"unspecified\", aaaa,1:nested1,2:nested2,3:nested3,4:,999:end] +func GetNested_StatusAnnoteValue(s string) int { + return int(__Nested_StatusAnnoteMapping_Value[s]) } // Enum value mapping for Nested_Nested1_Type. @@ -110,7 +133,8 @@ func (x Nested_Nested1_Type) IntoNumberString() string { } // MappingDescriptor mapping description. -// Type 类型, [0:禁用,1:启用] +// Type 类型 +// [0:禁用,1:启用] func (x Nested_Nested1_Type) MappingDescriptor() string { return __Nested_Nested1_TypeMapping_Desc[x] } @@ -120,18 +144,52 @@ func (Nested_Nested1_Type) EnumCount() int { return 2 } -// Value_Type_Disable 禁用 -func (Nested_Nested1_Type) Value_Disable() Nested_Nested1_Type { - return Nested_Nested1_Type_Disable +// GetNested_Nested1_TypeValue get mapping value +// Type 类型 +// [0:禁用,1:启用] +func GetNested_Nested1_TypeValue(s string) int { + return int(__Nested_Nested1_TypeMapping_Value[s]) +} + +// Enum value mapping for Nested_Nested1_TypeAnnote. +var ( + __Nested_Nested1_TypeAnnoteMapping_Desc = map[Nested_Nested1_TypeAnnote]string{ + 0: "禁用", + 1: "启用", + } + __Nested_Nested1_TypeAnnoteMapping_Value = map[string]Nested_Nested1_TypeAnnote{ + "禁用": 0, + "启用": 1, + } +) + +// IntoNumber returns the enum value as an integer. +func (x Nested_Nested1_TypeAnnote) IntoNumber() int32 { + return int32(x) } -// Value_Type_Enable 启用 -func (Nested_Nested1_Type) Value_Enable() Nested_Nested1_Type { - return Nested_Nested1_Type_Enable +// IntoNumberString returns the enum value as an integer string. +func (x Nested_Nested1_TypeAnnote) IntoNumberString() string { + return strconv.FormatInt(int64(x), 10) } -// GetNested_Nested1_TypeValue get mapping value -// Type 类型, [0:禁用,1:启用] -func GetNested_Nested1_TypeValue(s string) int { - return int(__Nested_Nested1_TypeMapping_Value[s]) +// MappingDescriptor mapping description. +// TypeAnnote 类型 +// #[Enum] +// [0:禁用,1:启用] +func (x Nested_Nested1_TypeAnnote) MappingDescriptor() string { + return __Nested_Nested1_TypeAnnoteMapping_Desc[x] +} + +// EnumCount the number of enum values. +func (Nested_Nested1_TypeAnnote) EnumCount() int { + return 2 +} + +// GetNested_Nested1_TypeAnnoteValue get mapping value +// TypeAnnote 类型 +// #[Enum] +// [0:禁用,1:启用] +func GetNested_Nested1_TypeAnnoteValue(s string) int { + return int(__Nested_Nested1_TypeAnnoteMapping_Value[s]) } diff --git a/example/enums/nested.pb.go b/example/enums/nested.pb.go index 1428c37..963d0cb 100644 --- a/example/enums/nested.pb.go +++ b/example/enums/nested.pb.go @@ -87,6 +87,72 @@ func (Nested_Status) EnumDescriptor() ([]byte, []int) { return file_nested_proto_rawDescGZIP(), []int{0, 0} } +// StatusAnnote 状态值 +// #[Enum] +type Nested_StatusAnnote int32 + +const ( + // "unspecified" + // aaaa + Nested_StatusAnnote_Unspecified Nested_StatusAnnote = 0 + // nested1 + Nested_StatusAnnote_Up Nested_StatusAnnote = 1 + // nested2 + Nested_StatusAnnote_Down Nested_StatusAnnote = 2 + // nested3 + Nested_StatusAnnote_Left Nested_StatusAnnote = 3 + Nested_StatusAnnote_Right Nested_StatusAnnote = 4 + // end + Nested_StatusAnnote_End Nested_StatusAnnote = 999 +) + +// Enum value maps for Nested_StatusAnnote. +var ( + Nested_StatusAnnote_name = map[int32]string{ + 0: "StatusAnnote_Unspecified", + 1: "StatusAnnote_Up", + 2: "StatusAnnote_Down", + 3: "StatusAnnote_Left", + 4: "StatusAnnote_Right", + 999: "StatusAnnote_End", + } + Nested_StatusAnnote_value = map[string]int32{ + "StatusAnnote_Unspecified": 0, + "StatusAnnote_Up": 1, + "StatusAnnote_Down": 2, + "StatusAnnote_Left": 3, + "StatusAnnote_Right": 4, + "StatusAnnote_End": 999, + } +) + +func (x Nested_StatusAnnote) Enum() *Nested_StatusAnnote { + p := new(Nested_StatusAnnote) + *p = x + return p +} + +func (x Nested_StatusAnnote) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (Nested_StatusAnnote) Descriptor() protoreflect.EnumDescriptor { + return file_nested_proto_enumTypes[1].Descriptor() +} + +func (Nested_StatusAnnote) Type() protoreflect.EnumType { + return &file_nested_proto_enumTypes[1] +} + +func (x Nested_StatusAnnote) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use Nested_StatusAnnote.Descriptor instead. +func (Nested_StatusAnnote) EnumDescriptor() ([]byte, []int) { + return file_nested_proto_rawDescGZIP(), []int{0, 1} +} + // Type 类型 type Nested_Nested1_Type int32 @@ -120,11 +186,11 @@ func (x Nested_Nested1_Type) String() string { } func (Nested_Nested1_Type) Descriptor() protoreflect.EnumDescriptor { - return file_nested_proto_enumTypes[1].Descriptor() + return file_nested_proto_enumTypes[2].Descriptor() } func (Nested_Nested1_Type) Type() protoreflect.EnumType { - return &file_nested_proto_enumTypes[1] + return &file_nested_proto_enumTypes[2] } func (x Nested_Nested1_Type) Number() protoreflect.EnumNumber { @@ -136,6 +202,56 @@ func (Nested_Nested1_Type) EnumDescriptor() ([]byte, []int) { return file_nested_proto_rawDescGZIP(), []int{0, 0, 0} } +// TypeAnnote 类型 +// #[Enum] +type Nested_Nested1_TypeAnnote int32 + +const ( + // 禁用 + Nested_Nested1_TypeAnnote_Disable Nested_Nested1_TypeAnnote = 0 + // 启用 + Nested_Nested1_TypeAnnote_Enable Nested_Nested1_TypeAnnote = 1 +) + +// Enum value maps for Nested_Nested1_TypeAnnote. +var ( + Nested_Nested1_TypeAnnote_name = map[int32]string{ + 0: "TypeAnnote_Disable", + 1: "TypeAnnote_Enable", + } + Nested_Nested1_TypeAnnote_value = map[string]int32{ + "TypeAnnote_Disable": 0, + "TypeAnnote_Enable": 1, + } +) + +func (x Nested_Nested1_TypeAnnote) Enum() *Nested_Nested1_TypeAnnote { + p := new(Nested_Nested1_TypeAnnote) + *p = x + return p +} + +func (x Nested_Nested1_TypeAnnote) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (Nested_Nested1_TypeAnnote) Descriptor() protoreflect.EnumDescriptor { + return file_nested_proto_enumTypes[3].Descriptor() +} + +func (Nested_Nested1_TypeAnnote) Type() protoreflect.EnumType { + return &file_nested_proto_enumTypes[3] +} + +func (x Nested_Nested1_TypeAnnote) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use Nested_Nested1_TypeAnnote.Descriptor instead. +func (Nested_Nested1_TypeAnnote) EnumDescriptor() ([]byte, []int) { + return file_nested_proto_rawDescGZIP(), []int{0, 0, 1} +} + type Nested struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -219,25 +335,39 @@ var file_nested_proto_rawDesc = []byte{ 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x67, 0x65, 0x6e, 0x5f, 0x73, 0x61, 0x62, 0x65, 0x72, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x73, 0x2e, 0x65, 0x6e, 0x75, 0x6d, 0x73, 0x1a, 0x19, 0x65, 0x6e, 0x75, 0x6d, 0x65, 0x72, 0x61, 0x74, 0x65, 0x2f, 0x65, 0x6e, 0x75, 0x6d, 0x65, 0x72, 0x61, - 0x74, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xc4, 0x01, 0x0a, 0x06, 0x4e, 0x65, 0x73, - 0x74, 0x65, 0x64, 0x1a, 0x3c, 0x0a, 0x07, 0x4e, 0x65, 0x73, 0x74, 0x65, 0x64, 0x31, 0x22, 0x31, + 0x74, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xa2, 0x03, 0x0a, 0x06, 0x4e, 0x65, 0x73, + 0x74, 0x65, 0x64, 0x1a, 0x79, 0x0a, 0x07, 0x4e, 0x65, 0x73, 0x74, 0x65, 0x64, 0x31, 0x22, 0x31, 0x0a, 0x04, 0x54, 0x79, 0x70, 0x65, 0x12, 0x10, 0x0a, 0x0c, 0x54, 0x79, 0x70, 0x65, 0x5f, 0x44, 0x69, 0x73, 0x61, 0x62, 0x6c, 0x65, 0x10, 0x00, 0x12, 0x0f, 0x0a, 0x0b, 0x54, 0x79, 0x70, 0x65, 0x5f, 0x45, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x10, 0x01, 0x1a, 0x06, 0xc8, 0x85, 0xb7, 0xeb, 0x02, - 0x01, 0x22, 0x7c, 0x0a, 0x06, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x16, 0x0a, 0x12, 0x53, - 0x74, 0x61, 0x74, 0x75, 0x73, 0x5f, 0x55, 0x6e, 0x73, 0x70, 0x65, 0x63, 0x69, 0x66, 0x69, 0x65, - 0x64, 0x10, 0x00, 0x12, 0x0d, 0x0a, 0x09, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x5f, 0x55, 0x70, - 0x10, 0x01, 0x12, 0x0f, 0x0a, 0x0b, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x5f, 0x44, 0x6f, 0x77, - 0x6e, 0x10, 0x02, 0x12, 0x0f, 0x0a, 0x0b, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x5f, 0x4c, 0x65, - 0x66, 0x74, 0x10, 0x03, 0x12, 0x10, 0x0a, 0x0c, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x5f, 0x52, - 0x69, 0x67, 0x68, 0x74, 0x10, 0x04, 0x12, 0x0f, 0x0a, 0x0a, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, - 0x5f, 0x45, 0x6e, 0x64, 0x10, 0xe7, 0x07, 0x1a, 0x06, 0xc8, 0x85, 0xb7, 0xeb, 0x02, 0x01, 0x42, - 0x50, 0x0a, 0x1a, 0x63, 0x6e, 0x2e, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x73, 0x2d, 0x67, 0x6f, 0x2e, - 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x73, 0x2e, 0x65, 0x6e, 0x75, 0x6d, 0x42, 0x05, 0x45, - 0x6e, 0x75, 0x6d, 0x73, 0x50, 0x01, 0x5a, 0x29, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, - 0x6f, 0x6d, 0x2f, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x73, 0x2d, 0x67, 0x6f, 0x2f, 0x65, 0x78, 0x61, - 0x6d, 0x70, 0x6c, 0x65, 0x73, 0x2f, 0x65, 0x6e, 0x75, 0x6d, 0x73, 0x3b, 0x65, 0x6e, 0x75, 0x6d, - 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x01, 0x22, 0x3b, 0x0a, 0x0a, 0x54, 0x79, 0x70, 0x65, 0x41, 0x6e, 0x6e, 0x6f, 0x74, 0x65, 0x12, + 0x16, 0x0a, 0x12, 0x54, 0x79, 0x70, 0x65, 0x41, 0x6e, 0x6e, 0x6f, 0x74, 0x65, 0x5f, 0x44, 0x69, + 0x73, 0x61, 0x62, 0x6c, 0x65, 0x10, 0x00, 0x12, 0x15, 0x0a, 0x11, 0x54, 0x79, 0x70, 0x65, 0x41, + 0x6e, 0x6e, 0x6f, 0x74, 0x65, 0x5f, 0x45, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x10, 0x01, 0x22, 0x7c, + 0x0a, 0x06, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x16, 0x0a, 0x12, 0x53, 0x74, 0x61, 0x74, + 0x75, 0x73, 0x5f, 0x55, 0x6e, 0x73, 0x70, 0x65, 0x63, 0x69, 0x66, 0x69, 0x65, 0x64, 0x10, 0x00, + 0x12, 0x0d, 0x0a, 0x09, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x5f, 0x55, 0x70, 0x10, 0x01, 0x12, + 0x0f, 0x0a, 0x0b, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x5f, 0x44, 0x6f, 0x77, 0x6e, 0x10, 0x02, + 0x12, 0x0f, 0x0a, 0x0b, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x5f, 0x4c, 0x65, 0x66, 0x74, 0x10, + 0x03, 0x12, 0x10, 0x0a, 0x0c, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x5f, 0x52, 0x69, 0x67, 0x68, + 0x74, 0x10, 0x04, 0x12, 0x0f, 0x0a, 0x0a, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x5f, 0x45, 0x6e, + 0x64, 0x10, 0xe7, 0x07, 0x1a, 0x06, 0xc8, 0x85, 0xb7, 0xeb, 0x02, 0x01, 0x22, 0x9e, 0x01, 0x0a, + 0x0c, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x41, 0x6e, 0x6e, 0x6f, 0x74, 0x65, 0x12, 0x1c, 0x0a, + 0x18, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x41, 0x6e, 0x6e, 0x6f, 0x74, 0x65, 0x5f, 0x55, 0x6e, + 0x73, 0x70, 0x65, 0x63, 0x69, 0x66, 0x69, 0x65, 0x64, 0x10, 0x00, 0x12, 0x13, 0x0a, 0x0f, 0x53, + 0x74, 0x61, 0x74, 0x75, 0x73, 0x41, 0x6e, 0x6e, 0x6f, 0x74, 0x65, 0x5f, 0x55, 0x70, 0x10, 0x01, + 0x12, 0x15, 0x0a, 0x11, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x41, 0x6e, 0x6e, 0x6f, 0x74, 0x65, + 0x5f, 0x44, 0x6f, 0x77, 0x6e, 0x10, 0x02, 0x12, 0x15, 0x0a, 0x11, 0x53, 0x74, 0x61, 0x74, 0x75, + 0x73, 0x41, 0x6e, 0x6e, 0x6f, 0x74, 0x65, 0x5f, 0x4c, 0x65, 0x66, 0x74, 0x10, 0x03, 0x12, 0x16, + 0x0a, 0x12, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x41, 0x6e, 0x6e, 0x6f, 0x74, 0x65, 0x5f, 0x52, + 0x69, 0x67, 0x68, 0x74, 0x10, 0x04, 0x12, 0x15, 0x0a, 0x10, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, + 0x41, 0x6e, 0x6e, 0x6f, 0x74, 0x65, 0x5f, 0x45, 0x6e, 0x64, 0x10, 0xe7, 0x07, 0x42, 0x50, 0x0a, + 0x1a, 0x63, 0x6e, 0x2e, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x73, 0x2d, 0x67, 0x6f, 0x2e, 0x65, 0x78, + 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x73, 0x2e, 0x65, 0x6e, 0x75, 0x6d, 0x42, 0x05, 0x45, 0x6e, 0x75, + 0x6d, 0x73, 0x50, 0x01, 0x5a, 0x29, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, + 0x2f, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x73, 0x2d, 0x67, 0x6f, 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, + 0x6c, 0x65, 0x73, 0x2f, 0x65, 0x6e, 0x75, 0x6d, 0x73, 0x3b, 0x65, 0x6e, 0x75, 0x6d, 0x73, 0x62, + 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -252,13 +382,15 @@ func file_nested_proto_rawDescGZIP() []byte { return file_nested_proto_rawDescData } -var file_nested_proto_enumTypes = make([]protoimpl.EnumInfo, 2) +var file_nested_proto_enumTypes = make([]protoimpl.EnumInfo, 4) var file_nested_proto_msgTypes = make([]protoimpl.MessageInfo, 2) var file_nested_proto_goTypes = []interface{}{ - (Nested_Status)(0), // 0: protogen_saber.examples.enums.Nested.Status - (Nested_Nested1_Type)(0), // 1: protogen_saber.examples.enums.Nested.Nested1.Type - (*Nested)(nil), // 2: protogen_saber.examples.enums.Nested - (*Nested_Nested1)(nil), // 3: protogen_saber.examples.enums.Nested.Nested1 + (Nested_Status)(0), // 0: protogen_saber.examples.enums.Nested.Status + (Nested_StatusAnnote)(0), // 1: protogen_saber.examples.enums.Nested.StatusAnnote + (Nested_Nested1_Type)(0), // 2: protogen_saber.examples.enums.Nested.Nested1.Type + (Nested_Nested1_TypeAnnote)(0), // 3: protogen_saber.examples.enums.Nested.Nested1.TypeAnnote + (*Nested)(nil), // 4: protogen_saber.examples.enums.Nested + (*Nested_Nested1)(nil), // 5: protogen_saber.examples.enums.Nested.Nested1 } var file_nested_proto_depIdxs = []int32{ 0, // [0:0] is the sub-list for method output_type @@ -304,7 +436,7 @@ func file_nested_proto_init() { File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_nested_proto_rawDesc, - NumEnums: 2, + NumEnums: 4, NumMessages: 2, NumExtensions: 0, NumServices: 0, diff --git a/example/enums/nested.proto b/example/enums/nested.proto index 6791ca3..dc16f3e 100644 --- a/example/enums/nested.proto +++ b/example/enums/nested.proto @@ -7,6 +7,7 @@ option java_multiple_files = true; option java_package = "cn.things-go.examples.enum"; option java_outer_classname = "Enums"; + import "enumerate/enumerate.proto"; message Nested { @@ -26,6 +27,22 @@ message Nested { Status_Right = 4; // end Status_End = 999; + } + // StatusAnnote 状态值 + // #[Enum] + enum StatusAnnote { + // "unspecified" + // aaaa + StatusAnnote_Unspecified = 0; + // nested1 + StatusAnnote_Up = 1; + // nested2 + StatusAnnote_Down = 2; + // nested3 + StatusAnnote_Left = 3; + StatusAnnote_Right = 4; + // end + StatusAnnote_End = 999; } message Nested1 { // Type 类型 @@ -36,5 +53,13 @@ message Nested { // 启用 Type_Enable = 1; } + // TypeAnnote 类型 + // #[Enum] + enum TypeAnnote { + // 禁用 + TypeAnnote_Disable = 0; + // 启用 + TypeAnnote_Enable = 1; + } } } diff --git a/example/enums/non_nested.mapping.pb.go b/example/enums/non_nested.mapping.pb.go index 50aca5b..5283666 100644 --- a/example/enums/non_nested.mapping.pb.go +++ b/example/enums/non_nested.mapping.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-saber-enum. DO NOT EDIT. // versions: -// - protoc-gen-saber-enum v0.0.6 +// - protoc-gen-saber-enum v1.0.0 // - protoc v4.24.0 // source: non_nested.proto @@ -39,7 +39,8 @@ func (x NonNestedStatus) IntoNumberString() string { } // MappingDescriptor mapping description. -// NonNestedStatus 状态值, [0:未定义,1:打开,2:关闭,3:左,4:右] +// NonNestedStatus 状态值 +// [0:未定义,1:打开,2:关闭,3:左,4:右] func (x NonNestedStatus) MappingDescriptor() string { return __NonNestedStatusMapping_Desc[x] } @@ -49,33 +50,58 @@ func (NonNestedStatus) EnumCount() int { return 5 } -// Value_NonNestedStatus_Unspecified 未定义 -func (NonNestedStatus) Value_Unspecified() NonNestedStatus { - return NonNestedStatus_NonNestedStatus_Unspecified +// GetNonNestedStatusValue get mapping value +// NonNestedStatus 状态值 +// [0:未定义,1:打开,2:关闭,3:左,4:右] +func GetNonNestedStatusValue(s string) int { + return int(__NonNestedStatusMapping_Value[s]) } -// Value_NonNestedStatus_Up 打开 -func (NonNestedStatus) Value_Up() NonNestedStatus { - return NonNestedStatus_NonNestedStatus_Up +// Enum value mapping for NonNestedStatusAnnote. +var ( + __NonNestedStatusAnnoteMapping_Desc = map[NonNestedStatusAnnote]string{ + 0: "未定义", + 1: "打开", + 2: "关闭", + 3: "左", + 4: "右", + } + __NonNestedStatusAnnoteMapping_Value = map[string]NonNestedStatusAnnote{ + "未定义": 0, + "打开": 1, + "关闭": 2, + "左": 3, + "右": 4, + } +) + +// IntoNumber returns the enum value as an integer. +func (x NonNestedStatusAnnote) IntoNumber() int32 { + return int32(x) } -// Value_NonNestedStatus_Down 关闭 -func (NonNestedStatus) Value_Down() NonNestedStatus { - return NonNestedStatus_NonNestedStatus_Down +// IntoNumberString returns the enum value as an integer string. +func (x NonNestedStatusAnnote) IntoNumberString() string { + return strconv.FormatInt(int64(x), 10) } -// Value_NonNestedStatus_Left 左 -func (NonNestedStatus) Value_Left() NonNestedStatus { - return NonNestedStatus_NonNestedStatus_Left +// MappingDescriptor mapping description. +// NonNestedStatus 状态值 +// #[Enum] +// [0:未定义,1:打开,2:关闭,3:左,4:右] +func (x NonNestedStatusAnnote) MappingDescriptor() string { + return __NonNestedStatusAnnoteMapping_Desc[x] } -// Value_NonNestedStatus_Right 右 -func (NonNestedStatus) Value_Right() NonNestedStatus { - return NonNestedStatus_NonNestedStatus_Right +// EnumCount the number of enum values. +func (NonNestedStatusAnnote) EnumCount() int { + return 5 } -// GetNonNestedStatusValue get mapping value -// NonNestedStatus 状态值, [0:未定义,1:打开,2:关闭,3:左,4:右] -func GetNonNestedStatusValue(s string) int { - return int(__NonNestedStatusMapping_Value[s]) +// GetNonNestedStatusAnnoteValue get mapping value +// NonNestedStatus 状态值 +// #[Enum] +// [0:未定义,1:打开,2:关闭,3:左,4:右] +func GetNonNestedStatusAnnoteValue(s string) int { + return int(__NonNestedStatusAnnoteMapping_Value[s]) } diff --git a/example/enums/non_nested.pb.go b/example/enums/non_nested.pb.go index ed96508..b8cd45c 100644 --- a/example/enums/non_nested.pb.go +++ b/example/enums/non_nested.pb.go @@ -82,6 +82,72 @@ func (NonNestedStatus) EnumDescriptor() ([]byte, []int) { return file_non_nested_proto_rawDescGZIP(), []int{0} } +// NonNestedStatus 状态值 +// #[Enum] +type NonNestedStatusAnnote int32 + +const ( + // 未定义 + NonNestedStatusAnnote_NonNestedStatusAnnote_Unspecified NonNestedStatusAnnote = 0 + // 打开 + // #[Enum(mapping="打开")] + NonNestedStatusAnnote_NonNestedStatusAnnote_Up NonNestedStatusAnnote = 1 + // 关闭 + // #[Enum(mapping="关闭")] + NonNestedStatusAnnote_NonNestedStatusAnnote_Down NonNestedStatusAnnote = 2 + // 左 + // #[Enum(mapping="左")] + NonNestedStatusAnnote_NonNestedStatusAnnote_Left NonNestedStatusAnnote = 3 + // 右 + // #[Enum(mapping="右")] + NonNestedStatusAnnote_NonNestedStatusAnnote_Right NonNestedStatusAnnote = 4 +) + +// Enum value maps for NonNestedStatusAnnote. +var ( + NonNestedStatusAnnote_name = map[int32]string{ + 0: "NonNestedStatusAnnote_Unspecified", + 1: "NonNestedStatusAnnote_Up", + 2: "NonNestedStatusAnnote_Down", + 3: "NonNestedStatusAnnote_Left", + 4: "NonNestedStatusAnnote_Right", + } + NonNestedStatusAnnote_value = map[string]int32{ + "NonNestedStatusAnnote_Unspecified": 0, + "NonNestedStatusAnnote_Up": 1, + "NonNestedStatusAnnote_Down": 2, + "NonNestedStatusAnnote_Left": 3, + "NonNestedStatusAnnote_Right": 4, + } +) + +func (x NonNestedStatusAnnote) Enum() *NonNestedStatusAnnote { + p := new(NonNestedStatusAnnote) + *p = x + return p +} + +func (x NonNestedStatusAnnote) String() string { + return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x)) +} + +func (NonNestedStatusAnnote) Descriptor() protoreflect.EnumDescriptor { + return file_non_nested_proto_enumTypes[1].Descriptor() +} + +func (NonNestedStatusAnnote) Type() protoreflect.EnumType { + return &file_non_nested_proto_enumTypes[1] +} + +func (x NonNestedStatusAnnote) Number() protoreflect.EnumNumber { + return protoreflect.EnumNumber(x) +} + +// Deprecated: Use NonNestedStatusAnnote.Descriptor instead. +func (NonNestedStatusAnnote) EnumDescriptor() ([]byte, []int) { + return file_non_nested_proto_rawDescGZIP(), []int{1} +} + var File_non_nested_proto protoreflect.FileDescriptor var file_non_nested_proto_rawDesc = []byte{ @@ -102,7 +168,19 @@ var file_non_nested_proto_rawDesc = []byte{ 0x1a, 0x09, 0xea, 0x8b, 0xb7, 0xeb, 0x02, 0x03, 0xe5, 0xb7, 0xa6, 0x12, 0x24, 0x0a, 0x15, 0x4e, 0x6f, 0x6e, 0x4e, 0x65, 0x73, 0x74, 0x65, 0x64, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x5f, 0x52, 0x69, 0x67, 0x68, 0x74, 0x10, 0x04, 0x1a, 0x09, 0xea, 0x8b, 0xb7, 0xeb, 0x02, 0x03, 0xe5, 0x8f, - 0xb3, 0x1a, 0x06, 0xc8, 0x85, 0xb7, 0xeb, 0x02, 0x01, 0x42, 0x50, 0x0a, 0x1a, 0x63, 0x6e, 0x2e, + 0xb3, 0x1a, 0x06, 0xc8, 0x85, 0xb7, 0xeb, 0x02, 0x01, 0x2a, 0xbd, 0x01, 0x0a, 0x15, 0x4e, 0x6f, + 0x6e, 0x4e, 0x65, 0x73, 0x74, 0x65, 0x64, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x41, 0x6e, 0x6e, + 0x6f, 0x74, 0x65, 0x12, 0x25, 0x0a, 0x21, 0x4e, 0x6f, 0x6e, 0x4e, 0x65, 0x73, 0x74, 0x65, 0x64, + 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x41, 0x6e, 0x6e, 0x6f, 0x74, 0x65, 0x5f, 0x55, 0x6e, 0x73, + 0x70, 0x65, 0x63, 0x69, 0x66, 0x69, 0x65, 0x64, 0x10, 0x00, 0x12, 0x1c, 0x0a, 0x18, 0x4e, 0x6f, + 0x6e, 0x4e, 0x65, 0x73, 0x74, 0x65, 0x64, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x41, 0x6e, 0x6e, + 0x6f, 0x74, 0x65, 0x5f, 0x55, 0x70, 0x10, 0x01, 0x12, 0x1e, 0x0a, 0x1a, 0x4e, 0x6f, 0x6e, 0x4e, + 0x65, 0x73, 0x74, 0x65, 0x64, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x41, 0x6e, 0x6e, 0x6f, 0x74, + 0x65, 0x5f, 0x44, 0x6f, 0x77, 0x6e, 0x10, 0x02, 0x12, 0x1e, 0x0a, 0x1a, 0x4e, 0x6f, 0x6e, 0x4e, + 0x65, 0x73, 0x74, 0x65, 0x64, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x41, 0x6e, 0x6e, 0x6f, 0x74, + 0x65, 0x5f, 0x4c, 0x65, 0x66, 0x74, 0x10, 0x03, 0x12, 0x1f, 0x0a, 0x1b, 0x4e, 0x6f, 0x6e, 0x4e, + 0x65, 0x73, 0x74, 0x65, 0x64, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x41, 0x6e, 0x6e, 0x6f, 0x74, + 0x65, 0x5f, 0x52, 0x69, 0x67, 0x68, 0x74, 0x10, 0x04, 0x42, 0x50, 0x0a, 0x1a, 0x63, 0x6e, 0x2e, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x73, 0x2d, 0x67, 0x6f, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x73, 0x2e, 0x65, 0x6e, 0x75, 0x6d, 0x42, 0x05, 0x45, 0x6e, 0x75, 0x6d, 0x73, 0x50, 0x01, 0x5a, 0x29, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x74, 0x68, 0x69, @@ -123,9 +201,10 @@ func file_non_nested_proto_rawDescGZIP() []byte { return file_non_nested_proto_rawDescData } -var file_non_nested_proto_enumTypes = make([]protoimpl.EnumInfo, 1) +var file_non_nested_proto_enumTypes = make([]protoimpl.EnumInfo, 2) var file_non_nested_proto_goTypes = []interface{}{ - (NonNestedStatus)(0), // 0: protogen_saber.enums.NonNestedStatus + (NonNestedStatus)(0), // 0: protogen_saber.enums.NonNestedStatus + (NonNestedStatusAnnote)(0), // 1: protogen_saber.enums.NonNestedStatusAnnote } var file_non_nested_proto_depIdxs = []int32{ 0, // [0:0] is the sub-list for method output_type @@ -145,7 +224,7 @@ func file_non_nested_proto_init() { File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_non_nested_proto_rawDesc, - NumEnums: 1, + NumEnums: 2, NumMessages: 0, NumExtensions: 0, NumServices: 0, diff --git a/example/enums/non_nested.proto b/example/enums/non_nested.proto index b870db6..fe5634c 100644 --- a/example/enums/non_nested.proto +++ b/example/enums/non_nested.proto @@ -24,3 +24,23 @@ enum NonNestedStatus { // 右 NonNestedStatus_Right = 4 [(things_go.enumerate.mapping) = "右"]; } + +// NonNestedStatus 状态值 +// #[Enum] +enum NonNestedStatusAnnote { + // 未定义 + NonNestedStatusAnnote_Unspecified = 0; + // 打开 + // #[Enum(mapping="打开")] + NonNestedStatusAnnote_Up = 1; + // 关闭 + // #[Enum(mapping="关闭")] + NonNestedStatusAnnote_Down = 2; + // 左 + // #[Enum(mapping="左")] + NonNestedStatusAnnote_Left = 3; + // 右 + // #[Enum(mapping="右")] + NonNestedStatusAnnote_Right = 4; +} + diff --git a/example/seaql/create_table.sql b/example/seaql/create_table.sql index 65b67b9..3a2f843 100644 --- a/example/seaql/create_table.sql +++ b/example/seaql/create_table.sql @@ -1,10 +1,10 @@ -- Code generated by protoc-gen-saber-seaql. DO NOT EDIT. -- versions: --- - protoc-gen-saber-seaql v0.0.4 --- - protoc v4.22.2 +-- - protoc-gen-saber-seaql v0.0.5 +-- - protoc v4.24.0 -- source: seaql.proto --- 字典表 +-- 字典表 aaa CREATE TABLE `dict` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '系统序号', @@ -14,7 +14,7 @@ CREATE TABLE `created_at` datetime NOT NULL COMMENT '创建时间', `updated_at` datetime NOT NULL COMMENT '更新时间', PRIMARY KEY (`id`) - ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '字典表'; + ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '字典表 aaa'; -- 字典项表 CREATE TABLE diff --git a/example/seaql/dict.sql b/example/seaql/dict.sql index ba460dd..6ed4880 100644 --- a/example/seaql/dict.sql +++ b/example/seaql/dict.sql @@ -1,10 +1,10 @@ -- Code generated by protoc-gen-saber-seaql. DO NOT EDIT. -- versions: --- - protoc-gen-saber-seaql v0.0.4 --- - protoc v4.22.2 +-- - protoc-gen-saber-seaql v0.0.5 +-- - protoc v4.24.0 -- source: seaql.proto --- 字典表 +-- 字典表 aaa CREATE TABLE `dict` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '系统序号', @@ -14,4 +14,4 @@ CREATE TABLE `created_at` datetime NOT NULL COMMENT '创建时间', `updated_at` datetime NOT NULL COMMENT '更新时间', PRIMARY KEY (`id`) - ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '字典表'; + ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '字典表 aaa'; diff --git a/example/seaql/dict_item.sql b/example/seaql/dict_item.sql index ab5fb33..35248cb 100644 --- a/example/seaql/dict_item.sql +++ b/example/seaql/dict_item.sql @@ -1,7 +1,7 @@ -- Code generated by protoc-gen-saber-seaql. DO NOT EDIT. -- versions: --- - protoc-gen-saber-seaql v0.0.4 --- - protoc v4.22.2 +-- - protoc-gen-saber-seaql v0.0.5 +-- - protoc v4.24.0 -- source: seaql.proto -- 字典项表 diff --git a/example/seaql/enums.proto b/example/seaql/enums.proto index 89c7363..290547f 100644 --- a/example/seaql/enums.proto +++ b/example/seaql/enums.proto @@ -7,12 +7,10 @@ option java_multiple_files = true; option java_package = "cn.things-go.examples.Seaql"; option java_outer_classname = "Seaql"; -import "enumerate/enumerate.proto"; - message Infra { // Status 状态值 + // #[enum] enum Status { - option (things_go.enumerate.enabled) = true; // unspecified STATUS_UNSPECIFIED = 0; // nested1 @@ -25,8 +23,8 @@ message Infra { STATUS_RIGHT = 4; } message Nested1 { + // #[enum] enum Type { - option (things_go.enumerate.enabled) = true; // 禁用 TYPE_DISABLE = 0; // 启用 diff --git a/example/seaql/seaql.proto b/example/seaql/seaql.proto index fa6293e..6599856 100644 --- a/example/seaql/seaql.proto +++ b/example/seaql/seaql.proto @@ -7,32 +7,32 @@ option java_multiple_files = true; option java_package = "cn.things-go.examples.Seaql"; option java_outer_classname = "Seaql"; -import "seaql/seaql.proto"; import "enums.proto"; // Dict 字典表 +// #[seaql(name="dict")] +// #[seaql(engine="InnoDB")] +// #[seaql(charset="utf8mb4")] +// #[seaql(index="PRIMARY KEY (`id`)")] message Dict { - option (things_go.seaql.options) = { - table_name: "dict", - engine: 'InnoDB', - charset: "utf8mb4", - index: [ - 'PRIMARY KEY (`id`)' - ]; -}; - -// 系统序号 -int64 id = 1 [(things_go.seaql.field) = { type: "bigint NOT NULL AUTO_INCREMENT" }]; -// 名称 -string key = 2 [(things_go.seaql.field) = { type: "varchar(64) NOT NULL DEFAULT ''" }]; -// 名称 -string name = 3 [(things_go.seaql.field) = { type: "varchar(64) NOT NULL DEFAULT ''" }]; -// 是否锁定 -bool is_pin = 4 [(things_go.seaql.field) = { type: "tinyint(1) NOT NULL" }]; -// 创建时间 -int64 created_at = 5 [(things_go.seaql.field) = { type: "datetime NOT NULL" }]; -// 更新时间 -int64 updated_at = 6 [(things_go.seaql.field) = { type: "datetime NOT NULL" }]; + // 系统序号 + // #[seaql(type="bigint NOT NULL AUTO_INCREMENT")] + int64 id = 1 ; + // 键 + // #[seaql(type="varchar(64) NOT NULL DEFAULT ''")] + string key = 2 ; + // 名称 + // #[seaql(type="varchar(64) NOT NULL DEFAULT ''")] + string name = 3 ; + // 是否锁定 + // #[seaql(type="tinyint(1) NOT NULL")] + bool is_pin = 4; + // 创建时间 + // #[seaql(type="datetime NOT NULL")] + int64 created_at = 5; + // 更新时间 + // #[seaql(type="datetime NOT NULL")] + int64 updated_at = 6; } // DictItem 字典项表 @@ -41,16 +41,22 @@ message DictItem { }; // 系统序号 - int64 id = 1 [(things_go.seaql.field) = { type: "bigint NOT NULL AUTO_INCREMENT" }]; - // 名称 - string key = 2 [(things_go.seaql.field) = { type: "varchar(64) NOT NULL DEFAULT ''" }]; + // #[seaql(type="bigint NOT NULL AUTO_INCREMENT")] + int64 id = 1; + // 键 + // #[seaql(type="varchar(64) NOT NULL DEFAULT ''")] + string key = 2; // 名称 - string name = 3 [(things_go.seaql.field) = { type: "varchar(64) NOT NULL DEFAULT ''" }]; + // #[seaql(type="varchar(64) NOT NULL DEFAULT ''")] + string name = 3; // 状态 // 这是状态 - Infra.Status status = 4 [(things_go.seaql.field) = { type: "tinyint(3) unsigned NOT NULL" }]; + // #[seaql(type="tinyint(3) unsigned NOT NULL")] + Infra.Status status = 4; // 创建时间 - int64 created_at = 5 [(things_go.seaql.field) = { type: "datetime NOT NULL" }]; + // #[seaql(type="datetime NOT NULL")] + int64 created_at = 5; // 更新时间 - int64 updated_at = 6 [(things_go.seaql.field) = { type: "datetime NOT NULL" }]; + // #[seaql(type="datetime NOT NULL")] + int64 updated_at = 6; } diff --git a/internal/protoenum/enum.go b/internal/protoenum/enum.go index b8df843..d420f31 100644 --- a/internal/protoenum/enum.go +++ b/internal/protoenum/enum.go @@ -4,6 +4,7 @@ import ( "strings" "github.com/things-go/protogen-saber/internal/infra" + "github.com/things-go/protogen-saber/internal/protoutil" "github.com/things-go/protogen-saber/protosaber/enumerate" "google.golang.org/protobuf/compiler/protogen" "google.golang.org/protobuf/proto" @@ -53,22 +54,35 @@ func IntoEnums(nestedMessageName string, protoEnums []*protogen.Enum) []*Enum { if len(pe.Values) == 0 { continue } - isEnabled := proto.GetExtension(pe.Desc.Options(), enumerate.E_Enabled) - ok := isEnabled.(bool) - if !ok { - continue + + // 先判断注解, 再判断扩展 + emComment := protoutil.NewComments(pe.Comments.Leading) + annotate := emComment.FindAnnotation("enum") + if len(annotate) == 0 { + isEnabled := proto.GetExtension(pe.Desc.Options(), enumerate.E_Enabled) + if ok := isEnabled.(bool); !ok { + continue + } } - enumName := string(pe.Desc.Name()) - eValueMp := make(map[int]string, len(pe.Values)) - eValues := make([]*EnumValue, 0, len(pe.Values)) + emName := string(pe.Desc.Name()) + emValueMp := make(map[int]string, len(pe.Values)) + emValues := make([]*EnumValue, 0, len(pe.Values)) for _, v := range pe.Values { - mpv := proto.GetExtension(v.Desc.Options(), enumerate.E_Mapping) - mappingValue, _ := mpv.(string) + mappingValue := "" comment := strings.TrimSpace(strings.TrimSuffix(string(v.Comments.Leading), "\n")) - if mappingValue == "" { - mappingValue = comment + // 先判断注解, 再判断扩展 + annotateVal := protoutil.NewComments(v.Comments.Leading).FindAnnotationValues("enum", "mapping") + if len(annotateVal) > 0 && annotateVal[0] != "" { + mappingValue = annotateVal[0] + } else { + mpv := proto.GetExtension(v.Desc.Options(), enumerate.E_Mapping) + mappingValue, _ = mpv.(string) + if mappingValue == "" { + mappingValue = comment + } } + comment = strings.ReplaceAll(strings.ReplaceAll(comment, "\n", ","), `"`, `\"`) mappingValue = strings.ReplaceAll(strings.ReplaceAll(mappingValue, "\n", ","), `"`, `\"`) @@ -77,29 +91,29 @@ func IntoEnums(nestedMessageName string, protoEnums []*protogen.Enum) []*Enum { Number: int(v.Desc.Number()), Value: enumValueName, CamelValue: infra.CamelCase(enumValueName), - TrimValue: strings.TrimPrefix(strings.TrimPrefix(enumValueName, enumName), "_"), + TrimValue: strings.TrimPrefix(strings.TrimPrefix(enumValueName, emName), "_"), Mapping: mappingValue, Comment: comment, } //* duplicate - if _, ev.IsDuplicate = eValueMp[ev.Number]; !ev.IsDuplicate { - eValueMp[ev.Number] = mappingValue + if _, ev.IsDuplicate = emValueMp[ev.Number]; !ev.IsDuplicate { + emValueMp[ev.Number] = mappingValue } - eValues = append(eValues, ev) + emValues = append(emValues, ev) } - comment := strings.TrimSpace(strings.ReplaceAll(string(pe.Comments.Leading), "\n", "")) - bb := infra.ToArrayString(eValueMp) - if comment == "" { - comment = bb - } else { - comment += ", " + bb + comment := strings.TrimSpace(string(pe.Comments.Leading.String())) + bb := infra.ToArrayString(emValueMp) + if comment != "" { + comment = comment + "\n" } + comment = comment + "// " + bb + enums = append(enums, &Enum{ MessageName: nestedMessageName, - Name: enumName, + Name: emName, Comment: comment, - Values: eValues, + Values: emValues, }) } return enums @@ -110,22 +124,29 @@ func IntoEnumComment(pe *protogen.Enum) string { if pe == nil || len(pe.Values) == 0 { return "" } - isEnabled := proto.GetExtension(pe.Desc.Options(), enumerate.E_Enabled) - ok := isEnabled.(bool) - if !ok { - return "" + annotate := protoutil.NewComments(pe.Comments.Leading).FindAnnotation("enum") + if len(annotate) == 0 { + isEnabled := proto.GetExtension(pe.Desc.Options(), enumerate.E_Enabled) + if ok := isEnabled.(bool); !ok { + return "" + } } - eValueMp := make(map[int]string, len(pe.Values)) + emValueMp := make(map[int]string, len(pe.Values)) for _, v := range pe.Values { - mpv := proto.GetExtension(v.Desc.Options(), enumerate.E_Mapping) - mappingValue, _ := mpv.(string) - comment := strings.TrimSpace(strings.TrimSuffix(string(v.Comments.Leading), "\n")) - if mappingValue == "" { - mappingValue = comment + mappingValue := "" + annotateVal := protoutil.NewComments(v.Comments.Leading).FindAnnotationValues("enum", "mapping") + if len(annotateVal) > 0 && annotateVal[0] != "" { + mappingValue = annotateVal[0] + } else { + mpv := proto.GetExtension(v.Desc.Options(), enumerate.E_Mapping) + mappingValue, _ = mpv.(string) + if mappingValue == "" { + mappingValue = strings.TrimSpace(strings.TrimSuffix(string(v.Comments.Leading), "\n")) + } } mappingValue = strings.ReplaceAll(strings.ReplaceAll(mappingValue, "\n", ","), `"`, `\"`) - eValueMp[int(v.Desc.Number())] = mappingValue + emValueMp[int(v.Desc.Number())] = mappingValue } - return infra.ToArrayString(eValueMp) + return infra.ToArrayString(emValueMp) } diff --git a/internal/protoutil/annotation.go b/internal/protoutil/annotation.go new file mode 100644 index 0000000..81182bf --- /dev/null +++ b/internal/protoutil/annotation.go @@ -0,0 +1,55 @@ +package protoutil + +import ( + "regexp" + "strings" +) + +// annotation matches the following pattern +// `// #[Enum]“ will get `// #[enum]`,`Enum`,“,“,“ +// `// #[Enum(mapping="aaa")]` will get `// #[Enum(mapping="aaa")]`, "Enum", `(mapping="aaa")`, `mapping`, `aaa` +var rxAnnotation = regexp.MustCompile(`^//\s*#\[(\w+)\s*(\(\s*(\w+)\s*=\s*"(.*)"\s*\))?\s*\].*$`) + +type Annotation struct { + Path string + Key string + Value string +} + +func MatchAnnotation(s string) *Annotation { + matches := rxAnnotation.FindStringSubmatch(s) + if len(matches) != 5 { + return nil + } + return &Annotation{ + Path: matches[1], + Key: matches[3], + Value: matches[4], + } +} + +type Annotations []*Annotation + +func (c Annotations) Len() int { + return len(c) +} + +func (c Annotations) FindValues(path, key string) []string { + vs := make([]string, 0, len(c)) + for _, v := range c { + if strings.EqualFold(v.Path, path) && strings.EqualFold(v.Key, key) { + vs = append(vs, v.Value) + } + } + return vs +} + +func (c Annotations) Find(path string) Annotations { + vs := make([]*Annotation, 0, len(c)) + for _, v := range c { + if strings.EqualFold(v.Path, path) { + vs = append(vs, v) + } + } + return vs +} diff --git a/internal/protoutil/annotation_test.go b/internal/protoutil/annotation_test.go new file mode 100644 index 0000000..b626629 --- /dev/null +++ b/internal/protoutil/annotation_test.go @@ -0,0 +1,134 @@ +package protoutil + +import ( + "reflect" + "regexp" + "testing" +) + +func TestEnumRegexp(t *testing.T) { + testCases := []struct { + name string + rx *regexp.Regexp + arg string + want []string + }{ + { + name: "单个 - 标准", + rx: rxAnnotation, + arg: "// #[Enum]", + want: []string{"// #[Enum]", "Enum", "", "", ""}, + }, + { + name: "单个 - //后面不带空格", + rx: rxAnnotation, + arg: "//#[Enum]", + want: []string{"//#[Enum]", "Enum", "", "", ""}, + }, + { + name: "单个 - //后面带多个空格", + rx: rxAnnotation, + arg: "// #[Enum]", + want: []string{"// #[Enum]", "Enum", "", "", ""}, + }, + { + name: "单个 - 各种带空格或不带空格的乱七八槽", + rx: rxAnnotation, + arg: "// #[Enum] ", + want: []string{"// #[Enum] ", "Enum", "", "", ""}, + }, + { + name: "单个 - 尾部带乱七八槽", + rx: rxAnnotation, + arg: "// #[Enum]dfadfadf", + want: []string{"// #[Enum]dfadfadf", "Enum", "", "", ""}, + }, + { + name: "复合 - 1", + rx: rxAnnotation, + arg: `// #[Enum(mapping="aaa")]`, + want: []string{`// #[Enum(mapping="aaa")]`, "Enum", `(mapping="aaa")`, "mapping", "aaa"}, + }, + { + name: "复合 - 2", + rx: rxAnnotation, + arg: `// #[Enum( mapping="aaa" )]`, + want: []string{`// #[Enum( mapping="aaa" )]`, "Enum", `( mapping="aaa" )`, "mapping", "aaa"}, + }, + { + name: "复合 - 3", + rx: rxAnnotation, + arg: `// #[Enum( mapping = "aaa" )]`, + want: []string{`// #[Enum( mapping = "aaa" )]`, "Enum", `( mapping = "aaa" )`, "mapping", "aaa"}, + }, + } + for _, tt := range testCases { + t.Run(tt.name, func(t *testing.T) { + match := tt.rx.FindStringSubmatch(tt.arg) + if !reflect.DeepEqual(match, tt.want) { + t.Errorf("expected want: %v, got %v", tt.want, match) + } + }) + } +} + +func TestMatchAnnotation(t *testing.T) { + tests := []struct { + name string + arg string + want *Annotation + }{ + { + name: "非法", + arg: "// sd #[Enum]", + want: nil, + }, + { + name: "单个 - 标准", + arg: "// #[Enum]", + want: &Annotation{"Enum", "", ""}, + }, + { + name: "单个 - //后面不带空格", + arg: "//#[Enum]", + want: &Annotation{"Enum", "", ""}, + }, + { + name: "单个 - //后面带多个空格", + arg: "// #[Enum]", + want: &Annotation{"Enum", "", ""}, + }, + { + name: "单个 - 各种带空格或不带空格的乱七八槽", + arg: "// #[Enum] ", + want: &Annotation{"Enum", "", ""}, + }, + { + name: "单个 - 尾部带乱七八槽", + arg: "// #[Enum]dfadfadf", + want: &Annotation{"Enum", "", ""}, + }, + { + name: "复合 - 1", + arg: `// #[Enum(mapping="aaa")]`, + want: &Annotation{"Enum", "mapping", "aaa"}, + }, + { + name: "复合 - 2", + arg: `// #[Enum( mapping="aaa" )]`, + want: &Annotation{"Enum", "mapping", "aaa"}, + }, + { + name: "复合 - 3", + arg: `// #[Enum( mapping = "aaa" )]`, + want: &Annotation{"Enum", "mapping", "aaa"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := MatchAnnotation(tt.arg); !reflect.DeepEqual(got, tt.want) { + t.Errorf("MatchAnnotation() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/internal/protoutil/comment.go b/internal/protoutil/comment.go new file mode 100644 index 0000000..95400df --- /dev/null +++ b/internal/protoutil/comment.go @@ -0,0 +1,63 @@ +package protoutil + +import ( + "strings" + + "google.golang.org/protobuf/compiler/protogen" +) + +type Comments []string + +func NewComments(s protogen.Comments) Comments { + return strings.Split(strings.TrimSuffix(s.String(), "\n"), "\n") +} + +func (c Comments) Append(s string) Comments { + return append(c, "// "+s) +} + +func (c Comments) Annotations() Annotations { + ms := make([]*Annotation, 0, len(c)) + for _, v := range c { + if m := MatchAnnotation(v); m != nil { + ms = append(ms, m) + } + } + return ms +} + +func (c Comments) FindAnnotation(path string) Annotations { + ms := make([]*Annotation, 0, len(c)) + for _, v := range c { + m := MatchAnnotation(v) + if m != nil && strings.EqualFold(m.Path, path) { + ms = append(ms, m) + } + } + return ms +} + +func (c Comments) FindAnnotationValues(path, key string) []string { + ms := make([]string, 0, len(c)) + for _, v := range c { + m := MatchAnnotation(v) + if m != nil && strings.EqualFold(m.Path, path) && strings.EqualFold(m.Key, key) { + ms = append(ms, m.Value) + } + } + return ms +} + +func (c Comments) String() string { + if len(c) == 0 { + return "" + } + var b []byte + for i, line := range c { + b = append(b, line...) + if i+1 < len(c) { + b = append(b, "\n"...) + } + } + return string(b) +}