From 152b2d790e735ee8a564f78b2a85ed2c2bd01734 Mon Sep 17 00:00:00 2001 From: Roberth Kulbin Date: Fri, 24 Jan 2020 14:54:50 +0000 Subject: [PATCH 1/4] add table alias support --- db.go | 9 +++++++-- go.mod | 2 +- table.go | 9 ++++++++- table_test.go | 13 +++++++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/db.go b/db.go index 54de22c..295b031 100644 --- a/db.go +++ b/db.go @@ -13,19 +13,24 @@ import ( // DB is a DynamoDB client. type DB struct { client dynamodbiface.DynamoDBAPI + Alias map[string]string // Maps table aliases to physical 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. diff --git a/go.mod b/go.mod index d01664a..0650e04 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/table.go b/table.go index a21b94f..e6286f0 100644 --- a/table.go +++ b/table.go @@ -25,8 +25,15 @@ 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. func (db *DB) Table(name string) Table { + if db.Alias != nil { + if v, ok := db.Alias[name]; ok { + name = v + } + } + return Table{ name: name, db: db, diff --git a/table_test.go b/table_test.go index 61ff681..8257628 100644 --- a/table_test.go +++ b/table_test.go @@ -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) { @@ -57,3 +58,15 @@ func TestAddConsumedCapacity(t *testing.T) { t.Error("bad ConsumedCapacity:", cc, "≠", expected) } } + +func TestTableAlias(t *testing.T) { + db := NewFromIface(nil) + + require.Equal(t, "MyTable", db.Table("MyTable").name) + + db.Alias["MyTable"] = "my-actual-table" + db.Alias["OtherTable"] = "my-other-table" + + require.Equal(t, "my-actual-table", db.Table("MyTable").name) + require.Equal(t, "AnotherTable", db.Table("AnotherTable").name) +} From cefd0ad6657b5efce6fb231ed92af95d745c1ea0 Mon Sep 17 00:00:00 2001 From: Roberth Kulbin Date: Fri, 24 Jan 2020 15:24:07 +0000 Subject: [PATCH 2/4] add table prefix support --- db.go | 1 + table.go | 4 +++- table_test.go | 8 +++++--- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/db.go b/db.go index 295b031..1cddcaf 100644 --- a/db.go +++ b/db.go @@ -14,6 +14,7 @@ import ( 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. diff --git a/table.go b/table.go index e6286f0..5f541b8 100644 --- a/table.go +++ b/table.go @@ -27,6 +27,8 @@ type Table struct { // 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 { if db.Alias != nil { if v, ok := db.Alias[name]; ok { @@ -35,7 +37,7 @@ func (db *DB) Table(name string) Table { } return Table{ - name: name, + name: db.Prefix + name, db: db, } } diff --git a/table_test.go b/table_test.go index 8257628..1286608 100644 --- a/table_test.go +++ b/table_test.go @@ -59,14 +59,16 @@ func TestAddConsumedCapacity(t *testing.T) { } } -func TestTableAlias(t *testing.T) { +func TestTableAliasAndPrefix(t *testing.T) { db := NewFromIface(nil) - require.Equal(t, "MyTable", db.Table("MyTable").name) db.Alias["MyTable"] = "my-actual-table" db.Alias["OtherTable"] = "my-other-table" - require.Equal(t, "my-actual-table", db.Table("MyTable").name) require.Equal(t, "AnotherTable", db.Table("AnotherTable").name) + + db.Prefix = "Test-" + require.Equal(t, "Test-my-actual-table", db.Table("MyTable").name) + require.Equal(t, "Test-AnotherTable", db.Table("AnotherTable").name) } From bd562728a2faf0ebff523a0efd4c9d3a915e9442 Mon Sep 17 00:00:00 2001 From: Roberth Kulbin Date: Fri, 24 Jan 2020 15:33:49 +0000 Subject: [PATCH 3/4] add table alias and prefix support to create table operation --- createtable.go | 5 ++++- createtable_test.go | 10 ++++++++++ db.go | 14 ++++++++++++++ table.go | 8 +------- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/createtable.go b/createtable.go index 4eafb80..f141531 100644 --- a/createtable.go +++ b/createtable.go @@ -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 { @@ -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, diff --git a/createtable_test.go b/createtable_test.go index 486b33d..baa91be 100644 --- a/createtable_test.go +++ b/createtable_test.go @@ -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 { @@ -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["Table1"] = "real-table-1" + db.Prefix = "Test-" + + input := db.CreateTable("Table1", Metric{}) + require.Equal(t, "Test-real-table-1", input.tableName) +} diff --git a/db.go b/db.go index 1cddcaf..8bae5c6 100644 --- a/db.go +++ b/db.go @@ -39,6 +39,20 @@ 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 { diff --git a/table.go b/table.go index 5f541b8..15dc5b3 100644 --- a/table.go +++ b/table.go @@ -30,14 +30,8 @@ type Table struct { // 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 { - if db.Alias != nil { - if v, ok := db.Alias[name]; ok { - name = v - } - } - return Table{ - name: db.Prefix + name, + name: db.resolveTableName(name), db: db, } } From a53b59c2935ef7c87879efff295aebb15f0b0adb Mon Sep 17 00:00:00 2001 From: Roberth Kulbin Date: Sat, 25 Jan 2020 10:36:29 +0000 Subject: [PATCH 4/4] test readability --- createtable_test.go | 8 ++++---- table_test.go | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/createtable_test.go b/createtable_test.go index baa91be..a97ca83 100644 --- a/createtable_test.go +++ b/createtable_test.go @@ -159,9 +159,9 @@ func TestCreateTableUintUnixTime(t *testing.T) { func TestCreateTableAliasAndPrefix(t *testing.T) { db := NewFromIface(nil) - db.Alias["Table1"] = "real-table-1" - db.Prefix = "Test-" + db.Alias["Alias1"] = "table1" + db.Prefix = "test-" - input := db.CreateTable("Table1", Metric{}) - require.Equal(t, "Test-real-table-1", input.tableName) + input := db.CreateTable("Alias1", Metric{}) + require.Equal(t, "test-table1", input.tableName) } diff --git a/table_test.go b/table_test.go index 1286608..e045ae1 100644 --- a/table_test.go +++ b/table_test.go @@ -61,14 +61,14 @@ func TestAddConsumedCapacity(t *testing.T) { func TestTableAliasAndPrefix(t *testing.T) { db := NewFromIface(nil) - require.Equal(t, "MyTable", db.Table("MyTable").name) + require.Equal(t, "Alias1", db.Table("Alias1").name) - db.Alias["MyTable"] = "my-actual-table" - db.Alias["OtherTable"] = "my-other-table" - require.Equal(t, "my-actual-table", db.Table("MyTable").name) - require.Equal(t, "AnotherTable", db.Table("AnotherTable").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-my-actual-table", db.Table("MyTable").name) - require.Equal(t, "Test-AnotherTable", db.Table("AnotherTable").name) + db.Prefix = "test-" + require.Equal(t, "test-table1", db.Table("Alias1").name) + require.Equal(t, "test-Alias3", db.Table("Alias3").name) }