Skip to content

Commit

Permalink
db/redis/storage: support float64
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco Souza committed Dec 22, 2016
1 parent 42278ac commit 24d9528
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions db/redis/storage/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ func (s *Storage) loadStruct(in map[string]string, out reflect.Value, prefixes .
return err
}
fieldValue.SetBool(boolValue)
case reflect.Float64:
floatValue, err := strconv.ParseFloat(value, 64)
if err != nil {
return err
}
fieldValue.SetFloat(floatValue)
case reflect.Int:
intValue, err := strconv.ParseInt(value, 10, 64)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions db/redis/storage/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func TestSave(t *testing.T) {
ID: "some-id",
Name: "gopher",
Age: 29,
Weight: 150.03,
BirthTime: time.Now().Add(-29 * 365 * 24 * time.Hour),
Address: Address{
Data: map[string]string{"first_line": "secret"},
Expand Down Expand Up @@ -104,6 +105,7 @@ func TestSave(t *testing.T) {
expected := map[string]string{
"name": "gopher",
"age": "29",
"weight": "150.03",
"birth": person.BirthTime.Format(time.RFC3339Nano),
"colors": "",
"address_city_name": "nyc",
Expand All @@ -121,6 +123,7 @@ func TestSavePointer(t *testing.T) {
ID: "some-id",
Name: "gopher",
Age: 29,
Weight: 153.2993,
BirthTime: time.Now().Add(-29 * 365 * 24 * time.Hour),
PreferredColors: []string{"red", "blue", "yellow"},
Address: Address{
Expand Down Expand Up @@ -150,6 +153,7 @@ func TestSavePointer(t *testing.T) {
expected := map[string]string{
"name": "gopher",
"age": "29",
"weight": "153.2993",
"birth": person.BirthTime.Format(time.RFC3339Nano),
"colors": "red%%%blue%%%yellow",
"address_city_name": "nyc",
Expand Down Expand Up @@ -335,6 +339,7 @@ func TestLoadStruct(t *testing.T) {
err = storage.Save("test-key", map[string]string{
"name": "Gopher",
"age": "29",
"weight": "159.332",
"birth": date.Format(time.RFC3339Nano),
"colors": "red%%%green%%%blue%%%black",
"address_number": "-2",
Expand All @@ -356,6 +361,7 @@ func TestLoadStruct(t *testing.T) {
expectedPerson.Address.Number = -2
expectedPerson.Name = "Gopher"
expectedPerson.Age = 29
expectedPerson.Weight = 159.332
expectedPerson.BirthTime = date
expectedPerson.PreferredColors = []string{"red", "green", "blue", "black"}
err = storage.Load("test-key", &person)
Expand Down
1 change: 1 addition & 0 deletions db/redis/storage/stub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Person struct {
Name string `redis-hash:"name"`
Address Address `redis-hash:"address,expand"`
Age uint `redis-hash:"age"`
Weight float64 `redis-hash:"weight"`
BirthTime time.Time `redis-hash:"birth"`
PreferredColors []string `redis-hash:"colors"`
NonTagged string
Expand Down

0 comments on commit 24d9528

Please sign in to comment.