helper/shadow: KeyedValue.ValueOk
This commit is contained in:
parent
17b909a59b
commit
d6168edc50
|
@ -16,27 +16,21 @@ type KeyedValue struct {
|
|||
// Value returns the value that was set for the given key, or blocks
|
||||
// until one is available.
|
||||
func (w *KeyedValue) Value(k string) interface{} {
|
||||
w.lock.Lock()
|
||||
w.once.Do(w.init)
|
||||
|
||||
// If we have this value already, return it
|
||||
if v, ok := w.values[k]; ok {
|
||||
w.lock.Unlock()
|
||||
v, val := w.valueWaiter(k)
|
||||
if val == nil {
|
||||
return v
|
||||
}
|
||||
|
||||
// No pending value, check for a waiter
|
||||
val := w.waiters[k]
|
||||
if val == nil {
|
||||
val = new(Value)
|
||||
w.waiters[k] = val
|
||||
}
|
||||
w.lock.Unlock()
|
||||
|
||||
// Return the value once we have it
|
||||
return val.Value()
|
||||
}
|
||||
|
||||
// ValueOk gets the value for the given key, returning immediately if the
|
||||
// value doesn't exist. The second return argument is true if the value exists.
|
||||
func (w *KeyedValue) ValueOk(k string) (interface{}, bool) {
|
||||
v, val := w.valueWaiter(k)
|
||||
return v, val == nil
|
||||
}
|
||||
|
||||
func (w *KeyedValue) SetValue(k string, v interface{}) {
|
||||
w.lock.Lock()
|
||||
defer w.lock.Unlock()
|
||||
|
@ -57,3 +51,25 @@ func (w *KeyedValue) init() {
|
|||
w.values = make(map[string]interface{})
|
||||
w.waiters = make(map[string]*Value)
|
||||
}
|
||||
|
||||
func (w *KeyedValue) valueWaiter(k string) (interface{}, *Value) {
|
||||
w.lock.Lock()
|
||||
w.once.Do(w.init)
|
||||
|
||||
// If we have this value already, return it
|
||||
if v, ok := w.values[k]; ok {
|
||||
w.lock.Unlock()
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// No pending value, check for a waiter
|
||||
val := w.waiters[k]
|
||||
if val == nil {
|
||||
val = new(Value)
|
||||
w.waiters[k] = val
|
||||
}
|
||||
w.lock.Unlock()
|
||||
|
||||
// Return the waiter
|
||||
return nil, val
|
||||
}
|
||||
|
|
|
@ -49,3 +49,27 @@ func TestKeyedValue_setFirst(t *testing.T) {
|
|||
t.Fatalf("bad: %#v", val)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeyedValueOk(t *testing.T) {
|
||||
var v KeyedValue
|
||||
|
||||
// Try
|
||||
val, ok := v.ValueOk("foo")
|
||||
if ok {
|
||||
t.Fatal("should not be ok")
|
||||
}
|
||||
|
||||
// Set
|
||||
v.SetValue("foo", 42)
|
||||
|
||||
// Try again
|
||||
val, ok = v.ValueOk("foo")
|
||||
if !ok {
|
||||
t.Fatal("should be ok")
|
||||
}
|
||||
|
||||
// Verify
|
||||
if val != 42 {
|
||||
t.Fatalf("bad: %#v", val)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue