forked from tucnak/telebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
141 lines (119 loc) · 3.21 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
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
package layout
import (
"strconv"
"time"
"github.com/goccy/go-yaml"
"github.com/spf13/cast"
tele "gopkg.in/telebot.v3"
)
// Config represents typed map interface related to the "config" section in layout.
type Config struct {
v map[string]interface{}
}
// Unmarshal parses the config into the out value. It's useful when you want to
// describe and to pre-define the fields in your custom configuration struct.
func (c *Config) Unmarshal(v interface{}) error {
data, err := yaml.Marshal(c.v)
if err != nil {
return err
}
return yaml.Unmarshal(data, v)
}
// Get returns child map field wrapped into Config.
// If the field isn't a map, returns nil.
func (c *Config) Get(k string) *Config {
v, ok := c.v[k].(map[string]interface{})
if !ok {
return nil
}
return &Config{v: v}
}
// Index returns an i element from the array field, wrapped into Config.
// If the element isn't a map, returns nil. See also: Strings, Ints, Floats.
func (c *Config) Index(k string, i int) *Config {
a, ok := c.v[k].([]interface{})
if !ok {
return nil
}
if i >= len(a) {
return nil
}
v, ok := a[i].(map[string]interface{})
if !ok {
return nil
}
return &Config{v: v}
}
// Each iterates over the array field. Use it only with map elements.
func (c *Config) Each(k string, f func(int, *Config)) {
a, ok := c.v[k].([]interface{})
if !ok {
return
}
for i, e := range a {
v, ok := e.(map[string]interface{})
if ok {
f(i, &Config{v: v})
}
}
}
// String returns a field casted to the string.
func (c *Config) String(k string) string {
return cast.ToString(c.v[k])
}
// Int returns a field casted to the int.
func (c *Config) Int(k string) int {
return cast.ToInt(c.v[k])
}
// Int64 returns a field casted to the int64.
func (c *Config) Int64(k string) int64 {
return cast.ToInt64(c.v[k])
}
// Float returns a field casted to the float64.
func (c *Config) Float(k string) float64 {
return cast.ToFloat64(c.v[k])
}
// Bool returns a field casted to the bool.
func (c *Config) Bool(k string) bool {
return cast.ToBool(c.v[k])
}
// Duration returns a field casted to the time.Duration.
// Accepts number-represented duration or a string in 0nsuµmh format.
func (c *Config) Duration(k string) time.Duration {
return cast.ToDuration(c.v[k])
}
// ChatID returns a field casted to the ChatID.
// The value must be an integer.
func (c *Config) ChatID(k string) tele.ChatID {
return tele.ChatID(c.Int64(k))
}
// Len returns the array field length.
func (c *Config) Len(k string) int {
a := c.v[k].([]interface{})
return len(a)
}
// Strings returns a field casted to the string slice.
func (c *Config) Strings(k string) []string {
return cast.ToStringSlice(c.v[k])
}
// Ints returns a field casted to the int slice.
func (c *Config) Ints(k string) []int {
return cast.ToIntSlice(c.v[k])
}
// Int64s returns a field casted to the int64 slice.
func (c *Config) Int64s(k string) (ints []int64) {
for _, s := range c.Strings(k) {
i, _ := strconv.ParseInt(s, 10, 64)
ints = append(ints, i)
}
return ints
}
// Floats returns a field casted to the float slice.
func (c *Config) Floats(k string) []float64 {
slice := cast.ToSlice(c.v[k])
fs := make([]float64, len(slice))
for i, a := range slice {
fs[i] = cast.ToFloat64(a)
}
return fs
}