Skip to content

Commit b149429

Browse files
authored
fix(experimental): NewScopedClient can now only take one context argument (#309)
Currently, NewScopedClient takes a variadic (zero or more) list of contexts to initially seed the scoped client. However, this means it's possible to pass in _zero_ contexts, resulting in an invalid state: a scoped client with zero contexts. To avoid this scenario, I propose changing `NewScopedClient` to only take in one context, which could be a multi-context. This is the simplest implementation choice and it doesn't block any of the common use cases I anticipate: adding 1 or N contexts will still be possible with this syntax.
1 parent 8345859 commit b149429

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

ldclient_scoped.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,23 @@ type LDScopedClient struct {
7777
// methods as LDClient, like BoolVariation et al., but without the
7878
// ldcontext.Context parameter.
7979
//
80-
// You may pass one or more contexts to NewScopedClient. All contexts will be
81-
// combined into a multi-context when the scoped client is used. Providing a
80+
// You may pass a single-kind or multi-kind context to NewScopedClient. Providing a
8281
// multi-context to NewScopedClient is equivalent to providing all of its
83-
// individual contexts separately.
82+
// individual contexts separately via [LDScopedClient.AddContext].
8483
//
8584
// For more information on how to use the scoped client, read the documentation
8685
// for LDScopedClient.
8786
//
8887
// This function is not stable, and not subject to any backwards compatibility
8988
// guarantees or semantic versioning. It is not suitable for production usage. Do
9089
// not use it. You have been warned.
91-
func NewScopedClient(client *LDClient, contexts ...ldcontext.Context) *LDScopedClient {
90+
func NewScopedClient(client *LDClient, context ldcontext.Context) *LDScopedClient {
9291
cc := &LDScopedClient{
9392
client: client,
9493
contexts: make(map[ldcontext.Kind]ldcontext.Context),
9594
rebuild: true,
9695
}
97-
cc.AddContext(contexts...)
96+
cc.AddContext(context)
9897
return cc
9998
}
10099

ldclient_scoped_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ func TestScopedClientCollectsContexts(t *testing.T) {
3636
client := makeTestClientWithConfig(func(config *Config) {
3737
config.Logging = ldcomponents.Logging().Loggers(logCapture.Loggers)
3838
})
39-
c := NewScopedClient(client, ldctx1, ldctx2)
40-
41-
c.AddContext(ldctx3, ldctx4)
39+
c := NewScopedClient(client, ldctx2)
40+
c.AddContext(ldctx1, ldctx3, ldctx4)
4241

4342
assert.Equal(t, ldcontext.NewMulti(ldctx1, ldctx2, ldctx3, ldctx4), c.CurrentContext())
4443
assert.Empty(t, logCapture.GetOutput(ldlog.Warn))
@@ -59,7 +58,7 @@ func TestScopedClientCollectsContexts(t *testing.T) {
5958

6059
t.Run("overwriting contexts", func(t *testing.T) {
6160
client := makeTestClient()
62-
c := NewScopedClient(client, ldctx1, ldctx2, ldctx3, ldctx4)
61+
c := NewScopedClient(client, ldcontext.NewMulti(ldctx1, ldctx2, ldctx3, ldctx4))
6362

6463
c.OverwriteContextByKind(dupeCtx)
6564
c.OverwriteContextByKind(ldctx2, ldctx3)

0 commit comments

Comments
 (0)