Skip to content

Commit

Permalink
[FAB-9238] Removed unmarshal lookup options
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
sudeshrshetty committed Apr 4, 2018
1 parent 582c2fc commit 4b15d65
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 117 deletions.
18 changes: 1 addition & 17 deletions pkg/common/providers/core/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,7 @@ type Providers interface {
//ConfigProvider provides config backend for SDK
type ConfigProvider func() (ConfigBackend, error)

//LookupOpts contains options for looking up key in config backend
type LookupOpts struct {
UnmarshalType interface{}
}

//LookupOption option to lookup key in config backend
type LookupOption func(opts *LookupOpts)

//ConfigBackend backend for all config types in SDK
type ConfigBackend interface {
//TODO lookupOption should be removed, unmarshal option should be handled externally
Lookup(key string, opts ...LookupOption) (interface{}, bool)
}

//WithUnmarshalType lookup option which can be used to unmarshal lookup value to provided type
func WithUnmarshalType(unmarshalType interface{}) LookupOption {
return func(opts *LookupOpts) {
opts.UnmarshalType = unmarshalType
}
Lookup(key string) (interface{}, bool)
}
13 changes: 4 additions & 9 deletions pkg/common/providers/test/mockcore/mockcore.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions pkg/core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
)

var logger = logging.NewLogger("fabsdk/core")

var logModules = [...]string{"fabsdk", "fabsdk/client", "fabsdk/core", "fabsdk/fab", "fabsdk/common",
"fabsdk/msp", "fabsdk/util", "fabsdk/context"}

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

// If a config file is found, read it in.
err = backend.configViper.MergeInConfig()
if err == nil {
logger.Debugf("Using config file: %s", backend.configViper.ConfigFileUsed())
} else {
if err != nil {
return nil, errors.Wrap(err, "loading config file failed")
}

Expand All @@ -77,7 +73,6 @@ func FromFile(name string, opts ...Option) core.ConfigProvider {
func FromRaw(configBytes []byte, configType string, opts ...Option) core.ConfigProvider {
return func() (core.ConfigBackend, error) {
buf := bytes.NewBuffer(configBytes)
logger.Debugf("config.FromRaw buf Len is %d, Cap is %d: %s", buf.Len(), buf.Cap(), buf)
return initFromReader(buf, configType, opts...)
}
}
Expand Down Expand Up @@ -154,7 +149,6 @@ func setLogLevel(backend core.ConfigBackend) {
logLevel := logging.INFO
if loggingLevelString != nil {
const logModule = "fabsdk" // TODO: allow more flexability in setting levels for different modules
logger.Debugf("%s logging level from the config: %v", logModule, loggingLevelString)
var err error
logLevel, err = logging.LogLevel(loggingLevelString.(string))
if err != nil {
Expand Down
18 changes: 1 addition & 17 deletions pkg/core/config/defbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ SPDX-License-Identifier: Apache-2.0
package config

import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/util/pathvar"
"github.com/pkg/errors"
"github.com/spf13/viper"
Expand All @@ -20,22 +19,7 @@ type defConfigBackend struct {
}

// Lookup gets the config item value by Key
func (c *defConfigBackend) Lookup(key string, opts ...core.LookupOption) (interface{}, bool) {
if len(opts) > 0 {
lookupOpts := &core.LookupOpts{}
for _, option := range opts {
option(lookupOpts)
}

if lookupOpts.UnmarshalType != nil {
err := c.configViper.UnmarshalKey(key, lookupOpts.UnmarshalType)
if err != nil {
//TODO may need debug logger here
return nil, false
}
return lookupOpts.UnmarshalType, true
}
}
func (c *defConfigBackend) Lookup(key string) (interface{}, bool) {
value := c.configViper.Get(key)
if value == nil {
return nil, false
Expand Down
19 changes: 16 additions & 3 deletions pkg/core/config/lookup/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/mitchellh/mapstructure"
"github.com/spf13/cast"
)

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

//UnmarshalKey unmarshals value for given key to rawval type
func (c *ConfigLookup) UnmarshalKey(key string, rawVal interface{}) bool {
_, ok := c.backend.Lookup(key, core.WithUnmarshalType(rawVal))
return ok
func (c *ConfigLookup) UnmarshalKey(key string, rawVal interface{}) error {
value, ok := c.backend.Lookup(key)
if !ok {
return nil
}

decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
Result: rawVal,
})
if err != nil {
return err
}

return decoder.Decode(value)
}
Loading

0 comments on commit 4b15d65

Please sign in to comment.