-
Notifications
You must be signed in to change notification settings - Fork 2
add implementation for UpdateItem #34
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
Open
csongor-halmai-oolio
wants to merge
5
commits into
main
Choose a base branch
from
csongor/add-UpdateItem
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+298
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
85dfb51
add implementation for UpdateItem
csongor-halmai-oolio d239dea
address CRAI comments
csongor-halmai-oolio 3016a5d
address CRAI comments
csongor-halmai-oolio f5289bd
add UpdateItem to the WriteAPI interface; update README.md
csongor-halmai-oolio 602e6d7
fix compilation error
csongor-halmai-oolio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| package tests | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/rand" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/aws" | ||
| ddbtypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" | ||
| "github.com/oolio-group/dynago" | ||
| ) | ||
|
|
||
| func TestUpdateItem(t *testing.T) { | ||
| ddbClient := prepareTable(t) | ||
|
|
||
| partitionKey := "org_123#" + rand.Text() // avoid interference with other tests | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| givePk dynago.Attribute | ||
| giveSk dynago.Attribute | ||
| giveUpdateExpr *string | ||
| giveExpressionAttributeValues map[string]ddbtypes.AttributeValue | ||
| giveOptions []dynago.UpdateOption | ||
| wantAttributes map[string]ddbtypes.AttributeValue | ||
| wantErrStr string | ||
| }{ | ||
| { | ||
| name: "inserting new item and requesting ALL_OLD attributes returns no attributes", | ||
| givePk: dynago.StringValue(partitionKey), | ||
| giveSk: dynago.StringValue("2026-jan"), | ||
| giveUpdateExpr: aws.String("SET Income = :v"), | ||
| giveExpressionAttributeValues: map[string]ddbtypes.AttributeValue{ | ||
| ":v": &ddbtypes.AttributeValueMemberN{Value: "1000"}, | ||
| }, | ||
| giveOptions: []dynago.UpdateOption{ | ||
| dynago.WithReturnValues("ALL_OLD"), | ||
| }, | ||
| wantAttributes: map[string]ddbtypes.AttributeValue{}, | ||
| }, | ||
| { | ||
| name: "inserting new item and requesting ALL_NEW attributes returns all attributes", | ||
| givePk: dynago.StringValue(partitionKey), | ||
| giveSk: dynago.StringValue("2026-feb"), | ||
| giveUpdateExpr: aws.String("SET Income = :v"), | ||
| giveExpressionAttributeValues: map[string]ddbtypes.AttributeValue{ | ||
| ":v": &ddbtypes.AttributeValueMemberN{Value: "1000"}, | ||
| }, | ||
| giveOptions: []dynago.UpdateOption{ | ||
| dynago.WithReturnValues("ALL_NEW"), | ||
| }, | ||
| wantAttributes: map[string]ddbtypes.AttributeValue{ | ||
| "Income": &ddbtypes.AttributeValueMemberN{Value: "1000"}, | ||
| "pk": &ddbtypes.AttributeValueMemberS{Value: partitionKey}, | ||
| "sk": &ddbtypes.AttributeValueMemberS{Value: "2026-feb"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "updating existing item and requesting ALL_OLD attributes returns all attributes", | ||
| givePk: dynago.StringValue(partitionKey), | ||
| giveSk: dynago.StringValue("2026-jan"), | ||
| giveUpdateExpr: aws.String("SET Income = :v"), | ||
| giveExpressionAttributeValues: map[string]ddbtypes.AttributeValue{ | ||
| ":v": &ddbtypes.AttributeValueMemberN{Value: "2000"}, | ||
| }, | ||
| giveOptions: []dynago.UpdateOption{ | ||
| dynago.WithReturnValues("ALL_OLD"), | ||
| }, | ||
| wantAttributes: map[string]ddbtypes.AttributeValue{ | ||
| "Income": &ddbtypes.AttributeValueMemberN{Value: "1000"}, // old value is returned | ||
| "pk": &ddbtypes.AttributeValueMemberS{Value: partitionKey}, | ||
| "sk": &ddbtypes.AttributeValueMemberS{Value: "2026-jan"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "updating existing item and requesting ALL_NEW attributes returns new attributes", | ||
| givePk: dynago.StringValue(partitionKey), | ||
| giveSk: dynago.StringValue("2026-jan"), | ||
| giveUpdateExpr: aws.String("SET Income = :v"), | ||
| giveExpressionAttributeValues: map[string]ddbtypes.AttributeValue{ | ||
| ":v": &ddbtypes.AttributeValueMemberN{Value: "3000"}, | ||
| }, | ||
| giveOptions: []dynago.UpdateOption{ | ||
| dynago.WithReturnValues("ALL_NEW"), | ||
| }, | ||
| wantAttributes: map[string]ddbtypes.AttributeValue{ | ||
| "Income": &ddbtypes.AttributeValueMemberN{Value: "3000"}, // new value is returned | ||
| "pk": &ddbtypes.AttributeValueMemberS{Value: partitionKey}, | ||
| "sk": &ddbtypes.AttributeValueMemberS{Value: "2026-jan"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "incrementing non-existing item with ALL_NEW returns new attributes", | ||
| givePk: dynago.StringValue(partitionKey), | ||
| giveSk: dynago.StringValue("2026-mar"), | ||
| giveUpdateExpr: aws.String("ADD Income :increment"), | ||
| giveExpressionAttributeValues: map[string]ddbtypes.AttributeValue{ | ||
| ":increment": &ddbtypes.AttributeValueMemberN{Value: "8"}, | ||
| }, | ||
| giveOptions: []dynago.UpdateOption{ | ||
| dynago.WithReturnValues("ALL_NEW"), | ||
| }, | ||
| wantAttributes: map[string]ddbtypes.AttributeValue{ | ||
| "Income": &ddbtypes.AttributeValueMemberN{Value: "8"}, | ||
| "pk": &ddbtypes.AttributeValueMemberS{Value: partitionKey}, | ||
| "sk": &ddbtypes.AttributeValueMemberS{Value: "2026-mar"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "incrementing existing item with ALL_NEW returns new attributes", | ||
| givePk: dynago.StringValue(partitionKey), | ||
| giveSk: dynago.StringValue("2026-mar"), | ||
| giveUpdateExpr: aws.String("ADD Income :increment"), | ||
| giveExpressionAttributeValues: map[string]ddbtypes.AttributeValue{ | ||
| ":increment": &ddbtypes.AttributeValueMemberN{Value: "8"}, | ||
| }, | ||
| giveOptions: []dynago.UpdateOption{ | ||
| dynago.WithReturnValues("ALL_NEW"), | ||
| }, | ||
| wantAttributes: map[string]ddbtypes.AttributeValue{ | ||
| "Income": &ddbtypes.AttributeValueMemberN{Value: "16"}, | ||
| "pk": &ddbtypes.AttributeValueMemberS{Value: partitionKey}, | ||
| "sk": &ddbtypes.AttributeValueMemberS{Value: "2026-mar"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "increment missing item with condition expression causes an error", | ||
| givePk: dynago.StringValue(partitionKey), | ||
| giveSk: dynago.StringValue("2026-may"), | ||
| giveUpdateExpr: aws.String("ADD Income :increment"), | ||
| giveExpressionAttributeValues: map[string]ddbtypes.AttributeValue{ | ||
| ":increment": &ddbtypes.AttributeValueMemberN{Value: "8"}, | ||
| }, | ||
| giveOptions: []dynago.UpdateOption{ | ||
| dynago.WithConditionExpression("attribute_exists(pk) AND attribute_exists(sk)"), // want failure is the item does not exist | ||
| dynago.WithReturnValues("ALL_NEW"), | ||
| }, | ||
| wantAttributes: nil, | ||
| wantErrStr: "ConditionalCheckFailedException: The conditional request failed", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| // t.Parallel() // commented out: DO NOT RUN THE TESTS IN PARALLEL, because they depend on each other (e.g., one test creates an item that another test updates) | ||
|
|
||
| gotResponse, gotErr := ddbClient.UpdateItem(context.TODO(), tc.givePk, tc.giveSk, tc.giveUpdateExpr, tc.giveExpressionAttributeValues, tc.giveOptions...) | ||
|
|
||
| if tc.wantErrStr != "" { | ||
| if gotErr == nil { | ||
| t.Fatalf("expected error but got nil") | ||
| } | ||
|
|
||
| if !strings.Contains(gotErr.Error(), tc.wantErrStr) { | ||
| t.Fatalf("error message does not contain expected substring: got %q, want %q", gotErr.Error(), tc.wantErrStr) | ||
| } | ||
| } else { | ||
| if gotErr != nil { | ||
| t.Fatalf("unexpected error: %v", gotErr) | ||
| } | ||
|
|
||
| if len(gotResponse.Attributes) != len(tc.wantAttributes) { | ||
| t.Fatalf("number of attributes does not match: got %d, want %d", len(gotResponse.Attributes), len(tc.wantAttributes)) | ||
| } | ||
|
|
||
| for key, value := range tc.wantAttributes { | ||
| gotResponseValue := gotResponse.Attributes[key] | ||
| if gotResponseValue == nil { | ||
| t.Errorf("attribute %q not found in response", key) | ||
| continue | ||
| } | ||
|
|
||
| switch v := gotResponseValue.(type) { | ||
| case *ddbtypes.AttributeValueMemberN: // number | ||
| if v.Value != value.(*ddbtypes.AttributeValueMemberN).Value { | ||
| t.Errorf("attribute %q does not match: got %q, want %q", key, v.Value, value.(*ddbtypes.AttributeValueMemberN).Value) | ||
| } | ||
| case *ddbtypes.AttributeValueMemberS: // string | ||
| if v.Value != value.(*ddbtypes.AttributeValueMemberS).Value { | ||
| t.Errorf("attribute %q does not match: got %q, want %q", key, v.Value, value.(*ddbtypes.AttributeValueMemberS).Value) | ||
| } | ||
| default: | ||
| t.Errorf("unsupported attribute value type for key %q: %T", key, gotResponseValue) | ||
| } | ||
|
csongor-halmai-oolio marked this conversation as resolved.
|
||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package dynago | ||
|
|
||
| import ( | ||
| "context" | ||
| //"fmt" | ||
| "log" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/service/dynamodb" | ||
| dynamodbTypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" | ||
| ) | ||
|
|
||
| type UpdateOption func(*dynamodb.UpdateItemInput) error | ||
|
|
||
| func WithReturnValues(returnValues string) UpdateOption { | ||
| return func(input *dynamodb.UpdateItemInput) error { | ||
| input.ReturnValues = dynamodbTypes.ReturnValue(returnValues) | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func WithConditionExpression(conditionExpression string) UpdateOption { | ||
| return func(input *dynamodb.UpdateItemInput) error { | ||
| input.ConditionExpression = &conditionExpression | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func WithReturnConsumedCapacity(returnConsumedCapacity string) UpdateOption { | ||
| return func(input *dynamodb.UpdateItemInput) error { | ||
| input.ReturnConsumedCapacity = dynamodbTypes.ReturnConsumedCapacity(returnConsumedCapacity) | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func WithReturnItemCollectionMetrics(returnItemCollectionMetrics string) UpdateOption { | ||
| return func(input *dynamodb.UpdateItemInput) error { | ||
| input.ReturnItemCollectionMetrics = dynamodbTypes.ReturnItemCollectionMetrics(returnItemCollectionMetrics) | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func WithReturnValuesOnConditionCheckFailure(returnValuesOnConditionCheckFailure string) UpdateOption { | ||
| return func(input *dynamodb.UpdateItemInput) error { | ||
| input.ReturnValuesOnConditionCheckFailure = dynamodbTypes.ReturnValuesOnConditionCheckFailure(returnValuesOnConditionCheckFailure) | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // UpdateItem updates a db record from dynamodb given a partition key and sort key | ||
| // @param item the item put into the database | ||
| // @return true if the record was updated, false otherwise | ||
| func (t *Client) UpdateItem( | ||
| ctx context.Context, | ||
| pk Attribute, | ||
| sk Attribute, | ||
| updateExpression *string, | ||
| expressionAttributeValues map[string]dynamodbTypes.AttributeValue, | ||
| opts ...UpdateOption, | ||
| ) (*dynamodb.UpdateItemOutput, error) { | ||
| input := &dynamodb.UpdateItemInput{ | ||
| TableName: &t.TableName, | ||
| Key: t.NewKeys(pk, sk), | ||
| UpdateExpression: updateExpression, | ||
| } | ||
|
|
||
| if len(expressionAttributeValues) > 0 { | ||
| input.ExpressionAttributeValues = expressionAttributeValues | ||
| } | ||
|
|
||
| // Apply option functions | ||
| if len(opts) > 0 { | ||
| for _, opt := range opts { | ||
| if err := opt(input); err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
csongor-halmai-oolio marked this conversation as resolved.
|
||
| } | ||
|
|
||
| ret, err := t.client.UpdateItem(ctx, input) | ||
| if err != nil { | ||
| log.Println("Failed to Update item" + err.Error()) | ||
| return nil, err | ||
| } | ||
|
|
||
| return ret, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.