-
Notifications
You must be signed in to change notification settings - Fork 568
/
config_parser.go
95 lines (78 loc) · 2.58 KB
/
config_parser.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
package osmoutils
import (
"fmt"
"strconv"
"strings"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/spf13/cast"
)
// ParseBool parses a boolean value from a server type option.
func ParseBool(opts servertypes.AppOptions, groupOptName, optName string, defaultValue bool) bool {
fullOptName := "osmosis-sqs." + optName
valueInterface := opts.Get(fullOptName)
value := defaultValue
if valueInterface != nil {
valueStr, ok := valueInterface.(string)
if !ok {
panic("invalidly configured osmosis-sqs." + optName)
}
valueStr = strings.TrimSpace(valueStr)
v, err := strconv.ParseBool(valueStr)
if err != nil {
fmt.Println("error in parsing" + optName + " as bool, setting to false")
return false
}
return v
}
return value
}
// ParseInt parses an integer value from a server type option.
func ParseInt(opts servertypes.AppOptions, groupOptName, optName string) int {
valueInterface := opts.Get(groupOptName + "." + optName)
if valueInterface == nil {
panic("missing config for osmosis-sqs." + optName)
}
value := cast.ToInt(valueInterface)
return value
}
// ParseUint64 parses a uint64 value from a server type option.
func ParseUint64Slice(opts servertypes.AppOptions, groupOptName, optName string) []uint64 {
stringSlice := ParseString(opts, groupOptName, optName)
valueUint64Slice, err := ParseStringToUint64Slice(stringSlice)
if err != nil {
panic(fmt.Sprintf("invalidly configured osmosis-sqs.%s, err= %v", optName, err))
}
return valueUint64Slice
}
// ParseStringToUint64Slice parses a string to a slice of uint64 values.
func ParseStringToUint64Slice(input string) ([]uint64, error) {
// Remove "[" and "]" from the input string
input = strings.Trim(input, "[]")
// Split the string into individual values
values := strings.Split(input, ",")
// Initialize a slice to store uint64 values
var result []uint64
if len(values) == 1 && values[0] == "" {
return result, nil
}
// Iterate over the values and convert them to uint64
for _, v := range values {
// Parse the string value to uint64
u, err := strconv.ParseUint(strings.TrimSpace(v), 10, 64)
if err != nil {
return nil, err
}
// Append the uint64 value to the result slice
result = append(result, u)
}
return result, nil
}
// ParseString parses a string value from a server type option.
func ParseString(opts servertypes.AppOptions, groupOptName, optName string) string {
valueInterface := opts.Get(groupOptName + "." + optName)
if valueInterface == nil {
panic("missing config for osmosis-sqs." + optName)
}
value := cast.ToString(valueInterface)
return value
}