Skip to content

Commit

Permalink
cleanup and names
Browse files Browse the repository at this point in the history
  • Loading branch information
THardy98 committed Feb 7, 2025
1 parent 3261083 commit 1a601a1
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 191 deletions.
30 changes: 13 additions & 17 deletions packages/common/src/converter/payload-search-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import { decode, encode } from '../encoding';
import { ValueError } from '../errors';
import { Payload, SearchAttributes } from '../interfaces';
import {
isTypedSearchAttribute,
isTypedSearchAttributeValue,
isTypedSearchAttributePair,
toMetadataType,
toSearchAttributeType,
TypedSearchAttributePair,
TypedSearchAttributes,
TypedSearchAttribute,
TypedSearchAttributeValue,
SearchAttributeType,
isTypedSearchAttributeUpdate,
searchAttributePair,
isTypedSearchAttributeUpdateValue,
} from '../typed-search-attributes';
import {
PayloadConverter,
Expand Down Expand Up @@ -93,7 +90,7 @@ export class TypedSearchAttributePayloadConverter implements PayloadConverter {

public toPayload<T>(typedSearchAttribute: T): Payload {
// We check for deletion as well as regular typed search attributes.
if (!isTypedSearchAttributeUpdate(typedSearchAttribute)) {
if (!isTypedSearchAttributeUpdateValue(typedSearchAttribute)) {
throw new ValueError(`Invalid typed search attribute: ${typedSearchAttribute}`);
}

Expand Down Expand Up @@ -124,7 +121,7 @@ export class TypedSearchAttributePayloadConverter implements PayloadConverter {
throw new ValueError('Missing payload metadata');
}
// Add encoded type of search attribute to metatdata
payload.metadata['type'] = encode(toMetadataType(type));
payload.metadata['type'] = encode(TypedSearchAttributes.toMetadataType(type));
return payload;
}

Expand All @@ -140,7 +137,7 @@ export class TypedSearchAttributePayloadConverter implements PayloadConverter {
if (payload.metadata.type == null) {
return undefined as T;
}
const type = toSearchAttributeType(decode(payload.metadata.type));
const type = TypedSearchAttributes.toSearchAttributeType(decode(payload.metadata.type));
// Unrecognized metadata type (sanity check).
if (type === undefined) {
return undefined as T;
Expand All @@ -161,7 +158,7 @@ export class TypedSearchAttributePayloadConverter implements PayloadConverter {
value = new Date(value as string);
}
// Check if the value is a valid typed search attribute. If not, skip.
if (!isTypedSearchAttribute([type, value])) {
if (!isTypedSearchAttributeValue([type, value])) {
return undefined as T;
}
return [type, value] as T;
Expand All @@ -178,13 +175,12 @@ export function encodeUnifiedSearchAttributes(
return {
...(searchAttributes ? mapToPayloads(searchAttributePayloadConverter, searchAttributes) : {}),
...(typedSearchAttributes
? typedMapToPayloads<string, TypedSearchAttribute>(
? typedMapToPayloads<string, TypedSearchAttributeValue>(
typedSearchAttributePayloadConverter,
Object.fromEntries(
(Array.isArray(typedSearchAttributes)
? typedSearchAttributes
: typedSearchAttributes.getSearchAttributes()
).map(([k, v]) => [k.name, v])
(Array.isArray(typedSearchAttributes) ? typedSearchAttributes : typedSearchAttributes.getAttributes()).map(
([k, v]) => [k.name, v]
)
)
)
: {}),
Expand All @@ -205,7 +201,7 @@ export function decodeTypedSearchAttributes(
): TypedSearchAttributes {
return new TypedSearchAttributes(
Object.entries(
typedMapFromPayloads<string, TypedSearchAttribute | undefined>(
typedMapFromPayloads<string, TypedSearchAttributeValue | undefined>(
typedSearchAttributePayloadConverter,
indexedFields
) ?? {}
Expand All @@ -214,7 +210,7 @@ export function decodeTypedSearchAttributes(
if (!v) {
return acc;
}
const pair = searchAttributePair(k, v[0], v[1]);
const pair = TypedSearchAttributes.createAttribute(k, v[0], v[1]);
// Ensure is valid pair.
if (isTypedSearchAttributePair(pair)) {
acc.push(pair);
Expand Down
Loading

0 comments on commit 1a601a1

Please sign in to comment.