Renamed methods of writes to coerce with the builder apis (#237)

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
This commit is contained in:
Francesco Guardiani 2020-09-28 08:58:57 +02:00 committed by GitHub
parent 1f58088f3c
commit 2982f07f55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 163 additions and 151 deletions

View File

@ -17,6 +17,7 @@
package io.cloudevents.rw; package io.cloudevents.rw;
import io.cloudevents.lang.Nullable;
import io.cloudevents.types.Time; import io.cloudevents.types.Time;
import java.net.URI; import java.net.URI;
@ -35,7 +36,7 @@ public interface CloudEventAttributesWriter {
* @param value * @param value
* @throws CloudEventRWException * @throws CloudEventRWException
*/ */
void setAttribute(String name, String value) throws CloudEventRWException; CloudEventAttributesWriter withAttribute(String name, @Nullable String value) throws CloudEventRWException;
/** /**
* Set attribute with type {@link URI}. * Set attribute with type {@link URI}.
@ -44,8 +45,8 @@ public interface CloudEventAttributesWriter {
* @param value * @param value
* @throws CloudEventRWException * @throws CloudEventRWException
*/ */
default void setAttribute(String name, URI value) throws CloudEventRWException { default CloudEventAttributesWriter withAttribute(String name, @Nullable URI value) throws CloudEventRWException {
setAttribute(name, value.toString()); return withAttribute(name, value == null ? null : value.toString());
} }
/** /**
@ -55,8 +56,8 @@ public interface CloudEventAttributesWriter {
* @param value * @param value
* @throws CloudEventRWException * @throws CloudEventRWException
*/ */
default void setAttribute(String name, OffsetDateTime value) throws CloudEventRWException { default CloudEventAttributesWriter withAttribute(String name, @Nullable OffsetDateTime value) throws CloudEventRWException {
setAttribute(name, Time.writeTime(value)); return withAttribute(name, value == null ? null : Time.writeTime(value));
} }
} }

View File

@ -17,6 +17,8 @@
package io.cloudevents.rw; package io.cloudevents.rw;
import io.cloudevents.lang.Nullable;
import java.net.URI; import java.net.URI;
/** /**
@ -31,7 +33,7 @@ public interface CloudEventExtensionsWriter {
* @param value * @param value
* @throws CloudEventRWException * @throws CloudEventRWException
*/ */
void setExtension(String name, String value) throws CloudEventRWException; CloudEventExtensionsWriter withExtension(String name, @Nullable String value) throws CloudEventRWException;
/** /**
* Set attribute with type {@link URI}. * Set attribute with type {@link URI}.
@ -40,8 +42,8 @@ public interface CloudEventExtensionsWriter {
* @param value * @param value
* @throws CloudEventRWException * @throws CloudEventRWException
*/ */
default void setExtension(String name, Number value) throws CloudEventRWException { default CloudEventExtensionsWriter withExtension(String name, @Nullable Number value) throws CloudEventRWException {
setExtension(name, value.toString()); return withExtension(name, value == null ? null : value.toString());
} }
/** /**
@ -51,8 +53,8 @@ public interface CloudEventExtensionsWriter {
* @param value * @param value
* @throws CloudEventRWException * @throws CloudEventRWException
*/ */
default void setExtension(String name, Boolean value) throws CloudEventRWException { default CloudEventExtensionsWriter withExtension(String name, @Nullable Boolean value) throws CloudEventRWException {
setExtension(name, value.toString()); return withExtension(name, value == null ? null : value.toString());
} }
} }

View File

