Skip to content

Commit 9e0fc98

Browse files
opensearch-trigger-bot[bot]github-actions[bot]dbwiddisowaiskazi19
authored
[Backport 2.x] Added a new parse util method to avoid repetition / refactored code with new method (#728)
Added a new parse util method to avoid repetition / refactored code with new method (#721) * Add user mapping to Workflow State index (#705) * Add user mapping to Workflow State index * Increment schema version --------- * Added a new parse util method to avoid repetition / refactored code with new method * refactored method name and added unit test * made method use generics + added test * fixed javadoc * Added workflow step for ReIndex Step (#718) * Initial commit for reindex workflow step with extra params * Addressed PR comments * Changed request per second to Float * Addressed string array for source indices and removed state index entry * Minor comments --------- * Incorporating parseIfExist method into ReindexStep class * Add param to delete workflow API to clear status even if resources exist (#719) * refactored method to use parseBoolean and parseFloat methods * Adding a missing param in javaDoc * Added workflow step for ReIndex Step (#718) * Initial commit for reindex workflow step with extra params * Addressed PR comments * Changed request per second to Float * Addressed string array for source indices and removed state index entry * Minor comments --------- * Add param to delete workflow API to clear status even if resources exist (#719) * Added a chagelog entry * fixed failing spotless check * Added workflow step for ReIndex Step (#718) * Initial commit for reindex workflow step with extra params * Addressed PR comments * Changed request per second to Float * Addressed string array for source indices and removed state index entry * Minor comments --------- * removed unnecessary changelog info --------- (cherry picked from commit 13b32f1) Signed-off-by: Daniel Widdis <[email protected]> Signed-off-by: martinpkr <[email protected]> Signed-off-by: owaiskazi19 <[email protected]> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Daniel Widdis <[email protected]> Co-authored-by: Owais Kazi <[email protected]>
1 parent e06abcb commit 9e0fc98

File tree

7 files changed

+62
-13
lines changed

7 files changed

+62
-13
lines changed

CHANGELOG.md

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
1717
### Enhancements
1818
- Add Workflow Step for Reindex from source index to destination ([#718](https://github.com/opensearch-project/flow-framework/pull/718))
1919
- Add param to delete workflow API to clear status even if resources exist ([#719](https://github.com/opensearch-project/flow-framework/pull/719))
20-
2120
### Bug Fixes
2221
- Add user mapping to Workflow State index ([#705](https://github.com/opensearch-project/flow-framework/pull/705))
2322

src/main/java/org/opensearch/flowframework/util/ParseUtils.java

+26
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.apache.logging.log4j.LogManager;
1212
import org.apache.logging.log4j.Logger;
1313
import org.opensearch.client.Client;
14+
import org.opensearch.common.Booleans;
1415
import org.opensearch.common.io.Streams;
1516
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
1617
import org.opensearch.common.xcontent.XContentHelper;
@@ -472,4 +473,29 @@ public static Map<String, String> convertStringToObjectMapToStringToStringMap(Ma
472473
return stringToStringMap;
473474
}
474475
}
476+
477+
/**
478+
* Checks if the inputs map contains the specified key and parses the associated value to a generic class.
479+
*
480+
* @param <T> the type to which the value should be parsed
481+
* @param inputs the map containing the input data
482+
* @param key the key to check in the map
483+
* @param type the class to parse the value to
484+
* @throws IllegalArgumentException if the type is not supported
485+
* @return the generic type value associated with the key if present, or null if the key is not found
486+
*/
487+
public static <T> T parseIfExists(Map<String, Object> inputs, String key, Class<T> type) {
488+
if (!inputs.containsKey(key)) {
489+
return null;
490+
}
491+
492+
Object value = inputs.get(key);
493+
if (type == Boolean.class) {
494+
return type.cast(Booleans.parseBoolean(value.toString()));
495+
} else if (type == Float.class) {
496+
return type.cast(Float.parseFloat(value.toString()));
497+
} else {
498+
throw new IllegalArgumentException("Unsupported type: " + type);
499+
}
500+
}
475501
}

src/main/java/org/opensearch/flowframework/workflow/AbstractRegisterLocalModelStep.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.apache.logging.log4j.Logger;
1313
import org.opensearch.ExceptionsHelper;
1414
import org.opensearch.action.support.PlainActionFuture;
15-
import org.opensearch.common.Booleans;
1615
import org.opensearch.common.xcontent.XContentHelper;
1716
import org.opensearch.core.action.ActionListener;
1817
import org.opensearch.core.common.bytes.BytesArray;
@@ -123,7 +122,7 @@ public PlainActionFuture<WorkflowData> execute(
123122
String modelGroupId = (String) inputs.get(MODEL_GROUP_ID);
124123
String allConfig = (String) inputs.get(ALL_CONFIG);
125124
String modelInterface = (String) inputs.get(INTERFACE_FIELD);
126-
final Boolean deploy = inputs.containsKey(DEPLOY_FIELD) ? Booleans.parseBoolean(inputs.get(DEPLOY_FIELD).toString()) : null;
125+
final Boolean deploy = ParseUtils.parseIfExists(inputs, DEPLOY_FIELD, Boolean.class);
127126

128127
// Build register model input
129128
MLRegisterModelInputBuilder mlInputBuilder = MLRegisterModelInput.builder()

src/main/java/org/opensearch/flowframework/workflow/RegisterModelGroupStep.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.apache.logging.log4j.Logger;
1313
import org.opensearch.ExceptionsHelper;
1414
import org.opensearch.action.support.PlainActionFuture;
15-
import org.opensearch.common.Booleans;
1615
import org.opensearch.core.action.ActionListener;
1716
import org.opensearch.core.common.util.CollectionUtils;
1817
import org.opensearch.core.rest.RestStatus;
@@ -147,9 +146,7 @@ public void onFailure(Exception ex) {
147146
if (inputs.containsKey(MODEL_ACCESS_MODE)) {
148147
modelAccessMode = AccessMode.from((inputs.get(MODEL_ACCESS_MODE)).toString().toLowerCase(Locale.ROOT));
149148
}
150-
Boolean isAddAllBackendRoles = inputs.containsKey(ADD_ALL_BACKEND_ROLES)
151-
? Booleans.parseBoolean(inputs.get(ADD_ALL_BACKEND_ROLES).toString())
152-
: null;
149+
Boolean isAddAllBackendRoles = ParseUtils.parseIfExists(inputs, ADD_ALL_BACKEND_ROLES, Boolean.class);
153150

154151
MLRegisterModelGroupInputBuilder builder = MLRegisterModelGroupInput.builder();
155152
builder.name(modelGroupName);

src/main/java/org/opensearch/flowframework/workflow/RegisterRemoteModelStep.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.opensearch.ExceptionsHelper;
1414
import org.opensearch.action.support.PlainActionFuture;
1515
import org.opensearch.action.update.UpdateResponse;
16-
import org.opensearch.common.Booleans;
1716
import org.opensearch.common.xcontent.XContentHelper;
1817
import org.opensearch.core.action.ActionListener;
1918
import org.opensearch.core.common.bytes.BytesArray;
@@ -100,7 +99,7 @@ public PlainActionFuture<WorkflowData> execute(
10099
String connectorId = (String) inputs.get(CONNECTOR_ID);
101100
Guardrails guardRails = (Guardrails) inputs.get(GUARDRAILS_FIELD);
102101
String modelInterface = (String) inputs.get(INTERFACE_FIELD);
103-
final Boolean deploy = inputs.containsKey(DEPLOY_FIELD) ? Booleans.parseBoolean(inputs.get(DEPLOY_FIELD).toString()) : null;
102+
final Boolean deploy = ParseUtils.parseIfExists(inputs, DEPLOY_FIELD, Boolean.class);
104103

105104
MLRegisterModelInputBuilder builder = MLRegisterModelInput.builder()
106105
.functionName(FunctionName.REMOTE)

src/main/java/org/opensearch/flowframework/workflow/ToolStep.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.apache.logging.log4j.LogManager;
1212
import org.apache.logging.log4j.Logger;
1313
import org.opensearch.action.support.PlainActionFuture;
14-
import org.opensearch.common.Booleans;
1514
import org.opensearch.core.rest.RestStatus;
1615
import org.opensearch.flowframework.exception.FlowFrameworkException;
1716
import org.opensearch.flowframework.exception.WorkflowStepException;
@@ -64,9 +63,7 @@ public PlainActionFuture<WorkflowData> execute(
6463
String type = (String) inputs.get(TYPE);
6564
String name = (String) inputs.get(NAME_FIELD);
6665
String description = (String) inputs.get(DESCRIPTION_FIELD);
67-
Boolean includeOutputInAgentResponse = inputs.containsKey(INCLUDE_OUTPUT_IN_AGENT_RESPONSE)
68-
? Booleans.parseBoolean(inputs.get(INCLUDE_OUTPUT_IN_AGENT_RESPONSE).toString())
69-
: null;
66+
Boolean includeOutputInAgentResponse = ParseUtils.parseIfExists(inputs, INCLUDE_OUTPUT_IN_AGENT_RESPONSE, Boolean.class);
7067
Map<String, String> parameters = getToolsParametersMap(inputs.get(PARAMETERS_FIELD), previousNodeInputs, outputs);
7168

7269
MLToolSpec.MLToolSpecBuilder builder = MLToolSpec.builder();

src/test/java/org/opensearch/flowframework/util/ParseUtilsTests.java

+32
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,36 @@ public void testGetInputsFromPreviousSteps() {
221221
assertEquals("Missing required inputs [not-here] in workflow [workflowId] node [nodeId]", e.getMessage());
222222
assertEquals(RestStatus.BAD_REQUEST, e.getRestStatus());
223223
}
224+
225+
public void testParseIfExistsWithBooleanClass() {
226+
Map<String, Object> inputs = new HashMap<>();
227+
inputs.put("key1", "true");
228+
inputs.put("key2", "false");
229+
inputs.put("key3", "true");
230+
231+
assertEquals(Boolean.TRUE, ParseUtils.parseIfExists(inputs, "key1", Boolean.class));
232+
assertEquals(Boolean.FALSE, ParseUtils.parseIfExists(inputs, "key2", Boolean.class));
233+
assertNull(ParseUtils.parseIfExists(inputs, "keyThatDoesntExist", Boolean.class));
234+
235+
}
236+
237+
public void testParseIfExistsWithFloatClass() {
238+
Map<String, Object> inputs = new HashMap<>();
239+
inputs.put("key1", "3.14");
240+
inputs.put("key2", "0.01");
241+
inputs.put("key3", "90.22");
242+
243+
assertEquals(Float.valueOf("3.14"), ParseUtils.parseIfExists(inputs, "key1", Float.class));
244+
assertEquals(Float.valueOf("0.01"), ParseUtils.parseIfExists(inputs, "key2", Float.class));
245+
assertNull(ParseUtils.parseIfExists(inputs, "keyThatDoesntExist", Float.class));
246+
247+
}
248+
249+
public void testParseIfExistWhenWrongTypeIsPassed() {
250+
251+
Map<String, Object> inputs = new HashMap<>();
252+
inputs.put("key1", "3.14");
253+
254+
assertThrows(IllegalArgumentException.class, () -> ParseUtils.parseIfExists(inputs, "key1", Integer.class));
255+
}
224256
}

0 commit comments

Comments
 (0)