Skip to content

Commit

Permalink
refactor(pubsublite): rename common.PublishMetadata to publish.Metada…
Browse files Browse the repository at this point in the history
…ta (#3457)

Packages named `common` are generally discouraged.
  • Loading branch information
tmdiep authored Dec 16, 2020
1 parent 98331bd commit 299de04
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
6 changes: 3 additions & 3 deletions pubsublite/internal/wire/publish_batcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"errors"
"fmt"

"cloud.google.com/go/pubsublite/common"
"cloud.google.com/go/pubsublite/publish"
"google.golang.org/api/support/bundler"
"google.golang.org/protobuf/proto"

Expand All @@ -28,7 +28,7 @@ import (
var errPublishQueueEmpty = errors.New("pubsublite: received publish response from server with no batches in flight")

// PublishResultFunc receives the result of a publish.
type PublishResultFunc func(*common.PublishMetadata, error)
type PublishResultFunc func(*publish.Metadata, error)

// messageHolder stores a message to be published, with associated metadata.
type messageHolder struct {
Expand Down Expand Up @@ -141,7 +141,7 @@ func (b *publishMessageBatcher) OnPublishResponse(firstOffset int64) error {
batch, _ := frontElem.Value.(*publishBatch)
for i, msgHolder := range batch.msgHolders {
// Messages are ordered, so the offset of each message is firstOffset + i.
pm := &common.PublishMetadata{Partition: b.partition, Offset: firstOffset + int64(i)}
pm := &publish.Metadata{Partition: b.partition, Offset: firstOffset + int64(i)}
msgHolder.onResult(pm, nil)
b.availableBufferBytes += msgHolder.size
}
Expand Down
6 changes: 3 additions & 3 deletions pubsublite/internal/wire/publish_batcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"time"

"cloud.google.com/go/internal/testutil"
"cloud.google.com/go/pubsublite/common"
"cloud.google.com/go/pubsublite/internal/test"
"cloud.google.com/go/pubsublite/publish"
"github.com/google/go-cmp/cmp"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -35,7 +35,7 @@ type testPublishResultReceiver struct {
done chan struct{}
msg string
t *testing.T
got *common.PublishMetadata
got *publish.Metadata
gotErr error
}

Expand All @@ -47,7 +47,7 @@ func newTestPublishResultReceiver(t *testing.T, msg *pb.PubSubMessage) *testPubl
}
}

func (r *testPublishResultReceiver) set(pm *common.PublishMetadata, err error) {
func (r *testPublishResultReceiver) set(pm *publish.Metadata, err error) {
r.got = pm
r.gotErr = err
close(r.done)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,36 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and

package common
package publish

import (
"fmt"
"strconv"
"strings"
)

// PublishMetadata holds the results of a published message.
type PublishMetadata struct {
// Metadata holds the results of a published message.
type Metadata struct {
Partition int
Offset int64
}

func (pm *PublishMetadata) String() string {
return fmt.Sprintf("%d:%d", pm.Partition, pm.Offset)
func (m *Metadata) String() string {
return fmt.Sprintf("%d:%d", m.Partition, m.Offset)
}

// ParsePublishMetadata converts a string obtained from PublishMetadata.String()
// back to PublishMetadata.
func ParsePublishMetadata(input string) (*PublishMetadata, error) {
// ParseMetadata converts a string obtained from Metadata.String()
// back to Metadata.
func ParseMetadata(input string) (*Metadata, error) {
parts := strings.Split(input, ":")
if len(parts) != 2 {
return nil, fmt.Errorf("pubsublite: invalid encoded PublishMetadata %q", input)
return nil, fmt.Errorf("pubsublite: invalid encoded publish metadata %q", input)
}

partition, pErr := strconv.ParseInt(parts[0], 10, 64)
offset, oErr := strconv.ParseInt(parts[1], 10, 64)
if pErr != nil || oErr != nil {
return nil, fmt.Errorf("pubsublite: invalid encoded PublishMetadata %q", input)
return nil, fmt.Errorf("pubsublite: invalid encoded publish metadata %q", input)
}
return &PublishMetadata{Partition: int(partition), Offset: offset}, nil
return &Metadata{Partition: int(partition), Offset: offset}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and

package common
package publish

import (
"testing"
Expand All @@ -23,18 +23,18 @@ func TestPublishMetadataStringEncoding(t *testing.T) {
for _, tc := range []struct {
desc string
input string
want *PublishMetadata
want *Metadata
wantErr bool
}{
{
desc: "valid: zero",
input: "0:0",
want: &PublishMetadata{Partition: 0, Offset: 0},
want: &Metadata{Partition: 0, Offset: 0},
},
{
desc: "valid: non-zero",
input: "3:1234",
want: &PublishMetadata{Partition: 3, Offset: 1234},
want: &Metadata{Partition: 3, Offset: 1234},
},
{
desc: "invalid: number",
Expand All @@ -53,14 +53,14 @@ func TestPublishMetadataStringEncoding(t *testing.T) {
},
} {
t.Run(tc.desc, func(t *testing.T) {
got, gotErr := ParsePublishMetadata(tc.input)
got, gotErr := ParseMetadata(tc.input)
if !testutil.Equal(got, tc.want) || (gotErr != nil) != tc.wantErr {
t.Errorf("ParsePublishMetadata(%q): got (%v, %v), want (%v, err=%v)", tc.input, got, gotErr, tc.want, tc.wantErr)
t.Errorf("ParseMetadata(%q): got (%v, %v), want (%v, err=%v)", tc.input, got, gotErr, tc.want, tc.wantErr)
}

if tc.want != nil {
if got := tc.want.String(); got != tc.input {
t.Errorf("PublishMetadata(%v).String(): got %q, want: %q", tc.want, got, tc.input)
t.Errorf("Metadata(%v).String(): got %q, want: %q", tc.want, got, tc.input)
}
}
})
Expand Down

0 comments on commit 299de04

Please sign in to comment.