From 48a3c9347c582f9ff82d110dd7772f5b7956e4d9 Mon Sep 17 00:00:00 2001 From: Troy Ronda Date: Tue, 23 Jan 2018 11:22:43 -0500 Subject: [PATCH] [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 --- pkg/fabsdk/client_test.go | 2 +- pkg/fabsdk/fabsdk.go | 14 ++++++++------ pkg/fabsdk/fabsdk_test.go | 36 +++++++++++++++++++++++++++--------- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/pkg/fabsdk/client_test.go b/pkg/fabsdk/client_test.go index d9fa0dce24..db56cdc3f8 100644 --- a/pkg/fabsdk/client_test.go +++ b/pkg/fabsdk/client_test.go @@ -40,7 +40,7 @@ func TestFromConfigGoodClientOpt(t *testing.T) { t.Fatalf("Unexpected error from config: %v", err) } - sdk, err := fromConfig(c) + sdk, err := New(WithConfig(c)) if err != nil { t.Fatalf("Expected no error from New, but got %v", err) } diff --git a/pkg/fabsdk/fabsdk.go b/pkg/fabsdk/fabsdk.go index f9532ede80..46afa681a5 100644 --- a/pkg/fabsdk/fabsdk.go +++ b/pkg/fabsdk/fabsdk.go @@ -45,7 +45,7 @@ type options struct { type Option func(opts *options) error // New initializes the SDK based on the set of options provided. -// configProvider provides the application configuration and is required. +// configProvider provides the application configuration. func New(cp apiconfig.ConfigProvider, opts ...Option) (*FabricSDK, error) { pkgSuite := defPkgSuite{} config, err := cp() @@ -55,11 +55,13 @@ func New(cp apiconfig.ConfigProvider, opts ...Option) (*FabricSDK, error) { return fromPkgSuite(config, &pkgSuite, opts...) } -// fromConfig initializes the SDK based on the set of options provided. -// configProvider provides the application configuration and is required. -func fromConfig(config apiconfig.Config, opts ...Option) (*FabricSDK, error) { - pkgSuite := defPkgSuite{} - return fromPkgSuite(config, &pkgSuite, opts...) +// WithConfig converts a Config interface to a ConfigProvider. +// This is a helper function for those who already loaded the config +// prior to instantiating the SDK. +func WithConfig(config apiconfig.Config) apiconfig.ConfigProvider { + return func() (apiconfig.Config, error) { + return config, nil + } } // fromPkgSuite creates an SDK based on the implementations in the provided pkg suite. diff --git a/pkg/fabsdk/fabsdk_test.go b/pkg/fabsdk/fabsdk_test.go index b121cf8f49..5f5f93f89b 100644 --- a/pkg/fabsdk/fabsdk_test.go +++ b/pkg/fabsdk/fabsdk_test.go @@ -58,8 +58,12 @@ func TestNewDefaultSDK(t *testing.T) { t.Fatalf("Error initializing SDK: %s", err) } + verifySDK(t, sdk) +} + +func verifySDK(t *testing.T, sdk *FabricSDK) { // Default channel client (uses organisation from client configuration) - _, err = sdk.NewClient(WithUser(sdkValidClientUser)).Channel("mychannel") + _, err := sdk.NewClient(WithUser(sdkValidClientUser)).Channel("mychannel") if err != nil { t.Fatalf("Failed to create new channel client: %s", err) } @@ -78,7 +82,21 @@ func TestNewDefaultSDK(t *testing.T) { if err != nil { t.Fatalf("Failed to create new channel client: %s", err) } +} + +func TestWithConfigOpt(t *testing.T) { + // Test New SDK with valid config file + c, err := configImpl.FromFile(sdkConfigFile)() + if err != nil { + t.Fatalf("Unexpected error from config: %v", err) + } + + sdk, err := New(WithConfig(c)) + if err != nil { + t.Fatalf("Error initializing SDK: %s", err) + } + verifySDK(t, sdk) } func TestWithCorePkg(t *testing.T) { @@ -88,7 +106,7 @@ func TestWithCorePkg(t *testing.T) { t.Fatalf("Unexpected error from config: %v", err) } - _, err = fromConfig(c) + _, err = New(WithConfig(c)) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -102,7 +120,7 @@ func TestWithCorePkg(t *testing.T) { factory.EXPECT().NewSigningManager(nil, c).Return(nil, nil) factory.EXPECT().NewFabricProvider(c, nil, nil, nil).Return(nil, nil) - _, err = fromConfig(c, WithCorePkg(factory)) + _, err = New(WithConfig(c), WithCorePkg(factory)) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -115,7 +133,7 @@ func TestWithServicePkg(t *testing.T) { t.Fatalf("Unexpected error from config: %v", err) } - _, err = fromConfig(c) + _, err = New(WithConfig(c)) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -127,7 +145,7 @@ func TestWithServicePkg(t *testing.T) { factory.EXPECT().NewDiscoveryProvider(c).Return(nil, nil) factory.EXPECT().NewSelectionProvider(c).Return(nil, nil) - _, err = fromConfig(c, WithServicePkg(factory)) + _, err = New(WithConfig(c), WithServicePkg(factory)) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -145,7 +163,7 @@ func TestWithContextPkg(t *testing.T) { t.Fatalf("Error initializing core factory: %s", err) } - _, err = fromConfig(c) + sdk, err := New(WithConfig(c)) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -169,7 +187,7 @@ func TestWithContextPkg(t *testing.T) { factory.EXPECT().NewCredentialManager(sdkValidClientOrg1, c, core.cryptoSuite).Return(cm, nil) - sdk, err := fromConfig(c, WithCorePkg(core), WithContextPkg(factory)) + sdk, err = New(WithConfig(c), WithCorePkg(core), WithContextPkg(factory)) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -193,7 +211,7 @@ func TestWithSessionPkg(t *testing.T) { t.Fatalf("Error initializing core factory: %s", err) } - _, err = fromConfig(c) + _, err = New(WithConfig(c)) if err != nil { t.Fatalf("Error initializing SDK: %s", err) } @@ -203,7 +221,7 @@ func TestWithSessionPkg(t *testing.T) { defer mockCtrl.Finish() factory := mockapisdk.NewMockSessionClientFactory(mockCtrl) - sdk, err := fromConfig(c, WithCorePkg(core), WithSessionPkg(factory)) + sdk, err := New(WithConfig(c), WithCorePkg(core), WithSessionPkg(factory)) if err != nil { t.Fatalf("Error initializing SDK: %s", err) }