From ef1d23bd3d39a001281d35a48a611248680d6f25 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sat, 20 Nov 2021 21:30:58 +0100 Subject: [PATCH] Add `fieldcollection` helper Signed-off-by: Knut Ahlers --- fieldcollection/fieldcollection.go | 367 ++++++++++++++++++++++++ fieldcollection/fieldcollection_test.go | 80 ++++++ go.mod | 1 + go.sum | 2 + 4 files changed, 450 insertions(+) create mode 100644 fieldcollection/fieldcollection.go create mode 100644 fieldcollection/fieldcollection_test.go diff --git a/fieldcollection/fieldcollection.go b/fieldcollection/fieldcollection.go new file mode 100644 index 0000000..3df3c2c --- /dev/null +++ b/fieldcollection/fieldcollection.go @@ -0,0 +1,367 @@ +package fieldcollection + +import ( + "encoding/json" + "fmt" + "strconv" + "strings" + "sync" + "time" + + "github.com/pkg/errors" +) + +var ( + ErrValueNotSet = errors.New("specified value not found") + ErrValueMismatch = errors.New("specified value has different format") +) + +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 +} + +// CanBool tries to read key name as bool and checks whether error is nil +func (f *FieldCollection) CanBool(name string) bool { + _, err := f.Bool(name) + return err == nil +} + +// CanDuration tries to read key name as time.Duration and checks whether error is nil +func (f *FieldCollection) CanDuration(name string) bool { + _, err := f.Duration(name) + return err == nil +} + +// CanInt64 tries to read key name as int64 and checks whether error is nil +func (f *FieldCollection) CanInt64(name string) bool { + _, err := f.Int64(name) + return err == nil +} + +// CanString tries to read key name as string and checks whether error is nil +func (f *FieldCollection) CanString(name string) bool { + _, err := f.String(name) + return err == nil +} + +// 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() + + var missing []string + + for _, k := range keys { + if _, ok := f.data[k]; !ok { + missing = append(missing, k) + } + } + + if len(missing) > 0 { + return errors.Errorf("missing key(s) %s", strings.Join(missing, ", ")) + } + + return nil +} + +// 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 +} + +// MustBool is a wrapper around Bool and panics if an error was returned +func (f *FieldCollection) MustBool(name string, defVal *bool) bool { + v, err := f.Bool(name) + if err != nil { + if defVal != nil { + return *defVal + } + panic(err) + } + return v +} + +// MustDuration is a wrapper around Duration and panics if an error was returned +func (f *FieldCollection) MustDuration(name string, defVal *time.Duration) time.Duration { + v, err := f.Duration(name) + if err != nil { + if defVal != nil { + return *defVal + } + panic(err) + } + return v +} + +// MustInt64 is a wrapper around Int64 and panics if an error was returned +func (f *FieldCollection) MustInt64(name string, defVal *int64) int64 { + v, err := f.Int64(name) + if err != nil { + if defVal != nil { + return *defVal + } + panic(err) + } + return v +} + +// MustString is a wrapper around String and panics if an error was returned +func (f *FieldCollection) MustString(name string, defVal *string) string { + v, err := f.String(name) + if err != nil { + if defVal != nil { + return *defVal + } + panic(err) + } + return v +} + +// 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] + 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 +} + +// 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() + + 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") +} + +// 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] + 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 +} + +// 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] + 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 +} + +// 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] + 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 +} + +// 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 +} diff --git a/fieldcollection/fieldcollection_test.go b/fieldcollection/fieldcollection_test.go new file mode 100644 index 0000000..c764707 --- /dev/null +++ b/fieldcollection/fieldcollection_test.go @@ -0,0 +1,80 @@ +package fieldcollection + +import ( + "bytes" + "encoding/json" + "strings" + "testing" + + "gopkg.in/yaml.v2" +) + +func TestFieldCollectionJSONMarshal(t *testing.T) { + var ( + buf = new(bytes.Buffer) + raw = `{"key1":"test1","key2":"test2"}` + f = NewFieldCollection() + ) + + if err := json.NewDecoder(strings.NewReader(raw)).Decode(f); err != nil { + t.Fatalf("Unable to unmarshal: %s", err) + } + + if err := json.NewEncoder(buf).Encode(f); err != nil { + t.Fatalf("Unable to marshal: %s", err) + } + + if raw != strings.TrimSpace(buf.String()) { + t.Errorf("Marshalled JSON does not match expectation: res=%s exp=%s", buf.String(), raw) + } +} + +func TestFieldCollectionYAMLMarshal(t *testing.T) { + var ( + buf = new(bytes.Buffer) + raw = "key1: test1\nkey2: test2" + f = NewFieldCollection() + ) + + if err := yaml.NewDecoder(strings.NewReader(raw)).Decode(f); err != nil { + t.Fatalf("Unable to unmarshal: %s", err) + } + + if err := yaml.NewEncoder(buf).Encode(f); err != nil { + t.Fatalf("Unable to marshal: %s", err) + } + + if raw != strings.TrimSpace(buf.String()) { + t.Errorf("Marshalled YAML does not match expectation: res=%s exp=%s", buf.String(), raw) + } +} + +func TestFieldCollectionNilModify(t *testing.T) { + var f *FieldCollection + + f.Set("foo", "bar") + + f = nil + f.SetFromData(map[string]interface{}{"foo": "bar"}) +} + +func TestFieldCollectionNilClone(t *testing.T) { + var f *FieldCollection + + f.Clone() +} + +func TestFieldCollectionNilDataGet(t *testing.T) { + var f *FieldCollection + + for name, fn := range map[string]func(name string) bool{ + "bool": f.CanBool, + "duration": f.CanDuration, + "int64": f.CanInt64, + "string": f.CanString, + } { + if fn("foo") { + t.Errorf("%s key is available", name) + } + } +} diff --git a/go.mod b/go.mod index 6238c92..d4da10f 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/nxadm/tail v1.4.6 // indirect github.com/onsi/ginkgo v1.15.0 github.com/onsi/gomega v1.10.5 + github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.7.0 github.com/stretchr/testify v1.7.0 // indirect golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect diff --git a/go.sum b/go.sum index 2b3a854..760e266 100644 --- a/go.sum +++ b/go.sum @@ -53,6 +53,8 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.10.5 h1:7n6FEkpFmfCoo2t+YYqXH0evK+a9ICQz0xcAy9dYcaQ= github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=