2021-07-22 22:54:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-08-01 00:13:05 +00:00
|
|
|
"strconv"
|
2021-07-25 11:42:56 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-07-22 22:54:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errValueNotSet = errors.New("specified value not found")
|
|
|
|
errValueMismatch = errors.New("specified value has different format")
|
|
|
|
)
|
|
|
|
|
|
|
|
type moduleAttributeStore map[string]interface{}
|
|
|
|
|
2021-07-25 11:42:56 +00:00
|
|
|
func (m moduleAttributeStore) Expect(keys ...string) error {
|
|
|
|
var missing []string
|
|
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
if _, ok := m[k]; !ok {
|
|
|
|
missing = append(missing, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(missing) > 0 {
|
|
|
|
return errors.Errorf("missing key(s) %s", strings.Join(missing, ", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-01 00:13:05 +00:00
|
|
|
func (m moduleAttributeStore) MustBool(name string, defVal *bool) bool {
|
|
|
|
v, err := m.Bool(name)
|
|
|
|
if err != nil {
|
|
|
|
if defVal != nil {
|
|
|
|
return *defVal
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-07-25 11:42:56 +00:00
|
|
|
func (m moduleAttributeStore) MustDuration(name string, defVal *time.Duration) time.Duration {
|
|
|
|
v, err := m.Duration(name)
|
|
|
|
if err != nil {
|
|
|
|
if defVal != nil {
|
|
|
|
return *defVal
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-07-22 22:54:11 +00:00
|
|
|
func (m moduleAttributeStore) MustInt64(name string, defVal *int64) int64 {
|
|
|
|
v, err := m.Int64(name)
|
|
|
|
if err != nil {
|
|
|
|
if defVal != nil {
|
|
|
|
return *defVal
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m moduleAttributeStore) MustString(name string, defVal *string) string {
|
|
|
|
v, err := m.String(name)
|
|
|
|
if err != nil {
|
|
|
|
if defVal != nil {
|
|
|
|
return *defVal
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-08-01 00:13:05 +00:00
|
|
|
func (m moduleAttributeStore) Bool(name string) (bool, error) {
|
|
|
|
v, ok := m[name]
|
|
|
|
if !ok {
|
|
|
|
return false, errValueNotSet
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := v.(type) {
|
|
|
|
case bool:
|
|
|
|
return v, nil
|
|
|
|
case string:
|
|
|
|
bv, err := strconv.ParseBool(v)
|
|
|
|
return bv, errors.Wrap(err, "parsing string to bool")
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, errValueMismatch
|
|
|
|
}
|
|
|
|
|
2021-07-25 11:42:56 +00:00
|
|
|
func (m moduleAttributeStore) Duration(name string) (time.Duration, error) {
|
|
|
|
v, err := m.String(name)
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Wrap(err, "getting string value")
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := time.ParseDuration(v)
|
|
|
|
return d, errors.Wrap(err, "parsing value")
|
|
|
|
}
|
|
|
|
|
2021-07-22 22:54:11 +00:00
|
|
|
func (m moduleAttributeStore) Int64(name string) (int64, error) {
|
|
|
|
v, ok := m[name]
|
|
|
|
if !ok {
|
|
|
|
return 0, errValueNotSet
|
|
|
|
}
|
|
|
|
|
2021-07-31 18:58:11 +00:00
|
|
|
switch v := v.(type) {
|
2021-07-22 22:54:11 +00:00
|
|
|
case int:
|
2021-07-31 18:58:11 +00:00
|
|
|
return int64(v), nil
|
2021-07-22 22:54:11 +00:00
|
|
|
case int16:
|
2021-07-31 18:58:11 +00:00
|
|
|
return int64(v), nil
|
2021-07-22 22:54:11 +00:00
|
|
|
case int32:
|
2021-07-31 18:58:11 +00:00
|
|
|
return int64(v), nil
|
2021-07-22 22:54:11 +00:00
|
|
|
case int64:
|
2021-07-31 18:58:11 +00:00
|
|
|
return v, nil
|
2021-07-22 22:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0, errValueMismatch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m moduleAttributeStore) String(name string) (string, error) {
|
|
|
|
v, ok := m[name]
|
|
|
|
if !ok {
|
|
|
|
return "", errValueNotSet
|
|
|
|
}
|
|
|
|
|
|
|
|
if sv, ok := v.(string); ok {
|
|
|
|
return sv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if iv, ok := v.(fmt.Stringer); ok {
|
|
|
|
return iv.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", errValueMismatch
|
|
|
|
}
|
2021-07-30 00:08:39 +00:00
|
|
|
|
|
|
|
func (m moduleAttributeStore) StringSlice(name string) ([]string, error) {
|
|
|
|
v, ok := m[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, errValueNotSet
|
|
|
|
}
|
|
|
|
|
2021-07-31 18:58:11 +00:00
|
|
|
switch v := v.(type) {
|
2021-07-30 00:08:39 +00:00
|
|
|
case []string:
|
2021-07-31 18:58:11 +00:00
|
|
|
return v, nil
|
2021-07-30 00:08:39 +00:00
|
|
|
|
|
|
|
case []interface{}:
|
|
|
|
var out []string
|
|
|
|
|
2021-07-31 18:58:11 +00:00
|
|
|
for _, iv := range v {
|
2021-07-30 00:08:39 +00:00
|
|
|
sv, ok := iv.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("value in slice was not string")
|
|
|
|
}
|
|
|
|
out = append(out, sv)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errValueMismatch
|
|
|
|
}
|