Skip to content

Commit fce5c9b

Browse files
committed
Intermediate migration to new value types
1 parent 68ddcd7 commit fce5c9b

11 files changed

Lines changed: 237 additions & 278 deletions

geo.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,60 +15,60 @@ import (
1515
const earthRadiusMeters = 6372797.560856
1616
const skGeoCell = skN1
1717

18-
type Location struct {
18+
type GLocation struct {
1919
Lat float64
2020
Lon float64
2121
}
2222

23-
func (l Location) s2CellID() string {
23+
func (l GLocation) s2CellID() string {
2424
return fmt.Sprintf("%d", s2.CellIDFromLatLng(l.s2LatLng()))
2525
}
2626

27-
func (l Location) Geohash() string {
27+
func (l GLocation) Geohash() string {
2828
return geohash.Encode(l.Lat, l.Lon)
2929
}
3030

31-
func (l Location) toAV() dynamodb.AttributeValue {
31+
func (l GLocation) toAV() dynamodb.AttributeValue {
3232
return dynamodb.AttributeValue{
3333
N: aws.String(l.s2CellID()),
3434
}
3535
}
3636

37-
func (l *Location) setCellIDString(cellIDStr string) {
37+
func (l *GLocation) setCellIDString(cellIDStr string) {
3838
cellID, _ := strconv.ParseUint(cellIDStr, 10, 64)
3939
s2Cell := s2.CellID(cellID)
4040
s2LatLon := s2Cell.LatLng()
4141
l.Lat = s2LatLon.Lat.Degrees()
4242
l.Lon = s2LatLon.Lng.Degrees()
4343
}
4444

45-
func (l Location) DistanceTo(other Location, unit Unit) float64 {
45+
func (l GLocation) DistanceTo(other GLocation, unit GUnit) (distance float64) {
4646
return Meters.To(unit, l.s2LatLng().Distance(other.s2LatLng()).Radians()*earthRadiusMeters)
4747
}
4848

49-
func (l Location) s2LatLng() s2.LatLng {
49+
func (l GLocation) s2LatLng() s2.LatLng {
5050
return s2.LatLngFromDegrees(l.Lat, l.Lon)
5151
}
5252

53-
func fromCellIDString(cellID string) (l Location) {
53+
func fromCellIDString(cellID string) (l GLocation) {
5454
l.setCellIDString(cellID)
5555
return
5656
}
5757

58-
type Unit float64
58+
type GUnit float64
5959

60-
func (from Unit) To(to Unit, d float64) float64 {
60+
func (from GUnit) To(to GUnit, d float64) float64 {
6161
return (d / float64(to)) * float64(from)
6262
}
6363

6464
const (
65-
Meters Unit = 1.0
66-
Kilometers Unit = 1000.0
67-
Miles Unit = 1609.34
68-
Feet Unit = 0.3048
65+
Meters GUnit = 1.0
66+
Kilometers GUnit = 1000.0
67+
Miles GUnit = 1609.34
68+
Feet GUnit = 0.3048
6969
)
7070

71-
func (c Client) GEOADD(key string, members map[string]Location) (addedCount int64, err error) {
71+
func (c Client) GEOADD(key string, members map[string]GLocation) (addedCount int64, err error) {
7272
for member, location := range members {
7373
builder := newExpresionBuilder()
7474
builder.updateSetAV(skGeoCell, location.toAV())
@@ -90,7 +90,7 @@ func (c Client) GEOADD(key string, members map[string]Location) (addedCount int6
9090
return addedCount, nil
9191
}
9292

93-
func (c Client) GEODIST(key string, member1, member2 string, unit Unit) (distance float64, ok bool, err error) {
93+
func (c Client) GEODIST(key string, member1, member2 string, unit GUnit) (distance float64, ok bool, err error) {
9494
locations, err := c.GEOPOS(key, member1, member2)
9595
if err != nil || len(locations) < 2 {
9696
return
@@ -113,8 +113,8 @@ func (c Client) GEOHASH(key string, members ...string) (geohashes []string, err
113113
return
114114
}
115115

116-
func (c Client) GEOPOS(key string, members ...string) (locations map[string]Location, err error) {
117-
locations = make(map[string]Location)
116+
func (c Client) GEOPOS(key string, members ...string) (locations map[string]GLocation, err error) {
117+
locations = make(map[string]GLocation)
118118

119119
for _, member := range members {
120120
resp, err := c.ddbClient.GetItemRequest(&dynamodb.GetItemInput{
@@ -135,8 +135,8 @@ func (c Client) GEOPOS(key string, members ...string) (locations map[string]Loca
135135
return
136136
}
137137

138-
func (c Client) GEORADIUS(key string, center Location, radius float64, radiusUnit Unit, count int64) (positions map[string]Location, err error) {
139-
positions = make(map[string]Location)
138+
func (c Client) GEORADIUS(key string, center GLocation, radius float64, radiusUnit GUnit, count int64) (positions map[string]GLocation, err error) {
139+
positions = make(map[string]GLocation)
140140
radiusCap := s2.CapFromCenterAngle(s2.PointFromLatLng(center.s2LatLng()), s1.Angle(radiusUnit.To(Meters, radius)/earthRadiusMeters))
141141

142142
for _, cellID := range radiusCap.CellUnionBound() {
@@ -186,7 +186,7 @@ func (c Client) GEORADIUS(key string, center Location, radius float64, radiusUni
186186
return
187187
}
188188

189-
func (c Client) GEORADIUSBYMEMBER(key string, member string, radius float64, radiusUnit Unit, count int64) (positions map[string]Location, err error) {
189+
func (c Client) GEORADIUSBYMEMBER(key string, member string, radius float64, radiusUnit GUnit, count int64) (positions map[string]GLocation, err error) {
190190
locations, err := c.GEOPOS(key, member)
191191
if err == nil {
192192
positions, err = c.GEORADIUS(key, locations[member], radius, radiusUnit, count)

geo_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestPointsAndDistances(t *testing.T) {
11-
l := Location{
11+
l := GLocation{
1212
Lat: 38.115556,
1313
Lon: 13.361389,
1414
}
@@ -21,7 +21,7 @@ func TestPointsAndDistances(t *testing.T) {
2121

2222
func TestGeoBasics(t *testing.T) {
2323
c := newClient(t)
24-
startingMap := map[string]Location{
24+
startingMap := map[string]GLocation{
2525
"Palermo": {38.115556, 13.361389},
2626
"Catania": {37.502669, 15.087269},
2727
}
@@ -63,7 +63,7 @@ func TestGeoBasics(t *testing.T) {
6363

6464
func TestGeoRadius(t *testing.T) {
6565
c := newClient(t)
66-
_, err := c.GEOADD("india", map[string]Location{
66+
_, err := c.GEOADD("india", map[string]GLocation{
6767
"chennai": {13.09, 80.28},
6868
"vellore": {12.9204, 79.15},
6969
"pondy": {11.935, 79.83},
@@ -73,7 +73,7 @@ func TestGeoRadius(t *testing.T) {
7373
})
7474
assert.NoError(t, err)
7575

76-
locations, err := c.GEORADIUS("india", Location{13.09, 80.28}, 180, Kilometers, 10)
76+
locations, err := c.GEORADIUS("india", GLocation{13.09, 80.28}, 180, Kilometers, 10)
7777
assert.NoError(t, err)
7878
assert.Equal(t, 3, len(locations))
7979
assert.InDelta(t, locations["chennai"].Lat, 13.09, 0.1)

hashes.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package redimo
22

33
import (
44
"context"
5-
"math/big"
5+
"log"
66
"strings"
77

88
"github.com/aws/aws-sdk-go-v2/aws"
99
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
1010
)
1111

12-
func (c Client) HGET(key string, field string) (val Value, err error) {
12+
func (c Client) HGET(key string, field string) (val ReturnValue, err error) {
1313
resp, err := c.ddbClient.GetItemRequest(&dynamodb.GetItemInput{
1414
ConsistentRead: aws.Bool(c.consistentReads),
1515
Key: keyDef{
@@ -34,7 +34,7 @@ func (c Client) HSET(key string, fieldValues map[string]Value) (savedCount int64
3434
PutRequest: &dynamodb.PutRequest{
3535
Item: itemDef{
3636
keyDef: keyDef{pk: key, sk: field},
37-
val: v,
37+
val: ReturnValue{v.ToAV()},
3838
}.eav(),
3939
},
4040
})
@@ -90,7 +90,7 @@ func (c Client) HMSET(key string, fieldValues map[string]Value) (err error) {
9090
return
9191
}
9292

93-
func (c Client) HMGET(key string, fields ...string) (values []Value, err error) {
93+
func (c Client) HMGET(key string, fields ...string) (values []ReturnValue, err error) {
9494
items := make([]dynamodb.TransactGetItem, len(fields))
9595
for i, field := range fields {
9696
items[i] = dynamodb.TransactGetItem{Get: &dynamodb.Get{
@@ -162,8 +162,8 @@ func (c Client) HEXISTS(key string, field string) (exists bool, err error) {
162162
return
163163
}
164164

165-
func (c Client) HGETALL(key string) (fieldValues map[string]Value, err error) {
166-
fieldValues = make(map[string]Value)
165+
func (c Client) HGETALL(key string) (fieldValues map[string]ReturnValue, err error) {
166+
fieldValues = make(map[string]ReturnValue)
167167
hasMoreResults := true
168168

169169
var lastEvaluatedKey map[string]dynamodb.AttributeValue
@@ -200,13 +200,22 @@ func (c Client) HGETALL(key string) (fieldValues map[string]Value, err error) {
200200
return
201201
}
202202

203-
func (c Client) HINCRBYFLOAT(key string, field string, delta *big.Float) (after *big.Float, err error) {
203+
func (c Client) HINCRBYFLOAT(key string, field string, delta float64) (after float64, err error) {
204+
rv, err := c.hIncr(key, field, FloatValue{delta})
205+
if err == nil {
206+
after = rv.Float()
207+
}
208+
209+
return
210+
}
211+
212+
func (c Client) hIncr(key string, field string, delta Value) (after ReturnValue, err error) {
204213
builder := newExpresionBuilder()
205214
builder.keys[vk] = struct{}{}
206215
resp, err := c.ddbClient.UpdateItemRequest(&dynamodb.UpdateItemInput{
207216
ExpressionAttributeNames: builder.expressionAttributeNames(),
208217
ExpressionAttributeValues: map[string]dynamodb.AttributeValue{
209-
":delta": NumericValue{delta}.toAV(),
218+
":delta": delta.ToAV(),
210219
},
211220
Key: keyDef{pk: key, sk: field}.toAV(),
212221
ReturnValues: dynamodb.ReturnValueAllNew,
@@ -215,19 +224,19 @@ func (c Client) HINCRBYFLOAT(key string, field string, delta *big.Float) (after
215224
}).Send(context.TODO())
216225

217226
if err == nil {
218-
after, _ = parseItem(resp.UpdateItemOutput.Attributes).val.AsNumeric()
227+
after = ReturnValue{resp.UpdateItemOutput.Attributes[vk]}
219228
}
220229

221230
return
222231
}
223232

224-
func (c Client) HINCRBY(key string, field string, delta *big.Int) (after *big.Int, err error) {
225-
afterFloat, err := c.HINCRBYFLOAT(key, field, new(big.Float).SetInt(delta))
226-
if err != nil {
227-
return
228-
}
233+
func (c Client) HINCRBY(key string, field string, delta int64) (after int64, err error) {
234+
rv, err := c.hIncr(key, field, IntValue{delta})
235+
log.Println(aws.StringValue(rv.AV.N))
229236

230-
after, _ = afterFloat.Int(nil)
237+
if err == nil {
238+
after = rv.Int()
239+
}
231240

232241
return
233242
}
@@ -271,7 +280,7 @@ func (c Client) HKEYS(key string) (keys []string, err error) {
271280
return
272281
}
273282

274-
func (c Client) HVALS(key string) (values []Value, err error) {
283+
func (c Client) HVALS(key string) (values []ReturnValue, err error) {
275284
all, err := c.HGETALL(key)
276285
if err == nil {
277286
for _, v := range all {

hashes_test.go

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package redimo
22

33
import (
4-
"math/big"
54
"testing"
65

76
"github.com/stretchr/testify/assert"
@@ -15,31 +14,32 @@ func TestBasicHashes(t *testing.T) {
1514

1615
val, err := c.HGET("k1", "f1")
1716
assert.NoError(t, err)
18-
assert.Equal(t, StringValue{"v1"}, val)
17+
assert.Equal(t, "v1", val.String())
1918

2019
val, err = c.HGET("k1", "f2")
2120
assert.NoError(t, err)
22-
assert.Equal(t, StringValue{"v2"}, val)
21+
assert.Equal(t, "v2", val.String())
2322

2423
exists, err := c.HEXISTS("k1", "f2")
2524
assert.NoError(t, err)
2625
assert.True(t, exists)
2726

2827
val, err = c.HGET("nosuchkey", "no such field")
2928
assert.NoError(t, err)
30-
assert.Nil(t, val)
29+
assert.True(t, val.Empty())
3130

3231
keyValues, err := c.HGETALL("k1")
3332
assert.NoError(t, err)
34-
assert.Equal(t, map[string]Value{"f1": StringValue{"v1"}, "f2": StringValue{"v2"}}, keyValues)
33+
assert.Len(t, keyValues, 2)
34+
assert.Equal(t, map[string]ReturnValue{"f1": {StringValue{"v1"}.ToAV()}, "f2": {StringValue{"v2"}.ToAV()}}, keyValues)
3535

3636
keys, err := c.HKEYS("k1")
3737
assert.NoError(t, err)
3838
assert.ElementsMatch(t, []string{"f1", "f2"}, keys)
3939

4040
vals, err := c.HVALS("k1")
4141
assert.NoError(t, err)
42-
assert.ElementsMatch(t, []Value{StringValue{"v1"}, StringValue{"v2"}}, vals)
42+
assert.ElementsMatch(t, []ReturnValue{{StringValue{"v1"}.ToAV()}, {StringValue{"v2"}.ToAV()}}, vals)
4343

4444
count, err := c.HLEN("k1")
4545
assert.NoError(t, err)
@@ -50,11 +50,11 @@ func TestBasicHashes(t *testing.T) {
5050

5151
val, err = c.HGET("k1", "f2")
5252
assert.NoError(t, err)
53-
assert.Nil(t, val)
53+
assert.True(t, val.Empty())
5454

5555
val, err = c.HGET("k1", "f1")
5656
assert.NoError(t, err)
57-
assert.Nil(t, val)
57+
assert.True(t, val.Empty())
5858

5959
exists, err = c.HEXISTS("k1", "f1")
6060
assert.NoError(t, err)
@@ -80,15 +80,16 @@ func TestAtomicHashOps(t *testing.T) {
8080

8181
val, err := c.HGET("k1", "f1")
8282
assert.NoError(t, err)
83-
assert.Equal(t, StringValue{"v1"}, val)
83+
assert.Equal(t, "v1", val.String())
8484

8585
val, err = c.HGET("k1", "f2")
8686
assert.NoError(t, err)
87-
assert.Equal(t, StringValue{"v2"}, val)
87+
assert.Equal(t, "v2", val.String())
8888

8989
values, err := c.HMGET("k1", "f1", "f2")
9090
assert.NoError(t, err)
91-
assert.Equal(t, []Value{StringValue{"v1"}, StringValue{"v2"}}, values)
91+
assert.Len(t, values, 2)
92+
assert.Equal(t, []string{"v1", "v2"}, []string{values[0].String(), values[1].String()})
9293

9394
ok, err := c.HSETNX("k1", "f1", StringValue{"v1"})
9495
assert.NoError(t, err)
@@ -100,42 +101,33 @@ func TestAtomicHashOps(t *testing.T) {
100101

101102
val, err = c.HGET("k1", "f9")
102103
assert.NoError(t, err)
103-
assert.Equal(t, StringValue{"v9"}, val)
104+
assert.Equal(t, "v9", val.String())
104105
}
105106

106107
func TestHashCounters(t *testing.T) {
107108
c := newClient(t)
108109

109-
after, err := c.HINCRBYFLOAT("k1", "f1", big.NewFloat(3.14))
110+
after, err := c.HINCRBYFLOAT("k1", "f1", 3.14)
110111
assert.NoError(t, err)
112+
assert.InDelta(t, 3.14, after, 0.001)
111113

112-
f, _ := after.Float64()
113-
assert.InDelta(t, 3.14, f, 0.001)
114-
115-
after, err = c.HINCRBYFLOAT("k1", "f1", big.NewFloat(-1.618))
114+
after, err = c.HINCRBYFLOAT("k1", "f1", -1.618)
116115
assert.NoError(t, err)
116+
assert.InDelta(t, 1.522, after, 0.001)
117117

118-
f, _ = after.Float64()
119-
assert.InDelta(t, 1.522, f, 0.001)
120-
121-
afterInt, err := c.HINCRBY("k1", "f1", big.NewInt(42))
118+
afterInt, err := c.HINCRBY("k1", "f1", 42)
122119
assert.NoError(t, err)
123-
assert.Equal(t, int64(43), afterInt.Int64())
120+
assert.Equal(t, int64(43), afterInt)
124121

125-
afterInt, err = c.HINCRBY("k1", "f1", big.NewInt(-13))
122+
afterInt, err = c.HINCRBY("k1", "f1", -13)
126123
assert.NoError(t, err)
127-
assert.Equal(t, int64(30), afterInt.Int64())
124+
assert.Equal(t, int64(30), afterInt)
128125

129-
afterInt, err = c.HINCRBY("k1", "f2", big.NewInt(42))
126+
afterInt, err = c.HINCRBY("k1", "f2", 42)
130127
assert.NoError(t, err)
131-
assert.Equal(t, int64(42), afterInt.Int64())
128+
assert.Equal(t, int64(42), afterInt)
132129

133130
v, err := c.HGET("k1", "f2")
134131
assert.NoError(t, err)
135-
136-
nval, ok := v.AsNumeric()
137-
assert.True(t, ok)
138-
139-
n, _ := nval.Int64()
140-
assert.Equal(t, n, int64(42))
132+
assert.Equal(t, int64(42), v.Int())
141133
}

0 commit comments

Comments
 (0)