2021-09-02 21:26:39 +00:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
2021-11-11 13:59:08 +00:00
|
|
|
"encoding/json"
|
2021-09-02 21:26:39 +00:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-11-11 13:59:08 +00:00
|
|
|
"sync"
|
2021-09-02 21:26:39 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrValueNotSet = errors.New("specified value not found")
|
|
|
|
ErrValueMismatch = errors.New("specified value has different format")
|
|
|
|
)
|
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
type FieldCollection struct {
|
|
|
|
data map[string]interface{}
|
|
|
|
lock sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFieldCollection creates a new FieldCollection with empty data store
|
|
|
|
func NewFieldCollection() *FieldCollection {
|
|
|
|
return &FieldCollection{data: make(map[string]interface{})}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FieldCollectionFromData is a wrapper around NewFieldCollection and SetFromData
|
|
|
|
func FieldCollectionFromData(data map[string]interface{}) *FieldCollection {
|
|
|
|
o := NewFieldCollection()
|
|
|
|
o.SetFromData(data)
|
|
|
|
return o
|
|
|
|
}
|
2021-09-02 21:26:39 +00:00
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
// CanBool tries to read key name as bool and checks whether error is nil
|
|
|
|
func (f *FieldCollection) CanBool(name string) bool {
|
2021-09-22 13:36:45 +00:00
|
|
|
_, err := f.Bool(name)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
// CanDuration tries to read key name as time.Duration and checks whether error is nil
|
|
|
|
func (f *FieldCollection) CanDuration(name string) bool {
|
2021-09-22 13:36:45 +00:00
|
|
|
_, err := f.Duration(name)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
// CanInt64 tries to read key name as int64 and checks whether error is nil
|
|
|
|
func (f *FieldCollection) CanInt64(name string) bool {
|
2021-09-22 13:36:45 +00:00
|
|
|
_, err := f.Int64(name)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
// CanString tries to read key name as string and checks whether error is nil
|
|
|
|
func (f *FieldCollection) CanString(name string) bool {
|
2021-09-22 13:36:45 +00:00
|
|
|
_, err := f.String(name)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
// Clone is a wrapper around n.SetFromData(o.Data())
|
|
|
|
func (f *FieldCollection) Clone() *FieldCollection {
|
|
|
|
out := new(FieldCollection)
|
|
|
|
out.SetFromData(f.Data())
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data creates a map-copy of the data stored inside the FieldCollection
|
|
|
|
func (f *FieldCollection) Data() map[string]interface{} {
|
|
|
|
if f == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f.lock.RLock()
|
|
|
|
defer f.lock.RUnlock()
|
|
|
|
|
|
|
|
out := make(map[string]interface{})
|
|
|
|
for k := range f.data {
|
|
|
|
out[k] = f.data[k]
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expect takes a list of keys and returns an error with all non-found names
|
|
|
|
func (f *FieldCollection) Expect(keys ...string) error {
|
|
|
|
if len(keys) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if f == nil || f.data == nil {
|
|
|
|
return errors.New("uninitialized field collection")
|
|
|
|
}
|
|
|
|
|
|
|
|
f.lock.RLock()
|
|
|
|
defer f.lock.RUnlock()
|
|
|
|
|
2021-09-02 21:26:39 +00:00
|
|
|
var missing []string
|
|
|
|
|
|
|
|
for _, k := range keys {
|
2021-11-11 13:59:08 +00:00
|
|
|
if _, ok := f.data[k]; !ok {
|
2021-09-02 21:26:39 +00:00
|
|
|
missing = append(missing, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(missing) > 0 {
|
|
|
|
return errors.Errorf("missing key(s) %s", strings.Join(missing, ", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
// HasAll takes a list of keys and returns whether all of them exist inside the FieldCollection
|
|
|
|
func (f *FieldCollection) HasAll(keys ...string) bool {
|
|
|
|
return f.Expect(keys...) == nil
|
2021-09-22 13:36:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
// MustBool is a wrapper around Bool and panics if an error was returned
|
|
|
|
func (f *FieldCollection) MustBool(name string, defVal *bool) bool {
|
2021-09-02 21:26:39 +00:00
|
|
|
v, err := f.Bool(name)
|
|
|
|
if err != nil {
|
|
|
|
if defVal != nil {
|
|
|
|
return *defVal
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
// MustDuration is a wrapper around Duration and panics if an error was returned
|
|
|
|
func (f *FieldCollection) MustDuration(name string, defVal *time.Duration) time.Duration {
|
2021-09-02 21:26:39 +00:00
|
|
|
v, err := f.Duration(name)
|
|
|
|
if err != nil {
|
|
|
|
if defVal != nil {
|
|
|
|
return *defVal
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
// MustInt64 is a wrapper around Int64 and panics if an error was returned
|
|
|
|
func (f *FieldCollection) MustInt64(name string, defVal *int64) int64 {
|
2021-09-02 21:26:39 +00:00
|
|
|
v, err := f.Int64(name)
|
|
|
|
if err != nil {
|
|
|
|
if defVal != nil {
|
|
|
|
return *defVal
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
// MustString is a wrapper around String and panics if an error was returned
|
|
|
|
func (f *FieldCollection) MustString(name string, defVal *string) string {
|
2021-09-02 21:26:39 +00:00
|
|
|
v, err := f.String(name)
|
|
|
|
if err != nil {
|
|
|
|
if defVal != nil {
|
|
|
|
return *defVal
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
// Bool tries to read key name as bool
|
|
|
|
func (f *FieldCollection) Bool(name string) (bool, error) {
|
|
|
|
if f == nil || f.data == nil {
|
|
|
|
return false, errors.New("uninitialized field collection")
|
|
|
|
}
|
|
|
|
|
|
|
|
f.lock.RLock()
|
|
|
|
defer f.lock.RUnlock()
|
|
|
|
|
|
|
|
v, ok := f.data[name]
|
2021-09-02 21:26:39 +00:00
|
|
|
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-11-11 13:59:08 +00:00
|
|
|
// Duration tries to read key name as time.Duration
|
|
|
|
func (f *FieldCollection) Duration(name string) (time.Duration, error) {
|
|
|
|
if f == nil || f.data == nil {
|
|
|
|
return 0, errors.New("uninitialized field collection")
|
|
|
|
}
|
|
|
|
|
|
|
|
f.lock.RLock()
|
|
|
|
defer f.lock.RUnlock()
|
|
|
|
|
2021-09-02 21:26:39 +00:00
|
|
|
v, err := f.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-11-11 13:59:08 +00:00
|
|
|
// Int64 tries to read key name as int64
|
|
|
|
func (f *FieldCollection) Int64(name string) (int64, error) {
|
|
|
|
if f == nil || f.data == nil {
|
|
|
|
return 0, errors.New("uninitialized field collection")
|
|
|
|
}
|
|
|
|
|
|
|
|
f.lock.RLock()
|
|
|
|
defer f.lock.RUnlock()
|
|
|
|
|
|
|
|
v, ok := f.data[name]
|
2021-09-02 21:26:39 +00:00
|
|
|
if !ok {
|
|
|
|
return 0, ErrValueNotSet
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := v.(type) {
|
|
|
|
case int:
|
|
|
|
return int64(v), nil
|
|
|
|
case int16:
|
|
|
|
return int64(v), nil
|
|
|
|
case int32:
|
|
|
|
return int64(v), nil
|
|
|
|
case int64:
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, ErrValueMismatch
|
|
|
|
}
|
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
// Set sets a single key to specified value
|
|
|
|
func (f *FieldCollection) Set(key string, value interface{}) {
|
|
|
|
if f == nil {
|
|
|
|
f = NewFieldCollection()
|
|
|
|
}
|
|
|
|
|
|
|
|
f.lock.Lock()
|
|
|
|
defer f.lock.Unlock()
|
|
|
|
|
|
|
|
if f.data == nil {
|
|
|
|
f.data = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
f.data[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFromData takes a map of data and copies all data into the FieldCollection
|
|
|
|
func (f *FieldCollection) SetFromData(data map[string]interface{}) {
|
|
|
|
if f == nil {
|
|
|
|
f = NewFieldCollection()
|
|
|
|
}
|
|
|
|
|
|
|
|
f.lock.Lock()
|
|
|
|
defer f.lock.Unlock()
|
|
|
|
|
|
|
|
if f.data == nil {
|
|
|
|
f.data = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range data {
|
|
|
|
f.data[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String tries to read key name as string
|
|
|
|
func (f *FieldCollection) String(name string) (string, error) {
|
|
|
|
if f == nil || f.data == nil {
|
|
|
|
return "", errors.New("uninitialized field collection")
|
|
|
|
}
|
|
|
|
|
|
|
|
f.lock.RLock()
|
|
|
|
defer f.lock.RUnlock()
|
|
|
|
|
|
|
|
v, ok := f.data[name]
|
2021-09-02 21:26:39 +00:00
|
|
|
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-11-11 13:59:08 +00:00
|
|
|
// StringSlice tries to read key name as []string
|
|
|
|
func (f *FieldCollection) StringSlice(name string) ([]string, error) {
|
|
|
|
if f == nil || f.data == nil {
|
|
|
|
return nil, errors.New("uninitialized field collection")
|
|
|
|
}
|
|
|
|
|
|
|
|
f.lock.RLock()
|
|
|
|
defer f.lock.RUnlock()
|
|
|
|
|
|
|
|
v, ok := f.data[name]
|
2021-09-02 21:26:39 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, ErrValueNotSet
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := v.(type) {
|
|
|
|
case []string:
|
|
|
|
return v, nil
|
|
|
|
|
|
|
|
case []interface{}:
|
|
|
|
var out []string
|
|
|
|
|
|
|
|
for _, iv := range v {
|
|
|
|
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
|
|
|
|
}
|
2021-11-11 13:59:08 +00:00
|
|
|
|
|
|
|
// Implement JSON marshalling to plain underlying map[string]interface{}
|
|
|
|
|
|
|
|
func (f *FieldCollection) MarshalJSON() ([]byte, error) {
|
|
|
|
if f == nil || f.data == nil {
|
|
|
|
return []byte("{}"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f.lock.RLock()
|
|
|
|
defer f.lock.RUnlock()
|
|
|
|
|
|
|
|
return json.Marshal(f.data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FieldCollection) UnmarshalJSON(raw []byte) error {
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal(raw, &data); err != nil {
|
|
|
|
return errors.Wrap(err, "unmarshalling from JSON")
|
|
|
|
}
|
|
|
|
|
|
|
|
f.SetFromData(data)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement YAML marshalling to plain underlying map[string]interface{}
|
|
|
|
|
|
|
|
func (f *FieldCollection) MarshalYAML() (interface{}, error) {
|
|
|
|
return f.Data(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FieldCollection) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
if err := unmarshal(&data); err != nil {
|
|
|
|
return errors.Wrap(err, "unmarshalling from YAML")
|
|
|
|
}
|
|
|
|
|
|
|
|
f.SetFromData(data)
|
|
|
|
return nil
|
|
|
|
}
|