All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cloud.localstack.awssdkv1.lambda.DDBEventParser Maven / Gradle / Ivy

The newest version!
package cloud.localstack.awssdkv1.lambda;

import com.amazonaws.services.dynamodbv2.model.*;
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent;

import java.nio.ByteBuffer;
import java.util.*;
import java.util.stream.Collectors;

import static cloud.localstack.LambdaExecutor.get;

public class DDBEventParser {

    public static DynamodbEvent parse(List> records) {

        DynamodbEvent dynamoDbEvent = new DynamodbEvent();
        dynamoDbEvent.setRecords(new LinkedList<>());
        for (Map record : records) {
            DynamodbEvent.DynamodbStreamRecord r = new DynamodbEvent.DynamodbStreamRecord();
            dynamoDbEvent.getRecords().add(r);

            r.setEventSourceARN((String) get(record, "eventSourceARN"));
            r.setEventSource((String) get(record, "eventSource"));
            r.setEventName(OperationType.fromValue((String) get(record, "eventName")));
            r.setEventVersion((String) get(record, "eventVersion"));

            r.setEventID((String) get(record, "eventID"));
            r.setAwsRegion((String) get(record, "awsRegion"));
            r.setUserIdentity((Identity) get(record, "userIdentity"));

            Map ddbMap = (Map) record.get("dynamodb");

            //DynamodbEvent
            StreamRecord streamRecord = new StreamRecord();
            r.setDynamodb(streamRecord);

            Date date = (Date) get(ddbMap, "approximateCreationDateTime");
            streamRecord.setApproximateCreationDateTime(date != null ? date : new Date());

            streamRecord.setSequenceNumber(UUID.randomUUID().toString());

            streamRecord.setKeys(fromSimpleMap((Map) get(ddbMap, "Keys")));
            streamRecord.setNewImage(fromSimpleMap((Map) get(ddbMap, "NewImage")));
            streamRecord.setOldImage(fromSimpleMap((Map) get(ddbMap, "OldImage")));

            streamRecord.setSizeBytes(((Integer) get(ddbMap, "SizeBytes")).longValue());
            streamRecord.setStreamViewType((String) get(ddbMap, "StreamViewType"));

        }

        return dynamoDbEvent;
    }

    public static Map fromSimpleMap(Map map) {
        if(map == null) {
            return null;
        } else {
            LinkedHashMap result = new LinkedHashMap<>();
            map.entrySet().stream().forEach(entry ->
                    result.put(entry.getKey(),toAttributeValue(entry.getValue()))
            );

            return result;
        }
    }

    /**
     * Reads a previously created Map of Maps in cloud.localstack.LambdaExecutor into Attribute Value
     * @param value the object which is expected to be Map
     * @return parsed AttributeValue
     */
    public static AttributeValue toAttributeValue(Object value) {

        AttributeValue result = new AttributeValue();

        if(value instanceof Map) {
            Map.Entry entry = ((Map) value).entrySet().iterator().next();
            String key = entry.getKey();

            switch (key) {
                case "M":
                    Map in1 = (Map) entry.getValue();
                    result.setM(new LinkedHashMap<>());
                    in1.entrySet().stream().forEach(mapEntry ->
                        result.addMEntry(mapEntry.getKey(), toAttributeValue(mapEntry.getValue()))
                    );
                    break;
                case "SS":
                    result.setSS((List) entry.getValue());
                    break;
                case "BS":
                    List in2 = (List) entry.getValue();
                    result.setBS(in2.stream()
                            .map(element -> ByteBuffer.wrap(element.getBytes()))
                            .collect(Collectors.toList()));
                    break;
                case "NS":
                    List in3 = (List) entry.getValue();
                    result.setNS(in3.stream().map(Object::toString).collect(Collectors.toList()));
                    break;
                case "L":
                    List in4 =(List) entry.getValue();
                    result.setL(in4.stream()
                            .map(el -> toAttributeValue(el))
                            .collect(Collectors.toList())
                    );
                    break;
                case "NULL":
                    result.withNULL(Boolean.parseBoolean(entry.getValue().toString()));
                    break;
                case "BOOL":
                    result.withBOOL(Boolean.parseBoolean(entry.getValue().toString()));
                    break;
                case "S":
                    result.withS((String) entry.getValue());
                    break;
                case "N":
                    String stringValue = entry.getValue().toString();
                    result.withN(stringValue);
                    break;
                case "B":
                    result.withBS(ByteBuffer.wrap(entry.getValue().toString().getBytes()));
                    break;
                default:
                    result.setM(new LinkedHashMap<>());
                    break;
            }
        }
        return result;
    }

}