Skip to content

Commit

Permalink
Replace ZonedDateTime with valid String representation
Browse files Browse the repository at this point in the history
Signed-off-by: David Turanski <dturanski@pivotal.io>
  • Loading branch information
David Turanski committed Dec 6, 2019
1 parent 0d7c860 commit a0fea8f
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 163 deletions.
7 changes: 7 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,20 @@
<version>3.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<properties>
<jackson.version>2.10.1</jackson.version>
<hibernate-validator.version>6.0.17.Final</hibernate-validator.version>
<jakarta.el.version>3.0.3</jakarta.el.version>
<gson.version>2.8.6</gson.version>
</properties>

</project>
2 changes: 0 additions & 2 deletions api/src/main/java/io/cloudevents/json/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public final class Json {
MAPPER.registerModule(new Jdk8Module());

final SimpleModule module = new SimpleModule();
module.addSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer());
module.addDeserializer(ZonedDateTime.class, new ZonedDateTimeDeserializer());
MAPPER.registerModule(module);
}

Expand Down

This file was deleted.

46 changes: 0 additions & 46 deletions api/src/main/java/io/cloudevents/json/ZonedDateTimeSerializer.java

This file was deleted.

25 changes: 14 additions & 11 deletions api/src/main/java/io/cloudevents/v02/AttributesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import io.cloudevents.Attributes;
import io.cloudevents.json.ZonedDateTimeDeserializer;

