mirror of
https://github.com/Luzifer/discord-community.git
synced 2024-12-20 10:21:22 +00:00
Add support for "Expect" check, add Duration support
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
00e4b2f859
commit
f59d4dc5b8
1 changed files with 41 additions and 1 deletions
|
@ -1,8 +1,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -12,6 +15,33 @@ var (
|
|||
|
||||
type moduleAttributeStore map[string]interface{}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (m moduleAttributeStore) MustInt64(name string, defVal *int64) int64 {
|
||||
v, err := m.Int64(name)
|
||||
if err != nil {
|
||||
|
@ -34,6 +64,16 @@ func (m moduleAttributeStore) MustString(name string, defVal *string) string {
|
|||
return v
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
func (m moduleAttributeStore) Int64(name string) (int64, error) {
|
||||
v, ok := m[name]
|
||||
if !ok {
|
||||
|
|
Loading…
Reference in a new issue