Created
May 28, 2020 07:53
-
-
Save esin/93d8ee3a8ac82ea93eb1aac2f2ab95d2 to your computer and use it in GitHub Desktop.
DynamoDB event
This file contains 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
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"math/big" | |
"net/http" | |
"strconv" | |
"strings" | |
"github.com/aws/aws-lambda-go/events" | |
"github.com/aws/aws-lambda-go/lambda" | |
"github.com/aws/aws-sdk-go/service/dynamodb" | |
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" | |
) | |
type itemStruct struct { | |
Wallet string | |
Uncle []int64 | |
TxBlock int64 // Блок на котором мы остановили наблюдения | |
Txs []string | |
Mined []int64 | |
Erc20TxBlock int64 | |
Erc20Txs []string | |
Blockchain string | |
} | |
func unmarshalStreamImage(attribute map[string]events.DynamoDBAttributeValue, out interface{}) error { | |
dbAttrMap := make(map[string]*dynamodb.AttributeValue) | |
for k, v := range attribute { | |
var dbAttr dynamodb.AttributeValue | |
bytes, marshalErr := v.MarshalJSON() | |
if marshalErr != nil { | |
return marshalErr | |
} | |
json.Unmarshal(bytes, &dbAttr) | |
dbAttrMap[k] = &dbAttr | |
} | |
return dynamodbattribute.UnmarshalMap(dbAttrMap, out) | |
} | |
func handler(e events.DynamoDBEvent) error { | |
for _, v := range e.Records { | |
switch v.EventName { | |
case "INSERT": | |
fallthrough | |
case "MODIFY": | |
tableName := strings.Split(v.EventSourceArn, "/")[1] | |
if tableName != "cryptosiren_wallets" { | |
return nil | |
} | |
updatedData := v.Change.NewImage // Old data | |
oldData := v.Change.OldImage // Update data | |
var parsedData *itemStruct | |
err := unmarshalStreamImage(updatedData, &parsedData) | |
if err != nil { | |
fmt.Println("Got error unmarshalling parsedData:") | |
fmt.Println(err.Error()) | |
} | |
var oldParsedData *itemStruct | |
err = unmarshalStreamImage(oldData, &oldParsedData) | |
if err != nil { | |
fmt.Println("Got error unmarshalling oldParsedData:") | |
fmt.Println(err.Error()) | |
} | |
default: | |
} | |
} | |
return nil | |
} | |
func main() { | |
lambda.Start(handler) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment