Skip to content

Commit 5f8c3ea

Browse files
authored
Merge pull request #33 from RelationalAI/tmp-empty-object-json-fix
Fixes issues with empty objects in toJson
2 parents fa20052 + 009d4f6 commit 5f8c3ea

File tree

3 files changed

+108
-34
lines changed

3 files changed

+108
-34
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@relationalai/rai-sdk-javascript",
33
"description": "RelationalAI SDK for JavaScript",
4-
"version": "0.6.0",
4+
"version": "0.6.1",
55
"author": {
66
"name": "RelationalAI",
77
"url": "https://relational.ai"

src/relationUtils.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,39 @@ describe('relationUtils', () => {
10751075
},
10761076
]);
10771077

1078+
const nestedEmptyObject: ArrowRelation[] = plainToArrow([
1079+
{
1080+
relationId: '/:object/:name/String',
1081+
columns: [['obj1']],
1082+
},
1083+
{
1084+
relationId: '/:object/:arr/:[]/Int64/:id/Int64',
1085+
columns: [
1086+
[1, 2],
1087+
[1, 2],
1088+
],
1089+
},
1090+
{
1091+
relationId: '/:object/:arr/:[]/Int64/:attrs',
1092+
columns: [[1]],
1093+
},
1094+
{
1095+
relationId: '/:object/:arr/:[]/Int64/:attrs/:attr1/String',
1096+
columns: [[2], ['value1']],
1097+
},
1098+
{
1099+
relationId: '/:idt/String',
1100+
columns: [['432412341234']],
1101+
},
1102+
]);
1103+
1104+
const rootEmptyObject: ArrowRelation[] = plainToArrow([
1105+
{
1106+
relationId: '/:foo',
1107+
columns: [],
1108+
},
1109+
]);
1110+
10781111
it('should handle empty inputs', () => {
10791112
const json = toJson([]);
10801113

@@ -1493,6 +1526,37 @@ describe('relationUtils', () => {
14931526
});
14941527
});
14951528

1529+
it('should handle nested empty objects', () => {
1530+
const json = toJson(nestedEmptyObject);
1531+
1532+
expect(json).toEqual({
1533+
idt: '432412341234',
1534+
object: {
1535+
name: 'obj1',
1536+
arr: [
1537+
{
1538+
id: 1,
1539+
attrs: {},
1540+
},
1541+
{
1542+
id: 2,
1543+
attrs: {
1544+
attr1: 'value1',
1545+
},
1546+
},
1547+
],
1548+
},
1549+
});
1550+
});
1551+
1552+
it('should handle root empty objects', () => {
1553+
const json = toJson(rootEmptyObject);
1554+
1555+
expect(json).toEqual({
1556+
foo: {},
1557+
});
1558+
});
1559+
14961560
it('should handle root prop', () => {
14971561
const json = toJson(rootPropOutput);
14981562

src/relationUtils.ts

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -255,43 +255,53 @@ export function toJson(output: Relation[] | ArrowRelation[]): any {
255255
}
256256
}
257257

258-
for (let i = 0; i < relation.columns[0].length; i++) {
259-
let pathToSet = propPath.slice();
260-
let value: any;
261-
262-
for (let j = 0; j < relation.columns.length; j++) {
263-
const colValue = relation.columns[j][i];
264-
const pathIndex = columnLookup[j];
265-
266-
if (pathIndex !== undefined) {
267-
const isArray = pathToSet[pathIndex] === ARRAY_MARKER;
268-
269-
if (isArray) {
270-
const arrayIndex =
271-
typeof colValue === 'number'
272-
? colValue - 1 // rel indices start from 1, not 0
273-
: i; // wow, non-number array index?
274-
pathToSet[columnLookup[j]] = arrayIndex;
275-
} else {
276-
pathToSet[columnLookup[j]] = colValue as string | number;
258+
// if the last key is a symbol, then we need to add it to the prop path
259+
// so we can set the value to an empty object
260+
if (keys[keys.length - 1][0] === SYMBOL_PREFIX) {
261+
propPath.push(keys[keys.length - 1].slice(1));
262+
}
263+
264+
if (relation.columns.length === 0) {
265+
set(result, propPath, {});
266+
} else {
267+
for (let i = 0; i < relation.columns[0].length; i++) {
268+
let pathToSet = propPath.slice();
269+
let value: any;
270+
271+
for (let j = 0; j < relation.columns.length; j++) {
272+
const colValue = relation.columns[j][i];
273+
const pathIndex = columnLookup[j];
274+
275+
if (pathIndex !== undefined) {
276+
const isArray = pathToSet[pathIndex] === ARRAY_MARKER;
277+
278+
if (isArray) {
279+
const arrayIndex =
280+
typeof colValue === 'number'
281+
? colValue - 1 // rel indices start from 1, not 0
282+
: i; // wow, non-number array index?
283+
pathToSet[columnLookup[j]] = arrayIndex;
284+
} else {
285+
pathToSet[columnLookup[j]] = colValue as string | number;
286+
}
287+
} else if (j === relation.columns.length - 1) {
288+
value = colValue;
277289
}
278-
} else if (j === relation.columns.length - 1) {
279-
value = colValue;
280290
}
281-
}
282291

283-
if (
284-
pathToSet[pathToSet.length - 1] === ARRAY_MARKER &&
285-
keys[keys.length - 1] === EMPTY_ARRAY_MARKER
286-
) {
287-
pathToSet = pathToSet.slice(0, -1);
288-
value = [];
289-
} else if (value === undefined) {
290-
// present value
291-
value = {};
292-
}
292+
if (
293+
pathToSet[pathToSet.length - 1] === ARRAY_MARKER &&
294+
keys[keys.length - 1] === EMPTY_ARRAY_MARKER
295+
) {
296+
pathToSet = pathToSet.slice(0, -1);
297+
value = [];
298+
} else if (value === undefined) {
299+
// present value
300+
value = {};
301+
}
293302

294-
set(result, pathToSet, value);
303+
set(result, pathToSet, value);
304+
}
295305
}
296306
});
297307

0 commit comments

Comments
 (0)