Skip to content

feat: Easy Event Deserialization #757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<lambda.core.version>1.2.1</lambda.core.version>
<lambda.events.version>3.11.0</lambda.events.version>
<lambda.serial.version>1.0.0</lambda.serial.version>
<maven-compiler-plugin.version>3.10.0</maven-compiler-plugin.version>
<aspectj-maven-plugin.version>1.14.0</aspectj-maven-plugin.version>
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
Expand Down Expand Up @@ -122,6 +123,11 @@
<artifactId>aws-lambda-java-events</artifactId>
<version>${lambda.events.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-serialization</artifactId>
<version>${lambda.serial.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
Expand Down
13 changes: 13 additions & 0 deletions powertools-serialization/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@
<groupId>io.burt</groupId>
<artifactId>jmespath-jackson</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In v4 of the events lib, there will be a dependency on Jackson, I don't think that changes anything, just worth noting.

</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand All @@ -57,6 +65,11 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-tests</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates.
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package software.amazon.lambda.powertools.utilities;

public class EventDeserializationException extends RuntimeException {
private static final long serialVersionUID = -5003158148870110442L;

public EventDeserializationException(String msg, Exception e) {
super(msg, e);
}

public EventDeserializationException(String msg) {
super(msg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates.
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package software.amazon.lambda.powertools.utilities;

import com.amazonaws.services.lambda.runtime.events.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.nio.charset.StandardCharsets.UTF_8;
import static software.amazon.lambda.powertools.utilities.jmespath.Base64Function.decode;
import static software.amazon.lambda.powertools.utilities.jmespath.Base64GZipFunction.decompress;

public class EventDeserializer {

private static final Logger LOG = LoggerFactory.getLogger(EventDeserializer.class);

public static EventPart from(Object obj) {
if (obj instanceof String) {
return new EventPart((String) obj);
} else if (obj instanceof Map) {
return new EventPart((Map<String, Object>) obj);
} else if (obj instanceof APIGatewayProxyRequestEvent) {
APIGatewayProxyRequestEvent event = (APIGatewayProxyRequestEvent) obj;
return new EventPart(event.getBody());
} else if (obj instanceof APIGatewayV2HTTPEvent) {
APIGatewayV2HTTPEvent event = (APIGatewayV2HTTPEvent) obj;
return new EventPart(event.getBody());
} else if (obj instanceof SNSEvent) {
SNSEvent event = (SNSEvent) obj;
return new EventPart(event.getRecords().get(0).getSNS().getMessage());
} else if (obj instanceof SQSEvent) {
SQSEvent event = (SQSEvent) obj;
return new EventPart(event.getRecords().stream().map(SQSEvent.SQSMessage::getBody).collect(Collectors.toList()));
} else if (obj instanceof ScheduledEvent) {
ScheduledEvent event = (ScheduledEvent) obj;
return new EventPart(event.getDetail());
} else if (obj instanceof ApplicationLoadBalancerRequestEvent) {
ApplicationLoadBalancerRequestEvent event = (ApplicationLoadBalancerRequestEvent) obj;
return new EventPart(event.getBody());
} else if (obj instanceof CloudWatchLogsEvent) {
CloudWatchLogsEvent event = (CloudWatchLogsEvent) obj;
return new EventPart(decompress(decode(event.getAwsLogs().getData().getBytes(UTF_8))));
} else if (obj instanceof CloudFormationCustomResourceEvent) {
CloudFormationCustomResourceEvent event = (CloudFormationCustomResourceEvent) obj;
return new EventPart(event.getResourceProperties());
} else if (obj instanceof KinesisEvent) {
KinesisEvent event = (KinesisEvent) obj;
return new EventPart(event.getRecords().stream().map(r -> decode(r.getKinesis().getData())).collect(Collectors.toList()));
} else if (obj instanceof KinesisFirehoseEvent) {
KinesisFirehoseEvent event = (KinesisFirehoseEvent) obj;
return new EventPart(event.getRecords().stream().map(r -> decode(r.getData())).collect(Collectors.toList()));
} else if (obj instanceof KafkaEvent) {
KafkaEvent event = (KafkaEvent) obj;
return new EventPart(event.getRecords().values().stream().flatMap(List::stream).map(r -> decode(r.getValue())).collect(Collectors.toList()));
} else if (obj instanceof ActiveMQEvent) {
ActiveMQEvent event = (ActiveMQEvent) obj;
return new EventPart(event.getMessages().stream().map(m -> decode(m.getData())).collect(Collectors.toList()));
} else if (obj instanceof RabbitMQEvent) {
RabbitMQEvent event = (RabbitMQEvent) obj;
return new EventPart(event.getRmqMessagesByQueue().values().stream().flatMap(List::stream).map(r -> decode(r.getData())).collect(Collectors.toList()));
} else if (obj instanceof KinesisAnalyticsFirehoseInputPreprocessingEvent) {
KinesisAnalyticsFirehoseInputPreprocessingEvent event = (KinesisAnalyticsFirehoseInputPreprocessingEvent) obj;
return new EventPart(event.getRecords().stream().map(r -> decode(r.getData())).collect(Collectors.toList()));
} else if (obj instanceof KinesisAnalyticsStreamsInputPreprocessingEvent) {
KinesisAnalyticsStreamsInputPreprocessingEvent event = (KinesisAnalyticsStreamsInputPreprocessingEvent) obj;
return new EventPart(event.getRecords().stream().map(r -> decode(r.getData())).collect(Collectors.toList()));
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What mechanisms can be have to update these when java lib introduces new event type?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking may be we can have a github action which somehow polls java lib weekly or daily or whatever and create an issue if for powertools if something new is added for events ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not all events make sense in here. I didn't add all, only those where there is a message or something meaningful to extract. Maybe I forgot some interesting ones.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can get an email on each release of the library. Dependabot will also raise a PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, may be i am overthinking this, we wait for doing anything more until we see it being a problem

// does not really make sense to use this EventLoader when you already have a typed object
// just not to throw an exception
LOG.warn("Consider using your object directly instead of using EventDeserializer");
return new EventPart(obj);
}
}

public static class EventPart {
private Map<String, Object> contentMap;
private String content;
private List<String> contentList;
private Object contentObject;

public EventPart(List<String> contentList) {
this.contentList = contentList;
}

public EventPart(String content) {
this.content = content;
}

public EventPart(Map<String, Object> contentMap) {
this.contentMap = contentMap;
}

public EventPart(Object content) {
this.contentObject = content;
}

public <T> T extractDataAs(Class<T> clazz) {
try {
if (content != null) {
if (content.getClass().equals(clazz)) {
// do not read json when returning String, just return the String
return (T) content;
}
return JsonConfig.get().getObjectMapper().reader().readValue(content, clazz);
}
if (contentMap != null) {
return JsonConfig.get().getObjectMapper().convertValue(contentMap, clazz);
}
if (contentObject != null) {
return (T) contentObject;
}
if (contentList != null) {
throw new EventDeserializationException("The content of this event is a list, consider using 'extractDataAsListOf' instead");
}
throw new EventDeserializationException("Event content is null");
} catch (IOException e) {
throw new EventDeserializationException("Cannot load the event as " + clazz.getSimpleName(), e);
}
}

public <T> List<T> extractDataAsListOf(Class<T> clazz) {
if (contentList == null) {
throw new EventDeserializationException("Event content is null");
}
return contentList.stream().map(s -> {
try {
return JsonConfig.get().getObjectMapper().reader().readValue(s, clazz);
} catch (IOException e) {
throw new EventDeserializationException("Cannot load the event as " + clazz.getSimpleName(), e);
}
}).collect(Collectors.toList());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import software.amazon.lambda.powertools.utilities.jmespath.Base64GZipFunction;
import software.amazon.lambda.powertools.utilities.jmespath.JsonFunction;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;

public class JsonConfig {
private JsonConfig() {
}
Expand All @@ -38,11 +36,7 @@ public static JsonConfig get() {
return ConfigHolder.instance;
}

private static final ThreadLocal<ObjectMapper> om = ThreadLocal.withInitial(() -> {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
return objectMapper;
});
private static final ThreadLocal<ObjectMapper> om = ThreadLocal.withInitial(ObjectMapper::new);

private final FunctionRegistry defaultFunctions = FunctionRegistry.defaultRegistry();
private final FunctionRegistry customFunctions = defaultFunctions.extend(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates.
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package software.amazon.lambda.powertools.utilities;

import com.amazonaws.services.lambda.runtime.events.*;
import com.amazonaws.services.lambda.runtime.tests.annotations.Event;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import software.amazon.lambda.powertools.utilities.model.Basket;
import software.amazon.lambda.powertools.utilities.model.Product;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static software.amazon.lambda.powertools.utilities.EventDeserializer.from;

public class EventDeserializerTest {

@Test
public void testDeserializeStringAsString_shouldReturnString() {
String stringEvent = "Hello World";
String result = from(stringEvent).extractDataAs(String.class);
assertThat(result).isEqualTo(stringEvent);
}

@Test
public void testDeserializeStringAsObject_shouldReturnObject() {
String productStr = "{\"id\":1234, \"name\":\"product\", \"price\":42}";
Product product = from(productStr).extractDataAs(Product.class);
assertProduct(product);
}

@Test
public void testDeserializeMapAsObject_shouldReturnObject() {
Map<String, Object> map = new HashMap<>();
map.put("id", 1234);
map.put("name", "product");
map.put("price", 42);
Product product = from(map).extractDataAs(Product.class);
assertProduct(product);
}

@ParameterizedTest
@Event(value = "apigw_event.json", type = APIGatewayProxyRequestEvent.class)
public void testDeserializeAPIGWEventBodyAsObject_shouldReturnObject(APIGatewayProxyRequestEvent event) {
Product product = from(event).extractDataAs(Product.class);
assertProduct(product);
}

@ParameterizedTest
@Event(value = "apigw_event.json", type = APIGatewayProxyRequestEvent.class)
public void testDeserializeAPIGWEventBodyAsWrongObjectType_shouldThrowException(APIGatewayProxyRequestEvent event) {
assertThatThrownBy(() -> from(event).extractDataAs(Basket.class))
.isInstanceOf(EventDeserializationException.class)
.hasMessage("Cannot load the event as Basket");
}

@ParameterizedTest
@Event(value = "sns_event.json", type = SNSEvent.class)
public void testDeserializeSNSEventMessageAsObject_shouldReturnObject(SNSEvent event) {
Product product = from(event).extractDataAs(Product.class);
assertProduct(product);
}

@ParameterizedTest
@Event(value = "sqs_event.json", type = SQSEvent.class)
public void testDeserializeSQSEventMessageAsList_shouldReturnList(SQSEvent event) {
List<Product> products = from(event).extractDataAsListOf(Product.class);
assertThat(products).hasSize(2);
assertProduct(products.get(0));
}

@ParameterizedTest
@Event(value = "kinesis_event.json", type = KinesisEvent.class)
public void testDeserializeKinesisEventMessageAsList_shouldReturnList(KinesisEvent event) {
List<Product> products = from(event).extractDataAsListOf(Product.class);
assertThat(products).hasSize(2);
assertProduct(products.get(0));
}

@ParameterizedTest
@Event(value = "kafka_event.json", type = KafkaEvent.class)
public void testDeserializeKafkaEventMessageAsList_shouldReturnList(KafkaEvent event) {
List<Product> products = from(event).extractDataAsListOf(Product.class);
assertThat(products).hasSize(2);
assertProduct(products.get(0));
}

@ParameterizedTest
@Event(value = "sqs_event.json", type = SQSEvent.class)
public void testDeserializeSQSEventMessageAsObject_shouldThrowException(SQSEvent event) {
assertThatThrownBy(() -> from(event).extractDataAs(Product.class))
.isInstanceOf(EventDeserializationException.class)
.hasMessageContaining("consider using 'extractDataAsListOf' instead");
}

@Test
public void testDeserializeProductAsProduct_shouldReturnProduct() {
Product myProduct = new Product(1234, "product", 42);
Product product = from(myProduct).extractDataAs(Product.class);
assertProduct(product);
}


private void assertProduct(Product product) {
assertThat(product.getId()).isEqualTo(1234);
assertThat(product.getName()).isEqualTo("product");
assertThat(product.getPrice()).isEqualTo(42);
}

}
Loading