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

Commit 4b15d65

Browse files
committed
[FAB-9238] Removed unmarshal lookup options
- sdk will take care of unmarshalling backend lookup value to desired types - removed loggers from config loading functions to avoid default logger initialization Change-Id: Ibb1ba426ebf74743fdb0787f066df1217ba965b6 Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
1 parent 582c2fc commit 4b15d65

File tree

10 files changed

+303
-117
lines changed

10 files changed

+303
-117
lines changed

pkg/common/providers/core/provider.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,7 @@ type Providers interface {
2929
//ConfigProvider provides config backend for SDK
3030
type ConfigProvider func() (ConfigBackend, error)
3131

32-
//LookupOpts contains options for looking up key in config backend
33-
type LookupOpts struct {
34-
UnmarshalType interface{}
35-
}
36-
37-
//LookupOption option to lookup key in config backend
38-
type LookupOption func(opts *LookupOpts)
39-
4032
//ConfigBackend backend for all config types in SDK
4133
type ConfigBackend interface {
42-
//TODO lookupOption should be removed, unmarshal option should be handled externally
43-
Lookup(key string, opts ...LookupOption) (interface{}, bool)
44-
}
45-
46-
//WithUnmarshalType lookup option which can be used to unmarshal lookup value to provided type
47-
func WithUnmarshalType(unmarshalType interface{}) LookupOption {
48-
return func(opts *LookupOpts) {
49-
opts.UnmarshalType = unmarshalType
50-
}
34+
Lookup(key string) (interface{}, bool)
5135
}

pkg/common/providers/test/mockcore/mockcore.gen.go

Lines changed: 4 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/core/config/config.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
2020
)
2121

22-
var logger = logging.NewLogger("fabsdk/core")
23-
2422
var logModules = [...]string{"fabsdk", "fabsdk/client", "fabsdk/core", "fabsdk/fab", "fabsdk/common",
2523
"fabsdk/msp", "fabsdk/util", "fabsdk/context"}
2624

@@ -61,9 +59,7 @@ func FromFile(name string, opts ...Option) core.ConfigProvider {
6159

6260
// If a config file is found, read it in.
6361
err = backend.configViper.MergeInConfig()
64-
if err == nil {
65-
logger.Debugf("Using config file: %s", backend.configViper.ConfigFileUsed())
66-
} else {
62+
if err != nil {
6763
return nil, errors.Wrap(err, "loading config file failed")
6864
}
6965

@@ -77,7 +73,6 @@ func FromFile(name string, opts ...Option) core.ConfigProvider {
7773
func FromRaw(configBytes []byte, configType string, opts ...Option) core.ConfigProvider {
7874
return func() (core.ConfigBackend, error) {
7975
buf := bytes.NewBuffer(configBytes)
80-
logger.Debugf("config.FromRaw buf Len is %d, Cap is %d: %s", buf.Len(), buf.Cap(), buf)
8176
return initFromReader(buf, configType, opts...)
8277
}
8378
}
@@ -154,7 +149,6 @@ func setLogLevel(backend core.ConfigBackend) {
154149
logLevel := logging.INFO
155150
if loggingLevelString != nil {
156151
const logModule = "fabsdk" // TODO: allow more flexability in setting levels for different modules
157-
logger.Debugf("%s logging level from the config: %v", logModule, loggingLevelString)
158152
var err error
159153
logLevel, err = logging.LogLevel(loggingLevelString.(string))
160154
if err != nil {

pkg/core/config/defbackend.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ SPDX-License-Identifier: Apache-2.0
77
package config
88

99
import (
10-
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
1110
"github.com/hyperledger/fabric-sdk-go/pkg/util/pathvar"
1211
"github.com/pkg/errors"
1312
"github.com/spf13/viper"
@@ -20,22 +19,7 @@ type defConfigBackend struct {
2019
}
2120

2221
// Lookup gets the config item value by Key
23-
func (c *defConfigBackend) Lookup(key string, opts ...core.LookupOption) (interface{}, bool) {
24-
if len(opts) > 0 {
25-
lookupOpts := &core.LookupOpts{}
26-
for _, option := range opts {
27-
option(lookupOpts)
28-
}
29-
30-
if lookupOpts.UnmarshalType != nil {
31-
err := c.configViper.UnmarshalKey(key, lookupOpts.UnmarshalType)
32-
if err != nil {
33-
//TODO may need debug logger here
34-
return nil, false
35-
}
36-
return lookupOpts.UnmarshalType, true
37-
}
38-
}
22+
func (c *defConfigBackend) Lookup(key string) (interface{}, bool) {
3923
value := c.configViper.Get(key)
4024
if value == nil {
4125
return nil, false

pkg/core/config/lookup/lookup.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
13+
"github.com/mitchellh/mapstructure"
1314
"github.com/spf13/cast"
1415
)
1516

@@ -60,7 +61,19 @@ func (c *ConfigLookup) GetDuration(key string) time.Duration {
6061
}
6162

6263
//UnmarshalKey unmarshals value for given key to rawval type
63-
func (c *ConfigLookup) UnmarshalKey(key string, rawVal interface{}) bool {
64-
_, ok := c.backend.Lookup(key, core.WithUnmarshalType(rawVal))
65-
return ok
64+
func (c *ConfigLookup) UnmarshalKey(key string, rawVal interface{}) error {
65+
value, ok := c.backend.Lookup(key)
66+
if !ok {
67+
return nil
68+
}
69+
70+
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
71+
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
72+
Result: rawVal,
73+
})
74+
if err != nil {
75+
return err
76+
}
77+
78+
return decoder.Decode(value)
6679
}

0 commit comments

Comments
 (0)