Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
refactor: use new EventHandler interface for subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspore committed Nov 14, 2022
1 parent 427692a commit b2cf3a6
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 23 deletions.
4 changes: 2 additions & 2 deletions channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (ch *Channels) PDelChan(ctx context.Context, pattern string) error {
}

// PSubscribe subscribes the client to the given patterns.
func (ch *Channels) PSubscribe(ctx context.Context, handler func(*GeofenceEvent) error, pattern string) error {
func (ch *Channels) PSubscribe(ctx context.Context, handler EventHandler, pattern string) error {
return ch.client.ExecuteStream(ctx, rawEventHandler(handler), "PSUBSCRIBE", pattern)
}

Expand All @@ -45,6 +45,6 @@ func (ch *Channels) SetChan(name string, query GeofenceQueryBuilder) SetChannelQ
}

// Subscribe subscribes the client to the specified Channels.
func (ch *Channels) Subscribe(ctx context.Context, handler func(*GeofenceEvent) error, Channels ...string) error {
func (ch *Channels) Subscribe(ctx context.Context, handler EventHandler, Channels ...string) error {
return ch.client.ExecuteStream(ctx, rawEventHandler(handler), "SUBSCRIBE", Channels...)
}
4 changes: 2 additions & 2 deletions e2e/geofence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func testGeofence(t *testing.T, client *t38c.Client) {

errOk := errors.New("success")
n := 0
handler := func(event *t38c.GeofenceEvent) error {
handler := t38c.EventHandlerFunc(func(event *t38c.GeofenceEvent) error {
b, err := json.Marshal(event)
if err != nil {
return err
Expand Down Expand Up @@ -60,7 +60,7 @@ func testGeofence(t *testing.T, client *t38c.Client) {

n++
return nil
}
})

err := client.Geofence.Nearby("geofence-test", 33, -112, 10000).
Actions(t38c.Enter, t38c.Exit).
Expand Down
4 changes: 2 additions & 2 deletions examples/geofencing.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ func main() {
}
defer tile38.Close()

handler := func(event *t38c.GeofenceEvent) error {
handler := t38c.EventHandlerFunc(func(event *t38c.GeofenceEvent) error {
b, err := json.Marshal(event)
if err != nil {
return fmt.Errorf("marshal event: %w", err)
}

fmt.Printf("event: %s\n", b)
return nil
}
})

if err := tile38.Geofence.Nearby("fleet", 33.462, -112.268, 6000).
Actions(t38c.Enter, t38c.Exit).
Expand Down
4 changes: 2 additions & 2 deletions examples/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ func main() {
log.Fatal(err)
}

handler := func(event *t38c.GeofenceEvent) error {
handler := t38c.EventHandlerFunc(func(event *t38c.GeofenceEvent) error {
b, err := json.Marshal(event)
if err != nil {
return fmt.Errorf("marshal event: %w", err)
}

fmt.Printf("event: %s\n", b)
return nil
}
})

if err := tile38.Channels.Subscribe(context.Background(), handler, "busstop"); err != nil {
log.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion geofence_query_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (query GeofenceQueryBuilder) toCmd() cmd {
}

// Do cmd
func (query GeofenceQueryBuilder) Do(ctx context.Context, handler func(*GeofenceEvent) error) error {
func (query GeofenceQueryBuilder) Do(ctx context.Context, handler EventHandler) error {
cmd := query.toCmd()
return query.client.ExecuteStream(ctx, rawEventHandler(handler), cmd.Name, cmd.Args...)
}
Expand Down
39 changes: 27 additions & 12 deletions objects.go → types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,34 @@ type field struct {
Value float64
}

// KeyStats struct
// KeyStats is a tile38 key stats.
type KeyStats struct {
InMemorySize int `json:"in_memory_size"`
NumObjects int `json:"num_objects"`
NumPoints int `json:"num_points"`
}

// Point struct
// Point is a tile38 point.
type Point struct {
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
}

// Bounds struct
// Bounds is a tile38 bounds object.
type Bounds struct {
SW Point `json:"sw"`
NE Point `json:"ne"`
}

// Object struct
// Object is a tile38 object.
type Object struct {
FeatureCollection *geojson.FeatureCollection `json:"featureCollection,omitempty"`
Feature *geojson.Feature `json:"feature,omitempty"`
Geometry *geojson.Geometry `json:"geometry,omitempty"`
String *string `json:"string,omitempty"`
}

// UnmarshalJSON ...
// UnmarshalJSON unmarshals object from the given json data.
func (ob *Object) UnmarshalJSON(data []byte) error {
res := gjson.ParseBytes(data)
objectType := res.Get("type")
Expand All @@ -63,7 +63,7 @@ func (ob *Object) UnmarshalJSON(data []byte) error {
return err
}

// SearchResponse struct
// SearchResponse is a tile38 search response.
type SearchResponse struct {
Cursor int `json:"cursor"`
Count int `json:"count"`
Expand Down Expand Up @@ -95,7 +95,7 @@ type SearchResponse struct {
IDs []string `json:"ids,omitempty"`
}

// OutputFormat ...
// OutputFormat specifies expected format.
type OutputFormat cmd

var (
Expand All @@ -116,27 +116,42 @@ var (
}
)

// Meta struct
// Meta is tile38 metadata.
type Meta struct {
Name string
Value string
}

// Hook struct
// Hook represents tile38 channel.
type Hook struct {
Endpoints []string `json:"endpoints"`
Chan
}

// Chan struct
// Chan represents tile38 channel.
type Chan struct {
Name string `json:"name"`
Key string `json:"key"`
Command []string `json:"command"`
Meta map[string]string `json:"meta"`
}

// GeofenceEvent struct
// EventHandler handles tile38 events.
type EventHandler interface {
// HandleEvent handles tile38 event.
HandleEvent(event *GeofenceEvent) error
}

// EventHandlerFunc is an adapter to allow the use of
// ordinary functions as tile38 event handlers.
type EventHandlerFunc func(event *GeofenceEvent) error

// HandleEvent handles tile38 event.
func (e EventHandlerFunc) HandleEvent(event *GeofenceEvent) error {
return e(event)
}

// GeofenceEvent is a tile38 geofence event.
type GeofenceEvent struct {
Command string `json:"command"`
Hook string `json:"hook,omitempty"`
Expand All @@ -154,7 +169,7 @@ type GeofenceEvent struct {
Fields map[string]float64 `json:"fields,omitempty"`
}

// RoamObject struct
// RoamObject is a tile38 roam object.
type RoamObject struct {
Key string `json:"key"`
ID string `json:"id"`
Expand Down
4 changes: 2 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ func floatString(val float64) string {
return strconv.FormatFloat(val, 'f', -1, 64)
}

func rawEventHandler(handler func(*GeofenceEvent) error) func([]byte) error {
func rawEventHandler(handler EventHandler) func([]byte) error {
return func(data []byte) error {
resp := &GeofenceEvent{}
if err := json.Unmarshal(data, resp); err != nil {
return fmt.Errorf("json unmarshal geofence response: %v", err)
}

return handler(resp)
return handler.HandleEvent(resp)
}
}

0 comments on commit b2cf3a6

Please sign in to comment.