Skip to content

Commit 0bcaea0

Browse files
yyle88yangyile1990
authored andcommitted
Streamline enum code with naming and comment updates
- Remove Hans() method with ambiguous semantics - Use slices.Clone() instead of append pattern - Shorten generic type params to P/E/M - Update English/Chinese comments - Update README documentation - Update demo examples in demo2x and demo3x - Update test comments and references
1 parent 1a95249 commit 0bcaea0

File tree

9 files changed

+84
-83
lines changed

9 files changed

+84
-83
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ var enums = protoenum.NewEnums(
118118
)
119119

120120
func main() {
121-
// Lookup by enum code (returns default when not found)
121+
// Lookup using enum code (returns default when not found)
122122
skip := enums.GetByCode(int32(protoenumresult.ResultEnum_SKIP))
123123
zaplog.LOG.Debug("pure", zap.String("msg", string(skip.Pure())))
124124
zaplog.LOG.Debug("desc", zap.String("msg", skip.Meta().Desc()))
125125

126-
// Lookup by Go native enum value (type-safe lookup)
126+
// Lookup using Go native enum value (type-safe)
127127
pass := enums.GetByPure(ResultTypePass)
128128
base := protoenumresult.ResultEnum(pass.Code())
129129
zaplog.LOG.Debug("base", zap.String("msg", base.String()))
@@ -133,10 +133,10 @@ func main() {
133133
zaplog.LOG.Debug("pass")
134134
}
135135

136-
// Lookup by enum name (safe with default fallback)
136+
// Lookup using enum name (safe with default fallback)
137137
miss := enums.GetByName("MISS")
138138
zaplog.LOG.Debug("pure", zap.String("msg", string(miss.Pure())))
139-
zaplog.LOG.Debug("hans", zap.String("msg", miss.Meta().Hans()))
139+
zaplog.LOG.Debug("desc", zap.String("msg", miss.Meta().Desc()))
140140
}
141141
```
142142

@@ -223,15 +223,15 @@ statusEnums := protoenum.NewEnums(
223223

224224
**Multiple lookup methods:**
225225
```go
226-
// By numeric code - always returns valid enum (default if not found)
226+
// Using numeric code - returns valid enum (default if not found)
227227
enum := statusEnums.GetByCode(1)
228228
fmt.Printf("Found: %s\n", enum.Meta().Desc())
229229

230-
// By enum name - guaranteed non-nil
230+
// Using enum name - guaranteed non-nil
231231
enum = statusEnums.GetByName("SUCCESS")
232232
fmt.Printf("Status: %s\n", enum.Meta().Desc())
233233

234-
// By Go native enum value - type-safe lookup
234+
// Using Go native enum value - type-safe lookup
235235
enum = statusEnums.GetByPure(StatusTypeSuccess)
236236
fmt.Printf("Pure: %s\n", enum.Pure())
237237

@@ -273,7 +273,7 @@ case StatusTypeUnknown:
273273
fmt.Println("Unknown status")
274274
}
275275

276-
// Lookup by Go native enum value
276+
// Lookup using Go native enum value
277277
found := enums.GetByPure(StatusTypeSuccess)
278278
fmt.Printf("Code: %d, Name: %s\n", found.Code(), found.Name())
279279
```
@@ -339,7 +339,7 @@ fmt.Printf("Fallback: %s\n", notFound.Meta().Desc()) // Safe without nil check
339339
enums.UnsetDefault() // Must unset first
340340
enums.SetDefault(enums.MustGetByCode(1)) // Then set new default
341341

342-
// After UnsetDefault, lookups panic if not found
342+
// Once UnsetDefault called, lookups panic if not found
343343
// This enforces single usage pattern: collections must have defaults
344344
```
345345

