Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package io.temporal.internal.client;

import static io.temporal.internal.common.HeaderUtils.convertMapFromObjectToBytes;
import static io.temporal.internal.common.HeaderUtils.intoPayloadMapWithDefaultConverter;
import static io.temporal.internal.common.HeaderUtils.toHeaderGrpc;
import static io.temporal.internal.common.SerializerUtils.toRetryPolicy;

Expand Down Expand Up @@ -84,12 +84,13 @@ StartWorkflowExecutionRequest newStartWorkflowExecutionRequest(
request.setCronSchedule(options.getCronSchedule());
}
if (options.getMemo() != null) {
request.setMemo(Memo.newBuilder().putAllFields(convertFromObjectToBytes(options.getMemo())));
request.setMemo(
Memo.newBuilder().putAllFields(intoPayloadMapWithDefaultConverter(options.getMemo())));
}
if (options.getSearchAttributes() != null) {
request.setSearchAttributes(
SearchAttributes.newBuilder()
.putAllIndexedFields(convertFromObjectToBytes(options.getSearchAttributes())));
.putAllIndexedFields(intoPayloadMapWithDefaultConverter(options.getSearchAttributes())));
}

Header grpcHeader =
Expand All @@ -100,10 +101,6 @@ StartWorkflowExecutionRequest newStartWorkflowExecutionRequest(
return request.build();
}

private Map<String, Payload> convertFromObjectToBytes(Map<String, Object> map) {
return convertMapFromObjectToBytes(map, clientOptions.getDataConverter());
}

private io.temporal.common.interceptors.Header extractContextsAndConvertToBytes(
List<ContextPropagator> contextPropagators) {
if (contextPropagators == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ public static Header toHeaderGrpc(
return builder.build();
}

public static Map<String, Payload> convertMapFromObjectToBytes(
Map<String, Object> map, DataConverter dataConverter) {
/*
* Converts a Map<String, Object> into a Map<String, Payload> by applying default data converter on each value.
* Note that this does not use user defined converters and should be used only for things like search attributes and
* memo that need to be converted back from bytes on the server.
*/
public static Map<String, Payload> intoPayloadMapWithDefaultConverter(Map<String, Object> map) {
if (map == null) {
return null;
}
DataConverter dataConverter = DataConverter.getDefaultInstance();
Map<String, Payload> result = new HashMap<>();
for (Map.Entry<String, Object> item : map.entrySet()) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public static Object getValueOrDefault(Object value, Class<?> valueClass) {
}

public static SearchAttributes convertMapToSearchAttributes(
Map<String, Object> searchAttributes, DataConverter converter) {
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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package io.temporal.internal.sync;

import static io.temporal.internal.common.HeaderUtils.convertMapFromObjectToBytes;
import static io.temporal.internal.common.HeaderUtils.intoPayloadMapWithDefaultConverter;
import static io.temporal.internal.common.HeaderUtils.toHeaderGrpc;
import static io.temporal.internal.common.SerializerUtils.toRetryPolicy;

Expand Down Expand Up @@ -732,15 +732,13 @@ public void continueAsNew(ContinueAsNewInput input) {
}
Map<String, Object> memo = ops.getMemo();
if (memo != null) {
attributes.setMemo(
Memo.newBuilder().putAllFields(convertMapFromObjectToBytes(memo, getDataConverter())));
attributes.setMemo(Memo.newBuilder().putAllFields(intoPayloadMapWithDefaultConverter(memo)));
}
Map<String, Object> searchAttributes = ops.getSearchAttributes();
if (searchAttributes != null) {
attributes.setSearchAttributes(
SearchAttributes.newBuilder()
.putAllIndexedFields(
convertMapFromObjectToBytes(searchAttributes, getDataConverter())));
.putAllIndexedFields(intoPayloadMapWithDefaultConverter(searchAttributes)));
}
}
Optional<Payloads> payloads = getDataConverter().toPayloads(input.getArgs());
Expand Down Expand Up @@ -790,8 +788,7 @@ public void upsertSearchAttributes(Map<String, Object> searchAttributes) {
throw new IllegalArgumentException("Empty search attributes");
}

SearchAttributes attr =
InternalUtils.convertMapToSearchAttributes(searchAttributes, getDataConverter());
SearchAttributes attr = InternalUtils.convertMapToSearchAttributes(searchAttributes);
context.upsertSearchAttributes(attr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static junit.framework.TestCase.assertEquals;

import io.temporal.api.common.v1.SearchAttributes;
import io.temporal.common.converter.DataConverter;
import io.temporal.common.converter.DataConverterException;
import java.io.FileOutputStream;
import java.util.HashMap;
Expand All @@ -37,8 +36,7 @@ public void testConvertMapToSearchAttributes() throws Throwable {
String value = "keyword";
attr.put("CustomKeywordField", value);

SearchAttributes result =
InternalUtils.convertMapToSearchAttributes(attr, DataConverter.getDefaultInstance());
SearchAttributes result = InternalUtils.convertMapToSearchAttributes(attr);
assertEquals(
value,
SearchAttributesUtil.getValueFromSearchAttributes(
Expand All @@ -49,6 +47,6 @@ public void testConvertMapToSearchAttributes() throws Throwable {
public void testConvertMapToSearchAttributesException() throws Throwable {
Map<String, Object> attr = new HashMap<>();
attr.put("InvalidValue", new FileOutputStream("dummy"));
InternalUtils.convertMapToSearchAttributes(attr, DataConverter.getDefaultInstance());
InternalUtils.convertMapToSearchAttributes(attr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static junit.framework.TestCase.assertEquals;

import io.temporal.api.common.v1.SearchAttributes;
import io.temporal.common.converter.DataConverter;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
Expand All @@ -47,8 +46,7 @@ public void TestGetValueFromSearchAttributes() {
attr.put("CustomDoubleField", doubleVal);
Boolean boolVal = Boolean.TRUE;
attr.put("CustomBooleanField", boolVal);
SearchAttributes searchAttributes =
InternalUtils.convertMapToSearchAttributes(attr, DataConverter.getDefaultInstance());
SearchAttributes searchAttributes = InternalUtils.convertMapToSearchAttributes(attr);

assertEquals(
stringVal,
Expand All @@ -73,8 +71,7 @@ public void TestGetValueFromSearchAttributesRepeated() {
Map<String, Object> attr = new HashMap<>();
String stringVal = "keyword";
attr.put("CustomKeywordField", stringVal);
SearchAttributes searchAttributes =
InternalUtils.convertMapToSearchAttributes(attr, DataConverter.getDefaultInstance());
SearchAttributes searchAttributes = InternalUtils.convertMapToSearchAttributes(attr);
assertEquals(
stringVal,
SearchAttributesUtil.getValueFromSearchAttributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public void setUp() {
public void testUpsertSearchAttributes() throws Throwable {
Map<String, Object> attr = new HashMap<>();
attr.put("CustomKeywordField", "keyword");
SearchAttributes serializedAttr =
InternalUtils.convertMapToSearchAttributes(attr, DataConverter.getDefaultInstance());
SearchAttributes serializedAttr = InternalUtils.convertMapToSearchAttributes(attr);

context.upsertSearchAttributes(attr);
verify(mockReplayWorkflowContext, times(1)).upsertSearchAttributes(serializedAttr);
Expand Down