diff --git a/channels.go b/channels.go index 8748a19..ba51ee9 100644 --- a/channels.go +++ b/channels.go @@ -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) } @@ -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...) } diff --git a/e2e/geofence_test.go b/e2e/geofence_test.go index f6a3d45..10b6872 100644 --- a/e2e/geofence_test.go +++ b/e2e/geofence_test.go @@ -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 @@ -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). diff --git a/examples/geofencing.go b/examples/geofencing.go index 2aef2b9..c4be3a4 100644 --- a/examples/geofencing.go +++ b/examples/geofencing.go @@ -22,7 +22,7 @@ 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) @@ -30,7 +30,7 @@ func main() { fmt.Printf("event: %s\n", b) return nil - } + }) if err := tile38.Geofence.Nearby("fleet", 33.462, -112.268, 6000). Actions(t38c.Enter, t38c.Exit). diff --git a/examples/pubsub.go b/examples/pubsub.go index a88490c..1a1d217 100644 --- a/examples/pubsub.go +++ b/examples/pubsub.go @@ -29,7 +29,7 @@ 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) @@ -37,7 +37,7 @@ func main() { fmt.Printf("event: %s\n", b) return nil - } + }) if err := tile38.Channels.Subscribe(context.Background(), handler, "busstop"); err != nil { log.Fatal(err) diff --git a/geofence_query_builder.go b/geofence_query_builder.go index 0f8565f..e8e8a6d 100644 --- a/geofence_query_builder.go +++ b/geofence_query_builder.go @@ -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...) } diff --git a/objects.go b/types.go similarity index 85% rename from objects.go rename to types.go index 97c314b..94063e3 100644 --- a/objects.go +++ b/types.go @@ -13,26 +13,26 @@ 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"` @@ -40,7 +40,7 @@ type Object struct { 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") @@ -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"` @@ -95,7 +95,7 @@ type SearchResponse struct { IDs []string `json:"ids,omitempty"` } -// OutputFormat ... +// OutputFormat specifies expected format. type OutputFormat cmd var ( @@ -116,19 +116,19 @@ 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"` @@ -136,7 +136,22 @@ type Chan struct { 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"` @@ -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"` diff --git a/utils.go b/utils.go index 7daeba6..c8c1f14 100644 --- a/utils.go +++ b/utils.go @@ -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) } }