use of io.temporal.common.converter.DataConverter in project sdk-java by temporalio.
the class InternalUtils method convertMapToSearchAttributes.
public static SearchAttributes convertMapToSearchAttributes(Map<String, Object> searchAttributes) {
DataConverter converter = DataConverter.getDefaultInstance();
Map<String, Payload> mapOfByteBuffer = new HashMap<>();
searchAttributes.forEach((key, value) -> mapOfByteBuffer.put(key, converter.toPayload(value).get()));
return SearchAttributes.newBuilder().putAllIndexedFields(mapOfByteBuffer).build();
}
use of io.temporal.common.converter.DataConverter in project sdk-java by temporalio.
the class WorkflowContextTest method TestMergeSearchAttributes.
@Test
public void TestMergeSearchAttributes() {
WorkflowExecutionStartedEventAttributes startAttr = WorkflowExecutionStartedEventAttributes.getDefaultInstance();
WorkflowContext workflowContext = new WorkflowContext("namespace", null, startAttr, 0, null);
DataConverter converter = DataConverter.getDefaultInstance();
Map<String, Payload> indexedFields = new HashMap<>();
indexedFields.put("CustomKeywordField", converter.toPayload("key").get());
SearchAttributes searchAttributes = SearchAttributes.newBuilder().putAllIndexedFields(indexedFields).build();
workflowContext.mergeSearchAttributes(searchAttributes);
assertEquals("key", SearchAttributesUtil.getValueFromSearchAttributes(workflowContext.getSearchAttributes(), "CustomKeywordField", String.class));
}
use of io.temporal.common.converter.DataConverter in project sdk-java by temporalio.
the class SyncWorkflowContext method sideEffect.
@Override
public <R> R sideEffect(Class<R> resultClass, Type resultType, Func<R> func) {
try {
DataConverter dataConverter = getDataConverter();
CompletablePromise<Optional<Payloads>> result = Workflow.newPromise();
context.sideEffect(() -> {
R r = func.apply();
return dataConverter.toPayloads(r);
}, (p) -> runner.executeInWorkflowThread("side-effect-callback", () -> result.complete(Objects.requireNonNull(p))));
return dataConverter.fromPayloads(0, result.get(), resultClass, resultType);
} catch (Exception e) {
// fail the workflow task by throwing an Error.
throw new Error(e);
}
}
use of io.temporal.common.converter.DataConverter in project sdk-java by temporalio.
the class SearchAttributesTest method testSearchAttributes.
@Test
public void testSearchAttributes() {
if (testWorkflowRule.isUseExternalService()) {
// LocalDateTime fails to deserialize in the real service, with a message like
// INVALID_ARGUMENT: 2021-08-26T13:21:52.059738 is not a valid value for search attribute
// CustomDatetimeField of type Datetime
// Tracked in https://github.com/temporalio/sdk-java/issues/673
searchAttributes.remove(testKeyDateTime);
}
WorkflowOptions workflowOptions = SDKTestOptions.newWorkflowOptionsWithTimeouts(testWorkflowRule.getTaskQueue()).toBuilder().setSearchAttributes(searchAttributes).build();
TestNoArgsWorkflowFunc stubF = testWorkflowRule.getWorkflowClient().newWorkflowStub(TestNoArgsWorkflowFunc.class, workflowOptions);
WorkflowExecution executionF = WorkflowClient.start(stubF::func);
GetWorkflowExecutionHistoryResponse historyResp = WorkflowClientHelper.getHistoryPage(testWorkflowRule.getTestEnvironment().getWorkflowService(), SDKTestWorkflowRule.NAMESPACE, executionF, ByteString.EMPTY, new NoopScope());
HistoryEvent startEvent = historyResp.getHistory().getEvents(0);
SearchAttributes searchAttrFromEvent = startEvent.getWorkflowExecutionStartedEventAttributes().getSearchAttributes();
Map<String, Payload> fieldsMap = searchAttrFromEvent.getIndexedFieldsMap();
Payload searchAttrStringBytes = fieldsMap.get(testKeyString);
DataConverter converter = DataConverter.getDefaultInstance();
String retrievedString = converter.fromPayload(searchAttrStringBytes, String.class, String.class);
assertEquals(testValueString, retrievedString);
Payload searchAttrIntegerBytes = fieldsMap.get(testKeyInteger);
Integer retrievedInteger = converter.fromPayload(searchAttrIntegerBytes, Integer.class, Integer.class);
assertEquals(testValueInteger, retrievedInteger);
Payload searchAttrDateTimeBytes = fieldsMap.get(testKeyDateTime);
if (!testWorkflowRule.isUseExternalService()) {
LocalDateTime retrievedDateTime = converter.fromPayload(searchAttrDateTimeBytes, LocalDateTime.class, LocalDateTime.class);
assertEquals(testValueDateTime, retrievedDateTime);
}
Payload searchAttrBoolBytes = fieldsMap.get(testKeyBool);
Boolean retrievedBool = converter.fromPayload(searchAttrBoolBytes, Boolean.class, Boolean.class);
assertEquals(testValueBool, retrievedBool);
Payload searchAttrDoubleBytes = fieldsMap.get(testKeyDouble);
Double retrievedDouble = converter.fromPayload(searchAttrDoubleBytes, Double.class, Double.class);
assertEquals(testValueDouble, retrievedDouble);
}
use of io.temporal.common.converter.DataConverter in project samples-java by temporalio.
the class HelloSearchAttributes method getKeywordFromSearchAttribute.
// example for extracting a value from search attributes
private static String getKeywordFromSearchAttribute(SearchAttributes searchAttributes) {
Payload field = searchAttributes.getIndexedFieldsOrThrow("CustomKeywordField");
DataConverter dataConverter = DataConverter.getDefaultInstance();
return dataConverter.fromPayload(field, String.class, String.class);
}
Aggregations