Skip to content

Commit

Permalink
refactor projection builder
Browse files Browse the repository at this point in the history
  • Loading branch information
nthienan committed May 13, 2023
1 parent 423186b commit eb70ab9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 44 deletions.
71 changes: 29 additions & 42 deletions internal/plugins/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
Expand Down Expand Up @@ -49,34 +48,34 @@ func InitDynamodb(configs []DynamoDbConfiguration, initializeCache bool) {
}
}

func buildProjectionExpression(fieldExpr string) *string {
if len(fieldExpr) != 0 {
fields := strings.Split(fieldExpr, ",")
println(PrintPrefix, "Projection fields: ", fields)
proj := expression.NamesList(expression.Name(fields[0]))
for i := 1; i < len(fields); i++ {
proj = expression.AddNames(proj, expression.Name(fields[i]))
}

expr, err := expression.NewBuilder().
WithProjection(proj).
Build()
if err != nil {
println(PrintPrefix, "Caught an unexpected error: %s", err)
}
return expr.Projection()
}
return nil
}

// Load data from Dynamodb
func LoadData(config DynamoDbConfiguration) bool {
if config.HashKey != "" {

// Set up the input parameters for the Scan operation
var params *dynamodb.ScanInput

if len(config.Fields) != 0 {
fields := strings.Split(config.Fields, ",")
proj := expression.NamesList(expression.Name(fields[0]))
for i := 1; i < len(fields); i++ {
proj = expression.AddNames(proj, expression.Name(fields[i]))
}

expr, err := expression.NewBuilder().
WithProjection(proj).
Build()
if err != nil {
println(PrintPrefix, "Caught an unexpected error: %s", err)
}
params = &dynamodb.ScanInput{
TableName: aws.String(config.Table),
ProjectionExpression: expr.Projection(),
}
} else {
params = &dynamodb.ScanInput{
TableName: aws.String(config.Table),
}
params := &dynamodb.ScanInput{
TableName: aws.String(config.Table),
ProjectionExpression: buildProjectionExpression(config.Fields),
}

// Execute the Scan operation to read every item in the table.
Expand Down Expand Up @@ -188,28 +187,16 @@ func GetData(config DynamoDbConfiguration) string {
UpdateAttributeMap(attributeMap, config)

result, err := dynamoDbClient.GetItem(&dynamodb.GetItemInput{
TableName: aws.String(config.Table),
Key: attributeMap,
TableName: aws.String(config.Table),
Key: attributeMap,
ProjectionExpression: buildProjectionExpression(config.Fields),
})

if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case dynamodb.ErrCodeProvisionedThroughputExceededException:
println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error())
case dynamodb.ErrCodeResourceNotFoundException:
println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
case dynamodb.ErrCodeRequestLimitExceeded:
println(dynamodb.ErrCodeRequestLimitExceeded, aerr.Error())
case dynamodb.ErrCodeInternalServerError:
println(dynamodb.ErrCodeInternalServerError, aerr.Error())
default:
println(PrintPrefix, PrettyPrint(aerr.Error()))
}
} else {
println(PrintPrefix, PrettyPrint(err.Error()))
}
println(PrintPrefix, PrettyPrint(err.Error()))
return ""
}

if result.Item == nil {
println(PrintPrefix, "Could not find '"+config.HashKeyValue+"'")
return ""
Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ func main() {
println(plugins.PrintPrefix, "Exiting")
}()

res, err := extensionClient.Register(ctx, plugins.ExtensionName)
_, err := extensionClient.Register(ctx, plugins.ExtensionName)
if err != nil {
panic(err)
}
println(plugins.PrintPrefix, "Register response:", plugins.PrettyPrint(res))

// Initialize all the cache plugins
extension.InitCacheExtensions()
Expand Down

0 comments on commit eb70ab9

Please sign in to comment.