MCPcopy Create free account
hub / github.com/aws-samples/aws-lambda-extensions / GetData

Function GetData

cache-extension-demo/plugins/dynamodb.go:51–110  ·  view source on GitHub ↗

Read data from Dynamodb

(dynamodbConfig DynamodbConfiguration)

Source from the content-addressed store, hash-verified

49
50// Read data from Dynamodb
51func GetData(dynamodbConfig DynamodbConfiguration) string {
52 if dynamodbConfig.HashKey != "" {
53 // Create attributeValue map based on hash and sort key
54 var attributeMap = map[string]*dynamodb.AttributeValue{}
55 UpdateAttributeMap(attributeMap, dynamodbConfig)
56
57 result, err := dynamoDbClient.GetItem(&dynamodb.GetItemInput{
58 TableName: aws.String(dynamodbConfig.Table),
59 Key: attributeMap,
60 })
61 if err != nil {
62 if aerr, ok := err.(awserr.Error); ok {
63 switch aerr.Code() {
64 case dynamodb.ErrCodeProvisionedThroughputExceededException:
65 println(dynamodb.ErrCodeProvisionedThroughputExceededException, aerr.Error())
66 case dynamodb.ErrCodeResourceNotFoundException:
67 println(dynamodb.ErrCodeResourceNotFoundException, aerr.Error())
68 case dynamodb.ErrCodeRequestLimitExceeded:
69 println(dynamodb.ErrCodeRequestLimitExceeded, aerr.Error())
70 case dynamodb.ErrCodeInternalServerError:
71 println(dynamodb.ErrCodeInternalServerError, aerr.Error())
72 default:
73 println(PrintPrefix, PrettyPrint(aerr.Error()))
74 }
75 } else {
76 println(PrintPrefix, PrettyPrint(err.Error()))
77 }
78 return ""
79 }
80 if result.Item == nil {
81 println(PrintPrefix, "Could not find '"+dynamodbConfig.HashKeyValue+"'")
82 return ""
83 }
84
85 // Convert data from Map to JSON string
86 var data = make(map[string]string)
87 _ = dynamodbattribute.UnmarshalMap(result.Item, &data)
88
89 // Convert map to JSON string
90 jsonData, err := json.Marshal(data)
91 if err != nil {
92 println(err.Error())
93 }
94
95 // Add it to the cache
96 var value = string(jsonData)
97 dynamoDbCache[GetKey(dynamodbConfig)] = Dynamodb{
98 CacheData: CacheData{
99 Data: value,
100 CacheExpiry: GetCacheExpiry(),
101 },
102 DynamodbConfiguration: dynamodbConfig,
103 }
104
105 return value
106 } else {
107 println(PrintPrefix, "HashKey not available so caching will not be enabled for %s", dynamodbConfig.HashKey)
108 return ""

Callers 2

InitDynamodbFunction · 0.85
GetDynamodbCacheFunction · 0.85

Calls 5

UpdateAttributeMapFunction · 0.85
PrettyPrintFunction · 0.85
GetKeyFunction · 0.85
GetCacheExpiryFunction · 0.85
StringMethod · 0.80

Tested by

no test coverage detected