engine: resources: Clean up KV resource

This commit is contained in:
James Shubin
2019-03-09 18:48:26 -05:00
parent a0c909914d
commit 5bbb474db6

View File

@@ -56,14 +56,30 @@ type KVRes struct {
init *engine.Init init *engine.Init
// XXX: shouldn't the name be the key? // Key represents the key to set. If it is not specified, the Name value
Key string `yaml:"key"` // key to set // is used instead.
Value *string `yaml:"value"` // value to set (nil to delete) Key string `lang:"key" yaml:"key"`
SkipLessThan bool `yaml:"skiplessthan"` // skip updates as long as stored value is greater // Value represents the string value to set. If this value is nil or,
SkipCmpStyle KVResSkipCmpStyle `yaml:"skipcmpstyle"` // how to do the less than cmp // undefined, then this will delete that key.
Value *string `lang:"value" yaml:"value"`
// SkipLessThan causes the value to be updated as long as it is greater.
SkipLessThan bool `lang:"skiplessthan" yaml:"skiplessthan"`
// SkipCmpStyle is the type of compare function used when determining if
// the value is greater when using the SkipLessThan parameter.
SkipCmpStyle KVResSkipCmpStyle `lang:"skipcmpstyle" yaml:"skipcmpstyle"`
// TODO: does it make sense to have different backends here? (eg: local) // TODO: does it make sense to have different backends here? (eg: local)
} }
// getKey returns the key to be used for this resource. If the Key field is
// specified, it will use that, otherwise it uses the Name.
func (obj *KVRes) getKey() string {
if obj.Key != "" {
return obj.Key
}
return obj.Name()
}
// Default returns some sensible defaults for this resource. // Default returns some sensible defaults for this resource.
func (obj *KVRes) Default() engine.Res { func (obj *KVRes) Default() engine.Res {
return &KVRes{} return &KVRes{}
@@ -71,7 +87,7 @@ func (obj *KVRes) Default() engine.Res {
// Validate if the params passed in are valid data. // Validate if the params passed in are valid data.
func (obj *KVRes) Validate() error { func (obj *KVRes) Validate() error {
if obj.Key == "" { if obj.getKey() == "" {
return fmt.Errorf("key must not be empty") return fmt.Errorf("key must not be empty")
} }
if obj.SkipLessThan { if obj.SkipLessThan {
@@ -104,7 +120,7 @@ func (obj *KVRes) Close() error {
func (obj *KVRes) Watch() error { func (obj *KVRes) Watch() error {
obj.init.Running() // when started, notify engine that we're running obj.init.Running() // when started, notify engine that we're running
ch := obj.init.World.StrMapWatch(obj.Key) // get possible events! ch := obj.init.World.StrMapWatch(obj.getKey()) // get possible events!
var send = false // send event? var send = false // send event?
for { for {
@@ -118,7 +134,7 @@ func (obj *KVRes) Watch() error {
return errwrap.Wrapf(err, "unknown %s watcher error", obj) return errwrap.Wrapf(err, "unknown %s watcher error", obj)
} }
if obj.init.Debug { if obj.init.Debug {
obj.init.Logf("Event!") obj.init.Logf("event!")
} }
send = true send = true
@@ -182,7 +198,7 @@ func (obj *KVRes) CheckApply(apply bool) (checkOK bool, err error) {
} }
hostname := obj.init.Hostname // me hostname := obj.init.Hostname // me
keyMap, err := obj.init.World.StrMapGet(obj.Key) keyMap, err := obj.init.World.StrMapGet(obj.getKey())
if err != nil { if err != nil {
return false, errwrap.Wrapf(err, "check error during StrGet") return false, errwrap.Wrapf(err, "check error during StrGet")
} }
@@ -202,7 +218,7 @@ func (obj *KVRes) CheckApply(apply bool) (checkOK bool, err error) {
return true, nil // nothing to delete, we're good! return true, nil // nothing to delete, we're good!
} else if ok && obj.Value == nil { // delete } else if ok && obj.Value == nil { // delete
err := obj.init.World.StrMapDel(obj.Key) err := obj.init.World.StrMapDel(obj.getKey())
return false, errwrap.Wrapf(err, "apply error during StrDel") return false, errwrap.Wrapf(err, "apply error during StrDel")
} }
@@ -210,7 +226,7 @@ func (obj *KVRes) CheckApply(apply bool) (checkOK bool, err error) {
return false, nil return false, nil
} }
if err := obj.init.World.StrMapSet(obj.Key, *obj.Value); err != nil { if err := obj.init.World.StrMapSet(obj.getKey(), *obj.Value); err != nil {
return false, errwrap.Wrapf(err, "apply error during StrSet") return false, errwrap.Wrapf(err, "apply error during StrSet")
} }
@@ -219,39 +235,31 @@ func (obj *KVRes) CheckApply(apply bool) (checkOK bool, err error) {
// Cmp compares two resources and returns an error if they are not equivalent. // Cmp compares two resources and returns an error if they are not equivalent.
func (obj *KVRes) Cmp(r engine.Res) error { func (obj *KVRes) Cmp(r engine.Res) error {
if !obj.Compare(r) {
return fmt.Errorf("did not compare")
}
return nil
}
// Compare two resources and return if they are equivalent.
func (obj *KVRes) Compare(r engine.Res) bool {
// we can only compare KVRes to others of the same resource kind // we can only compare KVRes to others of the same resource kind
res, ok := r.(*KVRes) res, ok := r.(*KVRes)
if !ok { if !ok {
return false return fmt.Errorf("not a %s", obj.Kind())
} }
if obj.Key != res.Key { if obj.getKey() != res.getKey() {
return false return fmt.Errorf("the Key differs")
} }
if (obj.Value == nil) != (res.Value == nil) { // xor if (obj.Value == nil) != (res.Value == nil) { // xor
return false return fmt.Errorf("the Value differs")
} }
if obj.Value != nil && res.Value != nil { if obj.Value != nil && res.Value != nil {
if *obj.Value != *res.Value { // compare the strings if *obj.Value != *res.Value { // compare the strings
return false return fmt.Errorf("the contents of Value differs")
} }
} }
if obj.SkipLessThan != res.SkipLessThan { if obj.SkipLessThan != res.SkipLessThan {
return false return fmt.Errorf("the SkipLessThan param differs")
} }
if obj.SkipCmpStyle != res.SkipCmpStyle { if obj.SkipCmpStyle != res.SkipCmpStyle {
return false return fmt.Errorf("the SkipCmpStyle param differs")
} }
return true return nil
} }
// KVUID is the UID struct for KVRes. // KVUID is the UID struct for KVRes.