Skip to content

Commit 2a3bf94

Browse files
authored
feat(link-utils): make parsed links complete memory addresses (#1342)
feat(link-utils): introduce `type` field for parsed links, making them memory addresses - Added a new `type` property to normalized link structures in `link-utils.ts`, defaulting to `application/json`. - Updated link parsing, normalization, and equality logic to handle the new `type` field. - Refactored related functions and tests to ensure correct handling and propagation of the `type` property. - Enhanced test cases in `link-utils.test.ts` to cover scenarios involving the `type` field and its default value.
1 parent f2442e1 commit 2a3bf94

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

packages/runner/src/link-utils.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
isQueryResultForDereferencing,
2121
QueryResultInternals,
2222
} from "./query-result-proxy.ts";
23+
import { type IMemoryAddress } from "./storage/interface.ts";
2324

2425
/**
2526
* Normalized link structure returned by parsers
@@ -28,22 +29,19 @@ export type NormalizedLink = {
2829
id?: URI; // URI format with "of:" prefix
2930
path: string[];
3031
space?: MemorySpace;
32+
type?: string; // Default is "application/json"
3133
schema?: JSONSchema;
3234
rootSchema?: JSONSchema;
3335
overwrite?: "redirect"; // "this" gets normalized away to undefined
3436
};
3537

3638
/**
37-
* Normalized link with required id and space (when base Cell is provided)
39+
* Full normalized link that from a complete link, i.e. with required id, space
40+
* and type. Gets created by parseLink if a base is provided.
41+
*
42+
* Any such link can be used as a memory address.
3843
*/
39-
export type NormalizedFullLink = {
40-
id: URI; // URI format with "of:" prefix
41-
path: string[];
42-
space: MemorySpace;
43-
schema?: JSONSchema;
44-
rootSchema?: JSONSchema;
45-
overwrite?: "redirect"; // "this" gets normalized away to undefined
46-
};
44+
export type NormalizedFullLink = NormalizedLink & IMemoryAddress;
4745

