Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 48a3c93

Browse files
committed
[FAB-7862] Add WithConfig helper method to fabsdk
For those that want to directly provide the underlying Config, this new helper converts a Config to a ConfigProvider. Example: sdk, err := fabsdk.New(fabsdk.WithConfig(c)) Change-Id: I5d6b370163bc80388f72d89766c197a3c19908f4 Signed-off-by: Troy Ronda <troy@troyronda.com>
1 parent 40b253e commit 48a3c93

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

pkg/fabsdk/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestFromConfigGoodClientOpt(t *testing.T) {
4040
t.Fatalf("Unexpected error from config: %v", err)
4141
}
4242

43-
sdk, err := fromConfig(c)
43+
sdk, err := New(WithConfig(c))
4444
if err != nil {
4545
t.Fatalf("Expected no error from New, but got %v", err)
4646
}

pkg/fabsdk/fabsdk.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type options struct {
4545
type Option func(opts *options) error
4646

4747
// New initializes the SDK based on the set of options provided.
48-
// configProvider provides the application configuration and is required.
48+
// configProvider provides the application configuration.
4949
func New(cp apiconfig.ConfigProvider, opts ...Option) (*FabricSDK, error) {
5050
pkgSuite := defPkgSuite{}
5151
config, err := cp()
@@ -55,11 +55,13 @@ func New(cp apiconfig.ConfigProvider, opts ...Option) (*FabricSDK, error) {
5555
return fromPkgSuite(config, &pkgSuite, opts...)
5656
}
5757

58-
// fromConfig initializes the SDK based on the set of options provided.
59-
// configProvider provides the application configuration and is required.
60-
func fromConfig(config apiconfig.Config, opts ...Option) (*FabricSDK, error) {
61-
pkgSuite := defPkgSuite{}
62-
return fromPkgSuite(config, &pkgSuite, opts...)
58+
// WithConfig converts a Config interface to a ConfigProvider.
59+
// This is a helper function for those who already loaded the config
60+
// prior to instantiating the SDK.
61+
func WithConfig(config apiconfig.Config) apiconfig.ConfigProvider {
62+
return func() (apiconfig.Config, error) {
63+
return config, nil
64+
}
6365
}
6466

6567
// fromPkgSuite creates an SDK based on the implementations in the provided pkg suite.

pkg/fabsdk/fabsdk_test.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ func TestNewDefaultSDK(t *testing.T) {
5858
t.Fatalf("Error initializing SDK: %s", err)
5959
}
6060

61+
verifySDK(t, sdk)
62+
}
63+
64+
func verifySDK(t *testing.T, sdk *FabricSDK) {
6165
// Default channel client (uses organisation from client configuration)
62-
_, err = sdk.NewClient(WithUser(sdkValidClientUser)).Channel("mychannel")
66+
_, err := sdk.NewClient(WithUser(sdkValidClientUser)).Channel("mychannel")
6367
if err != nil {
6468
t.Fatalf("Failed to create new channel client: %s", err)
6569
}
@@ -78,7 +82,21 @@ func TestNewDefaultSDK(t *testing.T) {
7882
if err != nil {
7983
t.Fatalf("Failed to create new channel client: %s", err)
8084
}
85+
}
86+
87+
func TestWithConfigOpt(t *testing.T) {
88+
// Test New SDK with valid config file
89+
c, err := configImpl.FromFile(sdkConfigFile)()
90+
if err != nil {
91+
t.Fatalf("Unexpected error from config: %v", err)
92+
}
93+
94+
sdk, err := New(WithConfig(c))
95+
if err != nil {
96+
t.Fatalf("Error initializing SDK: %s", err)
97+
}
8198

99+
verifySDK(t, sdk)
82100
}
83101

84102
func TestWithCorePkg(t *testing.T) {
@@ -88,7 +106,7 @@ func TestWithCorePkg(t *testing.T) {
88106
t.Fatalf("Unexpected error from config: %v", err)
89107
}
90108

91-
_, err = fromConfig(c)
109+
_, err = New(WithConfig(c))
92110
if err != nil {
93111
t.Fatalf("Error initializing SDK: %s", err)
94112
}
@@ -102,7 +120,7 @@ func TestWithCorePkg(t *testing.T) {
102120
factory.EXPECT().NewSigningManager(nil, c).Return(nil, nil)
103121
factory.EXPECT().NewFabricProvider(c, nil, nil, nil).Return(nil, nil)
104122

105-
_, err = fromConfig(c, WithCorePkg(factory))
123+
_, err = New(WithConfig(c), WithCorePkg(factory))
106124
if err != nil {
107125
t.Fatalf("Error initializing SDK: %s", err)
108126
}
@@ -115,7 +133,7 @@ func TestWithServicePkg(t *testing.T) {
115133
t.Fatalf("Unexpected error from config: %v", err)
116134
}
117135

118-
_, err = fromConfig(c)
136+
_, err = New(WithConfig(c))
119137
if err != nil {
120138
t.Fatalf("Error initializing SDK: %s", err)
121139
}
@@ -127,7 +145,7 @@ func TestWithServicePkg(t *testing.T) {
127145
factory.EXPECT().NewDiscoveryProvider(c).Return(nil, nil)
128146
factory.EXPECT().NewSelectionProvider(c).Return(nil, nil)
129147

130-
_, err = fromConfig(c, WithServicePkg(factory))
148+
_, err = New(WithConfig(c), WithServicePkg(factory))
131149
if err != nil {
132150
t.Fatalf("Error initializing SDK: %s", err)
133151
}
@@ -145,7 +163,7 @@ func TestWithContextPkg(t *testing.T) {
145163
t.Fatalf("Error initializing core factory: %s", err)
146164
}
147165

148-
_, err = fromConfig(c)
166+
sdk, err := New(WithConfig(c))
149167
if err != nil {
150168
t.Fatalf("Error initializing SDK: %s", err)
151169
}
@@ -169,7 +187,7 @@ func TestWithContextPkg(t *testing.T) {
169187

170188
factory.EXPECT().NewCredentialManager(sdkValidClientOrg1, c, core.cryptoSuite).Return(cm, nil)
171189

172-
sdk, err := fromConfig(c, WithCorePkg(core), WithContextPkg(factory))
190+
sdk, err = New(WithConfig(c), WithCorePkg(core), WithContextPkg(factory))
173191
if err != nil {
174192
t.Fatalf("Error initializing SDK: %s", err)
175193
}
@@ -193,7 +211,7 @@ func TestWithSessionPkg(t *testing.T) {
193211
t.Fatalf("Error initializing core factory: %s", err)
194212
}
195213

196-
_, err = fromConfig(c)
214+
_, err = New(WithConfig(c))
197215
if err != nil {
198216
t.Fatalf("Error initializing SDK: %s", err)
199217
}
@@ -203,7 +221,7 @@ func TestWithSessionPkg(t *testing.T) {
203221
defer mockCtrl.Finish()
204222
factory := mockapisdk.NewMockSessionClientFactory(mockCtrl)
205223

206-
sdk, err := fromConfig(c, WithCorePkg(core), WithSessionPkg(factory))
224+
sdk, err := New(WithConfig(c), WithCorePkg(core), WithSessionPkg(factory))
207225
if err != nil {
208226
t.Fatalf("Error initializing SDK: %s", err)
209227
}

0 commit comments

Comments
 (0)