Skip to content
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

Rework the Event interface #108

Merged
merged 21 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
51 changes: 19 additions & 32 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,40 @@
<version>1.3.0</version>
</parent>

<groupId>io.cloudevents</groupId>
<artifactId>cloudevents-api</artifactId>
<name>CloudEvents - API</name>
<version>1.3.0</version>
<version>${parent.version}</version>
<packaging>jar</packaging>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>${jackson.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>

<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>

<!-- Test deps -->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>${jackson.version}</version>
</dependency>

<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator.version}</version>
</dependency>

<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.el</artifactId>
<version>${jakarta.el.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>

Expand All @@ -76,10 +69,4 @@

</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>
</properties>

</project>
34 changes: 16 additions & 18 deletions api/src/main/java/io/cloudevents/Attributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,48 @@
package io.cloudevents;

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

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
* The marker interface for CloudEvents attributes
*
*
* @author fabiojose
*
*/
public interface Attributes {

/**
* @return The version of the CloudEvents specification which the event uses
*/
SpecVersion getSpecVersion();

/**
* @return Identifies the event. Producers MUST ensure that source + id is unique for each distinct event
*/
@NotBlank
String getId();

/**
* @return A value describing the type of event related to the originating occurrence.
*/
@NotBlank
String getType();

/**
* @return The context in which an event happened.
*/
@NotNull
URI getSource();

/**
* @return The version of the CloudEvents specification which the event uses
*/
@NotBlank
String getSpecversion();

/**
* TODO
* A common way to get the media type of CloudEvents 'data';
* @return If has a value, it MUST follows the <a href="https://tools.ietf.org/html/rfc2046">RFC2046</a>
*/
@JsonIgnore
Optional<String> getMediaType();

Optional<String> getDataContentType();

Optional<URI> getDataSchema();
slinkydeveloper marked this conversation as resolved.
Show resolved Hide resolved

Optional<String> getSubject();

Optional<ZonedDateTime> getTime();

}
39 changes: 0 additions & 39 deletions api/src/main/java/io/cloudevents/Builder.java

This file was deleted.

43 changes: 37 additions & 6 deletions api/src/main/java/io/cloudevents/CloudEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,62 @@
*/
package io.cloudevents;

import com.fasterxml.jackson.databind.JsonNode;

import java.util.Map;
import java.util.Optional;

/**
* An abstract event envelope
* @param <A> The attributes type
* @param <T> The 'data' type
* @author fabiojose
* @author slinkydeveloper
*/
public interface CloudEvent<A extends Attributes, T> {
public interface CloudEvent {
Copy link
Member

Choose a reason for hiding this comment

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

yes this is a good change. too many generics makes this really hard to use.


/**
* The event context attributes
*/
A getAttributes();
Attributes getAttributes();
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI #118

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

@slinkydeveloper slinkydeveloper Apr 20, 2020

Choose a reason for hiding this comment

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

I think the solutions here are two:

  • CloudEvent is implemented once (impl.CloudEventImpl) and different attributes versions are added through composition of objects.
  • CloudEvent is implemented multiple times, one for each event version.

The reason why i went with the first alternative is to simplify the implementation of json marshal/unmarshal and because i'm more a composition guy, but let's review this comment later when this pr is ready.


/**
* The event data
*/
Optional<T> getData();
Optional<String> getDataAsString() throws DataConversionException;

byte[] getDataBase64();
/**
* The event data
*/
Optional<byte[]> getDataAsBytes() throws DataConversionException;

/**
* The event data
*/
Optional<JsonNode> getDataAsJson() throws DataConversionException;
slinkydeveloper marked this conversation as resolved.
Show resolved Hide resolved

/**
* The event extensions
*
* Extensions values could be String/Number/Boolean
*/
Map<String, Object> getExtensions();

/**
* Write an extension into this cloud event
* @param e
*/
default void writeExtension(Extension e) {
slinkydeveloper marked this conversation as resolved.
Show resolved Hide resolved
e.writeToEvent(this);
}

static io.cloudevents.v1.CloudEventBuilder build() {
slinkydeveloper marked this conversation as resolved.
Show resolved Hide resolved
return buildV1();
}

static io.cloudevents.v1.CloudEventBuilder buildV1() {
return new io.cloudevents.v1.CloudEventBuilder();
}

static io.cloudevents.v03.CloudEventBuilder buildV03() {
return new io.cloudevents.v03.CloudEventBuilder();
}
}
8 changes: 8 additions & 0 deletions api/src/main/java/io/cloudevents/DataConversionException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.cloudevents;

public class DataConversionException extends RuntimeException {

public DataConversionException(String from, String to, Throwable cause) {
slinkydeveloper marked this conversation as resolved.
Show resolved Hide resolved
super("Cannot convert " + from + " data to " + to, cause);
}
}
9 changes: 9 additions & 0 deletions api/src/main/java/io/cloudevents/Extension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.cloudevents;

public interface Extension {

void readFromEvent(CloudEvent event);

void writeToEvent(CloudEvent event);

}
25 changes: 25 additions & 0 deletions api/src/main/java/io/cloudevents/SpecVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.cloudevents;

public enum SpecVersion {
V03("0.3"),
V1("1.0");

private final String stringValue;

SpecVersion(String stringValue) {
this.stringValue = stringValue;
}

@Override
public String toString() {
return this.stringValue;
}

public static SpecVersion parse(String sv) {
switch (sv) {
case "0.3": return SpecVersion.V03;
case "1.0": return SpecVersion.V1;
default: throw new IllegalArgumentException("Unrecognized SpecVersion "+ sv);
slinkydeveloper marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Loading