@@ -11,14 +11,19 @@ import (
11
11
12
12
var ErrKeyNotSet = xerrors .New ("key is not set" )
13
13
14
- // TODO: comment
14
+ // Value wraps the type used by the serpent library for its option values.
15
+ // This gives us a seam should serpent ever move away from its current implementation.
15
16
type Value pflag.Value
16
17
18
+ // Entry is designed to wrap any type which satisfies the Value interface, which currently all serpent.Option instances do.
19
+ // serpent.Option provide configurability to Value instances, and we use this Entry type to extend the functionality of
20
+ // those Value instances.
17
21
type Entry [T Value ] struct {
18
22
k string
19
23
v T
20
24
}
21
25
26
+ // New creates a new T instance with a defined key and value.
22
27
func New [T Value ](key , val string ) (out Entry [T ], err error ) {
23
28
out .k = key
24
29
@@ -29,6 +34,7 @@ func New[T Value](key, val string) (out Entry[T], err error) {
29
34
return out , nil
30
35
}
31
36
37
+ // MustNew is like New but panics if an error occurs.
32
38
func MustNew [T Value ](key , val string ) Entry [T ] {
33
39
out , err := New [T ](key , val )
34
40
if err != nil {
@@ -37,13 +43,15 @@ func MustNew[T Value](key, val string) Entry[T] {
37
43
return out
38
44
}
39
45
46
+ // val fronts the T value in the struct, and initializes it should the value be nil.
40
47
func (e * Entry [T ]) val () T {
41
48
if reflect .ValueOf (e .v ).IsNil () {
42
49
e .v = create [T ]()
43
50
}
44
51
return e .v
45
52
}
46
53
54
+ // key returns the configured key, or fails with ErrKeyNotSet.
47
55
func (e * Entry [T ]) key () (string , error ) {
48
56
if e .k == "" {
49
57
return "" , ErrKeyNotSet
@@ -52,29 +60,36 @@ func (e *Entry[T]) key() (string, error) {
52
60
return e .k , nil
53
61
}
54
62
63
+ // SetKey allows the key to be set.
55
64
func (e * Entry [T ]) SetKey (k string ) {
56
65
e .k = k
57
66
}
58
67
68
+ // Set is an alias of SetStartupValue.
59
69
func (e * Entry [T ]) Set (s string ) error {
60
70
return e .SetStartupValue (s )
61
71
}
62
72
63
- func (e * Entry [T ]) SetStartupValue (s string ) error {
64
- return e .val ().Set (s )
65
- }
66
-
73
+ // MustSet is like Set but panics on error.
67
74
func (e * Entry [T ]) MustSet (s string ) {
68
75
err := e .val ().Set (s )
69
76
if err != nil {
70
77
panic (err )
71
78
}
72
79
}
73
80
81
+ // SetStartupValue sets the value of the wrapped field. This ONLY sets the value locally, not in the store.
82
+ // See SetRuntimeValue.
83
+ func (e * Entry [T ]) SetStartupValue (s string ) error {
84
+ return e .val ().Set (s )
85
+ }
86
+
87
+ // Type returns the wrapped value's type.
74
88
func (e * Entry [T ]) Type () string {
75
89
return e .val ().Type ()
76
90
}
77
91
92
+ // String returns the wrapper value's string representation.
78
93
func (e * Entry [T ]) String () string {
79
94
return e .val ().String ()
80
95
}
@@ -86,6 +101,7 @@ func (e *Entry[T]) StartupValue() T {
86
101
return e .val ()
87
102
}
88
103
104
+ // SetRuntimeValue attempts to update the runtime value of this field in the store via the given Mutator.
89
105
func (e * Entry [T ]) SetRuntimeValue (ctx context.Context , m Mutator , val T ) error {
90
106
key , err := e .key ()
91
107
if err != nil {
@@ -95,6 +111,7 @@ func (e *Entry[T]) SetRuntimeValue(ctx context.Context, m Mutator, val T) error
95
111
return m .UpsertRuntimeSetting (ctx , key , val .String ())
96
112
}
97
113
114
+ // UnsetRuntimeValue removes the runtime value from the store.
98
115
func (e * Entry [T ]) UnsetRuntimeValue (ctx context.Context , m Mutator ) error {
99
116
key , err := e .key ()
100
117
if err != nil {
@@ -104,6 +121,7 @@ func (e *Entry[T]) UnsetRuntimeValue(ctx context.Context, m Mutator) error {
104
121
return m .DeleteRuntimeSetting (ctx , key )
105
122
}
106
123
124
+ // Resolve attempts to resolve the runtime value of this field from the store via the given Resolver.
107
125
func (e * Entry [T ]) Resolve (ctx context.Context , r Resolver ) (T , error ) {
108
126
var zero T
109
127
@@ -124,6 +142,8 @@ func (e *Entry[T]) Resolve(ctx context.Context, r Resolver) (T, error) {
124
142
return inst , nil
125
143
}
126
144
145
+ // Coalesce attempts to resolve the runtime value of this field from the store via the given Resolver. Should no runtime
146
+ // value be found, the startup value will be used.
127
147
func (e * Entry [T ]) Coalesce (ctx context.Context , r Resolver ) (T , error ) {
128
148
var zero T
129
149
0 commit comments