-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||
| } |
| 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) | ||
| } |
| 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) | ||
| } |
| 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) | ||
| } | ||
| } | ||
| } | ||
| 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) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ import ( | |
| "golang.org/x/oauth2" | ||
|
|
||
| "github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/gateway" | ||
| "github.com/NVIDIA/OpenShell/sdk/go/openshell/v1/internal/options" | ||
| ) | ||
|
|
||
| // ClientCredentials performs a non-interactive OAuth2 client credentials | ||
|
|
@@ -37,9 +38,7 @@ func ClientCredentials(ctx context.Context, opts ...LoginOption) (*oauth2.Token, | |
|
|
||
| func resolveClientCredentialsConfig(opts ...LoginOption) (*loginConfig, error) { | ||
| cfg := &loginConfig{} | ||
| for _, opt := range opts { | ||
| opt(cfg) | ||
| } | ||
| options.Apply(cfg, opts) | ||
| cfg.applyDefaults() | ||
|
Comment on lines
+41
to
42
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also out of scope: Why do we apply defaults after applying the inputs? Does this mean that user inputs could be overridden?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. |
||
|
|
||
| // Client credentials should not send interactive scopes by default. | ||
|
|
||
| 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) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.