Skip to content

Commit 24d9528

Browse files
author
Francisco Souza
committed
db/redis/storage: support float64
1 parent 42278ac commit 24d9528

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

db/redis/storage/redis.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,12 @@ func (s *Storage) loadStruct(in map[string]string, out reflect.Value, prefixes .
272272
return err
273273
}
274274
fieldValue.SetBool(boolValue)
275+
case reflect.Float64:
276+
floatValue, err := strconv.ParseFloat(value, 64)
277+
if err != nil {
278+
return err
279+
}
280+
fieldValue.SetFloat(floatValue)
275281
case reflect.Int:
276282
intValue, err := strconv.ParseInt(value, 10, 64)
277283
if err != nil {

db/redis/storage/redis_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func TestSave(t *testing.T) {
7676
ID: "some-id",
7777
Name: "gopher",
7878
Age: 29,
79+
Weight: 150.03,
7980
BirthTime: time.Now().Add(-29 * 365 * 24 * time.Hour),
8081
Address: Address{
8182
Data: map[string]string{"first_line": "secret"},
@@ -104,6 +105,7 @@ func TestSave(t *testing.T) {
104105
expected := map[string]string{
105106
"name": "gopher",
106107
"age": "29",
108+
"weight": "150.03",
107109
"birth": person.BirthTime.Format(time.RFC3339Nano),
108110
"colors": "",
109111
"address_city_name": "nyc",
@@ -121,6 +123,7 @@ func TestSavePointer(t *testing.T) {
121123
ID: "some-id",
122124
Name: "gopher",
123125
Age: 29,
126+
Weight: 153.2993,
124127
BirthTime: time.Now().Add(-29 * 365 * 24 * time.Hour),
125128
PreferredColors: []string{"red", "blue", "yellow"},
126129
Address: Address{
@@ -150,6 +153,7 @@ func TestSavePointer(t *testing.T) {
150153
expected := map[string]string{
151154
"name": "gopher",
152155
"age": "29",
156+
"weight": "153.2993",
153157
"birth": person.BirthTime.Format(time.RFC3339Nano),
154158
"colors": "red%%%blue%%%yellow",
155159
"address_city_name": "nyc",
@@ -335,6 +339,7 @@ func TestLoadStruct(t *testing.T) {
335339
err = storage.Save("test-key", map[string]string{
336340
"name": "Gopher",
337341
"age": "29",
342+
"weight": "159.332",
338343
"birth": date.Format(time.RFC3339Nano),
339344
"colors": "red%%%green%%%blue%%%black",
340345
"address_number": "-2",
@@ -356,6 +361,7 @@ func TestLoadStruct(t *testing.T) {
356361
expectedPerson.Address.Number = -2
357362
expectedPerson.Name = "Gopher"
358363
expectedPerson.Age = 29
364+
expectedPerson.Weight = 159.332
359365
expectedPerson.BirthTime = date
360366
expectedPerson.PreferredColors = []string{"red", "green", "blue", "black"}
361367
err = storage.Load("test-key", &person)

db/redis/storage/stub_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type Person struct {
77
Name string `redis-hash:"name"`
88
Address Address `redis-hash:"address,expand"`
99
Age uint `redis-hash:"age"`
10+
Weight float64 `redis-hash:"weight"`
1011
BirthTime time.Time `redis-hash:"birth"`
1112
PreferredColors []string `redis-hash:"colors"`
1213
NonTagged string

0 commit comments

Comments
 (0)