Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfa7193e

Browse files
dannykoppingEmyrk
authored andcommitted
Initializer interface, rename "key" to "name"
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent43e9e85 commitfa7193e

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-
varErrKeyNotSet=xerrors.New("key is not set")
12+
varErrNameNotSet=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
typeEntry[TValue]struct {
22-
kstring
23-
vT
23+
nstring
24+
innerT
2425
}
2526

26-
// New creates a new T instance with a definedkey and value.
27-
funcNew[TValue](key,valstring) (outEntry[T],errerror) {
28-
out.k=key
27+
// New creates a new T instance with a definedname and value.
28+
funcNew[TValue](name,valstring) (outEntry[T],errerror) {
29+
out.n=name
2930

3031
iferr=out.SetStartupValue(val);err!=nil {
3132
returnout,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-
funcMustNew[TValue](key,valstring)Entry[T] {
39-
out,err:=New[T](key,val)
39+
funcMustNew[TValue](name,valstring)Entry[T] {
40+
out,err:=New[T](name,val)
4041
iferr!=nil {
4142
panic(err)
4243
}
4344
returnout
4445
}
4546

47+
// Initialize sets the entry's name, and initializes the value.
48+
func (e*Entry[T])Initialize(namestring) {
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-
ifreflect.ValueOf(e.v).IsNil() {
49-
e.v=create[T]()
55+
ifreflect.ValueOf(e.inner).IsNil() {
56+
e.inner=create[T]()
5057
}
51-
returne.v
58+
returne.inner
5259
}
5360

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

60-
returne.k,nil
61-
}
62-
63-
// SetKey allows the key to be set.
64-
func (e*Entry[T])SetKey(kstring) {
65-
e.k=k
67+
returne.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,mMutator,valT)error {
106-
key,err:=e.key()
108+
name,err:=e.name()
107109
iferr!=nil {
108110
returnerr
109111
}
110112

111-
returnm.UpsertRuntimeSetting(ctx,key,val.String())
113+
returnm.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,mMutator)error {
116-
key,err:=e.key()
118+
name,err:=e.name()
117119
iferr!=nil {
118120
returnerr
119121
}
120122

121-
returnm.DeleteRuntimeSetting(ctx,key)
123+
returnm.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,rResolver) (T,error) {
126128
varzeroT
127129

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

133-
val,err:=r.GetRuntimeSetting(ctx,key)
135+
val,err:=r.GetRuntimeSetting(ctx,name)
134136
iferr!=nil {
135137
returnzero,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 thekey on the runtimeconfig.Entry.
65-
// Attempting to perform any operation which accesses the store will enforce the need for akey.
64+
// One new constraint is that we have to set thename on the runtimeconfig.Entry.
65+
// Attempting to perform any operation which accesses the store will enforce the need for aname.
6666
_,err:=field.Resolve(ctx,resolver)
67-
require.ErrorIs(t,err,runtimeconfig.ErrKeyNotSet)
67+
require.ErrorIs(t,err,runtimeconfig.ErrNameNotSet)
6868

69-
// Let'ssee thatkey. The environment var name is likely to be the most stable.
70-
field.SetKey(opt.Env)
69+
// Let'sset thatname; 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! Akey has not been set for this entry.
109+
// NB! Aname has not been set for this entry; it is "uninitialized".
110110
varfield runtimeconfig.Entry[*serpent.Bool]
111111
varzero 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+
typeInitializerinterface {
6+
Initialize(namestring)
7+
}
8+
59
// Resolver is an interface for resolving runtime settings.
610
typeResolverinterface {
7-
// GetRuntimeSetting gets a runtime setting bykey.
8-
GetRuntimeSetting(ctx context.Context,keystring) (string,error)
11+
// GetRuntimeSetting gets a runtime setting byname.
12+
GetRuntimeSetting(ctx context.Context,namestring) (string,error)
913
}
1014

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp