Skip to content

Commit 0306ef1

Browse files
committed
Extend Enums with default accessors and valid list methods
- Add GetDefaultEnum() and GetDefaultPure() methods - Add ListValidEnums() and ListValidPures() methods (excluding default) - Fix English/Chinese comment wording in test and demo files - Update demo1x/demo2x/demo3x with new function examples - Update English/Chinese README with API documentation - Add tests covering new methods and edge cases
1 parent 0bcaea0 commit 0306ef1

File tree

8 files changed

+286
-27
lines changed

8 files changed

+286
-27
lines changed

README.md

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ func main() {
8282
if base == protoenumstatus.StatusEnum_SUCCESS {
8383
zaplog.LOG.Debug("done")
8484
}
85+
86+
// Get default plain enum value (first item becomes default)
87+
defaultPure := enums.GetDefaultPure()
88+
zaplog.LOG.Debug("default", zap.String("msg", string(defaultPure)))
8589
}
8690
```
8791

@@ -137,6 +141,12 @@ func main() {
137141
miss := enums.GetByName("MISS")
138142
zaplog.LOG.Debug("pure", zap.String("msg", string(miss.Pure())))
139143
zaplog.LOG.Debug("desc", zap.String("msg", miss.Meta().Desc()))
144+
145+
// List each plain enum value in defined sequence
146+
pures := enums.ListPures()
147+
for _, pure := range pures {
148+
zaplog.LOG.Debug("list", zap.String("pure", string(pure)))
149+
}
140150
}
141151
```
142152

