-
Notifications
You must be signed in to change notification settings - Fork 1.2k
refactor(sdk/go): unify functional-option handling with shared applier #3232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+357
−48
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package edge | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/internal/options" | ||
| ) | ||
|
|
||
| func TestTunnelOption_NilIgnored(t *testing.T) { | ||
| cfg := tunnelConfig{closeTimeout: defaultCloseTimeout} | ||
| options.Apply(&cfg, []TunnelOption{ | ||
| WithTunnelLogger(nil), | ||
| nil, | ||
| WithCloseTimeout(10 * time.Second), | ||
| }) | ||
| assert.Equal(t, 10*time.Second, cfg.closeTimeout) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package fake | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/types" | ||
| ) | ||
|
|
||
| func TestClientOption_NilIgnored(t *testing.T) { | ||
| hr := &types.HealthResult{Healthy: false, Version: "1.2.3"} | ||
| cu := &types.CurrentUser{Subject: "user-42", DisplayName: "Test User"} | ||
|
|
||
| fc := NewClient( | ||
| WithHealthResult(hr), | ||
| nil, | ||
| WithCurrentUser(cu), | ||
| ) | ||
| require.NotNil(t, fc) | ||
|
|
||
| healthClient := fc.health.(*fakeHealthClient) | ||
| assert.Equal(t, false, healthClient.result.Healthy) | ||
| assert.Equal(t, "1.2.3", healthClient.result.Version) | ||
| assert.Equal(t, "Test User", healthClient.currentUser.DisplayName) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package gateway | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/internal/options" | ||
| ) | ||
|
|
||
| func TestClientOption_NilIgnored(t *testing.T) { | ||
| var cfg clientConfig | ||
| options.Apply(&cfg, []ClientOption{ | ||
| WithTimeout(30 * time.Second), | ||
| nil, | ||
| WithLogger(nil), | ||
| }) | ||
| assert.Equal(t, 30*time.Second, cfg.timeout) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Package options provides a shared mechanism for applying functional options | ||
| // across all SDK entry points. It enforces a single nil-option rule: nil | ||
| // entries in an option list are silently ignored. | ||
| package options | ||
|
|
||
| // Apply folds opts into target in order, skipping nil entries. | ||
| // O is constrained to any named function type whose underlying type is | ||
| // func(*T), so callers write options.Apply(&cfg, opts) and both type | ||
| // parameters are inferred. | ||
| func Apply[T any, O ~func(*T)](target *T, opts []O) { | ||
|
elezar marked this conversation as resolved.
|
||
| for _, opt := range opts { | ||
| if opt != nil { | ||
| opt(target) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package options | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| // testConfig is a plain config struct for testing Apply. | ||
| type testConfig struct { | ||
| a int | ||
| b string | ||
| } | ||
|
|
||
| // testOption is a named option type, mirroring the SDK's pattern. | ||
| type testOption func(*testConfig) | ||
|
|
||
| func TestApply_EmptySlice(t *testing.T) { | ||
| var cfg testConfig | ||
| Apply(&cfg, []testOption{}) | ||
| assert.Equal(t, testConfig{}, cfg) | ||
| } | ||
|
|
||
| func TestApply_NilSlice(t *testing.T) { | ||
| var cfg testConfig | ||
| Apply[testConfig, testOption](&cfg, nil) | ||
| assert.Equal(t, testConfig{}, cfg) | ||
| } | ||
|
|
||
| func TestApply_AllNil(t *testing.T) { | ||
| var cfg testConfig | ||
| Apply(&cfg, []testOption{nil, nil, nil}) | ||
| assert.Equal(t, testConfig{}, cfg) | ||
| } | ||
|
|
||
| func TestApply_NilFirst(t *testing.T) { | ||
| var cfg testConfig | ||
| Apply(&cfg, []testOption{ | ||
| nil, | ||
| func(c *testConfig) { c.a = 1 }, | ||
| }) | ||
| assert.Equal(t, 1, cfg.a) | ||
| } | ||
|
|
||
| func TestApply_NilMiddle(t *testing.T) { | ||
| var cfg testConfig | ||
| Apply(&cfg, []testOption{ | ||
| func(c *testConfig) { c.a = 1 }, | ||
| nil, | ||
| func(c *testConfig) { c.b = "two" }, | ||
| }) | ||
| assert.Equal(t, 1, cfg.a) | ||
| assert.Equal(t, "two", cfg.b) | ||
| } | ||
|
|
||
| func TestApply_NilLast(t *testing.T) { | ||
| var cfg testConfig | ||
| Apply(&cfg, []testOption{ | ||
| func(c *testConfig) { c.a = 1 }, | ||
| nil, | ||
| }) | ||
| assert.Equal(t, 1, cfg.a) | ||
| } | ||
|
|
||
| func TestApply_OrderPreserved(t *testing.T) { | ||
| var cfg testConfig | ||
| Apply(&cfg, []testOption{ | ||
| func(c *testConfig) { c.a = 1 }, | ||
| func(c *testConfig) { c.a = 2 }, | ||
| func(c *testConfig) { c.a = 3 }, | ||
| }) | ||
| // The last option wins because options apply in order. | ||
| assert.Equal(t, 3, cfg.a) | ||
| } | ||
|
|
||
| // liveObject simulates the fake.Client pattern where options mutate a live | ||
| // object rather than a plain config struct. | ||
| type liveObject struct { | ||
| name string | ||
| count int | ||
| } | ||
|
|
||
| type liveOption func(*liveObject) | ||
|
|
||
| func TestApply_LiveObjectTarget(t *testing.T) { | ||
| obj := &liveObject{name: "initial", count: 0} | ||
| Apply(obj, []liveOption{ | ||
| nil, | ||
| func(o *liveObject) { o.name = "updated" }, | ||
| nil, | ||
| func(o *liveObject) { o.count = 42 }, | ||
| }) | ||
| assert.Equal(t, "updated", obj.name) | ||
| assert.Equal(t, 42, obj.count) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package oidc | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/internal/options" | ||
| ) | ||
|
|
||
| func TestLoginOption_NilIgnored(t *testing.T) { | ||
| var cfg loginConfig | ||
| options.Apply(&cfg, []LoginOption{ | ||
| WithIssuer("https://auth.example.com"), | ||
| nil, | ||
| WithClientID("my-app"), | ||
| }) | ||
| assert.Equal(t, "https://auth.example.com", cfg.issuer) | ||
| assert.Equal(t, "my-app", cfg.clientID) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.