Skip to content

Commit 355afdc

Browse files
committed
Initializer interface, rename "key" to "name"
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 8b5942c commit 355afdc

File tree

3 files changed

+49
-43
lines changed

3 files changed

+49
-43
lines changed

coderd/runtimeconfig/config.go

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"golang.org/x/xerrors"
1010
)
1111

12-
var ErrKeyNotSet = xerrors.New("key is not set")
12+
var ErrNameNotSet = xerrors.New("name is not set")
1313

1414
// Value wraps the type used by the serpent library for its option values.
1515
// This gives us a seam should serpent ever move away from its current implementation.
@@ -18,14 +18,15 @@ type Value pflag.Value
1818
// Entry is designed to wrap any type which satisfies the Value interface, which currently all serpent.Option instances do.
1919
// serpent.Option provide configurability to Value instances, and we use this Entry type to extend the functionality of
2020
// those Value instances.
21+
// An Entry has a "name" which is used to identify it in the store.
2122
type Entry[T Value] struct {
22-
k string
23-
v T
23+
n string
24+
inner T
2425
}
2526

26-
// New creates a new T instance with a defined key 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 defined name and value.
28+
func New[T Value](name, val string) (out Entry[T], err error) {
29+
out.n = name
2930

3031
if err = out.SetStartupValue(val); err != nil {
3132
return out, err
@@ -35,34 +36,35 @@ func New[T Value](key, val string) (out Entry[T], err error) {
3536
}
3637

3738
// 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)
4041
if err != nil {
4142
panic(err)
4243
}
4344
return out
4445
}
4546

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+
4653
// val fronts the T value in the struct, and initializes it should the value be nil.
4754
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]()
5057
}
51-
return e.v
58+
return e.inner
5259
}
5360

