mirror of https://github.com/go-redis/redis.git
Merge pull request #355 from go-redis/fix/geo-pos-rename
Fix/geo pos rename
This commit is contained in:
commit
342961af9e
|
@ -5,8 +5,8 @@ services:
|
||||||
- redis-server
|
- redis-server
|
||||||
|
|
||||||
go:
|
go:
|
||||||
- 1.5
|
|
||||||
- 1.6
|
- 1.6
|
||||||
|
- 1.7
|
||||||
- tip
|
- tip
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
|
|
20
command.go
20
command.go
|
@ -806,10 +806,6 @@ type GeoLocation struct {
|
||||||
GeoHash int64
|
GeoHash int64
|
||||||
}
|
}
|
||||||
|
|
||||||
type GeoPosition struct {
|
|
||||||
Longitude, Latitude float64
|
|
||||||
}
|
|
||||||
|
|
||||||
// GeoRadiusQuery is used with GeoRadius to query geospatial index.
|
// GeoRadiusQuery is used with GeoRadius to query geospatial index.
|
||||||
type GeoRadiusQuery struct {
|
type GeoRadiusQuery struct {
|
||||||
Radius float64
|
Radius float64
|
||||||
|
@ -886,10 +882,16 @@ func (cmd *GeoLocationCmd) readReply(cn *pool.Conn) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
type GeoPos struct {
|
||||||
|
Longitude, Latitude float64
|
||||||
|
}
|
||||||
|
|
||||||
type GeoPosCmd struct {
|
type GeoPosCmd struct {
|
||||||
baseCmd
|
baseCmd
|
||||||
|
|
||||||
positions []*GeoPosition
|
positions []*GeoPos
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGeoPosCmd(args ...interface{}) *GeoPosCmd {
|
func NewGeoPosCmd(args ...interface{}) *GeoPosCmd {
|
||||||
|
@ -897,11 +899,11 @@ func NewGeoPosCmd(args ...interface{}) *GeoPosCmd {
|
||||||
return &GeoPosCmd{baseCmd: cmd}
|
return &GeoPosCmd{baseCmd: cmd}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *GeoPosCmd) Val() []*GeoPosition {
|
func (cmd *GeoPosCmd) Val() []*GeoPos {
|
||||||
return cmd.positions
|
return cmd.positions
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *GeoPosCmd) Result() ([]*GeoPosition, error) {
|
func (cmd *GeoPosCmd) Result() ([]*GeoPos, error) {
|
||||||
return cmd.Val(), cmd.Err()
|
return cmd.Val(), cmd.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -915,12 +917,12 @@ func (cmd *GeoPosCmd) reset() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cmd *GeoPosCmd) readReply(cn *pool.Conn) error {
|
func (cmd *GeoPosCmd) readReply(cn *pool.Conn) error {
|
||||||
reply, err := cn.Rd.ReadArrayReply(newGeoPositionSliceParser())
|
reply, err := cn.Rd.ReadArrayReply(geoPosSliceParser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cmd.err = err
|
cmd.err = err
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cmd.positions = reply.([]*GeoPosition)
|
cmd.positions = reply.([]*GeoPos)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
commands.go
10
commands.go
|
@ -234,7 +234,7 @@ type Cmdable interface {
|
||||||
ClusterAddSlots(slots ...int) *StatusCmd
|
ClusterAddSlots(slots ...int) *StatusCmd
|
||||||
ClusterAddSlotsRange(min, max int) *StatusCmd
|
ClusterAddSlotsRange(min, max int) *StatusCmd
|
||||||
GeoAdd(key string, geoLocation ...*GeoLocation) *IntCmd
|
GeoAdd(key string, geoLocation ...*GeoLocation) *IntCmd
|
||||||
GeoPos(key string, name ...string) *GeoPosCmd
|
GeoPos(key string, members ...string) *GeoPosCmd
|
||||||
GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd
|
GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd
|
||||||
GeoRadiusByMember(key, member string, query *GeoRadiusQuery) *GeoLocationCmd
|
GeoRadiusByMember(key, member string, query *GeoRadiusQuery) *GeoLocationCmd
|
||||||
GeoDist(key string, member1, member2, unit string) *FloatCmd
|
GeoDist(key string, member1, member2, unit string) *FloatCmd
|
||||||
|
@ -2055,12 +2055,12 @@ func (c *cmdable) GeoHash(key string, members ...string) *StringSliceCmd {
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *cmdable) GeoPos(key string, names ...string) *GeoPosCmd {
|
func (c *cmdable) GeoPos(key string, members ...string) *GeoPosCmd {
|
||||||
args := make([]interface{}, 2+len(names))
|
args := make([]interface{}, 2+len(members))
|
||||||
args[0] = "geopos"
|
args[0] = "geopos"
|
||||||
args[1] = key
|
args[1] = key
|
||||||
for i, name := range names {
|
for i, member := range members {
|
||||||
args[2+i] = name
|
args[2+i] = member
|
||||||
}
|
}
|
||||||
cmd := NewGeoPosCmd(args...)
|
cmd := NewGeoPosCmd(args...)
|
||||||
c.process(cmd)
|
c.process(cmd)
|
||||||
|
|
|
@ -2689,20 +2689,35 @@ var _ = Describe("Commands", func() {
|
||||||
// "166274.15156960033"
|
// "166274.15156960033"
|
||||||
// GEODIST Sicily Palermo Catania km
|
// GEODIST Sicily Palermo Catania km
|
||||||
// "166.27415156960032"
|
// "166.27415156960032"
|
||||||
geoDist := client.GeoDist("Sicily", "Palermo", "Catania", "km")
|
dist, err := client.GeoDist("Sicily", "Palermo", "Catania", "km").Result()
|
||||||
Expect(geoDist.Err()).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(geoDist.Val()).To(BeNumerically("~", 166.27, 0.01))
|
Expect(dist).To(BeNumerically("~", 166.27, 0.01))
|
||||||
|
|
||||||
geoDist = client.GeoDist("Sicily", "Palermo", "Catania", "m")
|
dist, err = client.GeoDist("Sicily", "Palermo", "Catania", "m").Result()
|
||||||
Expect(geoDist.Err()).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(geoDist.Val()).To(BeNumerically("~", 166274.15, 0.01))
|
Expect(dist).To(BeNumerically("~", 166274.15, 0.01))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("should get geo hash in string representation", func() {
|
It("should get geo hash in string representation", func() {
|
||||||
res, err := client.GeoHash("Sicily", "Palermo", "Catania").Result()
|
hashes, err := client.GeoHash("Sicily", "Palermo", "Catania").Result()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(res[0]).To(Equal("sqc8b49rny0"))
|
Expect(hashes).To(ConsistOf([]string{"sqc8b49rny0", "sqdtr74hyu0"}))
|
||||||
Expect(res[1]).To(Equal("sqdtr74hyu0"))
|
})
|
||||||
|
|
||||||
|
It("should return geo position", func() {
|
||||||
|
pos, err := client.GeoPos("Sicily", "Palermo", "Catania", "NonExisting").Result()
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(pos).To(ConsistOf([]*redis.GeoPos{
|
||||||
|
{
|
||||||
|
Longitude: 13.361389338970184,
|
||||||
|
Latitude: 38.1155563954963,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Longitude: 15.087267458438873,
|
||||||
|
Latitude: 37.50266842333162,
|
||||||
|
},
|
||||||
|
nil,
|
||||||
|
}))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
62
parser.go
62
parser.go
|
@ -273,46 +273,42 @@ func newGeoLocationSliceParser(q *GeoRadiusQuery) proto.MultiBulkParse {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newGeoPositionParser() proto.MultiBulkParse {
|
func geoPosParser(rd *proto.Reader, n int64) (interface{}, error) {
|
||||||
return func(rd *proto.Reader, n int64) (interface{}, error) {
|
var pos GeoPos
|
||||||
var pos GeoPosition
|
var err error
|
||||||
var err error
|
|
||||||
|
|
||||||
pos.Longitude, err = rd.ReadFloatReply()
|
pos.Longitude, err = rd.ReadFloatReply()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
|
||||||
pos.Latitude, err = rd.ReadFloatReply()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &pos, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pos.Latitude, err = rd.ReadFloatReply()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &pos, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newGeoPositionSliceParser() proto.MultiBulkParse {
|
func geoPosSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
|
||||||
return func(rd *proto.Reader, n int64) (interface{}, error) {
|
positions := make([]*GeoPos, 0, n)
|
||||||
positions := make([]*GeoPosition, 0, n)
|
for i := int64(0); i < n; i++ {
|
||||||
for i := int64(0); i < n; i++ {
|
v, err := rd.ReadReply(geoPosParser)
|
||||||
v, err := rd.ReadReply(newGeoPositionParser())
|
if err != nil {
|
||||||
if err != nil {
|
if err == Nil {
|
||||||
// response may contain nil results
|
positions = append(positions, nil)
|
||||||
if err == Nil {
|
continue
|
||||||
positions = append(positions, nil)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
switch vv := v.(type) {
|
|
||||||
case *GeoPosition:
|
|
||||||
positions = append(positions, vv)
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("got %T, expected *GeoPosition", v)
|
|
||||||
}
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
switch v := v.(type) {
|
||||||
|
case *GeoPos:
|
||||||
|
positions = append(positions, v)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("got %T, expected *GeoPos", v)
|
||||||
}
|
}
|
||||||
return positions, nil
|
|
||||||
}
|
}
|
||||||
|
return positions, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func commandInfoParser(rd *proto.Reader, n int64) (interface{}, error) {
|
func commandInfoParser(rd *proto.Reader, n int64) (interface{}, error) {
|
||||||
|
|
Loading…
Reference in New Issue