-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
164 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package kredis | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
) | ||
|
||
type Enum struct { | ||
Proxy | ||
defaultValue string | ||
values map[string]bool | ||
} | ||
|
||
var EmptyValues = errors.New("values cannot be empty") | ||
var InvalidValue = errors.New("invalid enum value") | ||
|
||
func NewEnum(key string, defaultValue string, values []string, opts ...ProxyOption) (*Enum, error) { | ||
if len(values) == 0 { | ||
return nil, EmptyValues | ||
} | ||
|
||
proxy, err := NewProxy(key, opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// TODO return runtime error if expiresIn option is used -- this option just | ||
// doesn't fit well into this Kredis data structure | ||
|
||
enum := &Enum{Proxy: *proxy, defaultValue: defaultValue, values: map[string]bool{}} | ||
for _, value := range values { | ||
enum.values[value] = true | ||
} | ||
|
||
err = enum.SetValue(defaultValue) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return enum, nil | ||
} | ||
|
||
func (e *Enum) Is(value string) bool { | ||
return e.Value() == value | ||
} | ||
|
||
func (e *Enum) Value() string { | ||
value, _ := e.client.Get(e.ctx, e.key).Result() | ||
return value | ||
} | ||
|
||
func (e *Enum) SetValue(value string) error { | ||
if _, ok := e.values[value]; !ok { | ||
return InvalidValue | ||
} | ||
|
||
_, err := e.client.Set(e.ctx, e.key, value, time.Duration(0)).Result() | ||
return err | ||
} | ||
|
||
func (e *Enum) Reset() (err error) { | ||
pipe := e.client.TxPipeline() | ||
pipe.Del(e.ctx, e.key) | ||
pipe.Set(e.ctx, e.key, e.defaultValue, time.Duration(0)) | ||
|
||
_, err = pipe.Exec(e.ctx) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package kredis | ||
|
||
func (s *KredisTestSuite) TestEnum() { | ||
enum, err := NewEnum("e", "ready", []string{"ready", "set", "go"}) | ||
s.NoError(err) | ||
|
||
s.Equal("ready", enum.Value()) | ||
s.True(enum.Is("ready")) | ||
|
||
s.NoError(enum.SetValue("set")) | ||
s.Equal("set", enum.Value()) | ||
s.True(enum.Is("set")) | ||
|
||
s.NoError(enum.SetValue("go")) | ||
s.Equal("go", enum.Value()) | ||
s.True(enum.Is("go")) | ||
|
||
err = enum.SetValue("badval") | ||
s.Error(err) | ||
s.Equal(InvalidValue, err) | ||
|
||
s.NoError(enum.Reset()) | ||
s.Equal("ready", enum.Value()) | ||
s.True(enum.Is("ready")) | ||
} | ||
|
||
func (s *KredisTestSuite) TestEnumWithEmptyValues() { | ||
_, err := NewEnum("key", "ready", []string{}) | ||
s.Error(err) | ||
s.Equal(EmptyValues, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,43 @@ | ||
package kredis | ||
|
||
import "time" | ||
|
||
func (s *KredisTestSuite) TestFlag() { | ||
flag, err := NewFlag("flag") | ||
s.NoError(err) | ||
s.False(flag.IsMarked()) | ||
|
||
s.NoError(flag.Mark()) | ||
s.True(flag.IsMarked()) | ||
|
||
s.NoError(flag.Remove()) | ||
s.False(flag.IsMarked()) | ||
} | ||
|
||
func (s *KredisTestSuite) TestFlagWithMarkOptions() { | ||
flag, _ := NewFlag("flag_ex") | ||
|
||
s.NoError(flag.Mark(WithFlagMarkExpiry("2ms"))) | ||
s.True(flag.IsMarked()) | ||
|
||
time.Sleep(1 * time.Millisecond) | ||
|
||
s.NoError(flag.Mark(WithFlagMarkExpiry("2ms"))) | ||
s.True(flag.IsMarked()) | ||
|
||
time.Sleep(2 * time.Millisecond) | ||
|
||
s.False(flag.IsMarked()) | ||
|
||
s.NoError(flag.Mark(WithFlagMarkExpiry("2ms"))) | ||
s.True(flag.IsMarked()) | ||
|
||
time.Sleep(1 * time.Millisecond) | ||
|
||
s.NoError(flag.Mark(WithFlagMarkExpiry("5ms"), WithFlagMarkForced())) | ||
s.True(flag.IsMarked()) | ||
|
||
time.Sleep(2 * time.Millisecond) | ||
|
||
s.True(flag.IsMarked()) // still marked because of forced SET cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters