9
9
"golang.org/x/xerrors"
10
10
)
11
11
12
- var ErrKeyNotSet = xerrors .New ("key is not set" )
12
+ var ErrNameNotSet = xerrors .New ("name is not set" )
13
13
14
14
// Value wraps the type used by the serpent library for its option values.
15
15
// This gives us a seam should serpent ever move away from its current implementation.
@@ -18,14 +18,15 @@ type Value pflag.Value
18
18
// Entry is designed to wrap any type which satisfies the Value interface, which currently all serpent.Option instances do.
19
19
// serpent.Option provide configurability to Value instances, and we use this Entry type to extend the functionality of
20
20
// those Value instances.
21
+ // An Entry has a "name" which is used to identify it in the store.
21
22
type Entry [T Value ]struct {
22
- k string
23
- v T
23
+ n string
24
+ inner T
24
25
}
25
26
26
- // New creates a new T instance with a definedkey and value.
27
- func New [T Value ](key ,val string ) (out Entry [T ],err error ) {
28
- out .k = key
27
+ // New creates a new T instance with a definedname and value.
28
+ func New [T Value ](name ,val string ) (out Entry [T ],err error ) {
29
+ out .n = name
29
30
30
31
if err = out .SetStartupValue (val );err != nil {
31
32
return out ,err
@@ -35,34 +36,35 @@ func New[T Value](key, val string) (out Entry[T], err error) {
35
36
}
36
37
37
38
// MustNew is like New but panics if an error occurs.
38
- func MustNew [T Value ](key ,val string )Entry [T ] {
39
- out ,err := New [T ](key ,val )
39
+ func MustNew [T Value ](name ,val string )Entry [T ] {
40
+ out ,err := New [T ](name ,val )
40
41
if err != nil {
41
42
panic (err )
42
43
}
43
44
return out
44
45
}
45
46
47
+ // Initialize sets the entry's name, and initializes the value.
48
+ func (e * Entry [T ])Initialize (name string ) {
49
+ e .n = name
50
+ e .val ()
51
+ }
52
+
46
53
// val fronts the T value in the struct, and initializes it should the value be nil.
47
54
func (e * Entry [T ])val ()T {
48
- if reflect .ValueOf (e .v ).IsNil () {
49
- e .v = create [T ]()
55
+ if reflect .ValueOf (e .inner ).IsNil () {
56
+ e .inner = create [T ]()
50
57
}
51
- return e .v
58
+ return e .inner
52
59
}
53
60
54
- //key returns the configuredkey , or fails withErrKeyNotSet .
55
- func (e * Entry [T ])key () (string ,error ) {
56
- if e .k == "" {
57
- return "" ,ErrKeyNotSet
61
+ //name returns the configuredname , or fails withErrNameNotSet .
62
+ func (e * Entry [T ])name () (string ,error ) {
63
+ if e .n == "" {
64
+ return "" ,ErrNameNotSet
58
65
}
59
66
60
- return e .k ,nil
61
- }
62
-
63
- // SetKey allows the key to be set.
64
- func (e * Entry [T ])SetKey (k string ) {
65
- e .k = k
67
+ return e .n ,nil
66
68
}
67
69
68
70
// Set is an alias of SetStartupValue.
@@ -103,34 +105,34 @@ func (e *Entry[T]) StartupValue() T {
103
105
104
106
// SetRuntimeValue attempts to update the runtime value of this field in the store via the given Mutator.
105
107
func (e * Entry [T ])SetRuntimeValue (ctx context.Context ,m Mutator ,val T )error {
106
- key ,err := e .key ()
108
+ name ,err := e .name ()
107
109
if err != nil {
108
110
return err
109
111
}
110
112
111
- return m .UpsertRuntimeSetting (ctx ,key ,val .String ())
113
+ return m .UpsertRuntimeSetting (ctx ,name ,val .String ())
112
114
}
113
115
114
116
// UnsetRuntimeValue removes the runtime value from the store.
115
117
func (e * Entry [T ])UnsetRuntimeValue (ctx context.Context ,m Mutator )error {
116
- key ,err := e .key ()
118
+ name ,err := e .name ()
117
119
if err != nil {
118
120
return err
119
121
}
120
122
121
- return m .DeleteRuntimeSetting (ctx ,key )
123
+ return m .DeleteRuntimeSetting (ctx ,name )
122
124
}
123
125
124
126
// Resolve attempts to resolve the runtime value of this field from the store via the given Resolver.
125
127
func (e * Entry [T ])Resolve (ctx context.Context ,r Resolver ) (T ,error ) {
126
128
var zero T
127
129
128
- key ,err := e .key ()
130
+ name ,err := e .name ()
129
131
if err != nil {
130
132
return zero ,err
131
133
}
132
134
133
- val ,err := r .GetRuntimeSetting (ctx ,key )
135
+ val ,err := r .GetRuntimeSetting (ctx ,name )
134
136
if err != nil {
135
137
return zero ,err
136
138
}