@ -123,6 +123,7 @@ public interface CloudEventBuilder extends CloudEventWriter<CloudEvent> {
* @param value value of the extension attribute * @param value value of the extension attribute
* @return self * @return self
*/ */
@Override
CloudEventBuilder withExtension(@Nonnull String key, String value); CloudEventBuilder withExtension(@Nonnull String key, String value);
/** /**
@ -132,6 +133,7 @@ public interface CloudEventBuilder extends CloudEventWriter<CloudEvent> {
* @param value value of the extension attribute * @param value value of the extension attribute
* @return self * @return self
*/ */
@Override
CloudEventBuilder withExtension(@Nonnull String key, Number value); CloudEventBuilder withExtension(@Nonnull String key, Number value);
/** /**
@ -141,7 +143,8 @@ public interface CloudEventBuilder extends CloudEventWriter<CloudEvent> {
* @param value value of the extension attribute * @param value value of the extension attribute
* @return self * @return self
*/ */
CloudEventBuilder withExtension(@Nonnull String key, boolean value); @Override
CloudEventBuilder withExtension(@Nonnull String key, Boolean value);
/** /**
* Add to the builder all the extension key/values of the provided extension * Add to the builder all the extension key/values of the provided extension

View File

@ -65,11 +65,11 @@ public abstract class BaseCloudEvent implements CloudEvent, CloudEventReader {
// TODO to be improved // TODO to be improved
for (Map.Entry<String, Object> entry : this.extensions.entrySet()) { for (Map.Entry<String, Object> entry : this.extensions.entrySet()) {
if (entry.getValue() instanceof String) { if (entry.getValue() instanceof String) {
visitor.setExtension(entry.getKey(), (String) entry.getValue()); visitor.withExtension(entry.getKey(), (String) entry.getValue());
} else if (entry.getValue() instanceof Number) { } else if (entry.getValue() instanceof Number) {
visitor.setExtension(entry.getKey(), (Number) entry.getValue()); visitor.withExtension(entry.getKey(), (Number) entry.getValue());
} else if (entry.getValue() instanceof Boolean) { } else if (entry.getValue() instanceof Boolean) {
visitor.setExtension(entry.getKey(), (Boolean) entry.getValue()); visitor.withExtension(entry.getKey(), (Boolean) entry.getValue());
} else { } else {
// This should never happen because we build that map only through our builders // This should never happen because we build that map only through our builders
throw new IllegalStateException("Illegal value inside extensions map: " + entry); throw new IllegalStateException("Illegal value inside extensions map: " + entry);

View File

@ -86,7 +86,7 @@ public abstract class BaseCloudEventBuilder<SELF extends BaseCloudEventBuilder<S
return self; return self;
} }
public SELF withExtension(@Nonnull String key, boolean value) { public SELF withExtension(@Nonnull String key, Boolean value) {
this.extensions.put(key, value); this.extensions.put(key, value);
return self; return self;
} }
@ -113,21 +113,6 @@ public abstract class BaseCloudEventBuilder<SELF extends BaseCloudEventBuilder<S
return self; return self;
} }
@Override
public void setExtension(String name, String value) throws CloudEventRWException {
this.withExtension(name, value);
}
@Override
public void setExtension(String name, Number value) throws CloudEventRWException {
this.withExtension(name, value);
}
@Override
public void setExtension(String name, Boolean value) throws CloudEventRWException {
this.withExtension(name, value);
}
@Override @Override
public CloudEvent end(byte[] value) throws CloudEventRWException { public CloudEvent end(byte[] value) throws CloudEventRWException {
this.data = value; this.data = value;

View File

@ -43,20 +43,20 @@ public class CloudEventReaderAdapter implements CloudEventReader {
@Override @Override
public void readAttributes(CloudEventAttributesWriter writer) throws RuntimeException { public void readAttributes(CloudEventAttributesWriter writer) throws RuntimeException {
writer.setAttribute("id", event.getId()); writer.withAttribute("id", event.getId());
writer.setAttribute("source", event.getSource()); writer.withAttribute("source", event.getSource());
writer.setAttribute("type", event.getType()); writer.withAttribute("type", event.getType());
if (event.getDataContentType() != null) { if (event.getDataContentType() != null) {
writer.setAttribute("datacontenttype", event.getDataContentType()); writer.withAttribute("datacontenttype", event.getDataContentType());
} }
if (event.getDataSchema() != null) { if (event.getDataSchema() != null) {
writer.setAttribute("dataschema", event.getDataSchema()); writer.withAttribute("dataschema", event.getDataSchema());
} }
if (event.getSubject() != null) { if (event.getSubject() != null) {
writer.setAttribute("subject", event.getSubject()); writer.withAttribute("subject", event.getSubject());
} }
if (event.getTime() != null) { if (event.getTime() != null) {
writer.setAttribute("time", event.getTime()); writer.withAttribute("time", event.getTime());
} }
} }
@ -65,11 +65,11 @@ public class CloudEventReaderAdapter implements CloudEventReader {
for (String key : event.getExtensionNames()) { for (String key : event.getExtensionNames()) {
Object value = event.getExtension(key); Object value = event.getExtension(key);
if (value instanceof String) { if (value instanceof String) {
visitor.setExtension(key, (String) value); visitor.withExtension(key, (String) value);
} else if (value instanceof Number) { } else if (value instanceof Number) {
visitor.setExtension(key, (Number) value); visitor.withExtension(key, (Number) value);
} else if (value instanceof Boolean) { } else if (value instanceof Boolean) {
visitor.setExtension(key, (Boolean) value); visitor.withExtension(key, (Boolean) value);
} else { } else {
// This should never happen because we build that map only through our builders // This should never happen because we build that map only through our builders
throw new IllegalStateException("Illegal value inside extensions map: " + key + " " + value); throw new IllegalStateException("Illegal value inside extensions map: " + key + " " + value);

View File

@ -51,16 +51,16 @@ public abstract class BaseGenericBinaryMessageReaderImpl<HK, HV> extends BaseBin
// in order to complete the visit in one loop // in order to complete the visit in one loop
this.forEachHeader((key, value) -> { this.forEachHeader((key, value) -> {
if (isContentTypeHeader(key)) { if (isContentTypeHeader(key)) {
visitor.setAttribute("datacontenttype", toCloudEventsValue(value)); visitor.withAttribute("datacontenttype", toCloudEventsValue(value));
} else if (isCloudEventsHeader(key)) { } else if (isCloudEventsHeader(key)) {
String name = toCloudEventsKey(key); String name = toCloudEventsKey(key);
if (name.equals("specversion")) { if (name.equals("specversion")) {
return; return;
} }
if (this.version.getAllAttributes().contains(name)) { if (this.version.getAllAttributes().contains(name)) {
visitor.setAttribute(name, toCloudEventsValue(value)); visitor.withAttribute(name, toCloudEventsValue(value));
} else { } else {
visitor.setExtension(name, toCloudEventsValue(value)); visitor.withExtension(name, toCloudEventsValue(value));
} }
} }
}); });
@ -77,14 +77,14 @@ public abstract class BaseGenericBinaryMessageReaderImpl<HK, HV> extends BaseBin
public void readAttributes(CloudEventAttributesWriter writer) throws RuntimeException { public void readAttributes(CloudEventAttributesWriter writer) throws RuntimeException {
this.forEachHeader((key, value) -> { this.forEachHeader((key, value) -> {
if (isContentTypeHeader(key)) { if (isContentTypeHeader(key)) {
writer.setAttribute("datacontenttype", toCloudEventsValue(value)); writer.withAttribute("datacontenttype", toCloudEventsValue(value));
} else if (isCloudEventsHeader(key)) { } else if (isCloudEventsHeader(key)) {
String name = toCloudEventsKey(key); String name = toCloudEventsKey(key);
if (name.equals("specversion")) { if (name.equals("specversion")) {
return; return;
} }
if (this.version.getAllAttributes().contains(name)) { if (this.version.getAllAttributes().contains(name)) {
writer.setAttribute(name, toCloudEventsValue(value)); writer.withAttribute(name, toCloudEventsValue(value));
} }
} }
}); });
@ -97,7 +97,7 @@ public abstract class BaseGenericBinaryMessageReaderImpl<HK, HV> extends BaseBin
if (isCloudEventsHeader(key)) { if (isCloudEventsHeader(key)) {
String name = toCloudEventsKey(key); String name = toCloudEventsKey(key);
if (!this.version.getAllAttributes().contains(name)) { if (!this.version.getAllAttributes().contains(name)) {
visitor.setExtension(name, toCloudEventsValue(value)); visitor.withExtension(name, toCloudEventsValue(value));
} }
} }
}); });

View File

@ -135,66 +135,66 @@ public final class CloudEventBuilder extends BaseCloudEventBuilder<CloudEventBui
// Message impl // Message impl
@Override @Override
public void setAttribute(String name, String value) throws CloudEventRWException { public CloudEventBuilder withAttribute(String name, String value) throws CloudEventRWException {
switch (name) { switch (name) {
case "id": case "id":
withId(value); withId(value);
return; return this;
case "source": case "source":
try { try {
withSource(new URI(value)); withSource(new URI(value));
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw CloudEventRWException.newInvalidAttributeValue("source", value, e); throw CloudEventRWException.newInvalidAttributeValue("source", value, e);
} }
return; return this;
case "type": case "type":
withType(value); withType(value);
return; return this;
case "datacontenttype": case "datacontenttype":
withDataContentType(value); withDataContentType(value);
return; return this;
case "datacontentencoding": case "datacontentencoding":
// No-op, this information is not saved in the event because it's useful only for parsing // No-op, this information is not saved in the event because it's useful only for parsing
return; return this;
case "schemaurl": case "schemaurl":
try { try {
withSchemaUrl(new URI(value)); withSchemaUrl(new URI(value));
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw CloudEventRWException.newInvalidAttributeValue("schemaurl", value, e); throw CloudEventRWException.newInvalidAttributeValue("schemaurl", value, e);
} }
return; return this;
case "subject": case "subject":
withSubject(value); withSubject(value);
return; return this;
case "time": case "time":
try { try {
withTime(Time.parseTime(value)); withTime(Time.parseTime(value));
} catch (DateTimeParseException e) { } catch (DateTimeParseException e) {
throw CloudEventRWException.newInvalidAttributeValue("time", value, e); throw CloudEventRWException.newInvalidAttributeValue("time", value, e);
} }
return; return this;
} }
throw CloudEventRWException.newInvalidAttributeName(name); throw CloudEventRWException.newInvalidAttributeName(name);
} }
@Override @Override
public void setAttribute(String name, URI value) throws CloudEventRWException { public CloudEventBuilder withAttribute(String name, URI value) throws CloudEventRWException {
switch (name) { switch (name) {
case "source": case "source":
withSource(value); withSource(value);
return; return this;
case "schemaurl": case "schemaurl":
withDataSchema(value); withDataSchema(value);
return; return this;
} }
throw CloudEventRWException.newInvalidAttributeType(name, URI.class); throw CloudEventRWException.newInvalidAttributeType(name, URI.class);
} }
@Override @Override
public void setAttribute(String name, OffsetDateTime value) throws CloudEventRWException { public CloudEventBuilder withAttribute(String name, OffsetDateTime value) throws CloudEventRWException {
if ("time".equals(name)) { if ("time".equals(name)) {
withTime(value); withTime(value);
return; return this;
} }
throw CloudEventRWException.newInvalidAttributeType(name, OffsetDateTime.class); throw CloudEventRWException.newInvalidAttributeType(name, OffsetDateTime.class);
} }

View File

@ -122,38 +122,38 @@ public final class CloudEventV03 extends BaseCloudEvent {
@Override @Override
public void readAttributes(CloudEventAttributesWriter writer) throws CloudEventRWException { public void readAttributes(CloudEventAttributesWriter writer) throws CloudEventRWException {
writer.setAttribute( writer.withAttribute(
ContextAttributes.ID.name().toLowerCase(), ContextAttributes.ID.name().toLowerCase(),
this.id this.id
); );
writer.setAttribute( writer.withAttribute(
ContextAttributes.SOURCE.name().toLowerCase(), ContextAttributes.SOURCE.name().toLowerCase(),
this.source this.source
); );
writer.setAttribute( writer.withAttribute(
ContextAttributes.TYPE.name().toLowerCase(), ContextAttributes.TYPE.name().toLowerCase(),
this.type this.type
); );
if (this.datacontenttype != null) { if (this.datacontenttype != null) {
writer.setAttribute( writer.withAttribute(
ContextAttributes.DATACONTENTTYPE.name().toLowerCase(), ContextAttributes.DATACONTENTTYPE.name().toLowerCase(),
this.datacontenttype this.datacontenttype
); );
} }
if (this.schemaurl != null) { if (this.schemaurl != null) {
writer.setAttribute( writer.withAttribute(
ContextAttributes.SCHEMAURL.name().toLowerCase(), ContextAttributes.SCHEMAURL.name().toLowerCase(),
this.schemaurl this.schemaurl
); );
} }
if (this.subject != null) { if (this.subject != null) {
writer.setAttribute( writer.withAttribute(
ContextAttributes.SUBJECT.name().toLowerCase(), ContextAttributes.SUBJECT.name().toLowerCase(),
this.subject this.subject
); );
} }
if (this.time != null) { if (this.time != null) {
writer.setAttribute( writer.withAttribute(
ContextAttributes.TIME.name().toLowerCase(), ContextAttributes.TIME.name().toLowerCase(),
this.time this.time
); );

View File

@ -35,63 +35,63 @@ class V1ToV03AttributesConverter implements CloudEventAttributesWriter {
} }
@Override @Override
public void setAttribute(String name, String value) throws CloudEventRWException { public V1ToV03AttributesConverter withAttribute(String name, String value) throws CloudEventRWException {
switch (name) { switch (name) {
case "id": case "id":
builder.withId(value); builder.withId(value);
return; return this;
case "source": case "source":
try { try {
builder.withSource(new URI(value)); builder.withSource(new URI(value));
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw CloudEventRWException.newInvalidAttributeValue("source", value, e); throw CloudEventRWException.newInvalidAttributeValue("source", value, e);
} }
return; return this;
case "type": case "type":
builder.withType(value); builder.withType(value);
return; return this;
case "datacontenttype": case "datacontenttype":
builder.withDataContentType(value); builder.withDataContentType(value);
return; return this;
case "dataschema": case "dataschema":
try { try {
builder.withSchemaUrl(new URI(value)); builder.withSchemaUrl(new URI(value));
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw CloudEventRWException.newInvalidAttributeValue("dataschema", value, e); throw CloudEventRWException.newInvalidAttributeValue("dataschema", value, e);
} }
return; return this;
case "subject": case "subject":
builder.withSubject(value); builder.withSubject(value);
return; return this;
case "time": case "time":
try { try {
builder.withTime(Time.parseTime(value)); builder.withTime(Time.parseTime(value));
} catch (DateTimeParseException e) { } catch (DateTimeParseException e) {
throw CloudEventRWException.newInvalidAttributeValue("time", value, e); throw CloudEventRWException.newInvalidAttributeValue("time", value, e);
} }
return; return this;
} }
throw CloudEventRWException.newInvalidAttributeName(name); throw CloudEventRWException.newInvalidAttributeName(name);
} }
@Override @Override
public void setAttribute(String name, URI value) throws CloudEventRWException { public V1ToV03AttributesConverter withAttribute(String name, URI value) throws CloudEventRWException {
switch (name) { switch (name) {
case "source": case "source":
builder.withSource(value); builder.withSource(value);
return; return this;
case "dataschema": case "dataschema":
builder.withSchemaUrl(value); builder.withSchemaUrl(value);
return; return this;
} }
throw CloudEventRWException.newInvalidAttributeType(name, URI.class); throw CloudEventRWException.newInvalidAttributeType(name, URI.class);
} }
@Override @Override
public void setAttribute(String name, OffsetDateTime value) throws CloudEventRWException { public V1ToV03AttributesConverter withAttribute(String name, OffsetDateTime value) throws CloudEventRWException {
if ("time".equals(name)) { if ("time".equals(name)) {
builder.withTime(value); builder.withTime(value);
return; return this;
} }
throw CloudEventRWException.newInvalidAttributeType(name, OffsetDateTime.class); throw CloudEventRWException.newInvalidAttributeType(name, OffsetDateTime.class);
} }

View File

@ -132,63 +132,63 @@ public final class CloudEventBuilder extends BaseCloudEventBuilder<CloudEventBui
// Message impl // Message impl
@Override @Override
public void setAttribute(String name, String value) throws CloudEventRWException { public CloudEventBuilder withAttribute(String name, String value) throws CloudEventRWException {
switch (name) { switch (name) {
case "id": case "id":
withId(value); withId(value);
return; return this;
case "source": case "source":
try { try {
withSource(new URI(value)); withSource(new URI(value));
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw CloudEventRWException.newInvalidAttributeValue("source", value, e); throw CloudEventRWException.newInvalidAttributeValue("source", value, e);
} }
return; return this;
case "type": case "type":
withType(value); withType(value);
return; return this;
case "datacontenttype": case "datacontenttype":
withDataContentType(value); withDataContentType(value);
return; return this;
case "dataschema": case "dataschema":
try { try {
withDataSchema(new URI(value)); withDataSchema(new URI(value));
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw CloudEventRWException.newInvalidAttributeValue("dataschema", value, e); throw CloudEventRWException.newInvalidAttributeValue("dataschema", value, e);
} }
return; return this;
case "subject": case "subject":
withSubject(value); withSubject(value);
return; return this;
case "time": case "time":
try { try {
withTime(Time.parseTime(value)); withTime(Time.parseTime(value));
} catch (DateTimeParseException e) { } catch (DateTimeParseException e) {
throw CloudEventRWException.newInvalidAttributeValue("time", value, e); throw CloudEventRWException.newInvalidAttributeValue("time", value, e);
} }
return; return this;
} }
throw CloudEventRWException.newInvalidAttributeName(name); throw CloudEventRWException.newInvalidAttributeName(name);
} }
@Override @Override
public void setAttribute(String name, URI value) throws CloudEventRWException { public CloudEventBuilder withAttribute(String name, URI value) throws CloudEventRWException {
switch (name) { switch (name) {
case "source": case "source":
withSource(value); withSource(value);
return; return this;
case "dataschema": case "dataschema":
withDataSchema(value); withDataSchema(value);
return; return this;
} }
throw CloudEventRWException.newInvalidAttributeType(name, URI.class); throw CloudEventRWException.newInvalidAttributeType(name, URI.class);
} }
@Override @Override
public void setAttribute(String name, OffsetDateTime value) throws CloudEventRWException { public CloudEventBuilder withAttribute(String name, OffsetDateTime value) throws CloudEventRWException {
if ("time".equals(name)) { if ("time".equals(name)) {
withTime(value); withTime(value);
return; return this;
} }
throw CloudEventRWException.newInvalidAttributeType(name, OffsetDateTime.class); throw CloudEventRWException.newInvalidAttributeType(name, OffsetDateTime.class);
} }

View File

@ -117,38 +117,38 @@ public final class CloudEventV1 extends BaseCloudEvent {
@Override @Override
public void readAttributes(CloudEventAttributesWriter writer) throws CloudEventRWException { public void readAttributes(CloudEventAttributesWriter writer) throws CloudEventRWException {
writer.setAttribute( writer.withAttribute(
ContextAttributes.ID.name().toLowerCase(), ContextAttributes.ID.name().toLowerCase(),
this.id this.id
); );
writer.setAttribute( writer.withAttribute(
ContextAttributes.SOURCE.name().toLowerCase(), ContextAttributes.SOURCE.name().toLowerCase(),
this.source this.source
); );
writer.setAttribute( writer.withAttribute(
ContextAttributes.TYPE.name().toLowerCase(), ContextAttributes.TYPE.name().toLowerCase(),
this.type this.type
); );
if (this.datacontenttype != null) { if (this.datacontenttype != null) {
writer.setAttribute( writer.withAttribute(
ContextAttributes.DATACONTENTTYPE.name().toLowerCase(), ContextAttributes.DATACONTENTTYPE.name().toLowerCase(),
this.datacontenttype this.datacontenttype
); );
} }
if (this.dataschema != null) { if (this.dataschema != null) {
writer.setAttribute( writer.withAttribute(
ContextAttributes.DATASCHEMA.name().toLowerCase(), ContextAttributes.DATASCHEMA.name().toLowerCase(),
this.dataschema this.dataschema
); );
} }
if (this.subject != null) { if (this.subject != null) {
writer.setAttribute( writer.withAttribute(
ContextAttributes.SUBJECT.name().toLowerCase(), ContextAttributes.SUBJECT.name().toLowerCase(),
this.subject this.subject
); );
} }
if (this.time != null) { if (this.time != null) {
writer.setAttribute( writer.withAttribute(
ContextAttributes.TIME.name().toLowerCase(), ContextAttributes.TIME.name().toLowerCase(),
this.time this.time
); );

View File

@ -35,63 +35,63 @@ class V03ToV1AttributesConverter implements CloudEventAttributesWriter {
} }
@Override @Override
public void setAttribute(String name, String value) throws CloudEventRWException { public V03ToV1AttributesConverter withAttribute(String name, String value) throws CloudEventRWException {
switch (name) { switch (name) {
case "id": case "id":
builder.withId(value); builder.withId(value);
return; return this;
case "source": case "source":
try { try {
builder.withSource(new URI(value)); builder.withSource(new URI(value));
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw CloudEventRWException.newInvalidAttributeValue("source", value, e); throw CloudEventRWException.newInvalidAttributeValue("source", value, e);
} }
return; return this;
case "type": case "type":
builder.withType(value); builder.withType(value);
return; return this;
case "datacontenttype": case "datacontenttype":
builder.withDataContentType(value); builder.withDataContentType(value);
return; return this;
case "schemaurl": case "schemaurl":
try { try {
builder.withDataSchema(new URI(value)); builder.withDataSchema(new URI(value));
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw CloudEventRWException.newInvalidAttributeValue("dataschema", value, e); throw CloudEventRWException.newInvalidAttributeValue("dataschema", value, e);
} }
return; return this;
case "subject": case "subject":
builder.withSubject(value); builder.withSubject(value);
return; return this;
case "time": case "time":
try { try {
builder.withTime(Time.parseTime(value)); builder.withTime(Time.parseTime(value));
} catch (DateTimeParseException e) { } catch (DateTimeParseException e) {
throw CloudEventRWException.newInvalidAttributeValue("time", value, e); throw CloudEventRWException.newInvalidAttributeValue("time", value, e);
} }
return; return this;
} }
throw CloudEventRWException.newInvalidAttributeName(name); throw CloudEventRWException.newInvalidAttributeName(name);
} }
@Override @Override
public void setAttribute(String name, URI value) throws CloudEventRWException { public V03ToV1AttributesConverter withAttribute(String name, URI value) throws CloudEventRWException {
switch (name) { switch (name) {
case "source": case "source":
builder.withSource(value); builder.withSource(value);
return; return this;
case "schemaurl": case "schemaurl":
builder.withDataSchema(value); builder.withDataSchema(value);
return; return this;
} }
throw CloudEventRWException.newInvalidAttributeType(name, URI.class); throw CloudEventRWException.newInvalidAttributeType(name, URI.class);
} }
@Override @Override
public void setAttribute(String name, OffsetDateTime value) throws CloudEventRWException { public V03ToV1AttributesConverter withAttribute(String name, OffsetDateTime value) throws CloudEventRWException {
if ("time".equals(name)) { if ("time".equals(name)) {
builder.withTime(value); builder.withTime(value);
return; return this;
} }
throw CloudEventRWException.newInvalidAttributeType(name, OffsetDateTime.class); throw CloudEventRWException.newInvalidAttributeType(name, OffsetDateTime.class);
} }

View File

@ -76,11 +76,11 @@ public class MockBinaryMessageWriter extends BaseBinaryMessageReader implements
public void readAttributes(CloudEventAttributesWriter writer) throws CloudEventRWException, IllegalStateException { public void readAttributes(CloudEventAttributesWriter writer) throws CloudEventRWException, IllegalStateException {
for (Map.Entry<String, Object> e : this.attributes.entrySet()) { for (Map.Entry<String, Object> e : this.attributes.entrySet()) {
if (e.getValue() instanceof String) { if (e.getValue() instanceof String) {
writer.setAttribute(e.getKey(), (String) e.getValue()); writer.withAttribute(e.getKey(), (String) e.getValue());
} else if (e.getValue() instanceof OffsetDateTime) { } else if (e.getValue() instanceof OffsetDateTime) {
writer.setAttribute(e.getKey(), (OffsetDateTime) e.getValue()); writer.withAttribute(e.getKey(), (OffsetDateTime) e.getValue());
} else if (e.getValue() instanceof URI) { } else if (e.getValue() instanceof URI) {
writer.setAttribute(e.getKey(), (URI) e.getValue()); writer.withAttribute(e.getKey(), (URI) e.getValue());
} else { } else {
// This should never happen because we build that map only through our builders // This should never happen because we build that map only through our builders
throw new IllegalStateException("Illegal value inside attributes map: " + e); throw new IllegalStateException("Illegal value inside attributes map: " + e);
@ -92,11 +92,11 @@ public class MockBinaryMessageWriter extends BaseBinaryMessageReader implements
public void readExtensions(CloudEventExtensionsWriter visitor) throws CloudEventRWException, IllegalStateException { public void readExtensions(CloudEventExtensionsWriter visitor) throws CloudEventRWException, IllegalStateException {
for (Map.Entry<String, Object> entry : this.extensions.entrySet()) { for (Map.Entry<String, Object> entry : this.extensions.entrySet()) {
if (entry.getValue() instanceof String) { if (entry.getValue() instanceof String) {
visitor.setExtension(entry.getKey(), (String) entry.getValue()); visitor.withExtension(entry.getKey(), (String) entry.getValue());
} else if (entry.getValue() instanceof Number) { } else if (entry.getValue() instanceof Number) {
visitor.setExtension(entry.getKey(), (Number) entry.getValue()); visitor.withExtension(entry.getKey(), (Number) entry.getValue());
} else if (entry.getValue() instanceof Boolean) { } else if (entry.getValue() instanceof Boolean) {
visitor.setExtension(entry.getKey(), (Boolean) entry.getValue()); visitor.withExtension(entry.getKey(), (Boolean) entry.getValue());
} else { } else {
// This should never happen because we build that map only through our builders // This should never happen because we build that map only through our builders
throw new IllegalStateException("Illegal value inside extensions map: " + entry); throw new IllegalStateException("Illegal value inside extensions map: " + entry);
@ -116,33 +116,39 @@ public class MockBinaryMessageWriter extends BaseBinaryMessageReader implements
} }
@Override @Override
public void setAttribute(String name, String value) throws CloudEventRWException { public MockBinaryMessageWriter withAttribute(String name, String value) throws CloudEventRWException {
this.attributes.put(name, value); this.attributes.put(name, value);
return this;
} }
@Override @Override
public void setAttribute(String name, URI value) throws CloudEventRWException { public MockBinaryMessageWriter withAttribute(String name, URI value) throws CloudEventRWException {
this.attributes.put(name, value); this.attributes.put(name, value);
return this;
} }
@Override @Override
public void setAttribute(String name, OffsetDateTime value) throws CloudEventRWException { public MockBinaryMessageWriter withAttribute(String name, OffsetDateTime value) throws CloudEventRWException {
this.attributes.put(name, value); this.attributes.put(name, value);
return this;
} }
@Override @Override
public void setExtension(String name, String value) throws CloudEventRWException { public MockBinaryMessageWriter withExtension(String name, String value) throws CloudEventRWException {
this.extensions.put(name, value); this.extensions.put(name, value);
return this;
} }
@Override @Override
public void setExtension(String name, Number value) throws CloudEventRWException { public MockBinaryMessageWriter withExtension(String name, Number value) throws CloudEventRWException {
this.extensions.put(name, value); this.extensions.put(name, value);
return this;
} }
@Override @Override
public void setExtension(String name, Boolean value) throws CloudEventRWException { public MockBinaryMessageWriter withExtension(String name, Boolean value) throws CloudEventRWException {
this.extensions.put(name, value); this.extensions.put(name, value);
return this;
} }
@Override @Override

View File

@ -60,14 +60,14 @@ public class CloudEventDeserializer extends StdDeserializer<CloudEvent> {
// Read mandatory attributes // Read mandatory attributes
for (String attr : specVersion.getMandatoryAttributes()) { for (String attr : specVersion.getMandatoryAttributes()) {
if (!"specversion".equals(attr)) { if (!"specversion".equals(attr)) {
visitor.setAttribute(attr, getStringNode(this.node, this.p, attr)); visitor.withAttribute(attr, getStringNode(this.node, this.p, attr));
} }
} }
// Parse datacontenttype if any // Parse datacontenttype if any
String contentType = getOptionalStringNode(this.node, this.p, "datacontenttype"); String contentType = getOptionalStringNode(this.node, this.p, "datacontenttype");
if (contentType != null) { if (contentType != null) {
visitor.setAttribute("datacontenttype", contentType); visitor.withAttribute("datacontenttype", contentType);
} }
// Read optional attributes // Read optional attributes
@ -75,7 +75,7 @@ public class CloudEventDeserializer extends StdDeserializer<CloudEvent> {
if (!"datacontentencoding".equals(attr)) { // Skip datacontentencoding, we need it later if (!"datacontentencoding".equals(attr)) { // Skip datacontentencoding, we need it later
String val = getOptionalStringNode(this.node, this.p, attr); String val = getOptionalStringNode(this.node, this.p, attr);
if (val != null) { if (val != null) {
visitor.setAttribute(attr, val); visitor.withAttribute(attr, val);
} }
} }
} }
@ -127,16 +127,16 @@ public class CloudEventDeserializer extends StdDeserializer<CloudEvent> {
switch (extensionValue.getNodeType()) { switch (extensionValue.getNodeType()) {
case BOOLEAN: case BOOLEAN:
visitor.setExtension(extensionName, extensionValue.booleanValue()); visitor.withExtension(extensionName, extensionValue.booleanValue());
break; break;
case NUMBER: case NUMBER:
visitor.setExtension(extensionName, extensionValue.numberValue()); visitor.withExtension(extensionName, extensionValue.numberValue());
break; break;
case STRING: case STRING:
visitor.setExtension(extensionName, extensionValue.textValue()); visitor.withExtension(extensionName, extensionValue.textValue());
break; break;
default: default:
visitor.setExtension(extensionName, extensionValue.toString()); visitor.withExtension(extensionName, extensionValue.toString());
} }
}); });

View File

@ -55,37 +55,41 @@ public class CloudEventSerializer extends StdSerializer<CloudEvent> {
} }
@Override @Override
public void setAttribute(String name, String value) throws CloudEventRWException { public FieldsSerializer withAttribute(String name, String value) throws CloudEventRWException {
try { try {
gen.writeStringField(name, value); gen.writeStringField(name, value);
return this;
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@Override @Override
public void setExtension(String name, String value) throws CloudEventRWException { public FieldsSerializer withExtension(String name, String value) throws CloudEventRWException {
try { try {
gen.writeStringField(name, value); gen.writeStringField(name, value);
return this;
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@Override @Override
public void setExtension(String name, Number value) throws CloudEventRWException { public FieldsSerializer withExtension(String name, Number value) throws CloudEventRWException {
try { try {
gen.writeFieldName(name); gen.writeFieldName(name);
provider.findValueSerializer(value.getClass()).serialize(value, gen, provider); provider.findValueSerializer(value.getClass()).serialize(value, gen, provider);
return this;
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@Override @Override
public void setExtension(String name, Boolean value) throws CloudEventRWException { public FieldsSerializer withExtension(String name, Boolean value) throws CloudEventRWException {
try { try {
gen.writeBooleanField(name, value); gen.writeBooleanField(name, value);
return this;
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@ -57,13 +57,15 @@ public class HttpMessageWriter implements CloudEventWriter<Void>, MessageWriter<
} }
@Override @Override
public void setAttribute(String name, String value) throws CloudEventRWException { public HttpMessageWriter withAttribute(String name, String value) throws CloudEventRWException {
putHeader.accept(CloudEventsHeaders.ATTRIBUTES_TO_HEADERS.get(name), value); putHeader.accept(CloudEventsHeaders.ATTRIBUTES_TO_HEADERS.get(name), value);
return this;
} }
@Override @Override
public void setExtension(String name, String value) throws CloudEventRWException { public HttpMessageWriter withExtension(String name, String value) throws CloudEventRWException {
putHeader.accept("ce-" + name, value); putHeader.accept("ce-" + name, value);
return this;
} }
@Override @Override

View File

@ -46,13 +46,15 @@ public final class RestfulWSClientMessageWriter implements CloudEventWriter<Void
} }
@Override @Override
public void setAttribute(String name, String value) throws CloudEventRWException { public RestfulWSClientMessageWriter withAttribute(String name, String value) throws CloudEventRWException {
this.context.getHeaders().add(CloudEventsHeaders.ATTRIBUTES_TO_HEADERS.get(name), value); this.context.getHeaders().add(CloudEventsHeaders.ATTRIBUTES_TO_HEADERS.get(name), value);
return this;
} }
@Override @Override
public void setExtension(String name, String value) throws CloudEventRWException { public RestfulWSClientMessageWriter withExtension(String name, String value) throws CloudEventRWException {
this.context.getHeaders().add(CloudEventsHeaders.CE_PREFIX + name, value); this.context.getHeaders().add(CloudEventsHeaders.CE_PREFIX + name, value);
return this;
} }
@Override @Override

View File

@ -48,13 +48,15 @@ public final class RestfulWSMessageWriter implements CloudEventWriter<Void>, Mes
} }
@Override @Override
public void setAttribute(String name, String value) throws CloudEventRWException { public RestfulWSMessageWriter withAttribute(String name, String value) throws CloudEventRWException {
this.httpHeaders.add(CloudEventsHeaders.ATTRIBUTES_TO_HEADERS.get(name), value); this.httpHeaders.add(CloudEventsHeaders.ATTRIBUTES_TO_HEADERS.get(name), value);
return this;
} }
@Override @Override
public void setExtension(String name, String value) throws CloudEventRWException { public RestfulWSMessageWriter withExtension(String name, String value) throws CloudEventRWException {
this.httpHeaders.add(CloudEventsHeaders.CE_PREFIX + name, value); this.httpHeaders.add(CloudEventsHeaders.CE_PREFIX + name, value);
return this;
} }
@Override @Override

View File

@ -45,13 +45,15 @@ public class VertxHttpServerResponseMessageWriterImpl implements MessageWriter<C
// Binary visitor // Binary visitor
@Override @Override
public void setAttribute(String name, String value) throws CloudEventRWException { public VertxHttpServerResponseMessageWriterImpl withAttribute(String name, String value) throws CloudEventRWException {
this.response.putHeader(CloudEventsHeaders.ATTRIBUTES_TO_HEADERS.get(name), value); this.response.putHeader(CloudEventsHeaders.ATTRIBUTES_TO_HEADERS.get(name), value);
return this;
} }
@Override @Override
public void setExtension(String name, String value) throws CloudEventRWException { public VertxHttpServerResponseMessageWriterImpl withExtension(String name, String value) throws CloudEventRWException {
this.response.putHeader("ce-" + name, value); this.response.putHeader("ce-" + name, value);
return this;
} }
@Override @Override

View File

@ -24,7 +24,6 @@ import io.cloudevents.rw.CloudEventRWException;
import io.cloudevents.rw.CloudEventWriter; import io.cloudevents.rw.CloudEventWriter;
import io.vertx.core.Future; import io.vertx.core.Future;
import io.vertx.core.buffer.Buffer; import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpHeaders; import io.vertx.core.http.HttpHeaders;
import io.vertx.ext.web.client.HttpRequest; import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.HttpResponse; import io.vertx.ext.web.client.HttpResponse;
@ -48,13 +47,15 @@ public class VertxWebClientRequestMessageWriterImpl implements MessageWriter<Clo
// Binary visitor // Binary visitor
@Override @Override
public void setAttribute(String name, String value) throws CloudEventRWException { public VertxWebClientRequestMessageWriterImpl withAttribute(String name, String value) throws CloudEventRWException {
this.request.headers().add(CloudEventsHeaders.ATTRIBUTES_TO_HEADERS.get(name), value); this.request.headers().add(CloudEventsHeaders.ATTRIBUTES_TO_HEADERS.get(name), value);
return this;
} }
@Override @Override
public void setExtension(String name, String value) throws CloudEventRWException { public VertxWebClientRequestMessageWriterImpl withExtension(String name, String value) throws CloudEventRWException {
this.request.headers().add("ce-" + name, value); this.request.headers().add("ce-" + name, value);
return this;
} }
@Override @Override

View File

@ -34,13 +34,15 @@ abstract class BaseKafkaMessageWriterImpl<R> implements MessageWriter<CloudEvent
} }
@Override @Override
public void setAttribute(String name, String value) throws CloudEventRWException { public BaseKafkaMessageWriterImpl<R> withAttribute(String name, String value) throws CloudEventRWException {
headers.add(new RecordHeader(KafkaHeaders.ATTRIBUTES_TO_HEADERS.get(name), value.getBytes())); headers.add(new RecordHeader(KafkaHeaders.ATTRIBUTES_TO_HEADERS.get(name), value.getBytes()));
return this;
} }
@Override @Override
public void setExtension(String name, String value) throws CloudEventRWException { public BaseKafkaMessageWriterImpl<R> withExtension(String name, String value) throws CloudEventRWException {
headers.add(new RecordHeader(KafkaHeaders.CE_PREFIX + name, value.getBytes())); headers.add(new RecordHeader(KafkaHeaders.CE_PREFIX + name, value.getBytes()));
return this;
} }
@Override @Override

View File

@ -44,7 +44,7 @@ public final class KafkaProducerMessageWriterImpl<K>
@Override @Override
public KafkaProducerMessageWriterImpl<K> create(SpecVersion version) { public KafkaProducerMessageWriterImpl<K> create(SpecVersion version) {
this.setAttribute("specversion", version.toString()); this.withAttribute("specversion", version.toString());
return this; return this;
} }
} }

View File

@ -28,7 +28,7 @@ public final class KafkaSerializerMessageWriterImpl extends BaseKafkaMessageWrit
@Override @Override
public KafkaSerializerMessageWriterImpl create(SpecVersion version) { public KafkaSerializerMessageWriterImpl create(SpecVersion version) {
this.setAttribute("specversion", version.toString()); this.withAttribute("specversion", version.toString());
return this; return this;
} }