forked from ipfs-cluster/ipfs-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
184 lines (166 loc) · 4.27 KB
/
util.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package config
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"time"
)
// Saver implements common functionality useful for ComponentConfigs
type Saver struct {
save chan struct{}
BaseDir string
}
// NotifySave signals the SaveCh() channel in a non-blocking fashion.
func (sv *Saver) NotifySave() {
if sv.save == nil {
sv.save = make(chan struct{}, 10)
}
// Non blocking, in case no one's listening
select {
case sv.save <- struct{}{}:
default:
logger.Warn("configuration save channel full")
}
}
// SaveCh returns a channel which is signaled when a component wants
// to persist its configuration
func (sv *Saver) SaveCh() <-chan struct{} {
if sv.save == nil {
sv.save = make(chan struct{})
}
return sv.save
}
// SetBaseDir is a setter for BaseDir and implements
// part of the ComponentConfig interface.
func (sv *Saver) SetBaseDir(dir string) {
sv.BaseDir = dir
}
// DefaultJSONMarshal produces pretty JSON with 2-space indentation
func DefaultJSONMarshal(v interface{}) ([]byte, error) {
bs, err := json.MarshalIndent(v, "", " ")
if err != nil {
return nil, err
}
return bs, nil
}
// SetIfNotDefault sets dest to the value of src if src is not the default
// value of the type.
// dest must be a pointer.
func SetIfNotDefault(src interface{}, dest interface{}) {
switch src.(type) {
case time.Duration:
t := src.(time.Duration)
if t != 0 {
*dest.(*time.Duration) = t
}
case string:
str := src.(string)
if str != "" {
*dest.(*string) = str
}
case uint64:
n := src.(uint64)
if n != 0 {
*dest.(*uint64) = n
}
case int:
n := src.(int)
if n != 0 {
*dest.(*int) = n
}
case float64:
n := src.(float64)
if n != 0 {
*dest.(*float64) = n
}
case bool:
b := src.(bool)
if b {
*dest.(*bool) = b
}
}
}
// DurationOpt provides a datatype to use with ParseDurations
type DurationOpt struct {
// The duration we need to parse
Duration string
// Where to store the result
Dst *time.Duration
// A variable name associated to it for helpful errors.
Name string
}
// ParseDurations takes a time.Duration src and saves it to the given dst.
func ParseDurations(component string, args ...*DurationOpt) error {
for _, arg := range args {
if arg.Duration == "" {
// don't do anything. Let the destination field
// stay at its default.
continue
}
t, err := time.ParseDuration(arg.Duration)
if err != nil {
return fmt.Errorf(
"error parsing %s.%s: %s",
component,
arg.Name,
err,
)
}
*arg.Dst = t
}
return nil
}
type hiddenField struct{}
func (hf hiddenField) MarshalJSON() ([]byte, error) {
return []byte(`"XXX_hidden_XXX"`), nil
}
func (hf hiddenField) UnmarshalJSON(b []byte) error { return nil }
// DisplayJSON takes pointer to a JSON-friendly configuration struct and
// returns the JSON-encoded representation of it filtering out any struct
// fields marked with the tag `hidden:"true"`, but keeping fields marked
// with `"json:omitempty"`.
func DisplayJSON(cfg interface{}) ([]byte, error) {
cfg = reflect.Indirect(reflect.ValueOf(cfg)).Interface()
origStructT := reflect.TypeOf(cfg)
if origStructT.Kind() != reflect.Struct {
panic("the given argument should be a struct")
}
hiddenFieldT := reflect.TypeOf(hiddenField{})
// create a new struct type with same fields
// but setting hidden fields as hidden.
finalStructFields := []reflect.StructField{}
for i := 0; i < origStructT.NumField(); i++ {
f := origStructT.Field(i)
hidden := f.Tag.Get("hidden") == "true"
if f.PkgPath != "" { // skip unexported
continue
}
if hidden {
f.Type = hiddenFieldT
}
// remove omitempty from tag, ignore other tags except json
var jsonTags []string
for _, s := range strings.Split(f.Tag.Get("json"), ",") {
if s != "omitempty" {
jsonTags = append(jsonTags, s)
}
}
f.Tag = reflect.StructTag(fmt.Sprintf("json:\"%s\"", strings.Join(jsonTags, ",")))
finalStructFields = append(finalStructFields, f)
}
// Parse the original JSON into the new
// struct and re-convert it to JSON.
finalStructT := reflect.StructOf(finalStructFields)
finalValue := reflect.New(finalStructT)
data := finalValue.Interface()
origJSON, err := json.Marshal(cfg)
if err != nil {
return nil, err
}
err = json.Unmarshal(origJSON, data)
if err != nil {
return nil, err
}
return DefaultJSONMarshal(data)
}