Skip to content

Commit d832a49

Browse files
committed
Touchups
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 6e8751b commit d832a49

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

coderd/runtimeconfig/config.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@ import (
1111

1212
var ErrKeyNotSet = xerrors.New("key is not set")
1313

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.
1516
type Value pflag.Value
1617

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.
1721
type Entry[T Value] struct {
1822
k string
1923
v T
2024
}
2125

26+
// New creates a new T instance with a defined key and value.
2227
func New[T Value](key, val string) (out Entry[T], err error) {
2328
out.k = key
2429

@@ -29,6 +34,7 @@ func New[T Value](key, val string) (out Entry[T], err error) {
2934
return out, nil
3035
}
3136

37+
// MustNew is like New but panics if an error occurs.
3238
func MustNew[T Value](key, val string) Entry[T] {
3339
out, err := New[T](key, val)
3440
if err != nil {
@@ -37,13 +43,15 @@ func MustNew[T Value](key, val string) Entry[T] {
3743
return out
3844
}
3945

46+
// val fronts the T value in the struct, and initializes it should the value be nil.
4047
func (e *Entry[T]) val() T {
4148
if reflect.ValueOf(e.v).IsNil() {
4249
e.v = create[T]()
4350
}
4451
return e.v
4552
}
4653

54+
// key returns the configured key, or fails with ErrKeyNotSet.
4755
func (e *Entry[T]) key() (string, error) {
4856
if e.k == "" {
4957
return "", ErrKeyNotSet
@@ -52,29 +60,36 @@ func (e *Entry[T]) key() (string, error) {
5260
return e.k, nil
5361
}
5462

63+
// SetKey allows the key to be set.
5564
func (e *Entry[T]) SetKey(k string) {
5665
e.k = k
5766
}
5867

68+
// Set is an alias of SetStartupValue.
5969
func (e *Entry[T]) Set(s string) error {
6070
return e.SetStartupValue(s)
6171
}
6272

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.
6774
func (e *Entry[T]) MustSet(s string) {
6875
err := e.val().Set(s)
6976
if err != nil {
7077
panic(err)
7178
}
7279
}
7380

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.
7488
func (e *Entry[T]) Type() string {
7589
return e.val().Type()
7690
}
7791

92+
// String returns the wrapper value's string representation.
7893
func (e *Entry[T]) String() string {
7994
return e.val().String()
8095
}
@@ -86,6 +101,7 @@ func (e *Entry[T]) StartupValue() T {
86101
return e.val()
87102
}
88103

104+
// SetRuntimeValue attempts to update the runtime value of this field in the store via the given Mutator.
89105
func (e *Entry[T]) SetRuntimeValue(ctx context.Context, m Mutator, val T) error {
90106
key, err := e.key()
91107
if err != nil {
@@ -95,6 +111,7 @@ func (e *Entry[T]) SetRuntimeValue(ctx context.Context, m Mutator, val T) error
95111
return m.UpsertRuntimeSetting(ctx, key, val.String())
96112
}
97113

114+
// UnsetRuntimeValue removes the runtime value from the store.
98115
func (e *Entry[T]) UnsetRuntimeValue(ctx context.Context, m Mutator) error {
99116
key, err := e.key()
100117
if err != nil {
@@ -104,6 +121,7 @@ func (e *Entry[T]) UnsetRuntimeValue(ctx context.Context, m Mutator) error {
104121
return m.DeleteRuntimeSetting(ctx, key)
105122
}
106123

124+
// Resolve attempts to resolve the runtime value of this field from the store via the given Resolver.
107125
func (e *Entry[T]) Resolve(ctx context.Context, r Resolver) (T, error) {
108126
var zero T
109127

@@ -124,6 +142,8 @@ func (e *Entry[T]) Resolve(ctx context.Context, r Resolver) (T, error) {
124142
return inst, nil
125143
}
126144

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.
127147
func (e *Entry[T]) Coalesce(ctx context.Context, r Resolver) (T, error) {
128148
var zero T
129149

coderd/runtimeconfig/spec.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ package runtimeconfig
22

33
import "context"
44

5-
type Initializer interface {
6-
Init(key string)
7-
}
8-
5+
// Resolver is an interface for resolving runtime settings.
96
type Resolver interface {
7+
// GetRuntimeSetting gets a runtime setting by key.
108
GetRuntimeSetting(ctx context.Context, key string) (string, error)
119
}
1210

11+
// Mutator is an interface for mutating runtime settings.
1312
type Mutator interface {
13+
// UpsertRuntimeSetting upserts a runtime setting by key.
1414
UpsertRuntimeSetting(ctx context.Context, key, val string) error
15+
// DeleteRuntimeSetting deletes a runtime setting by key.
1516
DeleteRuntimeSetting(ctx context.Context, key string) error
1617
}

0 commit comments

Comments
 (0)