Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add offset type to NewOffset func #844

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions kafka/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ func TestOffsetAPIs(t *testing.T) {
t.Errorf("Failed to change offset. Expect (%v), got (%v)\n", 10, offset)
}

// test passing explicit 'Offset' type
err = offset.Set(OffsetBeginning)
if err != nil {
t.Errorf("Cannot set offset to (%v). Error: %s \n", OffsetBeginning, err)
} else if offset != OffsetBeginning {
t.Errorf("Failed to change offset. Expect (%v), got %v\n", OffsetBeginning, offset)
}

// test passing explicit 'Offset' type
err = offset.Set(OffsetEnd)
if err != nil {
t.Errorf("Cannot set offset to (%v). Error: %s \n", OffsetEnd, err)
} else if offset != OffsetEnd {
t.Errorf("Failed to change offset. Expect (%v), got %v\n", OffsetEnd, offset)
}

// test OffsetTail()
tail := OffsetTail(offset)
t.Logf("offset tail %v\n", tail)
Expand Down
6 changes: 4 additions & 2 deletions kafka/offset.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func (o *Offset) Set(offset interface{}) error {
return err
}

// NewOffset creates a new Offset using the provided logical string, or an
// absolute int64 offset value.
// NewOffset creates a new Offset using the provided logical string, an
// absolute int64 offset value, or a concrete Offset type.
// Logical offsets: "beginning", "earliest", "end", "latest", "unset", "invalid", "stored"
func NewOffset(offset interface{}) (Offset, error) {

Expand Down Expand Up @@ -107,6 +107,8 @@ func NewOffset(offset interface{}) (Offset, error) {
return Offset((int64)(v)), nil
case int64:
return Offset(v), nil
case Offset:
return Offset(v), nil
default:
return OffsetInvalid, newErrorFromString(ErrInvalidArg,
fmt.Sprintf("Invalid offset type: %t", v))
Expand Down