Skip to content

Commit

Permalink
Interface change: rm seek target struct wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
tmdiep committed Jul 2, 2021
1 parent 8eb645a commit c075fbc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
8 changes: 4 additions & 4 deletions pubsublite/admin_test.go
Expand Up @@ -467,7 +467,7 @@ func TestAdminSeekSubscription(t *testing.T) {
}{
{
desc: "Beginning",
target: BacklogLocationSeekTarget{Beginning},
target: Beginning,
wantReq: &pb.SeekSubscriptionRequest{
Name: subscriptionPath,
Target: &pb.SeekSubscriptionRequest_NamedTarget_{
Expand All @@ -477,7 +477,7 @@ func TestAdminSeekSubscription(t *testing.T) {
},
{
desc: "End",
target: BacklogLocationSeekTarget{End},
target: End,
wantReq: &pb.SeekSubscriptionRequest{
Name: subscriptionPath,
Target: &pb.SeekSubscriptionRequest_NamedTarget_{
Expand All @@ -487,7 +487,7 @@ func TestAdminSeekSubscription(t *testing.T) {
},
{
desc: "PublishTime",
target: PublishTimeSeekTarget{time.Unix(1234, 0)},
target: PublishTime(time.Unix(1234, 0)),
wantReq: &pb.SeekSubscriptionRequest{
Name: subscriptionPath,
Target: &pb.SeekSubscriptionRequest_TimeTarget{
Expand All @@ -501,7 +501,7 @@ func TestAdminSeekSubscription(t *testing.T) {
},
{
desc: "EventTime",
target: EventTimeSeekTarget{time.Unix(2345, 0)},
target: EventTime(time.Unix(2345, 0)),
wantReq: &pb.SeekSubscriptionRequest{
Name: subscriptionPath,
Target: &pb.SeekSubscriptionRequest_TimeTarget{
Expand Down
3 changes: 1 addition & 2 deletions pubsublite/example_test.go
Expand Up @@ -184,8 +184,7 @@ func ExampleAdminClient_SeekSubscription() {
}

const subscription = "projects/my-project/locations/zone/subscriptions/my-subscription"
seekOp, err := admin.SeekSubscription(ctx, subscription,
pubsublite.BacklogLocationSeekTarget{pubsublite.Beginning})
seekOp, err := admin.SeekSubscription(ctx, subscription, pubsublite.Beginning)
if err != nil {
// TODO: Handle error.
}
Expand Down
45 changes: 19 additions & 26 deletions pubsublite/operations.go
Expand Up @@ -22,7 +22,14 @@ import (
tspb "google.golang.org/protobuf/types/known/timestamppb"
)

// SeekTarget is the target location to seek a subscription to. Implemented by
// BacklogLocation, PublishTime, EventTime.
type SeekTarget interface {
setRequest(req *pb.SeekSubscriptionRequest)
}

// BacklogLocation refers to a location with respect to the message backlog.
// It implements the SeekTarget interface.
type BacklogLocation int

const (
Expand All @@ -34,50 +41,36 @@ const (
Beginning
)

// SeekTarget is the target location to seek a subscription to. Implemented by
// BacklogLocationSeekTarget, PublishTimeSeekTarget, EventTimeSeekTarget.
type SeekTarget interface {
setRequest(req *pb.SeekSubscriptionRequest)
}

// BacklogLocationSeekTarget is a seek target to a location with respect to the
// message backlog.
type BacklogLocationSeekTarget struct {
Location BacklogLocation
}

func (b BacklogLocationSeekTarget) setRequest(req *pb.SeekSubscriptionRequest) {
func (b BacklogLocation) setRequest(req *pb.SeekSubscriptionRequest) {
target := pb.SeekSubscriptionRequest_TAIL
if b.Location == End {
if b == End {
target = pb.SeekSubscriptionRequest_HEAD
}
req.Target = &pb.SeekSubscriptionRequest_NamedTarget_{
NamedTarget: target,
}
}

// PublishTimeSeekTarget is a seek target to a message publish timestamp.
type PublishTimeSeekTarget struct {
Time time.Time
}
// PublishTime is a message publish timestamp. It implements the SeekTarget
// interface.
type PublishTime time.Time

func (p PublishTimeSeekTarget) setRequest(req *pb.SeekSubscriptionRequest) {
func (p PublishTime) setRequest(req *pb.SeekSubscriptionRequest) {
req.Target = &pb.SeekSubscriptionRequest_TimeTarget{
TimeTarget: &pb.TimeTarget{
Time: &pb.TimeTarget_PublishTime{tspb.New(p.Time)},
Time: &pb.TimeTarget_PublishTime{tspb.New(time.Time(p))},
},
}
}

// EventTimeSeekTarget is a seek target to a message event timestamp.
type EventTimeSeekTarget struct {
Time time.Time
}
// EventTime is a message event timestamp. It implements the SeekTarget
// interface.
type EventTime time.Time

func (p EventTimeSeekTarget) setRequest(req *pb.SeekSubscriptionRequest) {
func (e EventTime) setRequest(req *pb.SeekSubscriptionRequest) {
req.Target = &pb.SeekSubscriptionRequest_TimeTarget{
TimeTarget: &pb.TimeTarget{
Time: &pb.TimeTarget_EventTime{tspb.New(p.Time)},
Time: &pb.TimeTarget_EventTime{tspb.New(time.Time(e))},
},
}
}
Expand Down

0 comments on commit c075fbc

Please sign in to comment.