Skip to content

Commit 6e06702

Browse files
authored
[chore][configoptional] Use flavor for indicating internal state (#13198)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Use flavor instead of `hasValue` and `notNone` <!-- Issue number if applicable --> #### Link to tracking issue Updates #13181 using #13168 (comment)
1 parent 4263e8d commit 6e06702

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

config/configoptional/optional.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ import (
1212
"go.opentelemetry.io/collector/confmap"
1313
)
1414

15+
type flavor int
16+
17+
const (
18+
noneFlavor flavor = 0
19+
defaultFlavor flavor = 1
20+
someFlavor flavor = 2
21+
)
22+
1523
// Optional represents a value that may or may not be present.
1624
// It supports two flavors for all types: Some(value) and None.
1725
// It supports a third flavor for struct types: Default(defaultVal).
@@ -22,12 +30,9 @@ type Optional[T any] struct {
2230
// value is the value of the Optional.
2331
value T
2432

25-
// hasValue indicates if the Optional has a value.
26-
hasValue bool
27-
28-
// notNone is used to indicate that the Optional is not None.
29-
// This is used to differentiate between None and Default.
30-
notNone bool
33+
// flavor indicates the flavor of the Optional.
34+
// The zero value of flavor is noneFlavor.
35+
flavor flavor
3136
}
3237

3338
// deref a reflect.Type to its underlying type.
@@ -86,7 +91,7 @@ func Some[T any](value T) Optional[T] {
8691
if err := assertNoEnabledField[T](); err != nil {
8792
panic(err)
8893
}
89-
return Optional[T]{value: value, hasValue: true, notNone: true}
94+
return Optional[T]{value: value, flavor: someFlavor}
9095
}
9196

9297
// Default creates an Optional with a default value for unmarshaling.
@@ -99,7 +104,7 @@ func Default[T any](value T) Optional[T] {
99104
if err != nil {
100105
panic(err)
101106
}
102-
return Optional[T]{value: value, hasValue: false, notNone: true}
107+
return Optional[T]{value: value, flavor: defaultFlavor}
103108
}
104109

105110
// None has no value. It has the same behavior as a nil pointer when unmarshaling.
@@ -117,7 +122,7 @@ func None[T any]() Optional[T] {
117122

118123
// HasValue checks if the Optional has a value.
119124
func (o Optional[T]) HasValue() bool {
120-
return o.hasValue
125+
return o.flavor == someFlavor
121126
}
122127

123128
// Get returns the value of the Optional.
@@ -146,7 +151,7 @@ func (o *Optional[T]) Unmarshal(conf *confmap.Conf) error {
146151
return err
147152
}
148153

149-
if !o.hasValue && !o.notNone && conf.ToStringMap() == nil {
154+
if o.flavor == noneFlavor && conf.ToStringMap() == nil {
150155
// If the Optional is None and the configuration is nil, we do nothing.
151156
// This replicates the behavior of unmarshaling into a field with a nil pointer.
152157
return nil
@@ -156,6 +161,6 @@ func (o *Optional[T]) Unmarshal(conf *confmap.Conf) error {
156161
return err
157162
}
158163

159-
o.hasValue = true
164+
o.flavor = someFlavor
160165
return nil
161166
}

0 commit comments

Comments
 (0)