-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
77 lines (65 loc) · 1.76 KB
/
config.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
package forpay
import (
"reflect"
"strconv"
"time"
)
// Config provides configurations to change api client's behaviuor.
type Config struct {
AutoRetry bool `default:"true"`
MaxRetryTime int `default:"3"`
UserAgent string `default:""`
Debug bool `default:"false"`
Timeout time.Duration
}
// DefaultConfig returns config with default configurations.
func DefaultConfig() *Config {
config := &Config{}
initConfigWithDefaultTag(config)
return config
}
// EnableAutoRetry enables auto retry feature.
func (c *Config) EnableAutoRetry() *Config {
panic("not implemented yet")
// c.AutoRetry = true
// return c
}
// DisableAutoRetry disables auto retry feature.
func (c *Config) DisableAutoRetry() *Config {
c.AutoRetry = false
return c
}
// WithMaxRetryTime sets the maximum retry time of an api call.
func (c *Config) WithMaxRetryTime(maxRetryTime int) *Config {
c.MaxRetryTime = maxRetryTime
return c
}
// WithTimeout sets request timeout for client.
func (c *Config) WithTimeout(timeout time.Duration) *Config {
c.Timeout = timeout
return c
}
func initConfigWithDefaultTag(src interface{}) {
configType := reflect.TypeOf(src)
for i := 0; i < configType.Elem().NumField(); i++ {
field := configType.Elem().Field(i)
defaultValue := field.Tag.Get("default")
if defaultValue == "" {
continue
}
setter := reflect.ValueOf(src).Elem().Field(i)
switch field.Type.String() {
case "int":
intValue, _ := strconv.ParseInt(defaultValue, 10, 64)
setter.SetInt(intValue)
case "time.Duration":
intValue, _ := strconv.ParseInt(defaultValue, 10, 64)
setter.SetInt(intValue)
case "string":
setter.SetString(defaultValue)
case "bool":
boolValue, _ := strconv.ParseBool(defaultValue)
setter.SetBool(boolValue)
}
}
}