Skip to content

Commit 662fe2a

Browse files
committed
Don't set empty metadata dto in proto.
1 parent 3125515 commit 662fe2a

4 files changed

Lines changed: 48 additions & 34 deletions

File tree

psalm-baseline.xml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -442,19 +442,11 @@
442442
<MissingTemplateParam>
443443
<code><![CDATA[$fails]]></code>
444444
</MissingTemplateParam>
445-
<PossiblyNullArgument>
446-
<code><![CDATA[$options->retryOptions ? $options->retryOptions->toWorkflowRetryPolicy() : null]]></code>
447-
<code><![CDATA[$options->toMemo($this->converter)]]></code>
448-
<code><![CDATA[$options->toSearchAttributes($this->converter)]]></code>
449-
</PossiblyNullArgument>
450445
<PossiblyNullReference>
451446
<code><![CDATA[getName]]></code>
452447
<code><![CDATA[getStartWorkflow]]></code>
453448
<code><![CDATA[getUpdateWorkflow]]></code>
454449
</PossiblyNullReference>
455-
<RedundantConditionGivenDocblockType>
456-
<code><![CDATA[$delay !== null]]></code>
457-
</RedundantConditionGivenDocblockType>
458450
<UndefinedInterfaceMethod>
459451
<code><![CDATA[toHeader]]></code>
460452
</UndefinedInterfaceMethod>
@@ -878,6 +870,9 @@
878870
<code><![CDATA[self::FORMAT_MONTHS]]></code>
879871
<code><![CDATA[self::FORMAT_YEARS]]></code>
880872
</DeprecatedConstant>
873+
<RedundantCondition>
874+
<code><![CDATA[$nullEmpty && $seconds === 0 && $micros === 0]]></code>
875+
</RedundantCondition>
881876
</file>
882877
<file src="src/Internal/Support/DateTime.php">
883878
<InvalidReturnStatement>

src/Client/WorkflowOptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ public function withStaticDetails(string $details): self
517517
*/
518518
public function toMemo(DataConverterInterface $converter): ?Memo
519519
{
520-
if ($this->memo === null) {
520+
if ($this->memo === null || $this->memo === []) {
521521
return null;
522522
}
523523

src/Internal/Client/WorkflowStarter.php

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,6 @@ private function configureExecutionRequest(
316316
StartInput $input,
317317
): StartWorkflowExecutionRequest|SignalWithStartWorkflowExecutionRequest {
318318
$options = $input->options;
319-
$header = $input->header;
320-
321-
\assert($header instanceof Header);
322-
$header->setDataConverter($this->converter);
323-
324-
$metadata = (new UserMetadata())
325-
->setSummary($this->converter->toPayload($options->staticSummary))
326-
->setDetails($this->converter->toPayload($options->staticDetails));
327319

328320
$req->setRequestId(Uuid::v4())
329321
->setIdentity($this->clientOptions->identity)
@@ -332,22 +324,43 @@ private function configureExecutionRequest(
332324
->setWorkflowType(new WorkflowType(['name' => $input->workflowType]))
333325
->setWorkflowId($input->workflowId)
334326
->setCronSchedule($options->cronSchedule ?? '')
335-
->setRetryPolicy($options->retryOptions ? $options->retryOptions->toWorkflowRetryPolicy() : null)
336327
->setWorkflowIdReusePolicy($options->workflowIdReusePolicy)
337328
->setWorkflowIdConflictPolicy($options->workflowIdConflictPolicy->value)
338329
->setWorkflowRunTimeout(DateInterval::toDuration($options->workflowRunTimeout))
339330
->setWorkflowExecutionTimeout(DateInterval::toDuration($options->workflowExecutionTimeout))
340-
->setWorkflowTaskTimeout(DateInterval::toDuration($options->workflowTaskTimeout))
341-
->setMemo($options->toMemo($this->converter))
342-
->setSearchAttributes($options->toSearchAttributes($this->converter))
343-
->setHeader($header->toHeader())
344-
->setUserMetadata($metadata);
345-
346-
$delay = DateInterval::toDuration($options->workflowStartDelay);
347-
if ($delay !== null && ($delay->getSeconds() > 0 || $delay->getNanos() > 0)) {
348-
$req->setWorkflowStartDelay($delay);
331+
->setWorkflowTaskTimeout(DateInterval::toDuration($options->workflowTaskTimeout));
332+
333+
// Retry Policy
334+
$options->retryOptions === null or $req->setRetryPolicy($options->retryOptions->toWorkflowRetryPolicy());
335+
336+
// Memo
337+
$memo = $options->toMemo($this->converter);
338+
$memo === null or $req->setMemo($memo);
339+
340+
// Search Attributes
341+
$searchAttributes = $options->toSearchAttributes($this->converter);
342+
$searchAttributes === null or $req->setSearchAttributes($searchAttributes);
343+
344+
// Header
345+
$header = $input->header;
346+
\assert($header instanceof Header);
347+
if ($header->count() > 0) {
348+
$header->setDataConverter($this->converter);
349+
$req->setHeader($header->toHeader());
349350
}
350351

352+
// User metadata
353+
if ($options->staticSummary !== '' || $options->staticDetails !== '') {
354+
$metadata = (new UserMetadata());
355+
$options->staticSummary === '' or $metadata->setSummary($this->converter->toPayload($options->staticSummary));
356+
$options->staticDetails === '' or $metadata->setDetails($this->converter->toPayload($options->staticDetails));
357+
$req->setUserMetadata($metadata);
358+
}
359+
360+
// Start Delay
361+
$delay = DateInterval::toDuration($options->workflowStartDelay, true);
362+
$delay === null or $req->setWorkflowStartDelay($delay);
363+
351364
if ($req instanceof StartWorkflowExecutionRequest) {
352365
$req->setRequestEagerExecution($options->eagerStart);
353366
}

src/Internal/Support/DateInterval.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
/**
1818
* @psalm-type DateIntervalFormat = DateInterval::FORMAT_*
1919
* @psalm-type DateIntervalValue = string | int | float | \DateInterval | Duration | null
20+
* @internal
2021
*/
2122
final class DateInterval
2223
{
@@ -148,20 +149,25 @@ public static function assert($interval): bool
148149
}
149150

150151
/**
151-
* @return ($i is null ? null : Duration)
152+
* @param bool $nullEmpty return null if the interval is empty
153+
*
154+
* @return ($i is null ? null : ($nullEmpty is true ? Duration|null : Duration))
152155
*/
153-
public static function toDuration(?\DateInterval $i = null): ?Duration
156+
public static function toDuration(?\DateInterval $i = null, bool $nullEmpty = false): ?Duration
154157
{
155158
if ($i === null) {
156159
return null;
157160
}
158161

159-
$d = new Duration();
160162
$parsed = self::parse($i);
161-
$d->setSeconds((int) $parsed->totalSeconds);
162-
$d->setNanos($parsed->microseconds * 1000);
163-
164-
return $d;
163+
$seconds = (int) $parsed->totalSeconds;
164+
$micros = $parsed->microseconds;
165+
166+
return $nullEmpty && $seconds === 0 && $micros === 0
167+
? null
168+
: (new Duration())
169+
->setSeconds((int) $parsed->totalSeconds)
170+
->setNanos($parsed->microseconds * 1000);
165171
}
166172

167173
private static function validateFormat(string $format): void

0 commit comments

Comments
 (0)