Skip to content

Commit

Permalink
Rename GeoPosition to GeoPos for consistency with Redis Server. Simpl…
Browse files Browse the repository at this point in the history
…ify code where possible.
  • Loading branch information
vmihailenco committed Aug 22, 2016
1 parent 5e72ba7 commit 235dc49
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 46 deletions.
20 changes: 11 additions & 9 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,10 +806,6 @@ type GeoLocation struct {
GeoHash int64
}

type GeoPosition struct {
Longitude, Latitude float64
}

// GeoRadiusQuery is used with GeoRadius to query geospatial index.
type GeoRadiusQuery struct {
Radius float64
Expand Down Expand Up @@ -886,22 +882,28 @@ func (cmd *GeoLocationCmd) readReply(cn *pool.Conn) error {
return nil
}

//------------------------------------------------------------------------------

type GeoPos struct {
Longitude, Latitude float64
}

type GeoPosCmd struct {
baseCmd

positions []*GeoPosition
positions []*GeoPos
}

func NewGeoPosCmd(args ...interface{}) *GeoPosCmd {
cmd := newBaseCmd(args)
return &GeoPosCmd{baseCmd: cmd}
}

func (cmd *GeoPosCmd) Val() []*GeoPosition {
func (cmd *GeoPosCmd) Val() []*GeoPos {
return cmd.positions
}

func (cmd *GeoPosCmd) Result() ([]*GeoPosition, error) {
func (cmd *GeoPosCmd) Result() ([]*GeoPos, error) {
return cmd.Val(), cmd.Err()
}

Expand All @@ -915,12 +917,12 @@ func (cmd *GeoPosCmd) reset() {
}

func (cmd *GeoPosCmd) readReply(cn *pool.Conn) error {
reply, err := cn.Rd.ReadArrayReply(newGeoPositionSliceParser())
reply, err := cn.Rd.ReadArrayReply(geoPosSliceParser)
if err != nil {
cmd.err = err
return err
}
cmd.positions = reply.([]*GeoPosition)
cmd.positions = reply.([]*GeoPos)
return nil
}

Expand Down
10 changes: 5 additions & 5 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ type Cmdable interface {
ClusterAddSlots(slots ...int) *StatusCmd
ClusterAddSlotsRange(min, max int) *StatusCmd
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
GeoRadiusByMember(key, member string, query *GeoRadiusQuery) *GeoLocationCmd
GeoDist(key string, member1, member2, unit string) *FloatCmd
Expand Down Expand Up @@ -2055,12 +2055,12 @@ func (c *cmdable) GeoHash(key string, members ...string) *StringSliceCmd {
return cmd
}

func (c *cmdable) GeoPos(key string, names ...string) *GeoPosCmd {
args := make([]interface{}, 2+len(names))
func (c *cmdable) GeoPos(key string, members ...string) *GeoPosCmd {
args := make([]interface{}, 2+len(members))
args[0] = "geopos"
args[1] = key
for i, name := range names {
args[2+i] = name
for i, member := range members {
args[2+i] = member
}
cmd := NewGeoPosCmd(args...)
c.process(cmd)
Expand Down
60 changes: 28 additions & 32 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,46 +273,42 @@ func newGeoLocationSliceParser(q *GeoRadiusQuery) proto.MultiBulkParse {
}
}

func newGeoPositionParser() proto.MultiBulkParse {
return func(rd *proto.Reader, n int64) (interface{}, error) {
var pos GeoPosition
var err error
func geoPosParser(rd *proto.Reader, n int64) (interface{}, error) {
var pos GeoPos
var err error

pos.Longitude, err = rd.ReadFloatReply()
if err != nil {
return nil, err
}
pos.Latitude, err = rd.ReadFloatReply()
if err != nil {
return nil, err
}
pos.Longitude, 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 {
return func(rd *proto.Reader, n int64) (interface{}, error) {
positions := make([]*GeoPosition, 0, n)
for i := int64(0); i < n; i++ {
v, err := rd.ReadReply(newGeoPositionParser())
if err != nil {
// response may contain nil results
if err == Nil {
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)
func geoPosSliceParser(rd *proto.Reader, n int64) (interface{}, error) {
positions := make([]*GeoPos, 0, n)
for i := int64(0); i < n; i++ {
v, err := rd.ReadReply(geoPosParser)
if err != nil {
if err == Nil {
positions = append(positions, nil)
continue
}
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) {
Expand Down

0 comments on commit 235dc49

Please sign in to comment.