From 0ba59ba01ed5a9ae03f0fae74ec727e397aa2ec4 Mon Sep 17 00:00:00 2001 From: Joshua Hawxwell Date: Wed, 27 Nov 2024 08:20:08 +0000 Subject: [PATCH] Pass a dynamo.SK to PartialSK method --- cmd/event-received/main.go | 2 +- cmd/event-received/mock_dynamodbClient_test.go | 12 ++++++------ internal/app/app.go | 2 +- internal/app/mock_DynamoClient_test.go | 12 ++++++------ internal/dynamo/client.go | 4 ++-- internal/dynamo/client_test.go | 10 +++++----- internal/dynamo/keys.go | 4 ++-- internal/scheduled/mock_DynamoClient_test.go | 12 ++++++------ internal/scheduled/mock_test.go | 2 +- internal/scheduled/store.go | 4 ++-- internal/scheduled/store_test.go | 2 +- 11 files changed, 33 insertions(+), 33 deletions(-) diff --git a/cmd/event-received/main.go b/cmd/event-received/main.go index fdb3286e2d..294be97bbb 100644 --- a/cmd/event-received/main.go +++ b/cmd/event-received/main.go @@ -78,7 +78,7 @@ type uidEvent struct { type dynamodbClient interface { AnyByPK(ctx context.Context, pk dynamo.PK, v interface{}) error - AllByLpaUIDAndPartialSK(ctx context.Context, uid, partialSK string, v interface{}) error + AllByLpaUIDAndPartialSK(ctx context.Context, uid string, partialSK dynamo.SK, v interface{}) error Create(ctx context.Context, v interface{}) error CreateOnly(ctx context.Context, v interface{}) error DeleteOne(ctx context.Context, pk dynamo.PK, sk dynamo.SK) error diff --git a/cmd/event-received/mock_dynamodbClient_test.go b/cmd/event-received/mock_dynamodbClient_test.go index c8eb8d6ee2..ca9820e532 100644 --- a/cmd/event-received/mock_dynamodbClient_test.go +++ b/cmd/event-received/mock_dynamodbClient_test.go @@ -23,7 +23,7 @@ func (_m *mockDynamodbClient) EXPECT() *mockDynamodbClient_Expecter { } // AllByLpaUIDAndPartialSK provides a mock function with given fields: ctx, uid, partialSK, v -func (_m *mockDynamodbClient) AllByLpaUIDAndPartialSK(ctx context.Context, uid string, partialSK string, v interface{}) error { +func (_m *mockDynamodbClient) AllByLpaUIDAndPartialSK(ctx context.Context, uid string, partialSK dynamo.SK, v interface{}) error { ret := _m.Called(ctx, uid, partialSK, v) if len(ret) == 0 { @@ -31,7 +31,7 @@ func (_m *mockDynamodbClient) AllByLpaUIDAndPartialSK(ctx context.Context, uid s } var r0 error - if rf, ok := ret.Get(0).(func(context.Context, string, string, interface{}) error); ok { + if rf, ok := ret.Get(0).(func(context.Context, string, dynamo.SK, interface{}) error); ok { r0 = rf(ctx, uid, partialSK, v) } else { r0 = ret.Error(0) @@ -48,15 +48,15 @@ type mockDynamodbClient_AllByLpaUIDAndPartialSK_Call struct { // AllByLpaUIDAndPartialSK is a helper method to define mock.On call // - ctx context.Context // - uid string -// - partialSK string +// - partialSK dynamo.SK // - v interface{} func (_e *mockDynamodbClient_Expecter) AllByLpaUIDAndPartialSK(ctx interface{}, uid interface{}, partialSK interface{}, v interface{}) *mockDynamodbClient_AllByLpaUIDAndPartialSK_Call { return &mockDynamodbClient_AllByLpaUIDAndPartialSK_Call{Call: _e.mock.On("AllByLpaUIDAndPartialSK", ctx, uid, partialSK, v)} } -func (_c *mockDynamodbClient_AllByLpaUIDAndPartialSK_Call) Run(run func(ctx context.Context, uid string, partialSK string, v interface{})) *mockDynamodbClient_AllByLpaUIDAndPartialSK_Call { +func (_c *mockDynamodbClient_AllByLpaUIDAndPartialSK_Call) Run(run func(ctx context.Context, uid string, partialSK dynamo.SK, v interface{})) *mockDynamodbClient_AllByLpaUIDAndPartialSK_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(interface{})) + run(args[0].(context.Context), args[1].(string), args[2].(dynamo.SK), args[3].(interface{})) }) return _c } @@ -66,7 +66,7 @@ func (_c *mockDynamodbClient_AllByLpaUIDAndPartialSK_Call) Return(_a0 error) *mo return _c } -func (_c *mockDynamodbClient_AllByLpaUIDAndPartialSK_Call) RunAndReturn(run func(context.Context, string, string, interface{}) error) *mockDynamodbClient_AllByLpaUIDAndPartialSK_Call { +func (_c *mockDynamodbClient_AllByLpaUIDAndPartialSK_Call) RunAndReturn(run func(context.Context, string, dynamo.SK, interface{}) error) *mockDynamodbClient_AllByLpaUIDAndPartialSK_Call { _c.Call.Return(run) return _c } diff --git a/internal/app/app.go b/internal/app/app.go index 4bca9499ac..25acfe8c77 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -52,7 +52,7 @@ type DynamoClient interface { AllByKeys(ctx context.Context, keys []dynamo.Keys) ([]map[string]dynamodbtypes.AttributeValue, error) AllByPartialSK(ctx context.Context, pk dynamo.PK, partialSK dynamo.SK, v interface{}) error AllBySK(ctx context.Context, sk dynamo.SK, v interface{}) error - AllByLpaUIDAndPartialSK(ctx context.Context, uid, partialSK string, v interface{}) error + AllByLpaUIDAndPartialSK(ctx context.Context, uid string, partialSK dynamo.SK, v interface{}) error AllKeysByPK(ctx context.Context, pk dynamo.PK) ([]dynamo.Keys, error) AnyByPK(ctx context.Context, pk dynamo.PK, v interface{}) error BatchPut(ctx context.Context, items []interface{}) error diff --git a/internal/app/mock_DynamoClient_test.go b/internal/app/mock_DynamoClient_test.go index c5fb6cd6fd..2aa5651009 100644 --- a/internal/app/mock_DynamoClient_test.go +++ b/internal/app/mock_DynamoClient_test.go @@ -84,7 +84,7 @@ func (_c *mockDynamoClient_AllByKeys_Call) RunAndReturn(run func(context.Context } // AllByLpaUIDAndPartialSK provides a mock function with given fields: ctx, uid, partialSK, v -func (_m *mockDynamoClient) AllByLpaUIDAndPartialSK(ctx context.Context, uid string, partialSK string, v interface{}) error { +func (_m *mockDynamoClient) AllByLpaUIDAndPartialSK(ctx context.Context, uid string, partialSK dynamo.SK, v interface{}) error { ret := _m.Called(ctx, uid, partialSK, v) if len(ret) == 0 { @@ -92,7 +92,7 @@ func (_m *mockDynamoClient) AllByLpaUIDAndPartialSK(ctx context.Context, uid str } var r0 error - if rf, ok := ret.Get(0).(func(context.Context, string, string, interface{}) error); ok { + if rf, ok := ret.Get(0).(func(context.Context, string, dynamo.SK, interface{}) error); ok { r0 = rf(ctx, uid, partialSK, v) } else { r0 = ret.Error(0) @@ -109,15 +109,15 @@ type mockDynamoClient_AllByLpaUIDAndPartialSK_Call struct { // AllByLpaUIDAndPartialSK is a helper method to define mock.On call // - ctx context.Context // - uid string -// - partialSK string +// - partialSK dynamo.SK // - v interface{} func (_e *mockDynamoClient_Expecter) AllByLpaUIDAndPartialSK(ctx interface{}, uid interface{}, partialSK interface{}, v interface{}) *mockDynamoClient_AllByLpaUIDAndPartialSK_Call { return &mockDynamoClient_AllByLpaUIDAndPartialSK_Call{Call: _e.mock.On("AllByLpaUIDAndPartialSK", ctx, uid, partialSK, v)} } -func (_c *mockDynamoClient_AllByLpaUIDAndPartialSK_Call) Run(run func(ctx context.Context, uid string, partialSK string, v interface{})) *mockDynamoClient_AllByLpaUIDAndPartialSK_Call { +func (_c *mockDynamoClient_AllByLpaUIDAndPartialSK_Call) Run(run func(ctx context.Context, uid string, partialSK dynamo.SK, v interface{})) *mockDynamoClient_AllByLpaUIDAndPartialSK_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(interface{})) + run(args[0].(context.Context), args[1].(string), args[2].(dynamo.SK), args[3].(interface{})) }) return _c } @@ -127,7 +127,7 @@ func (_c *mockDynamoClient_AllByLpaUIDAndPartialSK_Call) Return(_a0 error) *mock return _c } -func (_c *mockDynamoClient_AllByLpaUIDAndPartialSK_Call) RunAndReturn(run func(context.Context, string, string, interface{}) error) *mockDynamoClient_AllByLpaUIDAndPartialSK_Call { +func (_c *mockDynamoClient_AllByLpaUIDAndPartialSK_Call) RunAndReturn(run func(context.Context, string, dynamo.SK, interface{}) error) *mockDynamoClient_AllByLpaUIDAndPartialSK_Call { _c.Call.Return(run) return _c } diff --git a/internal/dynamo/client.go b/internal/dynamo/client.go index 3cf2afba0a..3a7e42f328 100644 --- a/internal/dynamo/client.go +++ b/internal/dynamo/client.go @@ -86,7 +86,7 @@ func (c *Client) OneByUID(ctx context.Context, uid string, v interface{}) error return attributevalue.UnmarshalMap(response.Items[0], v) } -func (c *Client) AllByLpaUIDAndPartialSK(ctx context.Context, uid, partialSK string, v interface{}) error { +func (c *Client) AllByLpaUIDAndPartialSK(ctx context.Context, uid string, partialSK SK, v interface{}) error { response, err := c.svc.Query(ctx, &dynamodb.QueryInput{ TableName: aws.String(c.table), IndexName: aws.String(lpaUIDIndex), @@ -96,7 +96,7 @@ func (c *Client) AllByLpaUIDAndPartialSK(ctx context.Context, uid, partialSK str }, ExpressionAttributeValues: map[string]types.AttributeValue{ ":LpaUID": &types.AttributeValueMemberS{Value: uid}, - ":SK": &types.AttributeValueMemberS{Value: partialSK}, + ":SK": &types.AttributeValueMemberS{Value: partialSK.SK()}, }, KeyConditionExpression: aws.String("#LpaUID = :LpaUID"), FilterExpression: aws.String("begins_with(#SK, :SK)"), diff --git a/internal/dynamo/client_test.go b/internal/dynamo/client_test.go index 088c04ab40..df6c5872f4 100644 --- a/internal/dynamo/client_test.go +++ b/internal/dynamo/client_test.go @@ -974,7 +974,7 @@ func TestAllScheduledEventsByUID(t *testing.T) { }, ExpressionAttributeValues: map[string]types.AttributeValue{ ":LpaUID": &types.AttributeValueMemberS{Value: "lpa-uid"}, - ":SK": &types.AttributeValueMemberS{Value: "partial-sk"}, + ":SK": &types.AttributeValueMemberS{Value: "a-partial-sk"}, }, KeyConditionExpression: aws.String("#LpaUID = :LpaUID"), FilterExpression: aws.String("begins_with(#SK, :SK)"), @@ -984,7 +984,7 @@ func TestAllScheduledEventsByUID(t *testing.T) { c := &Client{table: "this", svc: dynamoDB} var v []map[string]string - err := c.AllByLpaUIDAndPartialSK(ctx, "lpa-uid", "partial-sk", &v) + err := c.AllByLpaUIDAndPartialSK(ctx, "lpa-uid", testSK("a-partial-sk"), &v) assert.Nil(t, err) } @@ -997,7 +997,7 @@ func TestAllScheduledEventsByUIDWhenQueryError(t *testing.T) { c := &Client{table: "this", svc: dynamoDB} var v []map[string]string - err := c.AllByLpaUIDAndPartialSK(ctx, "lpa-uid", "partial-sk", &v) + err := c.AllByLpaUIDAndPartialSK(ctx, "lpa-uid", testSK("a-partial-sk"), &v) assert.Equal(t, fmt.Errorf("failed to query scheduled event by UID: %w", expectedError), err) } @@ -1010,7 +1010,7 @@ func TestAllScheduledEventsByUIDWhenNoResults(t *testing.T) { c := &Client{table: "this", svc: dynamoDB} var v []map[string]string - err := c.AllByLpaUIDAndPartialSK(ctx, "lpa-uid", "partial-sk", &v) + err := c.AllByLpaUIDAndPartialSK(ctx, "lpa-uid", testSK("a-partial-sk"), &v) assert.Equal(t, NotFoundError{}, err) } @@ -1026,6 +1026,6 @@ func TestAllScheduledEventsByUIDWhenUnmarshalError(t *testing.T) { c := &Client{table: "this", svc: dynamoDB} var v []map[string]string - err := c.AllByLpaUIDAndPartialSK(ctx, "lpa-uid", "partial-sk", v) + err := c.AllByLpaUIDAndPartialSK(ctx, "lpa-uid", testSK("a-partial-sk"), v) assert.Error(t, err) } diff --git a/internal/dynamo/keys.go b/internal/dynamo/keys.go index 951c3cd8cc..cec455a581 100644 --- a/internal/dynamo/keys.go +++ b/internal/dynamo/keys.go @@ -322,8 +322,8 @@ func ScheduledKey(at time.Time, action int) ScheduledKeyType { return ScheduledKeyType(scheduledPrefix + "#" + at.Format(time.RFC3339) + "#" + strconv.Itoa(action)) } -func PartialScheduleKey() string { - return scheduledPrefix + "#" +func PartialScheduledKey() ScheduledKeyType { + return ScheduledKeyType(scheduledPrefix + "#") } type ReservedKeyType string diff --git a/internal/scheduled/mock_DynamoClient_test.go b/internal/scheduled/mock_DynamoClient_test.go index d752914fb4..68b55cee05 100644 --- a/internal/scheduled/mock_DynamoClient_test.go +++ b/internal/scheduled/mock_DynamoClient_test.go @@ -23,7 +23,7 @@ func (_m *mockDynamoClient) EXPECT() *mockDynamoClient_Expecter { } // AllByLpaUIDAndPartialSK provides a mock function with given fields: ctx, uid, partialSK, v -func (_m *mockDynamoClient) AllByLpaUIDAndPartialSK(ctx context.Context, uid string, partialSK string, v interface{}) error { +func (_m *mockDynamoClient) AllByLpaUIDAndPartialSK(ctx context.Context, uid string, partialSK dynamo.SK, v interface{}) error { ret := _m.Called(ctx, uid, partialSK, v) if len(ret) == 0 { @@ -31,7 +31,7 @@ func (_m *mockDynamoClient) AllByLpaUIDAndPartialSK(ctx context.Context, uid str } var r0 error - if rf, ok := ret.Get(0).(func(context.Context, string, string, interface{}) error); ok { + if rf, ok := ret.Get(0).(func(context.Context, string, dynamo.SK, interface{}) error); ok { r0 = rf(ctx, uid, partialSK, v) } else { r0 = ret.Error(0) @@ -48,15 +48,15 @@ type mockDynamoClient_AllByLpaUIDAndPartialSK_Call struct { // AllByLpaUIDAndPartialSK is a helper method to define mock.On call // - ctx context.Context // - uid string -// - partialSK string +// - partialSK dynamo.SK // - v interface{} func (_e *mockDynamoClient_Expecter) AllByLpaUIDAndPartialSK(ctx interface{}, uid interface{}, partialSK interface{}, v interface{}) *mockDynamoClient_AllByLpaUIDAndPartialSK_Call { return &mockDynamoClient_AllByLpaUIDAndPartialSK_Call{Call: _e.mock.On("AllByLpaUIDAndPartialSK", ctx, uid, partialSK, v)} } -func (_c *mockDynamoClient_AllByLpaUIDAndPartialSK_Call) Run(run func(ctx context.Context, uid string, partialSK string, v interface{})) *mockDynamoClient_AllByLpaUIDAndPartialSK_Call { +func (_c *mockDynamoClient_AllByLpaUIDAndPartialSK_Call) Run(run func(ctx context.Context, uid string, partialSK dynamo.SK, v interface{})) *mockDynamoClient_AllByLpaUIDAndPartialSK_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(context.Context), args[1].(string), args[2].(string), args[3].(interface{})) + run(args[0].(context.Context), args[1].(string), args[2].(dynamo.SK), args[3].(interface{})) }) return _c } @@ -66,7 +66,7 @@ func (_c *mockDynamoClient_AllByLpaUIDAndPartialSK_Call) Return(_a0 error) *mock return _c } -func (_c *mockDynamoClient_AllByLpaUIDAndPartialSK_Call) RunAndReturn(run func(context.Context, string, string, interface{}) error) *mockDynamoClient_AllByLpaUIDAndPartialSK_Call { +func (_c *mockDynamoClient_AllByLpaUIDAndPartialSK_Call) RunAndReturn(run func(context.Context, string, dynamo.SK, interface{}) error) *mockDynamoClient_AllByLpaUIDAndPartialSK_Call { _c.Call.Return(run) return _c } diff --git a/internal/scheduled/mock_test.go b/internal/scheduled/mock_test.go index 26c86cf83a..3c8be16c97 100644 --- a/internal/scheduled/mock_test.go +++ b/internal/scheduled/mock_test.go @@ -8,7 +8,7 @@ import ( ) func (c *mockDynamoClient_AllByLpaUIDAndPartialSK_Call) SetData(data any) { - c.Run(func(_ context.Context, _, _ string, v any) { + c.Run(func(_ context.Context, _ string, _ dynamo.SK, v any) { b, _ := attributevalue.Marshal(data) attributevalue.Unmarshal(b, v) }) diff --git a/internal/scheduled/store.go b/internal/scheduled/store.go index 0a582d388f..0df050fa3c 100644 --- a/internal/scheduled/store.go +++ b/internal/scheduled/store.go @@ -9,7 +9,7 @@ import ( ) type DynamoClient interface { - AllByLpaUIDAndPartialSK(ctx context.Context, uid, partialSK string, v interface{}) error + AllByLpaUIDAndPartialSK(ctx context.Context, uid string, partialSK dynamo.SK, v interface{}) error AnyByPK(ctx context.Context, pk dynamo.PK, v interface{}) error Move(ctx context.Context, oldKeys dynamo.Keys, value any) error DeleteKeys(ctx context.Context, keys []dynamo.Keys) error @@ -55,7 +55,7 @@ func (s *Store) Create(ctx context.Context, row Event) error { func (s *Store) DeleteAllByUID(ctx context.Context, uid string) error { var events []Event - if err := s.dynamoClient.AllByLpaUIDAndPartialSK(ctx, uid, dynamo.PartialScheduleKey(), &events); err != nil { + if err := s.dynamoClient.AllByLpaUIDAndPartialSK(ctx, uid, dynamo.PartialScheduledKey(), &events); err != nil { return err } diff --git a/internal/scheduled/store_test.go b/internal/scheduled/store_test.go index 45e2ad5100..600f2c6f8f 100644 --- a/internal/scheduled/store_test.go +++ b/internal/scheduled/store_test.go @@ -103,7 +103,7 @@ func TestDeleteAllByUID(t *testing.T) { dynamoClient := newMockDynamoClient(t) dynamoClient.EXPECT(). - AllByLpaUIDAndPartialSK(ctx, "lpa-uid", dynamo.PartialScheduleKey(), mock.Anything). + AllByLpaUIDAndPartialSK(ctx, "lpa-uid", dynamo.PartialScheduledKey(), mock.Anything). Return(nil). SetData([]Event{ {LpaUID: "lpa-uid", PK: dynamo.ScheduledDayKey(now), SK: dynamo.ScheduledKey(now, 98)},