-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenums_boolean.go
More file actions
89 lines (83 loc) · 2.72 KB
/
genums_boolean.go
File metadata and controls
89 lines (83 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package genums
// EnumBool is a specialized enum interface for boolean-based enumerations.
// It extends BaseEnum with bool as the underlying value type, providing
// type-safe boolean enum functionality.
//
// Type parameters:
// - T: The concrete enum type that implements this interface
type EnumBool[T any] interface {
BaseEnum[T, bool]
}
// enumBoolImpl is the internal implementation of the EnumBool interface.
// It embeds enumImpl with bool as the value type to provide all
// boolean-specific enum functionality.
//
// Type parameters:
// - T: The concrete enum type (used for type safety)
type enumBoolImpl[T any] struct {
enumImpl[T, bool]
}
// NewEnumBool creates a new boolean-based enum instance with the given value.
// This is the primary constructor for creating boolean enum values.
//
// Type parameters:
// - T: The target enum type to create
//
// Parameters:
// - value: The boolean value to wrap in the enum
//
// Returns:
// - A new EnumBool instance containing the specified value
//
// Example:
//
// type FeatureFlag EnumBool[FeatureFlag]
// enabled := NewEnumBool[FeatureFlag](true)
// disabled := NewEnumBool[FeatureFlag](false)
func NewEnumBool[T any](value bool) EnumBool[T] {
return enumBoolImpl[T]{enumImpl[T, bool]{value: value}}
}
// BoolFactory is a factory type that provides a convenient way to create
// boolean-based enum instances. It implements a factory pattern for enums,
// allowing for more flexible instantiation.
//
// Type parameters:
// - T: The enum type to be created (must implement EnumBool[T])
type BoolFactory[T EnumBool[T]] struct{}
// New creates a new enum instance of type T with the specified boolean value.
// This method allows the factory to produce properly typed enum instances
// using the underlying NewEnumBool constructor.
//
// Parameters:
// - value: The boolean value for the new enum instance
//
// Returns:
// - A new enum instance of type T with the given value
//
// Example:
//
// factory := NewBoolFactory[FeatureFlag]()
// enabled := factory.New(true)
// disabled := factory.New(false)
func (f BoolFactory[T]) New(value bool) T {
return any(NewEnumBool[T](value)).(T)
}
// NewBoolFactory creates a new BoolFactory instance for the specified enum type.
// This function initializes a factory that can be used to create multiple enum
// instances of the same type.
//
// Type parameters:
// - T: The enum type the factory will create (must implement EnumBool[T])
//
// Returns:
// - A new BoolFactory instance for type T
//
// Example:
//
// type FeatureFlag EnumBool[FeatureFlag]
// factory := NewBoolFactory[FeatureFlag]()
// darkMode := factory.New(true)
// notifications := factory.New(false)
func NewBoolFactory[T EnumBool[T]]() BoolFactory[T] {
return BoolFactory[T]{}
}