Skip to content

Commit 0b01792

Browse files
committed
refactors
1 parent afbd118 commit 0b01792

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

src/statement/dataTypes.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,7 @@ export const isFloatType = (type: string) => {
163163
export const isNumberType = (type: string) => {
164164
return INTEGER_TYPES.includes(type) || isFloatType(type);
165165
};
166+
167+
export const isStructType = (type: string) => {
168+
return STRUCT_TYPE.test(type);
169+
};

src/statement/hydrateResponse.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
isDateType,
77
isNumberType,
88
getInnerType,
9-
getStructTypes
9+
getStructTypes,
10+
isStructType
1011
} from "./dataTypes";
1112
import { hydrateDate } from "./hydrateDate";
1213

@@ -22,26 +23,38 @@ const hydrateInfNan = (value: string) => {
2223
return NaN;
2324
};
2425

26+
const hydrateStruct = (
27+
value: Record<string, unknown>,
28+
type: string,
29+
executeQueryOptions: ExecuteQueryOptions
30+
): Record<string, unknown> => {
31+
const hydratedStruct: Record<string, unknown> = {};
32+
const innerTypes = getStructTypes(type);
33+
// if number of keys does not match, return value as is
34+
if (Object.keys(innerTypes).length !== Object.keys(value).length) {
35+
return value;
36+
}
37+
for (const [key, innerType] of Object.entries(innerTypes)) {
38+
hydratedStruct[key] = getHydratedValue(
39+
value[key],
40+
innerType,
41+
executeQueryOptions
42+
);
43+
}
44+
return hydratedStruct;
45+
};
46+
2547
const getHydratedValue = (
2648
value: unknown,
2749
type: string,
2850
executeQueryOptions: ExecuteQueryOptions
2951
): any => {
30-
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
31-
const hydratedStruct: Record<string, unknown> = {};
32-
const innerTypes = getStructTypes(type);
33-
// if number of keys does not match, return value as is
34-
if (Object.keys(innerTypes).length !== Object.keys(value).length) {
35-
return value;
36-
}
37-
for (const [key, innerType] of Object.entries(innerTypes)) {
38-
hydratedStruct[key] = getHydratedValue(
39-
(value as Record<string, unknown>)[key],
40-
innerType,
41-
executeQueryOptions
42-
);
43-
}
44-
return hydratedStruct;
52+
if (isStructType(type)) {
53+
return hydrateStruct(
54+
value as Record<string, unknown>,
55+
type,
56+
executeQueryOptions
57+
);
4558
}
4659
if (Array.isArray(value)) {
4760
const innerType = getInnerType(type);

0 commit comments

Comments
 (0)