54-
// key returns the configured key, or fails with ErrKeyNotSet.
55-
func (e *Entry[T]) key() (string, error) {
56-
if e.k == "" {
57-
return "", ErrKeyNotSet
61+
// name returns the configured name, or fails with ErrNameNotSet.
62+
func (e *Entry[T]) name() (string, error) {
63+
if e.n == "" {
64+
return "", ErrNameNotSet
5865
}
5966

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
6668
}
6769

6870
// Set is an alias of SetStartupValue.
@@ -103,34 +105,34 @@ func (e *Entry[T]) StartupValue() T {
103105

104106
// SetRuntimeValue attempts to update the runtime value of this field in the store via the given Mutator.
105107
func (e *Entry[T]) SetRuntimeValue(ctx context.Context, m Mutator, val T) error {
106-
key, err := e.key()
108+
name, err := e.name()
107109
if err != nil {
108110
return err
109111
}
110112

111-
return m.UpsertRuntimeSetting(ctx, key, val.String())
113+
return m.UpsertRuntimeSetting(ctx, name, val.String())
112114
}
113115

114116
// UnsetRuntimeValue removes the runtime value from the store.
115117
func (e *Entry[T]) UnsetRuntimeValue(ctx context.Context, m Mutator) error {
116-
key, err := e.key()
118+
name, err := e.name()
117119
if err != nil {
118120
return err
119121
}
120122

121-
return m.DeleteRuntimeSetting(ctx, key)
123+
return m.DeleteRuntimeSetting(ctx, name)
122124
}
123125

124126
// Resolve attempts to resolve the runtime value of this field from the store via the given Resolver.
125127
func (e *Entry[T]) Resolve(ctx context.Context, r Resolver) (T, error) {
126128
var zero T
127129

128-
key, err := e.key()
130+
name, err := e.name()
129131
if err != nil {
130132
return zero, err
131133
}
132134

133-
val, err := r.GetRuntimeSetting(ctx, key)
135+
val, err := r.GetRuntimeSetting(ctx, name)
134136
if err != nil {
135137
return zero, err
136138
}

coderd/runtimeconfig/config_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ func TestUsage(t *testing.T) {
6161
// The value has to now be retrieved from a StartupValue() call.
6262
require.Equal(t, "localhost:1234", field.StartupValue().String())
6363

64-
// One new constraint is that we have to set the key on the runtimeconfig.Entry.
65-
// Attempting to perform any operation which accesses the store will enforce the need for a key.
64+
// One new constraint is that we have to set the name on the runtimeconfig.Entry.
65+
// Attempting to perform any operation which accesses the store will enforce the need for a name.
6666
_, err := field.Resolve(ctx, resolver)
67-
require.ErrorIs(t, err, runtimeconfig.ErrKeyNotSet)
67+
require.ErrorIs(t, err, runtimeconfig.ErrNameNotSet)
6868

69-
// Let's see that key. The environment var name is likely to be the most stable.
70-
field.SetKey(opt.Env)
69+
// Let's set that name; the environment var name is likely to be the most stable.
70+
field.Initialize(opt.Env)
7171

7272
newVal := serpent.HostPort{Host: "12.34.56.78", Port: "1234"}
7373
// Now that we've set it, we can update the runtime value of this field, which modifies given store.
@@ -94,19 +94,19 @@ func TestConfig(t *testing.T) {
9494

9595
require.Panics(t, func() {
9696
// "hello" cannot be set on a *serpent.Float64 field.
97-
runtimeconfig.MustNew[*serpent.Float64]("key", "hello")
97+
runtimeconfig.MustNew[*serpent.Float64]("my-field", "hello")
9898
})
9999

100100
require.NotPanics(t, func() {
101-
runtimeconfig.MustNew[*serpent.Float64]("key", "91.1234")
101+
runtimeconfig.MustNew[*serpent.Float64]("my-field", "91.1234")
102102
})
103103
})
104104

105105
t.Run("zero", func(t *testing.T) {
106106
t.Parallel()
107107

108108
// A zero-value declaration of a runtimeconfig.Entry should behave as a zero value of the generic type.
109-
// NB! A key has not been set for this entry.
109+
// NB! A name has not been set for this entry; it is "uninitialized".
110110
var field runtimeconfig.Entry[*serpent.Bool]
111111
var zero serpent.Bool
112112
require.Equal(t, field.StartupValue().Value(), zero.Value())
@@ -116,10 +116,10 @@ func TestConfig(t *testing.T) {
116116

117117
// But attempting to resolve will produce an error.
118118
_, err := field.Resolve(context.Background(), runtimeconfig.NewNoopResolver())
119-
require.ErrorIs(t, err, runtimeconfig.ErrKeyNotSet)
119+
require.ErrorIs(t, err, runtimeconfig.ErrNameNotSet)
120120
// But attempting to set the runtime value will produce an error.
121121
val := serpent.BoolOf(ptr.Ref(true))
122-
require.ErrorIs(t, field.SetRuntimeValue(context.Background(), runtimeconfig.NewNoopMutator(), val), runtimeconfig.ErrKeyNotSet)
122+
require.ErrorIs(t, field.SetRuntimeValue(context.Background(), runtimeconfig.NewNoopMutator(), val), runtimeconfig.ErrNameNotSet)
123123
})
124124

125125
t.Run("simple", func(t *testing.T) {

coderd/runtimeconfig/spec.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ package runtimeconfig
22

33
import "context"
44

5+
type Initializer interface {
6+
Initialize(name string)
7+
}
8+
59
// Resolver is an interface for resolving runtime settings.
610
type Resolver interface {
7-
// GetRuntimeSetting gets a runtime setting by key.
8-
GetRuntimeSetting(ctx context.Context, key string) (string, error)
11+
// GetRuntimeSetting gets a runtime setting by name.
12+
GetRuntimeSetting(ctx context.Context, name string) (string, error)
913
}
1014

1115
// Mutator is an interface for mutating runtime settings.
1216
type Mutator interface {
13-
// UpsertRuntimeSetting upserts a runtime setting by key.
14-
UpsertRuntimeSetting(ctx context.Context, key, val string) error
15-
// DeleteRuntimeSetting deletes a runtime setting by key.
16-
DeleteRuntimeSetting(ctx context.Context, key string) error
17+
// UpsertRuntimeSetting upserts a runtime setting by name.
18+
UpsertRuntimeSetting(ctx context.Context, name, val string) error
19+
// DeleteRuntimeSetting deletes a runtime setting by name.
20+
DeleteRuntimeSetting(ctx context.Context, name string) error
1721
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy