Skip to content

Commit 32cd26f

Browse files
committed
refactor(sdk/go): unify functional-option handling with shared applier
Introduce a single generic applier, options.Apply[T any, O ~func(*T)], in a new leaf package openshell/v1/internal/options, and route all option-application sites through it. Nil option entries are now ignored uniformly instead of panicking; non-nil options still apply in order, and each site keeps its existing pre-seeding and defaulting. - Add internal/options package with Apply and unit tests - Route the 15 application sites (root, types, oidc, fake, gateway, edge) through options.Apply; exported types.Apply*Options keep their signatures and delegate to it - Add one nil-handling test per affected package - Document the nil-option rule in README.md and the docs book No public API signature changes. Closes NVIDIA#3213 Signed-off-by: Roland Huß <[email protected]>
1 parent 118b250 commit 32cd26f

21 files changed

Lines changed: 357 additions & 48 deletions

‎sdk/go/README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ patterns will look familiar:
2828
- **Watch primitives**: channel-based watchers with `ResultChan()` and `Stop()`,
2929
identical to `watch.Interface` in client-go
3030
- **Functional options**: variadic option patterns for list filtering,
31-
pagination, and watch configuration
31+
pagination, and watch configuration. Nil options are silently ignored
32+
at every entry point, so conditional option lists are safe to pass
33+
without filtering out nil entries.
3234
- **Composable auth with token refresh**: wraps `oauth2.TokenSource` for
3335
automatic token caching and coalesced refresh, following the k8s client-go
3436
`cachingTokenSource` pattern

