@@ -42,7 +42,7 @@ type Enums[P ProtoEnum, E comparable, M any] struct {
4242// 返回创建的 Enums 集合指针,可用于各种查找操作
4343func 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 值的切片,次序与定义时一致。
248266func (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 值的切片,次序与定义时一致。
258276func (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