Skip to content

Commit

Permalink
chore(basic): extract common key builder
Browse files Browse the repository at this point in the history
  • Loading branch information
juricake committed Jan 21, 2024
1 parent 475a4f9 commit 172d98d
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions pkg/client/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ func (c *Client) Set(PK, SK string, input interface{}) error {
return err
}

dynamoModel[consts.PK] = &types.AttributeValueMemberS{Value: PK}
dynamoModel[consts.SK] = &types.AttributeValueMemberS{Value: SK}
for key, val := range keys(PK, SK) {
dynamoModel[key] = val
}

request := &dynamodb.PutItemInput{
TableName: aws.String(c.table),
Expand All @@ -32,10 +33,7 @@ func (c *Client) Set(PK, SK string, input interface{}) error {
func (c *Client) Get(PK, SK string, input interface{}) error {
req := &dynamodb.GetItemInput{
TableName: aws.String(c.table),
Key: map[string]types.AttributeValue{
consts.PK: &types.AttributeValueMemberS{Value: PK},
consts.SK: &types.AttributeValueMemberS{Value: SK},
},
Key: keys(PK, SK),
}

result, err := c.dynamo.GetItem(context.Background(), req)
Expand All @@ -54,12 +52,16 @@ func (c *Client) Get(PK, SK string, input interface{}) error {
func (c *Client) Del(PK, SK string) error {
request := &dynamodb.DeleteItemInput{
TableName: aws.String(c.table),
Key: map[string]types.AttributeValue{
consts.PK: &types.AttributeValueMemberS{Value: PK},
consts.SK: &types.AttributeValueMemberS{Value: SK},
},
Key: keys(PK, SK),
}

_, err := c.dynamo.DeleteItem(context.Background(), request)
return err
}

func keys(PK string, SK string) map[string]types.AttributeValue {
return map[string]types.AttributeValue{
consts.PK: &types.AttributeValueMemberS{Value: PK},
consts.SK: &types.AttributeValueMemberS{Value: SK},
}
}

0 comments on commit 172d98d

Please sign in to comment.