4846
/**
4947
* A type reflecting all possible link formats, including cells themselves.
@@ -175,6 +173,7 @@ export function isNormalizedFullLink(value: any): value is NormalizedFullLink {
175173
isRecord(value) &&
176174
typeof value.id === "string" &&
177175
typeof value.space === "string" &&
176+
typeof value.type === "string" &&
178177
Array.isArray(value.path)
179178
);
180179
}
@@ -248,6 +247,7 @@ export function parseLink(
248247
id: toURI(value.getDoc().entityId),
249248
path: value.path.map((p) => p.toString()),
250249
space: value.space,
250+
type: "application/json",
251251
schema: value.schema,
252252
rootSchema: value.rootSchema,
253253
};
@@ -259,6 +259,7 @@ export function parseLink(
259259
id: toURI(value.entityId),
260260
path: [],
261261
space: value.space,
262+
type: "application/json",
262263
};
263264
}
264265

@@ -280,6 +281,7 @@ export function parseLink(
280281
id: id,
281282
path: path.map((p) => p.toString()),
282283
space: resolvedSpace,
284+
type: "application/json",
283285
schema: link.schema,
284286
rootSchema: link.rootSchema,
285287
overwrite: link.overwrite === "redirect" ? "redirect" : undefined,
@@ -292,6 +294,7 @@ export function parseLink(
292294
id: toURI(value.cell.entityId),
293295
path: value.path.map((p) => p.toString()),
294296
space: value.cell.space,
297+
type: "application/json",
295298
schema: value.schema,
296299
rootSchema: value.rootSchema,
297300
};
@@ -303,6 +306,7 @@ export function parseLink(
303306
id: toURI(value.cell["/"]),
304307
path: value.path.map((p) => p.toString()),
305308
space: base?.space, // Space must come from context for JSON links
309+
type: "application/json",
306310
};
307311
}
308312

@@ -311,6 +315,7 @@ export function parseLink(
311315
id: toURI(value["/"]),
312316
path: [],
313317
space: base?.space, // Space must come from context for JSON links
318+
type: "application/json",
314319
};
315320
}
316321

@@ -341,6 +346,7 @@ export function parseLink(
341346
? alias.path.map((p) => p.toString())
342347
: [],
343348
space: resolvedSpace,
349+
type: "application/json",
344350
schema: alias.schema as JSONSchema | undefined,
345351
rootSchema: alias.rootSchema as JSONSchema | undefined,
346352
};
@@ -503,7 +509,8 @@ export function areNormalizedLinksSame(
503509
link2: NormalizedLink,
504510
): boolean {
505511
return link1.id === link2.id && link1.space === link2.space &&
506-
arrayEqual(link1.path, link2.path);
512+
arrayEqual(link1.path, link2.path) &&
513+
(link1.type ?? "application/json") === (link2.type ?? "application/json");
507514
}
508515

509516
export function createSigilLinkFromParsedLink(

packages/runner/test/link-utils.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ describe("link-utils", () => {
212212
id: expect.stringContaining("of:"),
213213
path: [],
214214
space: space,
215+
type: "application/json",
215216
schema: undefined,
216217
rootSchema: undefined,
217218
});
@@ -227,6 +228,7 @@ describe("link-utils", () => {
227228
id: expect.stringContaining("of:"),
228229
path: ["nested"],
229230
space: space,
231+
type: "application/json",
230232
schema: undefined,
231233
rootSchema: undefined,
232234
});
@@ -241,6 +243,7 @@ describe("link-utils", () => {
241243
id: expect.stringContaining("of:"),
242244
path: [],
243245
space: space,
246+
type: "application/json",
244247
});
245248
});
246249

@@ -262,6 +265,7 @@ describe("link-utils", () => {
262265
id: "of:test",
263266
path: ["nested", "value"],
264267
space: space,
268+
type: "application/json",
265269
schema: { type: "number" },
266270
rootSchema: { type: "object" },
267271
});
@@ -286,6 +290,7 @@ describe("link-utils", () => {
286290
id: "of:test",
287291
path: ["nested", "value"],
288292
space: space,
293+
type: "application/json",
289294
schema: { type: "number" },
290295
rootSchema: { type: "object" },
291296
});
@@ -298,6 +303,7 @@ describe("link-utils", () => {
298303
id: "of:test",
299304
path: ["nested", "value"],
300305
space: space,
306+
type: "application/json",
301307
schema: { type: "number" },
302308
rootSchema: { type: "object" },
303309
overwrite: "redirect",
@@ -310,6 +316,7 @@ describe("link-utils", () => {
310316
id: "of:test",
311317
path: ["nested", "value"],
312318
space: space,
319+
type: "application/json",
313320
schema: { type: "number" },
314321
rootSchema: { type: "object" },
315322
overwrite: "redirect",
@@ -332,6 +339,7 @@ describe("link-utils", () => {
332339
id: expect.stringContaining("of:"),
333340
path: ["nested", "value"],
334341
space: space,
342+
type: "application/json",
335343
schema: undefined,
336344
rootSchema: undefined,
337345
});
@@ -346,6 +354,7 @@ describe("link-utils", () => {
346354
id: expect.stringContaining("of:"),
347355
path: [],
348356
space: space,
357+
type: "application/json",
349358
schema: undefined,
350359
rootSchema: undefined,
351360
});
@@ -363,6 +372,7 @@ describe("link-utils", () => {
363372
id: "of:test",
364373
path: ["nested", "value"],
365374
space: space,
375+
type: "application/json",
366376
});
367377
});
368378

@@ -382,6 +392,7 @@ describe("link-utils", () => {
382392
id: expect.stringContaining("of:"),
383393
path: ["nested", "value"],
384394
space: space,
395+
type: "application/json",
385396
schema: { type: "number" },
386397
rootSchema: { type: "object" },
387398
});
@@ -399,6 +410,7 @@ describe("link-utils", () => {
399410
expect(result).toEqual({
400411
id: expect.stringContaining("of:"),
401412
path: ["nested", "value"],
413+
type: "application/json",
402414
space: space,
403415
schema: undefined,
404416
rootSchema: undefined,

packages/runner/test/schema.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ describe("Schema Support", () => {
290290
space,
291291
schema: current.schema,
292292
rootSchema: current.rootSchema,
293+
type: "application/json",
293294
});
294295

295296
// .get() the currently selected cell. This should not change when
@@ -313,6 +314,7 @@ describe("Schema Support", () => {
313314
id: toURI(initialEntityId),
314315
path: ["foo"],
315316
space,
317+
type: "application/json",
316318
schema: omitSchema,
317319
rootSchema: schema,
318320
});

0 commit comments

Comments
 (0)