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 table name alias an prefix support #116

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion createtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ type CreateTable struct {

// CreateTable begins a new operation to create a table with the given name.
// The second parameter must be a struct with appropriate hash and range key struct tags
// for the primary key and all indices.
// for the primary key and all indices. The name of the table follows the same alias and
// prefix resolution rules as db.Table().
//
// An example of a from struct follows:
// type UserAction struct {
Expand All @@ -70,6 +71,8 @@ type CreateTable struct {
// It creates two global secondary indices called UUID-index and Seq-ID-index,
// and a local secondary index called ID-Seq-index.
func (db *DB) CreateTable(name string, from interface{}) *CreateTable {
name = db.resolveTableName(name)

ct := &CreateTable{
db: db,
tableName: name,
Expand Down
10 changes: 10 additions & 0 deletions createtable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/stretchr/testify/require"
)

type UserAction struct {
Expand Down Expand Up @@ -155,3 +156,12 @@ func TestCreateTableUintUnixTime(t *testing.T) {
t.Error("unexpected input (unixtime tag)", input2)
}
}

func TestCreateTableAliasAndPrefix(t *testing.T) {
db := NewFromIface(nil)
db.Alias["Alias1"] = "table1"
db.Prefix = "test-"

input := db.CreateTable("Alias1", Metric{})
require.Equal(t, "test-table1", input.tableName)
}
24 changes: 22 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,46 @@ import (
// DB is a DynamoDB client.
type DB struct {
client dynamodbiface.DynamoDBAPI
Alias map[string]string // Maps table aliases to physical names.
Prefix string // Prepended to table names.
}

// New creates a new client with the given configuration.
func New(p client.ConfigProvider, cfgs ...*aws.Config) *DB {
db := &DB{
dynamodb.New(p, cfgs...),
client: dynamodb.New(p, cfgs...),
Alias: map[string]string{},
}
return db
}

// NewFromIface creates a new client with the given interface.
func NewFromIface(client dynamodbiface.DynamoDBAPI) *DB {
return &DB{client}
return &DB{
client: client,
Alias: map[string]string{},
}
}

// Client returns this DB's internal client used to make API requests.
func (db *DB) Client() dynamodbiface.DynamoDBAPI {
return db.client
}

func (db *DB) resolveTableName(name string) string {
if db == nil {
return name
}

if db.Alias != nil {
if v, ok := db.Alias[name]; ok {
name = v
}
}

return db.Prefix + name
}

// ListTables is a request to list tables.
// See: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_ListTables.html
type ListTables struct {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require (
github.com/cenkalti/backoff v2.1.1+incompatible
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gofrs/uuid v3.2.0+incompatible
github.com/stretchr/testify v1.3.0 // indirect
github.com/stretchr/testify v1.3.0
golang.org/x/net v0.0.0-20190318221613-d196dffd7c2b
)

Expand Down
7 changes: 5 additions & 2 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ type Table struct {
db *DB
}

// Table returns a Table handle specified by name.
// Table returns a Table handle specified by name. If an exact match to
// name exists in the DB's alias map, the value of that is used instead.
// If the DB is configured with a prefix, it is prepended to the table name
// regardless of whether an alias was used or not.
func (db *DB) Table(name string) Table {
return Table{
name: name,
name: db.resolveTableName(name),
db: db,
}
}
Expand Down
15 changes: 15 additions & 0 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/stretchr/testify/require"
)

func TestAddConsumedCapacity(t *testing.T) {
Expand Down Expand Up @@ -57,3 +58,17 @@ func TestAddConsumedCapacity(t *testing.T) {
t.Error("bad ConsumedCapacity:", cc, "≠", expected)
}
}

func TestTableAliasAndPrefix(t *testing.T) {
db := NewFromIface(nil)
require.Equal(t, "Alias1", db.Table("Alias1").name)

db.Alias["Alias1"] = "table1"
db.Alias["Alias2"] = "table2"
require.Equal(t, "table1", db.Table("Alias1").name)
require.Equal(t, "Alias3", db.Table("Alias3").name)

db.Prefix = "test-"
require.Equal(t, "test-table1", db.Table("Alias1").name)
require.Equal(t, "test-Alias3", db.Table("Alias3").name)
}