-
Design of a new parser API for adaptersIn this discussion, we will explore how to design the new parser API. Disadvantages of the current API:
Current API DesignThe current design delegates the parsing and publishing of events to the parser. Parser Interfacepublic interface Parser {
...
void parse(InputStream inputStream, IEventCollector collector) throws ParseException;
...
} Adapterpublic class ActualAdapter implements AdapterInterface {
...
@Override
public void onAdapterStarted(IAdapterParameterExtractor extractor, IEventCollector collector,
IAdapterRuntimeContext adapterRuntimeContext) throws AdapterException {
InputStream inputStream = // create inputStream from data
extractor.selectedParser().parse(inputStream, collector);
}
...
} Design 1: Iterator PatternOne possible improvement is to use the Iterator pattern to give the adapter more control over the actual events. Parser Interfacepublic interface Parser {
...
void init(InputStream inputStream);
boolean hasNext();
Map<String, Object> next();
...
} Adapterpublic class ActualAdapter implements AdapterInterface {
@Override
public void onAdapterStarted(IAdapterParameterExtractor extractor, IEventCollector collector,
IAdapterRuntimeContext adapterRuntimeContext) throws AdapterException {
...
InputStream inputStream = // create inputStream from data
var parser = extractor.selectedParser();
parser.init(inputStream);
while(parser.hasNext()) {
try {
var event = parser.next();
collector.collect(event);
} catch (ParseException e) {
// LOG exception
}
}
}
...
} Design 2: CallbackAnother possible improvement is to use a callback to publish the events. Parser Interfacepublic interface Parser {
...
void parse(InputStream inputStream, EventHandler eventHandler) throws ParseException;
...
}
public interface EventHandler {
void handle(Map<String, Object> event);
} Adapterpublic class ActualAdapter implements AdapterInterface {
...
@Override
public void onAdapterStarted(IAdapterParameterExtractor extractor, IEventCollector collector,
IAdapterRuntimeContext adapterRuntimeContext) throws AdapterException {
InputStream inputStream = // create inputStream from data
extractor.selectedParser().parse(inputStream, (Map<String, Object> event) -> {
collector.collect(event);
});
}
...
} Question
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Hi @tenthe, I prefer Design 2, as it seems to be easily understandable by developers of adapters. |
Beta Was this translation helpful? Give feedback.
-
For option 2, we would need two interfaces that publish events, one for the event collector that sends the events to the adapter's preprocessing pipeline, and one for the new event handler. public interface IEventHandler {
void handle(Map<String, Object> event) throws ParseException;
} public interface IEventCollector {
void collect(Map<String, Object> event);
} The interfaces have a different meaning, but they look the same. Do you think that can be confusing? |
Beta Was this translation helpful? Give feedback.
-
Thanks for your feedback @bossenti. |
Beta Was this translation helpful? Give feedback.
Thanks for your feedback @bossenti.
I have started to implement option 2. Once the branch is ready, you can take a look at it and we can discuss if it is confusing to use or if we need to adjust anything.