‎sdk/go/docs/src/architecture.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,27 @@ profiles, err := client.Providers().Profiles().List(ctx, "default")
8080
status, err := client.Providers().Refresh().GetStatus(ctx, "default", "openai", "api-key")
8181
```
8282

83+
## Functional Options
84+
85+
Many SDK entry points accept variadic option parameters that configure the call:
86+
87+
```go
88+
auth, err := v1.RefreshableToken(tokenSource,
89+
v1.WithLeeway(30*time.Second),
90+
v1.WithLogger(logger),
91+
)
92+
```
93+
94+
Options are applied in the order they are passed, so later options override earlier ones when they set the same field. **Nil options are silently ignored at every entry point.** You can safely build option lists conditionally without filtering out nil entries:
95+
96+
```go
97+
opts := []v1.RefreshOption{
98+
v1.WithLeeway(30 * time.Second),
99+
maybeLogger(), // may return nil
100+
}
101+
auth, err := v1.RefreshableToken(tokenSource, opts...)
102+
```
103+
83104
## gRPC Layer
84105

85106
Underneath, the SDK communicates with the OpenShell gateway over gRPC. The single `OpenShell` service in `proto/openshell.proto` defines all RPCs. The SDK maps each interface method to one or more RPCs:

‎sdk/go/openshell/v1/auth_refresh.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"golang.org/x/oauth2"
1313
"golang.org/x/sync/singleflight"
1414

15+
"github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/internal/options"
1516
"github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/types"
1617
)
1718

@@ -160,9 +161,7 @@ func RefreshableToken(src oauth2.TokenSource, opts ...RefreshOption) (AuthProvid
160161
}
161162

162163
cfg := defaultRefreshConfig()
163-
for _, o := range opts {
164-
o(&cfg)
165-
}
164+
options.Apply(&cfg, opts)
166165

167166
return &refreshableAuth{
168167
source: src,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package edge
5+
6+
import (
7+
"testing"
8+
"time"
9+
10+
"github.com/stretchr/testify/assert"
11+
12+
"github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/internal/options"
13+
)
14+
15+
func TestTunnelOption_NilIgnored(t *testing.T) {
16+
cfg := tunnelConfig{closeTimeout: defaultCloseTimeout}
17+
options.Apply(&cfg, []TunnelOption{
18+
WithTunnelLogger(nil),
19+
nil,
20+
WithCloseTimeout(10 * time.Second),
21+
})
22+
assert.Equal(t, 10*time.Second, cfg.closeTimeout)
23+
}

‎sdk/go/openshell/v1/edge/tunnel.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"github.com/coder/websocket"
1919

20+
"github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/internal/options"
2021
"github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/types"
2122
)
2223

@@ -107,9 +108,7 @@ func NewTunnelProxy(gatewayURL, edgeToken string, opts ...TunnelOption) (*Tunnel
107108
cfg := tunnelConfig{
108109
closeTimeout: defaultCloseTimeout,
109110
}
110-
for _, o := range opts {
111-
o(&cfg)
112-
}
111+
options.Apply(&cfg, opts)
113112

114113
listener, err := net.Listen("tcp", "127.0.0.1:0")
115114
if err != nil {

‎sdk/go/openshell/v1/fake/fake.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sync"
99

1010
v1 "github.com/NVIDIA/OpenShell/sdk/go/openshell/v1"
11+
"github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/internal/options"
1112
"github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/types"
1213
)
1314

@@ -97,9 +98,7 @@ func NewClient(opts ...ClientOption) *Client {
9798
fc.workspaces = newFakeWorkspaceClient(fc.workspaceStore, fc.memberStore, fc.isClosed)
9899
fc.inference = newFakeInferenceClient(fc.isClosed)
99100

100-
for _, opt := range opts {
101-
opt(fc)
102-
}
101+
options.Apply(fc, opts)
103102

104103
return fc
105104
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package fake
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
12+
"github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/types"
13+
)
14+
15+
func TestClientOption_NilIgnored(t *testing.T) {
16+
hr := &types.HealthResult{Healthy: false, Version: "1.2.3"}
17+
cu := &types.CurrentUser{Subject: "user-42", DisplayName: "Test User"}
18+
19+
fc := NewClient(
20+
WithHealthResult(hr),
21+
nil,
22+
WithCurrentUser(cu),
23+
)
24+
require.NotNil(t, fc)
25+
26+
healthClient := fc.health.(*fakeHealthClient)
27+
assert.Equal(t, false, healthClient.result.Healthy)
28+
assert.Equal(t, "1.2.3", healthClient.result.Version)
29+
assert.Equal(t, "Test User", healthClient.currentUser.DisplayName)
30+
}

‎sdk/go/openshell/v1/gateway/gateway.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88

99
v1 "github.com/NVIDIA/OpenShell/sdk/go/openshell/v1"
10+
"github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/internal/options"
1011
"github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/types"
1112
)
1213

@@ -27,9 +28,7 @@ func NewClient(name string, opts ...ClientOption) (*v1.Client, error) {
2728

2829
// Apply caller options.
2930
cc := &clientConfig{}
30-
for _, o := range opts {
31-
o(cc)
32-
}
31+
options.Apply(cc, opts)
3332

3433
// Resolve auth provider: caller override takes precedence.
3534
auth := cc.auth
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package gateway
5+
6+
import (
7+
"testing"
8+
"time"
9+
10+
"github.com/stretchr/testify/assert"
11+
12+
"github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/internal/options"
13+
)
14+
15+
func TestClientOption_NilIgnored(t *testing.T) {
16+
var cfg clientConfig
17+
options.Apply(&cfg, []ClientOption{
18+
WithTimeout(30 * time.Second),
19+
nil,
20+
WithLogger(nil),
21+
})
22+
assert.Equal(t, 30*time.Second, cfg.timeout)
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package options provides a shared mechanism for applying functional options
5+
// across all SDK entry points. It enforces a single nil-option rule: nil
6+
// entries in an option list are silently ignored.
7+
package options
8+
9+
// Apply folds opts into target in order, skipping nil entries.
10+
// O is constrained to any named function type whose underlying type is
11+
// func(*T), so callers write options.Apply(&cfg, opts) and both type
12+
// parameters are inferred.
13+
func Apply[T any, O ~func(*T)](target *T, opts []O) {
14+
for _, opt := range opts {
15+
if opt != nil {
16+
opt(target)
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)