Skip to content

refactor: Make SearchAttributesValue an array #692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2022
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
4 changes: 2 additions & 2 deletions packages/client/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SearchAttributeValue } from '@temporalio/internal-workflow-common';
import type { SearchAttributes } from '@temporalio/internal-workflow-common';
import { temporal } from '@temporalio/proto';
import type * as grpc from '@grpc/grpc-js';

Expand Down Expand Up @@ -35,7 +35,7 @@ export interface WorkflowExecutionDescription {
executionTime?: Date;
closeTime?: Date;
memo?: Record<string, unknown>;
searchAttributes: Record<string, SearchAttributeValue[]>;
searchAttributes: SearchAttributes;
parentExecution?: Required<temporal.api.common.v1.IWorkflowExecution>;
raw: DescribeWorkflowExecutionResponse;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/workflow-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
optionalTsToDate,
QueryDefinition,
Replace,
SearchAttributeValue,
SearchAttributes,
SignalDefinition,
tsToDate,
WithWorkflowArgs,
Expand Down Expand Up @@ -861,7 +861,7 @@ export class WorkflowClient {
searchAttributes: mapFromPayloads(
searchAttributePayloadConverter,
raw.workflowExecutionInfo!.searchAttributes?.indexedFields ?? {}
) as Record<string, SearchAttributeValue[]>,
) as SearchAttributes,
parentExecution: raw.workflowExecutionInfo?.parentExecution
? {
workflowId: raw.workflowExecutionInfo.parentExecution.workflowId!,
Expand Down
3 changes: 2 additions & 1 deletion packages/internal-workflow-common/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ export type WorkflowResultType<W extends Workflow> = ReturnType<W> extends Promi
*
* Dates are serialized as ISO strings.
*/
export type SearchAttributeValue = string | number | boolean | Date;
export type SearchAttributes = Record<string, SearchAttributeValue>;
export type SearchAttributeValue = string[] | number[] | boolean[] | Date[];
4 changes: 2 additions & 2 deletions packages/internal-workflow-common/src/workflow-options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { coresdk, google } from '@temporalio/proto';
import { SearchAttributeValue, Workflow } from './interfaces';
import { SearchAttributes, Workflow } from './interfaces';
import { RetryPolicy } from './retry-policy';
import { msOptionalToTs } from './time';
import { checkExtends, Replace } from './type-helpers';
Expand Down Expand Up @@ -58,7 +58,7 @@ export interface BaseWorkflowOptions {
*
* Values are always converted using {@link JsonPayloadConverter}, even when a custom data converter is provided.
*/
searchAttributes?: Record<string, SearchAttributeValue[]>;
searchAttributes?: SearchAttributes;
}

export type WithWorkflowArgs<W extends Workflow, T> = T &
Expand Down
4 changes: 2 additions & 2 deletions packages/test/src/workflows/return-search-attributes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SearchAttributeValue, workflowInfo } from '@temporalio/workflow';
import { SearchAttributes, workflowInfo } from '@temporalio/workflow';

export async function returnSearchAttributes(): Promise<Record<string, SearchAttributeValue[]> | undefined> {
export async function returnSearchAttributes(): Promise<SearchAttributes | undefined> {
const sa = workflowInfo().searchAttributes!; // eslint-disable-line @typescript-eslint/no-non-null-assertion
const datetime = (sa.CustomDatetimeField as Array<Date>)[0];
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { SearchAttributeValue, upsertSearchAttributes, workflowInfo } from '@temporalio/workflow';
import { SearchAttributes, upsertSearchAttributes, workflowInfo } from '@temporalio/workflow';

export async function upsertAndReadSearchAttributes(
msSinceEpoch: number
): Promise<Record<string, SearchAttributeValue[]> | undefined> {
export async function upsertAndReadSearchAttributes(msSinceEpoch: number): Promise<SearchAttributes | undefined> {
upsertSearchAttributes({
CustomIntField: [123],
CustomBoolField: [true],
Expand Down
4 changes: 2 additions & 2 deletions packages/worker/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
import {
optionalTsToDate,
optionalTsToMs,
SearchAttributeValue,
SearchAttributes,
tsToMs,
decompileRetryPolicy,
} from '@temporalio/internal-workflow-common';
Expand Down Expand Up @@ -975,7 +975,7 @@ export class Worker {
searchAttributes: mapFromPayloads(
searchAttributePayloadConverter,
searchAttributes?.indexedFields
) as Record<string, SearchAttributeValue[]> | undefined,
) as SearchAttributes | undefined,
memo: await decodeMapFromPayloads(this.options.loadedDataConverter, memo?.fields),
parent: convertToParentWorkflowType(parentWorkflowInfo),
lastResult: await decodeFromPayloadsAtIndex(
Expand Down
4 changes: 2 additions & 2 deletions packages/workflow/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RetryPolicy, TemporalFailure } from '@temporalio/common';
import { checkExtends, CommonWorkflowOptions, SearchAttributeValue } from '@temporalio/internal-workflow-common';
import { checkExtends, CommonWorkflowOptions, SearchAttributes } from '@temporalio/internal-workflow-common';
import type { coresdk } from '@temporalio/proto';

/**
Expand All @@ -25,7 +25,7 @@ export interface WorkflowInfo {
/**
* Indexed information attached to the Workflow Execution
*/
searchAttributes?: Record<string, SearchAttributeValue[]>;
searchAttributes?: SearchAttributes;

/**
* Non-indexed information attached to the Workflow Execution
Expand Down
6 changes: 3 additions & 3 deletions packages/workflow/src/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
msToNumber,
msToTs,
QueryDefinition,
SearchAttributeValue,
SearchAttributes,
SignalDefinition,
tsToMs,
WithWorkflowArgs,
Expand Down Expand Up @@ -1163,14 +1163,14 @@ export function setHandler<Ret, Args extends any[], T extends SignalDefinition<A
*
* @param searchAttributes The Record to merge. Use a value of `[]` to clear a Search Attribute.
*/
export function upsertSearchAttributes(searchAttributes: Record<string, SearchAttributeValue[]>): void {
export function upsertSearchAttributes(searchAttributes: SearchAttributes): void {
if (!state.info) {
throw new IllegalStateError('`state.info` should be defined');
}

const mergedSearchAttributes = { ...state.info.searchAttributes, ...searchAttributes };
if (!mergedSearchAttributes) {
throw new Error('searchAttributes must be a non-null Record<string, SearchAttributeValue[]>');
throw new Error('searchAttributes must be a non-null SearchAttributes');
}

state.pushCommand({
Expand Down