Skip to content

Commit

Permalink
Adds the EventKey and EventKeyFactory. (opensearch-project#4627)
Browse files Browse the repository at this point in the history
Adds the EventKey and EventKeyFactory. Resolves opensearch-project#1916.

Signed-off-by: David Venable <dlv@amazon.com>
  • Loading branch information
dlvenable committed Jun 17, 2024
1 parent 67a027c commit 52d2f0e
Show file tree
Hide file tree
Showing 23 changed files with 1,268 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@
*/
public interface Event extends Serializable {

/**
* Adds or updates the key with a given value in the Event
*
* @param key where the value will be set
* @param value value to set the key to
* @since 2.8
*/
void put(EventKey key, Object value);

/**
* Adds or updates the key with a given value in the Event
*
Expand All @@ -35,6 +44,17 @@ public interface Event extends Serializable {
*/
void put(String key, Object value);

/**
* Retrieves the given key from the Event
*
* @param key the value to retrieve from
* @param clazz the return type of the value
* @param <T> The type
* @return T a clazz object from the key
* @since 2.8
*/
<T> T get(EventKey key, Class<T> clazz);

/**
* Retrieves the given key from the Event
*
Expand All @@ -46,6 +66,17 @@ public interface Event extends Serializable {
*/
<T> T get(String key, Class<T> clazz);

/**
* Retrieves the given key from the Event as a List
*
* @param key the value to retrieve from
* @param clazz the return type of elements in the list
* @param <T> The type
* @return {@literal List<T>} a list of clazz elements
* @since 2.8
*/
<T> List<T> getList(EventKey key, Class<T> clazz);

/**
* Retrieves the given key from the Event as a List
*
Expand All @@ -57,6 +88,14 @@ public interface Event extends Serializable {
*/
<T> List<T> getList(String key, Class<T> clazz);

/**
* Deletes the given key from the Event
*
* @param key the field to be deleted
* @since 2.8
*/
void delete(EventKey key);

/**
* Deletes the given key from the Event
*
Expand Down Expand Up @@ -87,6 +126,15 @@ public interface Event extends Serializable {
*/
JsonNode getJsonNode();

/**
* Gets a serialized Json string of the specific key in the Event
*
* @param key the field to be returned
* @return Json string of the field
* @since 2.8
*/
String getAsJsonString(EventKey key);

/**
* Gets a serialized Json string of the specific key in the Event
*
Expand All @@ -104,6 +152,15 @@ public interface Event extends Serializable {
*/
EventMetadata getMetadata();

/**
* Checks if the key exists.
*
* @param key name of the key to look for
* @return returns true if the key exists, otherwise false
* @since 2.8
*/
boolean containsKey(EventKey key);

/**
* Checks if the key exists.
*
Expand All @@ -113,6 +170,15 @@ public interface Event extends Serializable {
*/
boolean containsKey(String key);

/**
* Checks if the value stored for the key is list
*
* @param key name of the key to look for
* @return returns true if the key is a list, otherwise false
* @since 2.8
*/
boolean isValueAList(EventKey key);

/**
* Checks if the value stored for the key is list
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.dataprepper.model.event;

/**
* Model class to represent a key into a Data Prepper {@link Event}.
*
* @since 2.9
*/
public interface EventKey {
/**
* The original key provided as a string.
*
* @return The key as a string
* @since 2.9
*/
String getKey();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.dataprepper.model.event;

import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;

/**
* A factory for producing {@link EventKey} objects.
*
* @since 2.9
*/
public interface EventKeyFactory {
/**
* Creates an {@link EventKey} with given actions.
*
* @param key The key
* @param forActions Actions to support
* @return The EventKey
* @since 2.9
*/
EventKey createEventKey(String key, EventAction... forActions);

/**
* Creates an {@link EventKey} for the default actions, which are all.
*
* @param key The key
* @return The EventKey
* @since 2.9
*/
default EventKey createEventKey(final String key) {
return createEventKey(key, EventAction.ALL);
}

/**
* An action on an Event.
*
* @since 2.9
*/
enum EventAction {
GET,
DELETE,
PUT,
ALL(GET, DELETE, PUT);

private final List<EventAction> includedActions;

EventAction(EventAction... eventActions) {
includedActions = Arrays.asList(eventActions);

}

boolean isMutableAction() {
return this != GET;
}

Set<EventAction> getSupportedActions() {
final EnumSet<EventAction> supportedActions = EnumSet.noneOf(EventAction.class);
supportedActions.add(this);
supportedActions.addAll(includedActions);

return Collections.unmodifiableSet(supportedActions);
}
}
}
Loading

0 comments on commit 52d2f0e

Please sign in to comment.