@@ -171,9 +181,13 @@ func main() {
171181
| `enums.MustGetByCode(code)` | Strict lookup by code (panics if not found) | `*Enum[P, E, M]` |
172182
| `enums.MustGetByName(name)` | Strict lookup by name (panics if not found) | `*Enum[P, E, M]` |
173183
| `enums.MustGetByPure(pure)` | Strict lookup by Go native enum (panics if not found) | `*Enum[P, E, M]` |
174-
| `enums.ListEnums()` | Returns a slice of all protoEnum values | `[]P` |
175-
| `enums.ListPures()` | Returns a slice of all plainEnum values | `[]E` |
184+
| `enums.ListEnums()` | Returns a slice of each protoEnum value | `[]P` |
185+
| `enums.ListPures()` | Returns a slice of each plainEnum value | `[]E` |
186+
| `enums.ListValidEnums()` | Returns protoEnum values excluding default | `[]P` |
187+
| `enums.ListValidPures()` | Returns plainEnum values excluding default | `[]E` |
176188
| `enums.GetDefault()` | Get current default value (panics if unset) | `*Enum[P, E, M]` |
189+
| `enums.GetDefaultEnum()` | Get default protoEnum value (panics if unset) | `P` |
190+
| `enums.GetDefaultPure()` | Get default plainEnum value (panics if unset) | `E` |
177191
| `enums.SetDefault(enum)` | Set default (requires no existing default) | `void` |
178192
| `enums.UnsetDefault()` | Remove default (requires existing default) | `void` |
179193
| `enums.WithDefaultEnum(enum)` | Chain: set default by enum instance | `*Enums[P, E, M]` |
@@ -240,15 +254,22 @@ enum = statusEnums.MustGetByCode(1)
240254
fmt.Printf("Strict: %s\n", enum.Meta().Desc())
241255
```
242256

243-
**Listing all values:**
257+
**Listing values:**
244258
```go
245-
// Get a slice of all registered proto enums
246-
allProtoEnums := statusEnums.ListEnums()
259+
// Get a slice of each registered proto enum
260+
protoEnums := statusEnums.ListEnums()
247261
// > [UNKNOWN, SUCCESS, FAILURE]
248262

249-
// Get a slice of all registered plain Go enums
250-
allPlainEnums := statusEnums.ListPures()
263+
// Get a slice of each registered plain Go enum
264+
plainEnums := statusEnums.ListPures()
251265
// > ["unknown", "success", "failure"]
266+
267+
// Get valid values (excluding default)
268+
validEnums := statusEnums.ListValidEnums()
269+
// > [SUCCESS, FAILURE] (UNKNOWN is default, excluded)
270+
271+
validPures := statusEnums.ListValidPures()
272+
// > ["success", "failure"]
252273
```
253274

254275
### Advanced Usage

README.zh.md

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ func main() {
8282
if base == protoenumstatus.StatusEnum_SUCCESS {
8383
zaplog.LOG.Debug("done")
8484
}
85+
86+
// 获取默认朴素枚举值(第一个元素成为默认值)
87+
defaultPure := enums.GetDefaultPure()
88+
zaplog.LOG.Debug("default", zap.String("msg", string(defaultPure)))
8589
}
8690
```
8791

@@ -137,6 +141,12 @@ func main() {
137141
miss := enums.GetByName("MISS")
138142
zaplog.LOG.Debug("pure", zap.String("msg", string(miss.Pure())))
139143
zaplog.LOG.Debug("desc", zap.String("msg", miss.Meta().Desc()))
144+
145+
// 按定义次序列出各朴素枚举值
146+
pures := enums.ListPures()
147+
for _, pure := range pures {
148+
zaplog.LOG.Debug("list", zap.String("pure", string(pure)))
149+
}
140150
}
141151
```
142152

@@ -171,9 +181,13 @@ func main() {
171181
| `enums.MustGetByCode(code)` | 严格按代码查找(找不到则 panic) | `*Enum[P, E, M]` |
172182
| `enums.MustGetByName(name)` | 严格按名称查找(找不到则 panic) | `*Enum[P, E, M]` |
173183
| `enums.MustGetByPure(pure)` | 严格按 Go 原生枚举查找(找不到则 panic) | `*Enum[P, E, M]` |
174-
| `enums.ListEnums()` | 返回所有 protoEnum 值的切片 | `[]P` |
175-
| `enums.ListPures()` | 返回所有 plainEnum 值的切片 | `[]E` |
184+
| `enums.ListEnums()` | 返回各 protoEnum 值的切片 | `[]P` |
185+
| `enums.ListPures()` | 返回各 plainEnum 值的切片 | `[]E` |
186+
| `enums.ListValidEnums()` | 返回排除默认值的 protoEnum 切片 | `[]P` |
187+
| `enums.ListValidPures()` | 返回排除默认值的 plainEnum 切片 | `[]E` |
176188
| `enums.GetDefault()` | 获取当前默认值(未设置则 panic) | `*Enum[P, E, M]` |
189+
| `enums.GetDefaultEnum()` | 获取默认 protoEnum 值(未设置则 panic) | `P` |
190+
| `enums.GetDefaultPure()` | 获取默认 plainEnum 值(未设置则 panic) | `E` |
177191
| `enums.SetDefault(enum)` | 设置默认值(要求当前无默认值) | `void` |
178192
| `enums.UnsetDefault()` | 移除默认值(要求当前有默认值) | `void` |
179193
| `enums.WithDefaultEnum(enum)` | 链式:通过枚举实例设置默认值 | `*Enums[P, E, M]` |
@@ -240,15 +254,22 @@ enum = statusEnums.MustGetByCode(1)
240254
fmt.Printf("严格: %s\n", enum.Meta().Desc())
241255
```
242256

243-
**列出所有值:**
257+
**列出枚举值:**
244258
```go
245-
// 获取所有已注册的 proto 枚举切片
246-
allProtoEnums := statusEnums.ListEnums()
259+
// 获取各已注册 proto 枚举的切片
260+
protoEnums := statusEnums.ListEnums()
247261
// > [UNKNOWN, SUCCESS, FAILURE]
248262

249-
// 获取所有已注册的 Go 原生枚举切片
250-
allPlainEnums := statusEnums.ListPures()
263+
// 获取各已注册 Go 原生枚举的切片
264+
plainEnums := statusEnums.ListPures()
251265
// > ["unknown", "success", "failure"]
266+
267+
// 获取有效值(排除默认值)
268+
validEnums := statusEnums.ListValidEnums()
269+
// > [SUCCESS, FAILURE](UNKNOWN 是默认值,被排除)
270+
271+
validPures := statusEnums.ListValidPures()
272+
// > ["success", "failure"]
252273
```
253274

254275
### 高级用法

internal/demos/demo1x/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@ func main() {
4242
if base == protoenumstatus.StatusEnum_SUCCESS {
4343
zaplog.LOG.Debug("done")
4444
}
45+
46+
// Get default plain enum value (first item becomes default)
47+
// 获取默认朴素枚举值(第一个元素成为默认值)
48+
defaultPure := enums.GetDefaultPure()
49+
zaplog.LOG.Debug("default", zap.String("msg", string(defaultPure)))
4550
}

internal/demos/demo2x/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,11 @@ func main() {
5151
miss := enums.GetByName("MISS")
5252
zaplog.LOG.Debug("pure", zap.String("msg", string(miss.Pure())))
5353
zaplog.LOG.Debug("desc", zap.String("msg", miss.Meta().Desc()))
54+
55+
// List each plain enum value in defined sequence
56+
// 按定义次序列出各朴素枚举值
57+
pures := enums.ListPures()
58+
for _, pure := range pures {
59+
zaplog.LOG.Debug("list", zap.String("pure", string(pure)))
60+
}
5461
}

internal/demos/demo3x/main.go

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

20-
// MetaI18n represents a custom metadata type with dual-language descriptions
21-
// MetaI18n 代表带有双语描述的自定义元数据类型
20+
// MetaI18n represents a custom metadata type with English/Chinese descriptions
21+
// MetaI18n 代表带有中英文描述的自定义元数据类型
2222
type MetaI18n struct {
2323
zhCN string // Chinese description // 中文描述
2424
enUS string // English description // 英文描述
@@ -27,8 +27,8 @@ 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 dual-language metadata
31-
// 构建带有自定义双语元数据的枚举集合
30+
// Build enum collection with custom English/Chinese metadata
31+
// 构建带有自定义中英文元数据的枚举集合
3232
var enums = protoenum.NewEnums(
3333
protoenum.NewEnumWithMeta(protoenumstatus.StatusEnum_UNKNOWN, StatusTypeUnknown, &MetaI18n{zhCN: "未知", enUS: "Unknown"}),
3434
protoenum.NewEnumWithMeta(protoenumstatus.StatusEnum_SUCCESS, StatusTypeSuccess, &MetaI18n{zhCN: "成功", enUS: "Success"}),
@@ -55,4 +55,11 @@ func main() {
5555
if success.Pure() == StatusTypeSuccess {
5656
zaplog.LOG.Debug("done")
5757
}
58+
59+
// List valid plain enum values (excluding default)
60+
// 列出有效的朴素枚举值(排除默认值)
61+
validPures := enums.ListValidPures()
62+
for _, pure := range validPures {
63+
zaplog.LOG.Debug("valid", zap.String("pure", string(pure)))
64+
}
5865
}

protoenum_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ func TestNewEnumWithMeta(t *testing.T) {
103103
StatusTypeFailure StatusType = "failure"
104104
)
105105

106-
// MetaI18n represents a custom metadata type with dual-language descriptions
107-
// MetaI18n 代表带有双语描述的自定义元数据类型
106+
// MetaI18n represents a custom metadata type with English/Chinese descriptions
107+
// MetaI18n 代表带有中英文描述的自定义元数据类型
108108
type MetaI18n struct {
109109
zh string // Chinese description // 中文描述
110110
en string // English description // 英文描述
111111
}
112112

113-
// Create enums with custom dual-language metadata
114-
// 使用自定义双语元数据创建枚举
113+
// Create enums with custom English/Chinese metadata
114+
// 使用自定义中英文元数据创建枚举
115115
enumUnknown := protoenum.NewEnumWithMeta(protoenumstatus.StatusEnum_UNKNOWN, StatusTypeUnknown, &MetaI18n{zh: "未知", en: "Unknown"})
116116
enumSuccess := protoenum.NewEnumWithMeta(protoenumstatus.StatusEnum_SUCCESS, StatusTypeSuccess, &MetaI18n{zh: "成功", en: "Success"})
117117
enumFailure := protoenum.NewEnumWithMeta(protoenumstatus.StatusEnum_FAILURE, StatusTypeFailure, &MetaI18n{zh: "失败", en: "Failure"})

protoenums.go

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type Enums[P ProtoEnum, E comparable, M any] struct {
4242
// 返回创建的 Enums 集合指针,可用于各种查找操作
4343
func NewEnums[P ProtoEnum, E comparable, M any](params ...*Enum[P, E, M]) *Enums[P, E, M] {
4444
res := &Enums[P, E, M]{
45-
enumElements: slices.Clone(params), // Create a distinct copy to maintain the sequence of all enum elements // 创建一个独立的副本以保持所有枚举元素的次序
45+
enumElements: slices.Clone(params), // Clone the slice to preserve the defined sequence of enum elements // 克隆切片以保持枚举元素的定义次序
4646
mapCode2Enum: make(map[int32]*Enum[P, E, M], len(params)),
4747
mapName2Enum: make(map[string]*Enum[P, E, M], len(params)),
4848
mapPure2Enum: make(map[E]*Enum[P, E, M], len(params)),
@@ -173,6 +173,24 @@ func (c *Enums[P, E, M]) GetDefault() *Enum[P, E, M] {
173173
return must.Full(c.defaultValue)
174174
}
175175

176+
// GetDefaultEnum returns the protoEnum value of the default Enum
177+
// Panics if no default value has been configured
178+
//
179+
// 返回默认 Enum 的 protoEnum 值
180+
// 如果未配置默认值则会 panic
181+
func (c *Enums[P, E, M]) GetDefaultEnum() P {
182+
return must.Full(c.GetDefault()).Base()
183+
}
184+
185+
// GetDefaultPure returns the plainEnum value of the default Enum
186+
// Panics if no default value has been configured
187+
//
188+
// 返回默认 Enum 的 plainEnum 值
189+
// 如果未配置默认值则会 panic
190+
func (c *Enums[P, E, M]) GetDefaultPure() E {
191+
return must.Full(c.GetDefault()).Pure()
192+
}
193+
176194
// SetDefault sets the default Enum value to return when lookups miss
177195
// Allows dynamic configuration of the fallback value post creation
178196
// Panics if defaultEnum is nil, use UnsetDefault to remove the default value
@@ -243,8 +261,8 @@ func (c *Enums[P, E, M]) WithUnsetDefault() *Enums[P, E, M] {
243261
return c
244262
}
245263

246-
// ListEnums returns a slice containing all protoEnum values in the sequence they were defined.
247-
// ListEnums 返回一个包含所有 protoEnum 值的切片,次序与定义时一致。
264+
// ListEnums returns a slice containing each protoEnum value in the defined sequence.
265+
// ListEnums 返回一个包含各 protoEnum 值的切片,次序与定义时一致。
248266
func (c *Enums[P, E, M]) ListEnums() []P {
249267
var enums = make([]P, 0, len(c.enumElements))
250268
for _, item := range c.enumElements {
@@ -253,12 +271,48 @@ func (c *Enums[P, E, M]) ListEnums() []P {
253271
return enums
254272
}
255273

256-
// ListPures returns a slice containing all plainEnum values in the sequence they were defined.
257-
// ListPures 返回一个包含所有 plainEnum 值的切片,次序与定义时一致。
274+
// ListPures returns a slice containing each plainEnum value in the defined sequence.
275+
// ListPures 返回一个包含各 plainEnum 值的切片,次序与定义时一致。
258276
func (c *Enums[P, E, M]) ListPures() []E {
259277
var pures = make([]E, 0, len(c.enumElements))
260278
for _, item := range c.enumElements {
261279
pures = append(pures, item.Pure())
262280
}
263281
return pures
264282
}
283+
284+
// ListValidEnums returns a slice excluding the default protoEnum value.
285+
// If no default value is configured, returns each protoEnum value.
286+
//
287+
// 返回一个切片,排除默认 protoEnum 值,其余按定义次序排列。
288+
// 如果未配置默认值,则返回所有 protoEnum 值。
289+
func (c *Enums[P, E, M]) ListValidEnums() []P {
290+
if c.defaultValue != nil {
291+
var enums []P
292+
for _, item := range c.enumElements {
293+
if item != c.defaultValue {
294+
enums = append(enums, item.Base())
295+
}
296+
}
297+
return enums
298+
}
299+
return c.ListEnums()
300+
}
301+
302+
// ListValidPures returns a slice excluding the default plainEnum value.
303+
// If no default value is configured, returns each plainEnum value.
304+
//
305+
// 返回一个切片,排除默认 plainEnum 值,其余按定义次序排列。
306+
// 如果未配置默认值,则返回所有 plainEnum 值。
307+
func (c *Enums[P, E, M]) ListValidPures() []E {
308+
if c.defaultValue != nil {
309+
var pures []E
310+
for _, item := range c.enumElements {
311+
if item != c.defaultValue {
312+
pures = append(pures, item.Pure())
313+
}
314+
}
315+
return pures
316+
}
317+
return c.ListPures()
318+
}

0 commit comments

Comments
 (0)