README.zh.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func main() {
136136
// 按枚举名称查找(安全且有默认值回退)
137137
miss := enums.GetByName("MISS")
138138
zaplog.LOG.Debug("pure", zap.String("msg", string(miss.Pure())))
139-
zaplog.LOG.Debug("hans", zap.String("msg", miss.Meta().Hans()))
139+
zaplog.LOG.Debug("desc", zap.String("msg", miss.Meta().Desc()))
140140
}
141141
```
142142

internal/demos/demo2x/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ var enums = protoenum.NewEnums(
2828
)
2929

3030
func main() {
31-
// Lookup by enum code (returns default when not found)
31+
// Lookup using enum code (returns default when not found)
3232
// 按枚举代码查找(找不到时返回默认值)
3333
skip := enums.GetByCode(int32(protoenumresult.ResultEnum_SKIP))
3434
zaplog.LOG.Debug("pure", zap.String("msg", string(skip.Pure())))
3535
zaplog.LOG.Debug("desc", zap.String("msg", skip.Meta().Desc()))
3636

37-
// Lookup by Go native enum value (type-safe lookup)
37+
// Lookup using Go native enum value (type-safe)
3838
// 按 Go 原生枚举值查找(类型安全查找)
3939
pass := enums.GetByPure(ResultTypePass)
4040
base := protoenumresult.ResultEnum(pass.Code())
@@ -46,9 +46,9 @@ func main() {
4646
zaplog.LOG.Debug("pass")
4747
}
4848

49-
// Lookup by enum name (safe with default fallback)
49+
// Lookup using enum name (safe with default fallback)
5050
// 按枚举名称查找(安全且有默认值回退)
5151
miss := enums.GetByName("MISS")
5252
zaplog.LOG.Debug("pure", zap.String("msg", string(miss.Pure())))
53-
zaplog.LOG.Debug("hans", zap.String("msg", miss.Meta().Hans()))
53+
zaplog.LOG.Debug("desc", zap.String("msg", miss.Meta().Desc()))
5454
}

internal/demos/demo3x/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const (
1717
StatusTypeFailure StatusType = "failure"
1818
)
1919

20-
// MetaI18n represents a custom metadata type with bilingual descriptions
20+
// MetaI18n represents a custom metadata type with dual-language descriptions
2121
// MetaI18n 代表带有双语描述的自定义元数据类型
2222
type MetaI18n struct {
2323
zhCN string // Chinese description // 中文描述
@@ -27,7 +27,7 @@ type MetaI18n struct {
2727
func (c *MetaI18n) Chinese() string { return c.zhCN }
2828
func (c *MetaI18n) English() string { return c.enUS }
2929

30-
// Build enum collection with custom bilingual metadata
30+
// Build enum collection with custom dual-language metadata
3131
// 构建带有自定义双语元数据的枚举集合
3232
var enums = protoenum.NewEnums(
3333
protoenum.NewEnumWithMeta(protoenumstatus.StatusEnum_UNKNOWN, StatusTypeUnknown, &MetaI18n{zhCN: "未知", enUS: "Unknown"}),
@@ -36,14 +36,14 @@ var enums = protoenum.NewEnums(
3636
)
3737

3838
func main() {
39-
// Lookup by Go native enum value (type-safe lookup)
39+
// Lookup using Go native enum value (type-safe)
4040
// 按 Go 原生枚举值查找(类型安全查找)
4141
success := enums.GetByPure(StatusTypeSuccess)
4242
zaplog.LOG.Debug("pure", zap.String("msg", string(success.Pure())))
4343
zaplog.LOG.Debug("zh-CN", zap.String("msg", success.Meta().Chinese()))
4444
zaplog.LOG.Debug("en-US", zap.String("msg", success.Meta().English()))
4545

46-
// Lookup by enum code (returns default when not found)
46+
// Lookup using enum code (returns default when not found)
4747
// 按枚举代码查找(找不到时返回默认值)
4848
failure := enums.GetByCode(int32(protoenumstatus.StatusEnum_FAILURE))
4949
zaplog.LOG.Debug("pure", zap.String("msg", string(failure.Pure())))

meta.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,3 @@ type MetaDesc struct{ description string }
1818
func (c *MetaDesc) Desc() string {
1919
return c.description
2020
}
21-
22-
// Hans returns the Chinese description of the enum
23-
// Alias to Desc method, convenient with Chinese language support
24-
//
25-
// 返回枚举的中文描述
26-
// Desc 方法的别名,方便中文语言支持
27-
func (c *MetaDesc) Hans() string {
28-
return c.description
29-
}

protoenum.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ func (c *Enum[protoEnum, plainEnum, extraMeta]) Name() string {
131131
// Pure returns the Go native enum value associated with this enum
132132
// Enables type-safe conversion from protobuf enum to Go native enum (e.g. type StatusType string)
133133
// Use this to get the plain enum value when working with Go native enum patterns
134-
// Bridges protobuf enums with existing Go enum-based business logic seamlessly
134+
// Bridges protobuf enums with existing Go enum-based business logic with ease
135135
//
136136
// 返回与此枚举关联的 Go 原生枚举值
137137
// 实现从 protobuf 枚举到 Go 原生枚举的类型安全转换(如 type StatusType string)
138138
// 在使用 Go 原生枚举模式时使用此方法获取朴素枚举值
139-
// 在桥接 protobuf 枚举与现有基于 Go 枚举的业务逻辑时至关重要
139+
// 无缝桥接 protobuf 枚举与现有基于 Go 枚举的业务逻辑
140140
func (c *Enum[protoEnum, plainEnum, extraMeta]) Pure() plainEnum {
141141
return c.pure
142142
}

protoenum_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@ func TestNewEnum(t *testing.T) {
2424
t.Log(enum.Name())
2525
t.Log(enum.Pure())
2626
t.Log(enum.Meta().Desc())
27-
t.Log(enum.Meta().Hans())
2827

2928
require.Equal(t, enum.Code(), int32(protoenumstatus.StatusEnum_SUCCESS.Number()))
3029
require.Equal(t, enum.Name(), protoenumstatus.StatusEnum_SUCCESS.String())
3130
require.Equal(t, enum.Pure(), StatusTypeSuccess)
3231
require.Equal(t, enum.Meta().Desc(), "任务完成")
33-
require.Equal(t, enum.Meta().Hans(), "任务完成")
3432
}
3533

3634
// TestEnum_Base tests the Base method returns the source enum
@@ -93,10 +91,10 @@ func TestEnum_Pure(t *testing.T) {
9391
}
9492

9593
// TestNewEnumWithMeta tests custom metadata type with NewEnumWithMeta
96-
// Checks that user-defined meta types work with the Enum instance
94+
// Checks that custom meta types work with the Enum instance
9795
//
9896
// 验证 NewEnumWithMeta 支持自定义元数据类型
99-
// 测试用户自定义的 meta 类型与 Enum 包装器配合工作
97+
// 测试自定义 meta 类型与 Enum 包装器配合工作
10098
func TestNewEnumWithMeta(t *testing.T) {
10199
type StatusType string
102100
const (
@@ -105,14 +103,14 @@ func TestNewEnumWithMeta(t *testing.T) {
105103
StatusTypeFailure StatusType = "failure"
106104
)
107105

108-
// MetaI18n represents a custom metadata type with bilingual descriptions
106+
// MetaI18n represents a custom metadata type with dual-language descriptions
109107
// MetaI18n 代表带有双语描述的自定义元数据类型
110108
type MetaI18n struct {
111109
zh string // Chinese description // 中文描述
112110
en string // English description // 英文描述
113111
}
114112

115-
// Create enums with custom bilingual metadata
113+
// Create enums with custom dual-language metadata
116114
// 使用自定义双语元数据创建枚举
117115
enumUnknown := protoenum.NewEnumWithMeta(protoenumstatus.StatusEnum_UNKNOWN, StatusTypeUnknown, &MetaI18n{zh: "未知", en: "Unknown"})
118116
enumSuccess := protoenum.NewEnumWithMeta(protoenumstatus.StatusEnum_SUCCESS, StatusTypeSuccess, &MetaI18n{zh: "成功", en: "Success"})
@@ -131,7 +129,7 @@ func TestNewEnumWithMeta(t *testing.T) {
131129
require.Equal(t, "失败", enumFailure.Meta().zh)
132130
require.Equal(t, "Failure", enumFailure.Meta().en)
133131

134-
// Check Pure still works with custom meta
132+
// Check Pure works with custom meta as expected
135133
// 验证 Pure 在自定义 meta 下仍然工作
136134
require.Equal(t, StatusTypeSuccess, enumSuccess.Pure())
137135
require.Equal(t, int32(protoenumstatus.StatusEnum_SUCCESS.Number()), enumSuccess.Code())

0 commit comments

Comments
 (0)