forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup.go
136 lines (114 loc) · 3.2 KB
/
lookup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package lookup
import (
"time"
"strings"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/mitchellh/mapstructure"
"github.com/spf13/cast"
)
//New providers lookup wrapper around given backend
func New(coreBackends ...core.ConfigBackend) *ConfigLookup {
return &ConfigLookup{backends: coreBackends}
}
//unmarshalOpts opts for unmarshal key function
type unmarshalOpts struct {
hooks []mapstructure.DecodeHookFunc
}
// UnmarshalOption describes a functional parameter unmarshaling
type UnmarshalOption func(o *unmarshalOpts)
// WithUnmarshalHookFunction provides an option to pass Custom Decode Hook Func
// for unmarshaling
func WithUnmarshalHookFunction(hookFunction mapstructure.DecodeHookFunc) UnmarshalOption {
return func(o *unmarshalOpts) {
o.hooks = append(o.hooks, hookFunction)
}
}
//ConfigLookup is wrapper for core.ConfigBackend which performs key lookup and unmarshalling
type ConfigLookup struct {
backends []core.ConfigBackend
}
//Lookup returns value for given key
func (c *ConfigLookup) Lookup(key string) (interface{}, bool) {
//loop through each backend to find the value by key, fallback to next one if not found
for _, backend := range c.backends {
if backend == nil {
continue
}
val, ok := backend.Lookup(key)
if ok {
return val, true
}
}
return nil, false
}
//GetBool returns bool value for given key
func (c *ConfigLookup) GetBool(key string) bool {
value, ok := c.Lookup(key)
if !ok {
return false
}
return cast.ToBool(value)
}
//GetString returns string value for given key
func (c *ConfigLookup) GetString(key string) string {
value, ok := c.Lookup(key)
if !ok {
return ""
}
return cast.ToString(value)
}
//GetLowerString returns lower case string value for given key
func (c *ConfigLookup) GetLowerString(key string) string {
value, ok := c.Lookup(key)
if !ok {
return ""
}
return strings.ToLower(cast.ToString(value))
}
//GetInt returns int value for given key
func (c *ConfigLookup) GetInt(key string) int {
value, ok := c.Lookup(key)
if !ok {
return 0
}
return cast.ToInt(value)
}
//GetDuration returns time.Duration value for given key
func (c *ConfigLookup) GetDuration(key string) time.Duration {
value, ok := c.Lookup(key)
if !ok {
return 0
}
return cast.ToDuration(value)
}
//UnmarshalKey unmarshals value for given key to rawval type
func (c *ConfigLookup) UnmarshalKey(key string, rawVal interface{}, opts ...UnmarshalOption) error {
value, ok := c.Lookup(key)
if !ok {
return nil
}
//mandatory hook func
var unmarshalHooks []mapstructure.DecodeHookFunc
unmarshalHooks = append(unmarshalHooks, mapstructure.StringToTimeDurationHookFunc())
//check for opts
unmarshalOptions := unmarshalOpts{}
for _, param := range opts {
param(&unmarshalOptions)
}
//compose multiple hook funcs to one if found in opts
hookFn := mapstructure.ComposeDecodeHookFunc(append(unmarshalHooks, unmarshalOptions.hooks...)...)
//build decoder
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: hookFn,
Result: rawVal,
})
if err != nil {
return err
}
//decode
return decoder.Decode(value)
}