-
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.
Change int->int64, add complex handler for Time and Duration.
- Loading branch information
Showing
7 changed files
with
148 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package complex | ||
|
||
import "github.com/shimmeringbee/persistence" | ||
|
||
func Store[T any](section persistence.Section, key string, val T, enc func(persistence.Section, string, T) error) error { | ||
return enc(section, key, val) | ||
} | ||
|
||
func Retrieve[T any](section persistence.Section, key string, dec func(persistence.Section, string) (T, bool), defValue ...T) (T, bool) { | ||
if v, ok := dec(section, key); ok { | ||
return v, ok | ||
} else { | ||
if len(defValue) > 0 { | ||
return defValue[0], false | ||
} else { | ||
v = *new(T) | ||
return v, false | ||
} | ||
} | ||
} |
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,30 @@ | ||
package complex | ||
|
||
import ( | ||
"github.com/shimmeringbee/persistence/impl/memory" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestRetrieve(t *testing.T) { | ||
t.Run("default value provided is returned if not found and default provided", func(t *testing.T) { | ||
s := memory.New() | ||
|
||
expected := time.Duration(1) | ||
|
||
actual, found := Retrieve(s, Key, DurationDecoder, expected) | ||
assert.False(t, found) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("zero value provided is returned if not found and no default", func(t *testing.T) { | ||
s := memory.New() | ||
|
||
expected := time.Duration(0) | ||
|
||
actual, found := Retrieve(s, Key, DurationDecoder) | ||
assert.False(t, found) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
} |
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,30 @@ | ||
package complex | ||
|
||
import ( | ||
"github.com/shimmeringbee/persistence" | ||
"time" | ||
) | ||
|
||
func TimeEncoder(s persistence.Section, k string, v time.Time) error { | ||
return s.Set(k, v.UnixMilli()) | ||
} | ||
|
||
func TimeDecoder(s persistence.Section, k string) (time.Time, bool) { | ||
if ev, found := s.Int(k); found { | ||
return time.UnixMilli(ev), true | ||
} else { | ||
return time.Time{}, false | ||
} | ||
} | ||
|
||
func DurationEncoder(s persistence.Section, k string, v time.Duration) error { | ||
return s.Set(k, v.Milliseconds()) | ||
} | ||
|
||
func DurationDecoder(s persistence.Section, k string) (time.Duration, bool) { | ||
if ev, found := s.Int(k); found { | ||
return time.Duration(ev) * time.Millisecond, true | ||
} else { | ||
return time.Duration(0), false | ||
} | ||
} |
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,40 @@ | ||
package complex | ||
|
||
import ( | ||
"github.com/shimmeringbee/persistence/impl/memory" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"time" | ||
) | ||
|
||
const Key = "key" | ||
|
||
func TestTime(t *testing.T) { | ||
t.Run("time is stored and retrieved to the millisecond level", func(t *testing.T) { | ||
s := memory.New() | ||
|
||
expected := time.UnixMilli(time.Now().UnixMilli()) | ||
|
||
err := Store(s, Key, expected, TimeEncoder) | ||
assert.NoError(t, err) | ||
|
||
actual, found := Retrieve(s, Key, TimeDecoder) | ||
assert.True(t, found) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
} | ||
|
||
func TestDuration(t *testing.T) { | ||
t.Run("duration is stored and retrieved to the millisecond level", func(t *testing.T) { | ||
s := memory.New() | ||
|
||
expected := time.Duration(1234) * time.Millisecond | ||
|
||
err := Store(s, Key, expected, DurationEncoder) | ||
assert.NoError(t, err) | ||
|
||
actual, found := Retrieve(s, Key, DurationDecoder) | ||
assert.True(t, found) | ||
assert.Equal(t, expected, actual) | ||
}) | ||
} |
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