-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PipelineProcessor class and XContent parsing, rename package
Signed-off-by: Daniel Widdis <widdis@gmail.com>
- Loading branch information
Showing
16 changed files
with
202 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/main/java/org/opensearch/flowframework/model/PipelineProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
package org.opensearch.flowframework.model; | ||
|
||
import org.opensearch.core.xcontent.ToXContentObject; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken; | ||
|
||
/** | ||
* This represents a processor associated with search and ingest pipelines in the {@link Template}. | ||
*/ | ||
public class PipelineProcessor implements ToXContentObject { | ||
|
||
/** The type field name for pipeline processors */ | ||
public static final String TYPE_FIELD = "type"; | ||
/** The params field name for pipeline processors */ | ||
public static final String PARAMS_FIELD = "params"; | ||
|
||
private final String type; | ||
private final Map<String, String> params; | ||
|
||
/** | ||
* Create this processor with a type and map of parameters | ||
* @param type the processor type | ||
* @param params a map of params | ||
*/ | ||
public PipelineProcessor(String type, Map<String, String> params) { | ||
this.type = type; | ||
this.params = params; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
XContentBuilder xContentBuilder = builder.startObject(); | ||
xContentBuilder.field(TYPE_FIELD, this.type); | ||
xContentBuilder.field(PARAMS_FIELD); | ||
Template.buildStringToStringMap(xContentBuilder, this.params); | ||
return xContentBuilder.endObject(); | ||
} | ||
|
||
/** | ||
* Parse raw json content into a processor instance. | ||
* | ||
* @param parser json based content parser | ||
* @return the parsed PipelineProcessor instance | ||
* @throws IOException if content can't be parsed correctly | ||
*/ | ||
public static PipelineProcessor parse(XContentParser parser) throws IOException { | ||
String type = null; | ||
Map<String, String> params = new HashMap<>(); | ||
|
||
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser); | ||
while (parser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
String fieldName = parser.currentName(); | ||
parser.nextToken(); | ||
switch (fieldName) { | ||
case TYPE_FIELD: | ||
type = parser.text(); | ||
break; | ||
case PARAMS_FIELD: | ||
params = Template.parseStringToStringMap(parser); | ||
break; | ||
default: | ||
throw new IOException("Unable to parse field [" + fieldName + "] in a pipeline processor object."); | ||
} | ||
} | ||
if (type == null) { | ||
throw new IOException("A processor object requires a type field."); | ||
} | ||
|
||
return new PipelineProcessor(type, params); | ||
} | ||
|
||
/** | ||
* Get the processor type | ||
* @return the type | ||
*/ | ||
public String type() { | ||
return type; | ||
} | ||
|
||
/** | ||
* Get the processor params | ||
* @return the params | ||
*/ | ||
public Map<String, String> params() { | ||
return params; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/test/java/org/opensearch/flowframework/model/PipelineProcessorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
package org.opensearch.flowframework.model; | ||
|
||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
public class PipelineProcessorTests extends OpenSearchTestCase { | ||
|
||
public void testProcessor() throws IOException { | ||
PipelineProcessor processor = new PipelineProcessor("foo", Map.of("bar", "baz")); | ||
|
||
assertEquals("foo", processor.type()); | ||
assertEquals(Map.of("bar", "baz"), processor.params()); | ||
|
||
String expectedJson = "{\"type\":\"foo\",\"params\":{\"bar\":\"baz\"}}"; | ||
String json = TemplateTestJsonUtil.parseToJson(processor); | ||
assertEquals(expectedJson, json); | ||
|
||
PipelineProcessor processorX = PipelineProcessor.parse(TemplateTestJsonUtil.jsonToParser(json)); | ||
assertEquals("foo", processorX.type()); | ||
assertEquals(Map.of("bar", "baz"), processorX.params()); | ||
} | ||
|
||
public void testExceptions() throws IOException { | ||
String badJson = "{\"badField\":\"foo\",\"params\":{\"bar\":\"baz\"}}"; | ||
IOException e = assertThrows(IOException.class, () -> PipelineProcessor.parse(TemplateTestJsonUtil.jsonToParser(badJson))); | ||
assertEquals("Unable to parse field [badField] in a pipeline processor object.", e.getMessage()); | ||
|
||
String noTypeJson = "{\"params\":{\"bar\":\"baz\"}}"; | ||
e = assertThrows(IOException.class, () -> PipelineProcessor.parse(TemplateTestJsonUtil.jsonToParser(noTypeJson))); | ||
assertEquals("A processor object requires a type field.", e.getMessage()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters