Skip to content

Commit

Permalink
Merge pull request #108 from slinkydeveloper/refactor-event-class
Browse files Browse the repository at this point in the history
Rework the Event interface
  • Loading branch information
fabiojose authored Apr 22, 2020
2 parents 242b58a + d207ecc commit 8d7c785
Show file tree
Hide file tree
Showing 191 changed files with 3,065 additions and 11,958 deletions.
26 changes: 5 additions & 21 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@ Here we will see how to use the pre-configure marshallers and unmarshallers.
The high-level API to marshal CloudEvents as binary content mode.

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

import io.cloudevents.extensions.DistributedTracingExtension;

import io.cloudevents.extensions.ExtensionFormat;
import io.cloudevents.format.Wire;
import io.cloudevents.v1.CloudEventBuilder;
import io.cloudevents.v1.CloudEventImpl;
import io.cloudevents.v1.http.Marshallers;

Expand All @@ -39,7 +36,7 @@ CloudEventImpl<String> ce =
CloudEventBuilder.<String>builder()
.withType("com.github.pull.create")
.withSource(URI.create("https://github.com/cloudevents/spec/pull"))
.withId("A234-1234-1234")
.withId("A234-1234-1234")
.withDataschema(URI.create("http://my.br"))
.withTime(ZonedDateTime.now())
.withDataContentType("text/plain")
Expand Down Expand Up @@ -69,13 +66,8 @@ wire.getPayload(); //Optional<String> which has the JSON
The high-level API to unmarshal CloudEvents from binary content mode.

```java
import java.util.HashMap;
import java.util.Map;

import io.cloudevents.CloudEvent;
import io.cloudevents.extensions.DistributedTracingExtension;
import io.cloudevents.v1.AttributesImpl;
import io.cloudevents.v1.CloudEventBuilder;

import io.cloudevents.v1.http.Unmarshallers;

// . . .
Expand Down Expand Up @@ -114,12 +106,9 @@ event.getExtensions();
The high-level API to marshal CloudEvents as structured content mode.

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

import io.cloudevents.extensions.DistributedTracingExtension;

import io.cloudevents.extensions.ExtensionFormat;
import io.cloudevents.v1.CloudEventBuilder;
import io.cloudevents.v1.CloudEventImpl;
import io.cloudevents.v1.http.Marshallers;

Expand Down Expand Up @@ -382,14 +371,9 @@ Wire<String, String, String> wire =
/*
* The imports used by the example bellow
*/
import io.cloudevents.CloudEvent;
import io.cloudevents.extensions.DistributedTracingExtension;
import io.cloudevents.format.BinaryUnmarshaller;
import io.cloudevents.format.builder.HeadersStep;
import io.cloudevents.json.Json;
import io.cloudevents.json.types.Much;
import io.cloudevents.v1.AttributesImpl;
import io.cloudevents.v1.CloudEventBuilder;

// . . .

Expand Down Expand Up @@ -520,7 +504,7 @@ HeadersStep<AttributesImpl, Much, String> step =
* - now we get the HeadersStep<AttributesImpl, Much, String>, a common step that event unmarshaller must returns
* - from here we just call withHeaders(), withPayload() and unmarshal()
*/
.map((payload, extensions) -> {
.map((payload, extensions) -> {
CloudEventImpl<Much> event =
Json.<CloudEventImpl<Much>>
decodeValue(payload, CloudEventImpl.class, Much.class);
Expand Down
77 changes: 38 additions & 39 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,30 @@
<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>

<dependencies>
<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>
<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>

<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>
<dependencies>

<!-- Test deps -->
<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 +59,26 @@

</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>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
38 changes: 20 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,52 @@
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();

Optional<String> getSubject();

Optional<ZonedDateTime> getTime();

Attributes toV03();

Attributes toV1();

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

This file was deleted.

41 changes: 34 additions & 7 deletions api/src/main/java/io/cloudevents/CloudEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,58 @@
*/
package io.cloudevents;

import io.cloudevents.format.EventFormat;
import io.cloudevents.message.BinaryMessage;
import io.cloudevents.message.StructuredMessage;

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 {

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

/**
* The event data
*/
Optional<T> getData();

byte[] getDataBase64();
Optional<byte[]> getData();

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

CloudEvent toV03();

CloudEvent toV1();

BinaryMessage asBinaryMessage();

StructuredMessage asStructuredMessage(EventFormat format);

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

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

static io.cloudevents.v03.CloudEventBuilder buildV03() {
return new io.cloudevents.v03.CloudEventBuilder();
}

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

import java.util.Map;

public interface Extension {

void readFromEvent(CloudEvent event);

Map<String, Object> asMap();

}
Loading

0 comments on commit 8d7c785

Please sign in to comment.