sqs-browse: remove assumption regarding table keys

Table keys are now retrieved from describe
This commit is contained in:
Leon Mika 2022-03-25 08:17:52 +11:00
parent 3428bd2a8a
commit 5a69e6c954
47 changed files with 150 additions and 98 deletions

View file

@ -4,6 +4,8 @@ import (
"context"
"flag"
"fmt"
"os"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
tea "github.com/charmbracelet/bubbletea"
@ -15,7 +17,6 @@ import (
"github.com/lmika/awstools/internal/dynamo-browse/services/tables"
"github.com/lmika/awstools/internal/dynamo-browse/ui"
"github.com/lmika/gopkgs/cli"
"os"
)
func main() {
@ -77,4 +78,3 @@ type msgLoopback struct {
func (m *msgLoopback) Send(msg tea.Msg) {
m.program.Send(msg)
}

View file

@ -4,6 +4,9 @@ import (
"context"
"flag"
"fmt"
"log"
"os"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sqs"
tea "github.com/charmbracelet/bubbletea"
@ -17,8 +20,6 @@ import (
"github.com/lmika/awstools/internal/sqs-browse/ui"
"github.com/lmika/events"
"github.com/lmika/gopkgs/cli"
"log"
"os"
)
func main() {

View file

@ -3,15 +3,16 @@ package main
import (
"context"
"flag"
"log"
"os"
"path/filepath"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
"github.com/pkg/errors"
"log"
"os"
"path/filepath"
"time"
"github.com/lmika/gopkgs/cli"
)

View file

@ -2,11 +2,12 @@ package commandctrl
import (
"context"
"strings"
"github.com/lmika/awstools/internal/common/ui/events"
"github.com/lmika/awstools/internal/common/ui/uimodels"
"github.com/lmika/shellwords"
"github.com/pkg/errors"
"strings"
)
type CommandController struct {

View file

@ -2,11 +2,12 @@ package commandctrl_test
import (
"context"
"testing"
"github.com/lmika/awstools/internal/common/ui/commandctrl"
"github.com/lmika/awstools/internal/common/ui/events"
"github.com/lmika/awstools/test/testuictx"
"github.com/stretchr/testify/assert"
"testing"
)
func TestCommandController_Prompt(t *testing.T) {

View file

@ -3,6 +3,7 @@ package commandctrl
import "context"
type commandArgContextKeyType struct{}
var commandArgContextKey = commandArgContextKeyType{}
func WithCommandArgs(ctx context.Context, args []string) context.Context {

View file

@ -2,6 +2,7 @@ package dispatcher
import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
"github.com/lmika/awstools/internal/common/ui/events"
"github.com/lmika/awstools/internal/common/ui/uimodels"

View file

@ -2,10 +2,11 @@ package dispatcher
import (
"context"
"sync"
"github.com/lmika/awstools/internal/common/ui/events"
"github.com/lmika/awstools/internal/common/ui/uimodels"
"github.com/pkg/errors"
"sync"
)
type Dispatcher struct {
@ -14,7 +15,6 @@ type Dispatcher struct {
publisher MessagePublisher
}
func NewDispatcher(publisher MessagePublisher) *Dispatcher {
return &Dispatcher{
mutex: new(sync.Mutex),
@ -44,6 +44,3 @@ func (d *Dispatcher) Start(ctx context.Context, operation uimodels.Operation) {
d.runningOp = nil
}()
}

View file

@ -3,6 +3,7 @@ package uimodels
import "context"
type uiContextKeyType struct{}
var uiContextKey = uiContextKeyType{}
func Ctx(ctx context.Context) UIContext {

View file

@ -11,4 +11,3 @@ type OperationFn func(ctx context.Context) error
func (f OperationFn) Execute(ctx context.Context) error {
return f(ctx)
}

View file

@ -3,6 +3,7 @@ package uimodels
import "context"
type promptValueKeyType struct{}
var promptValueKey = promptValueKeyType{}
func PromptValue(ctx context.Context) string {

View file

@ -2,6 +2,7 @@ package controllers
import (
"context"
"github.com/lmika/awstools/internal/dynamo-browse/models"
)

View file

@ -2,6 +2,7 @@ package controllers
import (
"context"
"github.com/lmika/awstools/internal/common/ui/uimodels"
"github.com/lmika/awstools/internal/dynamo-browse/models"
"github.com/lmika/awstools/internal/dynamo-browse/services/tables"

View file

@ -2,6 +2,7 @@ package controllers
import (
"context"
"github.com/lmika/awstools/internal/common/ui/uimodels"
"github.com/lmika/awstools/internal/dynamo-browse/models/modexpr"
"github.com/lmika/awstools/internal/dynamo-browse/services/tables"

View file

@ -2,6 +2,8 @@ package controllers_test
import (
"context"
"testing"
"github.com/lmika/awstools/internal/common/ui/events"
"github.com/lmika/awstools/internal/common/ui/uimodels"
"github.com/lmika/awstools/internal/dynamo-browse/controllers"
@ -10,7 +12,6 @@ import (
"github.com/lmika/awstools/test/testdynamo"
"github.com/lmika/awstools/test/testuictx"
"github.com/stretchr/testify/assert"
"testing"
)
func TestTableWriteController_ToggleReadWrite(t *testing.T) {
@ -47,7 +48,10 @@ func TestTableWriteController_Delete(t *testing.T) {
twc, ctrls, closeFn := setupController(t)
t.Cleanup(closeFn)
resultSet, err := ctrls.tableService.Scan(context.Background(), ctrls.tableName)
ti, err := ctrls.tableService.Describe(context.Background(), ctrls.tableName)
assert.NoError(t, err)
resultSet, err := ctrls.tableService.Scan(context.Background(), ti)
assert.NoError(t, err)
assert.Len(t, resultSet.Items, 3)
@ -71,7 +75,7 @@ func TestTableWriteController_Delete(t *testing.T) {
err = promptRequest.OnDone.Execute(uimodels.WithPromptValue(ctx, "y"))
assert.NoError(t, err)
afterResultSet, err := ctrls.tableService.Scan(context.Background(), ctrls.tableName)
afterResultSet, err := ctrls.tableService.Scan(context.Background(), ti)
assert.NoError(t, err)
assert.Len(t, afterResultSet.Items, 2)
assert.Contains(t, afterResultSet.Items, resultSet.Items[0])
@ -83,7 +87,10 @@ func TestTableWriteController_Delete(t *testing.T) {
twc, ctrls, closeFn := setupController(t)
t.Cleanup(closeFn)
resultSet, err := ctrls.tableService.Scan(context.Background(), ctrls.tableName)
ti, err := ctrls.tableService.Describe(context.Background(), ctrls.tableName)
assert.NoError(t, err)
resultSet, err := ctrls.tableService.Scan(context.Background(), ti)
assert.NoError(t, err)
assert.Len(t, resultSet.Items, 3)
@ -107,7 +114,7 @@ func TestTableWriteController_Delete(t *testing.T) {
err = promptRequest.OnDone.Execute(uimodels.WithPromptValue(ctx, "n"))
assert.Error(t, err)
afterResultSet, err := ctrls.tableService.Scan(context.Background(), ctrls.tableName)
afterResultSet, err := ctrls.tableService.Scan(context.Background(), ti)
assert.NoError(t, err)
assert.Len(t, afterResultSet.Items, 3)
assert.Contains(t, afterResultSet.Items, resultSet.Items[0])
@ -119,7 +126,10 @@ func TestTableWriteController_Delete(t *testing.T) {
tableWriteController, ctrls, closeFn := setupController(t)
t.Cleanup(closeFn)
resultSet, err := ctrls.tableService.Scan(context.Background(), ctrls.tableName)
ti, err := ctrls.tableService.Describe(context.Background(), ctrls.tableName)
assert.NoError(t, err)
resultSet, err := ctrls.tableService.Scan(context.Background(), ti)
assert.NoError(t, err)
assert.Len(t, resultSet.Items, 3)

View file

@ -1,8 +1,9 @@
package models
import (
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"math/big"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
func compareScalarAttributes(x, y types.AttributeValue) (int, bool) {

View file

@ -1,11 +1,12 @@
package modexpr_test
import (
"testing"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/lmika/awstools/internal/dynamo-browse/models"
"github.com/lmika/awstools/internal/dynamo-browse/models/modexpr"
"github.com/stretchr/testify/assert"
"testing"
)
func TestModExpr_Patch(t *testing.T) {

View file

@ -1,9 +1,10 @@
package modexpr
import (
"strconv"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/pkg/errors"
"strconv"
)
func (a *astLiteralValue) dynamoValue() (types.AttributeValue, error) {

View file

@ -1,18 +1,21 @@
package models_test
import (
"testing"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/lmika/awstools/internal/dynamo-browse/models"
"github.com/stretchr/testify/assert"
"testing"
)
func TestSort(t *testing.T) {
t.Run("pk and sk are both strings", func(t *testing.T) {
tableInfo := &models.TableInfo{Keys: models.KeyAttribute{PartitionKey: "pk", SortKey: "sk"}}
items := make([]models.Item, len(testStringData))
copy(items, testStringData)
models.Sort(items, "pk", "sk")
models.Sort(items, tableInfo)
assert.Equal(t, items[0], testStringData[1])
assert.Equal(t, items[1], testStringData[2])
@ -20,10 +23,12 @@ func TestSort(t *testing.T) {
})
t.Run("pk and sk are both numbers", func(t *testing.T) {
tableInfo := &models.TableInfo{Keys: models.KeyAttribute{PartitionKey: "pk", SortKey: "sk"}}
items := make([]models.Item, len(testNumberData))
copy(items, testNumberData)
models.Sort(items, "pk", "sk")
models.Sort(items, tableInfo)
assert.Equal(t, items[0], testNumberData[2])
assert.Equal(t, items[1], testNumberData[1])
@ -31,10 +36,12 @@ func TestSort(t *testing.T) {
})
t.Run("pk and sk are both bools", func(t *testing.T) {
tableInfo := &models.TableInfo{Keys: models.KeyAttribute{PartitionKey: "pk", SortKey: "sk"}}
items := make([]models.Item, len(testBoolData))
copy(items, testBoolData)
models.Sort(items, "pk", "sk")
models.Sort(items, tableInfo)
assert.Equal(t, items[0], testBoolData[2])
assert.Equal(t, items[1], testBoolData[1])

View file

@ -2,6 +2,7 @@ package dynamo
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"

View file

@ -2,11 +2,12 @@ package dynamo_test
import (
"context"
"testing"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/lmika/awstools/internal/dynamo-browse/providers/dynamo"
"github.com/lmika/awstools/test/testdynamo"
"github.com/stretchr/testify/assert"
"testing"
)
func TestProvider_ScanItems(t *testing.T) {

View file

@ -2,6 +2,7 @@ package tables
import (
"context"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/lmika/awstools/internal/dynamo-browse/models"
)

View file

@ -2,9 +2,10 @@ package tables
import (
"context"
"sort"
"github.com/lmika/awstools/internal/dynamo-browse/models"
"github.com/pkg/errors"
"sort"
)
type Service struct {

View file

@ -2,11 +2,12 @@ package tables_test
import (
"context"
"testing"
"github.com/lmika/awstools/internal/dynamo-browse/providers/dynamo"
"github.com/lmika/awstools/internal/dynamo-browse/services/tables"
"github.com/lmika/awstools/test/testdynamo"
"github.com/stretchr/testify/assert"
"testing"
)
func TestService_Describe(t *testing.T) {

View file

@ -3,6 +3,9 @@ package ui
import (
"context"
"fmt"
"strings"
"text/tabwriter"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
table "github.com/calyptia/go-bubble-table"
"github.com/charmbracelet/bubbles/textinput"
@ -14,8 +17,6 @@ import (
"github.com/lmika/awstools/internal/common/ui/events"
"github.com/lmika/awstools/internal/common/ui/uimodels"
"github.com/lmika/awstools/internal/dynamo-browse/controllers"
"strings"
"text/tabwriter"
)
var (

View file

@ -2,11 +2,12 @@ package ui
import (
"fmt"
"io"
"strings"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
table "github.com/calyptia/go-bubble-table"
"github.com/lmika/awstools/internal/dynamo-browse/models"
"io"
"strings"
)
type itemTableRow struct {

View file

@ -2,6 +2,7 @@ package controllers
import (
"context"
"github.com/lmika/awstools/internal/common/ui/uimodels"
"github.com/lmika/awstools/internal/sqs-browse/models"
"github.com/lmika/awstools/internal/sqs-browse/services/messages"

View file

@ -2,13 +2,14 @@ package sqs
import (
"context"
"log"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
"github.com/lmika/awstools/internal/sqs-browse/models"
"github.com/pkg/errors"
"log"
"time"
)
type Provider struct {

View file

@ -2,6 +2,7 @@ package stormstore
import (
"context"
"github.com/asdine/storm"
"github.com/lmika/awstools/internal/sqs-browse/models"
"github.com/pkg/errors"

View file

@ -2,6 +2,7 @@ package messages
import (
"context"
"github.com/lmika/awstools/internal/sqs-browse/models"
)

View file

@ -2,6 +2,7 @@ package messages
import (
"context"
"github.com/lmika/awstools/internal/sqs-browse/models"
"github.com/pkg/errors"
)

View file

@ -2,6 +2,7 @@ package pollmessage
import (
"context"
"github.com/lmika/awstools/internal/sqs-browse/models"
)

View file

@ -2,9 +2,10 @@ package pollmessage
import (
"context"
"log"
"github.com/lmika/events"
"github.com/pkg/errors"
"log"
)
type Service struct {

View file

@ -4,6 +4,9 @@ import (
"bytes"
"context"
"encoding/json"
"log"
"strings"
table "github.com/calyptia/go-bubble-table"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
@ -14,8 +17,6 @@ import (
"github.com/lmika/awstools/internal/common/ui/uimodels"
"github.com/lmika/awstools/internal/sqs-browse/controllers"
"github.com/lmika/awstools/internal/sqs-browse/models"
"log"
"strings"
)
var (

View file

@ -2,10 +2,11 @@ package ui
import (
"fmt"
"github.com/lmika/awstools/internal/sqs-browse/models"
table "github.com/calyptia/go-bubble-table"
"io"
"strings"
table "github.com/calyptia/go-bubble-table"
"github.com/lmika/awstools/internal/sqs-browse/models"
)
type messageTableRow models.Message

View file

@ -2,6 +2,8 @@ package main
import (
"context"
"log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
@ -12,7 +14,6 @@ import (
"github.com/lmika/awstools/internal/dynamo-browse/providers/dynamo"
"github.com/lmika/awstools/internal/dynamo-browse/services/tables"
"github.com/lmika/gopkgs/cli"
"log"
)
func main() {
@ -52,12 +53,17 @@ func main() {
log.Fatalf("warn: cannot create table: %v", tableName)
}
tableInfo := &models.TableInfo{
Name: tableName,
Keys: models.KeyAttribute{PartitionKey: "pk", SortKey: "sk"},
}
dynamoProvider := dynamo.NewProvider(dynamoClient)
tableService := tables.NewService(dynamoProvider)
for i := 0; i < totalItems; i++ {
key := uuid.New().String()
if err := tableService.Put(ctx, tableName, models.Item{
if err := tableService.Put(ctx, tableInfo, models.Item{
"pk": &types.AttributeValueMemberS{Value: key},
"sk": &types.AttributeValueMemberS{Value: key},
"name": &types.AttributeValueMemberS{Value: gofakeit.Name()},

View file

@ -2,6 +2,8 @@ package testdynamo
import (
"context"
"testing"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
@ -9,7 +11,6 @@ import (
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/stretchr/testify/assert"
"testing"
)
type TestData []map[string]interface{}

View file

@ -1,10 +1,11 @@
package testdynamo
import (
"testing"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/lmika/awstools/internal/dynamo-browse/models"
"github.com/stretchr/testify/assert"
"testing"
)
func TestRecordAsItem(t *testing.T, item map[string]interface{}) models.Item {

View file

@ -2,6 +2,7 @@ package testuictx
import (
"context"
tea "github.com/charmbracelet/bubbletea"
"github.com/lmika/awstools/internal/common/ui/dispatcher"
"github.com/lmika/awstools/internal/common/ui/uimodels"