/**
*
* @author fabiojose
* @author dturanski
* @version 0.2
*/
@JsonInclude(value = Include.NON_ABSENT)
Expand All @@ -58,10 +57,11 @@ public class AttributesImpl implements Attributes {

@NotBlank
private final String id;

private final ZonedDateTime time;
private final URI schemaurl;
private final String contenttype;


AttributesImpl(String type, String specversion, URI source,
String id, ZonedDateTime time, URI schemaurl, String contenttype) {
Expand Down Expand Up @@ -113,7 +113,6 @@ public URI getSource() {
/**
* Timestamp of when the event happened.
*/
@JsonDeserialize(using = ZonedDateTimeDeserializer.class)
public Optional<ZonedDateTime> getTime() {
return Optional.ofNullable(time);
}
Expand Down Expand Up @@ -145,11 +144,11 @@ public static AttributesImpl build(
@JsonProperty("source") URI source,
@JsonProperty("specversion") String specversion,
@JsonProperty("type") String type,
@JsonProperty("time") ZonedDateTime time,
@JsonProperty("time") String time,
@JsonProperty("schemaurl") URI schemaurl,
@JsonProperty("contenttype") String contenttype) {

return new AttributesImpl(type, specversion, source, id, time,
return new AttributesImpl(type, specversion, source, id, parseZonedDateTime(time).orElse(null),
schemaurl, contenttype);
}

Expand All @@ -159,11 +158,7 @@ public static AttributesImpl build(
*/
public static AttributesImpl unmarshal(Map<String, String> attributes) {
String type = attributes.get(ContextAttributes.type.name());
ZonedDateTime time =
Optional.ofNullable(attributes.get(ContextAttributes.time.name()))
.map((t) -> ZonedDateTime.parse(t,
ISO_ZONED_DATE_TIME))
.orElse(null);
String time = attributes.get(ContextAttributes.time.name());

String specversion = attributes.get(ContextAttributes.specversion.name());
URI source = URI.create(attributes.get(ContextAttributes.source.name()));
Expand Down Expand Up @@ -209,4 +204,12 @@ public static Map<String, String> marshal(AttributesImpl attributes) {

return result;
}

static Optional<String> formatZonedDateTime(ZonedDateTime zonedDateTime) {
return zonedDateTime == null? Optional.empty() : Optional.of(zonedDateTime.format(ISO_ZONED_DATE_TIME));
}

static Optional<ZonedDateTime> parseZonedDateTime(String zonedDateTime) {
return zonedDateTime == null ? Optional.empty(): Optional.of(ZonedDateTime.parse(zonedDateTime));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* CloudEvent instances builder
*
* @author fabiojose
* @author dturanski
* @version 0.2
*/
public class CloudEventBuilder<T> implements EventBuilder<T, AttributesImpl>,
Expand Down
7 changes: 4 additions & 3 deletions api/src/main/java/io/cloudevents/v02/CloudEventImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package io.cloudevents.v02;

import java.net.URI;
import java.time.ZonedDateTime;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
Expand All @@ -42,6 +42,7 @@
* The event implementation
*
* @author fabiojose
* @author dturanski
*
*/
@JsonInclude(value = Include.NON_ABSENT)
Expand Down Expand Up @@ -118,7 +119,7 @@ public static <T> CloudEventImpl<T> build(
@JsonProperty("id") String id,
@JsonProperty("source") URI source,
@JsonProperty("type") String type,
@JsonProperty("time") ZonedDateTime time,
@JsonProperty("time") String time,
@JsonProperty("schemaurl") URI schemaurl,
@JsonProperty("contenttype") String contenttype,
@JsonProperty("data") T data) {
Expand All @@ -127,7 +128,7 @@ public static <T> CloudEventImpl<T> build(
.withId(id)
.withSource(source)
.withType(type)
.withTime(time)
.withTime(AttributesImpl.parseZonedDateTime(time).orElse(null))
.withSchemaurl(schemaurl)
.withContenttype(contenttype)
.withData(data)
Expand Down
46 changes: 25 additions & 21 deletions api/src/main/java/io/cloudevents/v03/AttributesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
*/
package io.cloudevents.v03;

import static java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.net.URI;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand All @@ -30,19 +35,15 @@
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import io.cloudevents.Attributes;
import io.cloudevents.json.ZonedDateTimeDeserializer;

import static java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME;

/**
* The event attributes implementation for v0.3
*
* @author fabiojose
* @author dturanski
*
*/
@JsonInclude(value = Include.NON_ABSENT)
Expand All @@ -60,8 +61,7 @@ public class AttributesImpl implements Attributes {

@NotBlank
private final String type;

@JsonDeserialize(using = ZonedDateTimeDeserializer.class)

private final ZonedDateTime time;
private final URI schemaurl;

Expand All @@ -71,15 +71,15 @@ public class AttributesImpl implements Attributes {

@Size(min = 1)
private final String subject;

AttributesImpl(String id, URI source, String specversion, String type,
ZonedDateTime time, URI schemaurl, String datacontentencoding,
String datacontenttype, String subject) {
ZonedDateTime time, URI schemaurl, String datacontentencoding,
String datacontenttype, String subject) {
this.id = id;
this.source = source;
this.specversion = specversion;
this.type = type;

this.time = time;
this.schemaurl = schemaurl;
this.datacontentencoding = datacontentencoding;
Expand Down Expand Up @@ -140,13 +140,13 @@ public static AttributesImpl build(
@JsonProperty("source") URI source,
@JsonProperty("specversion") String specversion,
@JsonProperty("type") String type,
@JsonProperty("time") ZonedDateTime time,
@JsonProperty("time") String time,
@JsonProperty("schemaurl") URI schemaurl,
@JsonProperty("datacontentenconding") String datacontentencoding,
@JsonProperty("datacontenttype") String datacontenttype,
@JsonProperty("subject") String subject) {

return new AttributesImpl(id, source, specversion, type, time,
return new AttributesImpl(id, source, specversion, type, parseZonedDateTime(time).orElse(null),
schemaurl, datacontentencoding, datacontenttype, subject);
}

Expand Down Expand Up @@ -185,11 +185,7 @@ public static Map<String, String> marshal(AttributesImpl attributes) {
*/
public static AttributesImpl unmarshal(Map<String, String> attributes) {
String type = attributes.get(ContextAttributes.type.name());
ZonedDateTime time =
Optional.ofNullable(attributes.get(ContextAttributes.time.name()))
.map((t) -> ZonedDateTime.parse(t,
ISO_ZONED_DATE_TIME))
.orElse(null);
String time = attributes.get(ContextAttributes.time.name());

String specversion = attributes.get(ContextAttributes.specversion.name());
URI source = URI.create(attributes.get(ContextAttributes.source.name()));
Expand All @@ -213,4 +209,12 @@ public static AttributesImpl unmarshal(Map<String, String> attributes) {
time, schemaurl, datacontentencoding,
datacontenttype, subject);
}

static Optional<String> formatZonedDateTime(ZonedDateTime zonedDateTime) {
return zonedDateTime == null? Optional.empty() : Optional.of(zonedDateTime.format(ISO_ZONED_DATE_TIME));
}

static Optional<ZonedDateTime> parseZonedDateTime(String zonedDateTime) {
return zonedDateTime == null ? Optional.empty(): Optional.of(ZonedDateTime.parse(zonedDateTime));
}
}
4 changes: 2 additions & 2 deletions api/src/main/java/io/cloudevents/v03/CloudEventImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static <T> CloudEventImpl<T> build(
@JsonProperty("id") String id,
@JsonProperty("source") URI source,
@JsonProperty("type") String type,
@JsonProperty("time") ZonedDateTime time,
@JsonProperty("time") String time,
@JsonProperty("schemaurl") URI schemaurl,
@JsonProperty("datacontentencoding") String datacontentencoding,
@JsonProperty("datacontenttype") String datacontenttype,
Expand All @@ -126,7 +126,7 @@ public static <T> CloudEventImpl<T> build(
.withId(id)
.withSource(source)
.withType(type)
.withTime(time)
.withTime(AttributesImpl.parseZonedDateTime(time).orElse(null))
.withSchemaurl(schemaurl)
.withDatacontentencoding(datacontentencoding)
.withDatacontenttype(datacontenttype)
Expand Down
Loading

0 comments on commit a0fea8f

Please sign in to comment.