Skip to content

Commit

Permalink
Addressing PR comments, moving some common template parsing methods t…
Browse files Browse the repository at this point in the history
…o a common TemplateUtil class

Signed-off-by: Joshua Palis <jpalis@amazon.com>
  • Loading branch information
joshpalis committed Oct 12, 2023
1 parent 4d96e50 commit 8200441
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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.common;

import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.xcontent.NamedXContentRegistry;
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 java.util.Map.Entry;

import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;

/**
* Utility methods for Template parsing
*/
public class TemplateUtil {

Check warning on line 27 in src/main/java/org/opensearch/flowframework/common/TemplateUtil.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/common/TemplateUtil.java#L27

Added line #L27 was not covered by tests

/**
* Converts a JSON string into an XContentParser
*
* @param json the json string
* @return The XContent parser for the json string
* @throws IOException on failure to create the parser
*/
public static XContentParser jsonToParser(String json) throws IOException {
XContentParser parser = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
json
);
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
return parser;
}

/**
* Builds an XContent object representing a map of String keys to String values.
*
* @param xContentBuilder An XContent builder whose position is at the start of the map object to build
* @param map A map as key-value String pairs.
* @throws IOException on a build failure
*/
public static void buildStringToStringMap(XContentBuilder xContentBuilder, Map<?, ?> map) throws IOException {
xContentBuilder.startObject();
for (Entry<?, ?> e : map.entrySet()) {
xContentBuilder.field((String) e.getKey(), (String) e.getValue());
}
xContentBuilder.endObject();
}

/**
* Parses an XContent object representing a map of String keys to String values.
*
* @param parser An XContent parser whose position is at the start of the map object to parse
* @return A map as identified by the key-value pairs in the XContent
* @throws IOException on a parse failure
*/
public static Map<String, String> parseStringToStringMap(XContentParser parser) throws IOException {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
Map<String, String> map = new HashMap<>();
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
String fieldName = parser.currentName();
parser.nextToken();
map.put(fieldName, parser.text());
}
return map;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.Map;

import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.flowframework.common.TemplateUtil.buildStringToStringMap;
import static org.opensearch.flowframework.common.TemplateUtil.parseStringToStringMap;

/**
* This represents a processor associated with search and ingest pipelines in the {@link Template}.
Expand Down Expand Up @@ -46,7 +48,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
XContentBuilder xContentBuilder = builder.startObject();
xContentBuilder.field(TYPE_FIELD, this.type);
xContentBuilder.field(PARAMS_FIELD);
Template.buildStringToStringMap(xContentBuilder, this.params);
buildStringToStringMap(xContentBuilder, this.params);
return xContentBuilder.endObject();
}

Expand All @@ -70,7 +72,7 @@ public static PipelineProcessor parse(XContentParser parser) throws IOException
type = parser.text();
break;
case PARAMS_FIELD:
params = Template.parseStringToStringMap(parser);
params = parseStringToStringMap(parser);
break;
default:
throw new IOException("Unable to parse field [" + fieldName + "] in a pipeline processor object.");
Expand Down
52 changes: 2 additions & 50 deletions src/main/java/org/opensearch/flowframework/model/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.Map.Entry;

import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.flowframework.common.TemplateUtil.jsonToParser;
import static org.opensearch.flowframework.common.TemplateUtil.parseStringToStringMap;

/**
* The Template is the central data structure which configures workflows. This object is used to parse JSON communicated via REST API.
Expand Down Expand Up @@ -522,23 +524,6 @@ public static Template parse(XContentParser parser) throws IOException {
);
}

/**
* Converts a JSON string into an XContentParser
*
* @param json the json string
* @return The XContent parser for the json string
* @throws IOException on failure to create the parser
*/
public static XContentParser jsonToParser(String json) throws IOException {
XContentParser parser = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
json
);
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
return parser;
}

/**
* Parse a JSON use case template
*
Expand All @@ -556,39 +541,6 @@ public static Template parse(String json) throws IOException {
return parse(parser);
}

/**
* Builds an XContent object representing a map of String keys to String values.
*
* @param xContentBuilder An XContent builder whose position is at the start of the map object to build
* @param map A map as key-value String pairs.
* @throws IOException on a build failure
*/
public static void buildStringToStringMap(XContentBuilder xContentBuilder, Map<?, ?> map) throws IOException {
xContentBuilder.startObject();
for (Entry<?, ?> e : map.entrySet()) {
xContentBuilder.field((String) e.getKey(), (String) e.getValue());
}
xContentBuilder.endObject();
}

/**
* Parses an XContent object representing a map of String keys to String values.
*
* @param parser An XContent parser whose position is at the start of the map object to parse
* @return A map as identified by the key-value pairs in the XContent
* @throws IOException on a parse failure
*/
public static Map<String, String> parseStringToStringMap(XContentParser parser) throws IOException {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
Map<String, String> map = new HashMap<>();
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
String fieldName = parser.currentName();
parser.nextToken();
map.put(fieldName, parser.text());
}
return map;
}

/**
* Output this object in a compact JSON string.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.Objects;

import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.flowframework.common.TemplateUtil.buildStringToStringMap;
import static org.opensearch.flowframework.common.TemplateUtil.parseStringToStringMap;

/**
* This represents a process node (step) in a workflow graph in the {@link Template}.
Expand Down Expand Up @@ -75,7 +77,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (e.getValue() instanceof String) {
xContentBuilder.value(e.getValue());
} else if (e.getValue() instanceof Map<?, ?>) {
Template.buildStringToStringMap(xContentBuilder, (Map<?, ?>) e.getValue());
buildStringToStringMap(xContentBuilder, (Map<?, ?>) e.getValue());
} else if (e.getValue() instanceof Object[]) {
xContentBuilder.startArray();
if (PROCESSORS_FIELD.equals(e.getKey())) {
Expand All @@ -84,7 +86,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
}
} else {
for (Map<?, ?> map : (Map<?, ?>[]) e.getValue()) {
Template.buildStringToStringMap(xContentBuilder, map);
buildStringToStringMap(xContentBuilder, map);
}
}
xContentBuilder.endArray();
Expand Down Expand Up @@ -127,7 +129,7 @@ public static WorkflowNode parse(XContentParser parser) throws IOException {
inputs.put(inputFieldName, parser.text());
break;
case START_OBJECT:
inputs.put(inputFieldName, Template.parseStringToStringMap(parser));
inputs.put(inputFieldName, parseStringToStringMap(parser));
break;
case START_ARRAY:
if (PROCESSORS_FIELD.equals(inputFieldName)) {
Expand All @@ -139,7 +141,7 @@ public static WorkflowNode parse(XContentParser parser) throws IOException {
} else {
List<Map<String, String>> mapList = new ArrayList<>();
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
mapList.add(Template.parseStringToStringMap(parser));
mapList.add(parseStringToStringMap(parser));
}
inputs.put(inputFieldName, mapList.toArray(new Map[0]));
}
Expand Down

0 comments on commit 8200441

Please sign in to comment.