fix partitionkey value fetching (#1972)

* fix partitionkey value fetching

* fix partitionkey value fetching

* added unit test

* Added some unit tests

* move the constant
This commit is contained in:
sunghyunkang1111
2024-09-19 13:09:09 -05:00
committed by GitHub
parent 42a1c6c319
commit 869d81dfbc
3 changed files with 87 additions and 26 deletions

View File

@@ -95,6 +95,24 @@ export const queryPagesUntilContentPresent = async (
return await doRequest(firstItemIndex);
};
/* eslint-disable @typescript-eslint/no-explicit-any */
export const getValueForPath = (content: any, pathSegments: string[]): any => {
if (pathSegments.length === 0) {
return undefined;
}
let currentValue = content;
for (const segment of pathSegments) {
if (!currentValue || currentValue[segment] === undefined) {
return undefined;
}
currentValue = currentValue[segment];
}
return currentValue;
};
/* eslint-disable @typescript-eslint/no-explicit-any */
export const extractPartitionKeyValues = (
documentContent: any,
@@ -105,11 +123,15 @@ export const extractPartitionKeyValues = (
}
const partitionKeyValues: PartitionKey[] = [];
partitionKeyDefinition.paths.forEach((partitionKeyPath: string) => {
const partitionKeyPathWithoutSlash: string = partitionKeyPath.substring(1);
if (documentContent[partitionKeyPathWithoutSlash] !== undefined) {
partitionKeyValues.push(documentContent[partitionKeyPathWithoutSlash]);
const pathSegments: string[] = partitionKeyPath.substring(1).split("/");
const value = getValueForPath(documentContent, pathSegments);
if (value !== undefined) {
partitionKeyValues.push(value);
}
});
return partitionKeyValues;
};