Skip to content

Commit

Permalink
feat: Rename StartTS option methods (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
cynicaljoy authored Nov 12, 2024
1 parent 57ff77a commit e5021d3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
35 changes: 24 additions & 11 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,22 @@ func Typecheck(enabled bool) QueryOptFn {
// StreamOptFn function to set options on the [Client.Stream]
type StreamOptFn func(req *streamRequest)

// StartTime set the streams starting timestamp.
// StreamStartTime set the streams starting timestamp.
//
// Useful when resuming a stream at a given point in time.
func StartTime(ts int64) StreamOptFn {
return func(req *streamRequest) { req.StartTS = ts }
func StreamStartTime(ts time.Time) StreamOptFn {
return func(req *streamRequest) {
req.StartTS = ts.UnixMicro()
}
}

// StreamStartTimeUnixMicros set the stream starting timestamp.
//
// Useful when resuming a stream at a given point in time.
func StreamStartTimeUnixMicros(ts int64) StreamOptFn {
return func(req *streamRequest) {
req.StartTS = ts
}
}

// EventCursor set the stream starting point based on a previously received
Expand Down Expand Up @@ -178,20 +189,22 @@ func EventFeedCursor(cursor string) FeedOptFn {
}

// EventFeedStartTime set the start time for the [fauna.EventFeed]
// cannot be used with [EventFeedCursor] -- expects unix micro timestamp
func EventFeedStartTime(ts int64) FeedOptFn {
return func(req *feedOptions) { req.StartTS = &ts }
}

// EventFeedStartTimeFromTime set the start time for the [fauna.EventFeed]
// from a time.Time -- cannot be used with [EventFeedCursor]
func EventFeedStartTimeFromTime(ts time.Time) FeedOptFn {
// cannot be used with [EventFeedCursor]
func EventFeedStartTime(ts time.Time) FeedOptFn {
return func(req *feedOptions) {
asMicro := ts.UnixMicro()
req.StartTS = &asMicro
}
}

// EventFeedStartTimeUnixMicros set the start time for the [fauna.EventFeed]
// cannot be used with [EventFeedCursor]
func EventFeedStartTimeUnixMicros(ts int64) FeedOptFn {
return func(req *feedOptions) {
req.StartTS = &ts
}
}

// EventFeedPageSize set the page size for the [fauna.EventFeed]
func EventFeedPageSize(ts int) FeedOptFn {
return func(req *feedOptions) { req.PageSize = &ts }
Expand Down
4 changes: 2 additions & 2 deletions event_feed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestEventFeed(t *testing.T) {
unmarshalErr := req.Unmarshal(&response)
require.NoError(t, unmarshalErr, "failed to unmarshal EventSource")

_, feedErr := client.Feed(response, fauna.EventFeedStartTime(time.Now().UnixMicro()), fauna.EventFeedCursor("cursor"))
_, feedErr := client.Feed(response, fauna.EventFeedStartTime(time.Now()), fauna.EventFeedCursor("cursor"))
require.ErrorContains(t, feedErr, "cannot use EventFeedStartTime and EventFeedCursor together")
})
})
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestEventFeed(t *testing.T) {
eventSource = getEventSource(t, client)
require.NotNil(t, eventSource, "failed to get an EventSource")

feed, feedErr = client.Feed(eventSource, fauna.EventFeedStartTimeFromTime(time.Now().Add(-10*time.Minute)))
feed, feedErr = client.Feed(eventSource, fauna.EventFeedStartTime(time.Now().Add(-10*time.Minute)))
require.NoError(t, feedErr, "failed to init events feed")

eventsErr = feed.Next(&page)
Expand Down
2 changes: 1 addition & 1 deletion stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestStreaming(t *testing.T) {
bar, err := client.Query(createBarQ)
require.NoError(t, err)

events, err := client.Stream(stream, fauna.StartTime(foo.TxnTime))
events, err := client.Stream(stream, fauna.StreamStartTimeUnixMicros(foo.TxnTime))
require.NoError(t, err)
defer func() {
_ = events.Close()
Expand Down

0 comments on commit e5021d3

Please sign in to comment.