Skip to content

Commit

Permalink
add null check to proto mappers (#681)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkolodezny authored Jul 21, 2022
1 parent 58c195d commit d8ff2af
Show file tree
Hide file tree
Showing 8 changed files with 447 additions and 395 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
*/
package com.uber.cadence.internal.compatibility.proto;

import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import com.google.protobuf.ByteString;
import com.google.protobuf.DoubleValue;
import com.google.protobuf.Duration;
import com.google.protobuf.FieldMask;
import com.google.protobuf.Timestamp;
import com.google.protobuf.util.Durations;
import com.google.protobuf.util.Timestamps;
import java.util.Collections;
import java.util.List;
import java.util.Map;

class Helpers {

Expand Down Expand Up @@ -50,6 +54,18 @@ static Duration daysToDuration(int days) {
return Durations.fromDays(days);
}

static Map toNonNull(Map t) {
return MoreObjects.firstNonNull(t, Collections.emptyMap());
}

static String toNonNull(String t) {
return Strings.nullToEmpty(t);
}

static boolean toNonNull(boolean t) {
return MoreObjects.firstNonNull(t, false);
}

static ByteString arrayToByteString(byte[] t) {
if (t == null) {
return null;
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static com.uber.cadence.internal.compatibility.proto.Helpers.daysToDuration;
import static com.uber.cadence.internal.compatibility.proto.Helpers.newFieldMask;
import static com.uber.cadence.internal.compatibility.proto.Helpers.secondsToDuration;
import static com.uber.cadence.internal.compatibility.proto.Helpers.toNonNull;
import static com.uber.cadence.internal.compatibility.proto.TypeMapper.badBinaries;
import static com.uber.cadence.internal.compatibility.proto.TypeMapper.clusterReplicationConfigurationArray;
import static com.uber.cadence.internal.compatibility.proto.TypeMapper.failure;
Expand All @@ -49,7 +50,6 @@
import static com.uber.cadence.internal.compatibility.proto.TypeMapper.workflowType;
import static com.uber.cadence.internal.compatibility.proto.TypeMapper.workflowTypeFilter;

import com.google.protobuf.ByteString;
import com.uber.cadence.DomainConfiguration;
import com.uber.cadence.DomainReplicationConfiguration;
import com.uber.cadence.UpdateDomainInfo;
Expand Down Expand Up @@ -362,28 +362,29 @@ public static SignalWithStartWorkflowExecutionRequest signalWithStartWorkflowExe
if (t == null) {
return null;
}
StartWorkflowExecutionRequest.Builder builder =
StartWorkflowExecutionRequest.newBuilder()
.setDomain(t.getDomain())
.setWorkflowId(t.getWorkflowId())
.setWorkflowType(workflowType(t.getWorkflowType()))
.setTaskList(taskList(t.getTaskList()))
.setInput(payload(t.getInput()))
.setExecutionStartToCloseTimeout(
secondsToDuration(t.getExecutionStartToCloseTimeoutSeconds()))
.setTaskStartToCloseTimeout(secondsToDuration(t.getTaskStartToCloseTimeoutSeconds()))
.setIdentity(t.getIdentity())
.setRequestId(t.getRequestId())
.setWorkflowIdReusePolicy(workflowIdReusePolicy(t.getWorkflowIdReusePolicy()))
.setCronSchedule(t.getCronSchedule())
.setMemo(memo(t.getMemo()))
.setSearchAttributes(searchAttributes(t.getSearchAttributes()))
.setHeader(header(t.getHeader()))
.setDelayStart(secondsToDuration(t.getDelayStartSeconds()));
if (t.getRetryPolicy() != null) {
builder.setRetryPolicy(retryPolicy(t.getRetryPolicy()));
}
return SignalWithStartWorkflowExecutionRequest.newBuilder()
.setStartRequest(
StartWorkflowExecutionRequest.newBuilder()
.setDomain(t.getDomain())
.setWorkflowId(t.getWorkflowId())
.setWorkflowType(workflowType(t.getWorkflowType()))
.setTaskList(taskList(t.getTaskList()))
.setInput(payload(t.getInput()))
.setExecutionStartToCloseTimeout(
secondsToDuration(t.getExecutionStartToCloseTimeoutSeconds()))
.setTaskStartToCloseTimeout(
secondsToDuration(t.getTaskStartToCloseTimeoutSeconds()))
.setIdentity(t.getIdentity())
.setRequestId(t.getRequestId())
.setWorkflowIdReusePolicy(workflowIdReusePolicy(t.getWorkflowIdReusePolicy()))
.setRetryPolicy(retryPolicy(t.getRetryPolicy()))
.setCronSchedule(t.getCronSchedule())
.setMemo(memo(t.getMemo()))
.setSearchAttributes(searchAttributes(t.getSearchAttributes()))
.setHeader(header(t.getHeader()))
.setDelayStart(secondsToDuration(t.getDelayStartSeconds()))
.build())
.setStartRequest(builder.build())
.setSignalName(t.getSignalName())
.setSignalInput(payload(t.getSignalInput()))
.setControl(arrayToByteString(t.getControl()))
Expand Down Expand Up @@ -411,25 +412,30 @@ public static StartWorkflowExecutionRequest startWorkflowExecutionRequest(
if (t == null) {
return null;
}
return StartWorkflowExecutionRequest.newBuilder()
.setDomain(t.getDomain())
.setWorkflowId(t.getWorkflowId())
.setWorkflowType(workflowType(t.getWorkflowType()))
.setTaskList(taskList(t.getTaskList()))
.setInput(payload(t.getInput()))
.setExecutionStartToCloseTimeout(
secondsToDuration(t.getExecutionStartToCloseTimeoutSeconds()))
.setTaskStartToCloseTimeout(secondsToDuration(t.getTaskStartToCloseTimeoutSeconds()))
.setIdentity(t.getIdentity())
.setRequestId(t.getRequestId())
.setWorkflowIdReusePolicy(workflowIdReusePolicy(t.getWorkflowIdReusePolicy()))
.setRetryPolicy(retryPolicy(t.getRetryPolicy()))
.setCronSchedule(t.getCronSchedule())
.setMemo(memo(t.getMemo()))
.setSearchAttributes(searchAttributes(t.getSearchAttributes()))
.setHeader(header(t.getHeader()))
.setDelayStart(secondsToDuration(t.getDelayStartSeconds()))
.build();
StartWorkflowExecutionRequest.Builder request =
StartWorkflowExecutionRequest.newBuilder()
.setDomain(t.getDomain())
.setWorkflowId(t.getWorkflowId())
.setWorkflowType(workflowType(t.getWorkflowType()))
.setTaskList(taskList(t.getTaskList()))
.setInput(payload(t.getInput()))
.setExecutionStartToCloseTimeout(
secondsToDuration(t.getExecutionStartToCloseTimeoutSeconds()))
.setTaskStartToCloseTimeout(secondsToDuration(t.getTaskStartToCloseTimeoutSeconds()))
.setIdentity(toNonNull((t.getIdentity())))
.setWorkflowIdReusePolicy(workflowIdReusePolicy(t.getWorkflowIdReusePolicy()))
.setCronSchedule(toNonNull(t.getCronSchedule()))
.setMemo(memo(t.getMemo()))
.setSearchAttributes(searchAttributes(t.getSearchAttributes()))
.setHeader(header(t.getHeader()))
.setDelayStart(secondsToDuration(t.getDelayStartSeconds()));
if (t.getRetryPolicy() != null) {
request.setRetryPolicy(retryPolicy(t.getRetryPolicy()));
}
if (t.getRequestId() != null) {
request.setRequestId(t.getRequestId());
}
return request.build();
}

public static TerminateWorkflowExecutionRequest terminateWorkflowExecutionRequest(
Expand Down Expand Up @@ -527,7 +533,7 @@ public static PollForDecisionTaskRequest pollForDecisionTaskRequest(
.setDomain(t.getDomain())
.setTaskList(taskList(t.getTaskList()))
.setIdentity(t.getIdentity())
.setBinaryChecksum(t.getBinaryChecksum())
.setBinaryChecksum(toNonNull(t.getBinaryChecksum()))
.build();
}

Expand Down Expand Up @@ -577,19 +583,20 @@ public static RegisterDomainRequest registerDomainRequest(
}
RegisterDomainRequest request =
RegisterDomainRequest.newBuilder()
.setDescription(t.getDescription())
.setOwnerEmail(t.getOwnerEmail())
.setName(t.getName())
.setDescription(toNonNull(t.getDescription()))
.setOwnerEmail(toNonNull(t.getOwnerEmail()))
.setWorkflowExecutionRetentionPeriod(
daysToDuration(t.getWorkflowExecutionRetentionPeriodInDays()))
.addAllClusters(clusterReplicationConfigurationArray(t.getClusters()))
.setActiveClusterName(t.getActiveClusterName())
.putAllData(t.getData())
.setSecurityToken(t.getSecurityToken())
.setIsGlobalDomain(t.isIsGlobalDomain())
.setActiveClusterName(toNonNull(t.getActiveClusterName()))
.putAllData(toNonNull(t.getData()))
.setSecurityToken(toNonNull(t.getSecurityToken()))
.setIsGlobalDomain(toNonNull(t.isIsGlobalDomain()))
.setHistoryArchivalStatus(archivalStatus(t.getHistoryArchivalStatus()))
.setHistoryArchivalUri(t.getHistoryArchivalURI())
.setHistoryArchivalUri(toNonNull(t.getHistoryArchivalURI()))
.setVisibilityArchivalStatus(archivalStatus(t.getVisibilityArchivalStatus()))
.setVisibilityArchivalUri(t.getVisibilityArchivalURI())
.setVisibilityArchivalUri(toNonNull(t.getVisibilityArchivalURI()))
.build();
return request;
}
Expand Down
Loading

0 comments on commit d8ff2af

Please sign in to comment.