Skip to content

Commit 40dec5f

Browse files
authored
Merge pull request #34 from eijawerner/update-property-type
only allow list one level deep of property type
2 parents 7fa1bf1 + ccaf460 commit 40dec5f

File tree

10 files changed

+177
-80
lines changed

10 files changed

+177
-80
lines changed

.changeset/fluffy-spoons-camp.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@neo4j/graph-schema-utils": patch
3+
"@neo4j/graph-introspection": patch
4+
---
5+
6+
removed recursive property type

packages/graph-schema-utils/src/formatters/json/extensions.ts

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import {
66
NodeLabelIndex,
77
NodeObjectType,
88
Property,
9-
PropertyArrayType,
10-
PropertyBaseType,
11-
PropertyTypeRecursive,
9+
PrimitiveArrayPropertyType,
10+
PrimitivePropertyType,
11+
PropertyType,
1212
RelationshipObjectType,
1313
RelationshipType,
1414
RelationshipTypeConstraint,
@@ -18,19 +18,23 @@ import {
1818
isNodeLabelIndex,
1919
isRelationshipTypeConstraint,
2020
isRelationshipTypeIndex,
21+
isPrimitivePropertyType,
22+
isPrimitiveArrayPropertyType,
2123
} from "../../model/index.js";
2224
import {
2325
ConstraintJsonStruct,
2426
IndexJsonStruct,
27+
isPrimitiveArrayPropertyTypeJsonStruct,
28+
isPrimitivePropertyTypeJsonStruct,
2529
LookupIndexJsonStruct,
2630
NodeLabelConstraintJsonStruct,
2731
NodeLabelIndexJsonStruct,
2832
NodeLabelJsonStruct,
2933
NodeObjectTypeJsonStruct,
30-
PrimitivePropertyTypesArrayType,
31-
PrimitivePropertyTypesType,
34+
PrimitiveArrayPropertyTypeJsonStruct,
35+
PrimitivePropertyTypeJsonStruct,
3236
PropertyJsonStruct,
33-
PropertyTypeJsonStructRecrsive,
37+
PropertyTypeJsonStruct,
3438
RelationshipObjectTypeJsonStruct,
3539
RelationshipTypeConstraintJsonStruct,
3640
RelationshipTypeIndexJsonStruct,
@@ -470,24 +474,40 @@ const property = {
470474
};
471475

472476
const propertyType = {
473-
extract: (pt: PropertyTypeRecursive): PropertyTypeJsonStructRecrsive => {
477+
extract: (
478+
pt: PropertyType | PropertyType[]
479+
): PropertyTypeJsonStruct | PropertyTypeJsonStruct[] => {
474480
if (Array.isArray(pt)) {
475-
return pt.map(propertyType.extract);
481+
return pt.map((p) => {
482+
if (isPrimitivePropertyType(p)) {
483+
return propertyBaseType.extract(p);
484+
} else if (isPrimitiveArrayPropertyType(p)) {
485+
return propertyArrayType.extract(p);
486+
}
487+
throw Error(`Unknown property type in list ${p}`);
488+
});
476489
}
477-
if (pt instanceof PropertyBaseType) {
490+
if (isPrimitivePropertyType(pt)) {
478491
return propertyBaseType.extract(pt);
479-
} else if (pt instanceof PropertyArrayType) {
492+
} else if (isPrimitiveArrayPropertyType(pt)) {
480493
return propertyArrayType.extract(pt);
481494
}
482495
throw new Error(`Unknown property type ${pt}`);
483496
},
484497
create: (
485-
propertyTypeJson: PropertyTypeJsonStructRecrsive
486-
): PropertyTypeRecursive => {
498+
propertyTypeJson: PropertyTypeJsonStruct | PropertyTypeJsonStruct[]
499+
): PropertyType | PropertyType[] => {
487500
if (Array.isArray(propertyTypeJson)) {
488-
return propertyTypeJson.map((pt) => propertyType.create(pt));
501+
return propertyTypeJson.map((pt) => {
502+
if (isPrimitivePropertyTypeJsonStruct(pt)) {
503+
return propertyBaseType.create(pt);
504+
} else if (isPrimitiveArrayPropertyTypeJsonStruct(pt)) {
505+
return propertyArrayType.create(pt.items.type);
506+
}
507+
throw new Error(`Unknown property type in list ${pt}`);
508+
});
489509
}
490-
if (propertyTypeJson.type === "array") {
510+
if (isPrimitiveArrayPropertyTypeJsonStruct(propertyTypeJson)) {
491511
return propertyArrayType.create(propertyTypeJson.items.type);
492512
}
493513
return propertyBaseType.create(propertyTypeJson);
@@ -496,23 +516,23 @@ const propertyType = {
496516

497517
const propertyBaseType = {
498518
extract: (
499-
propertyBaseType: PropertyBaseType
500-
): PrimitivePropertyTypesType => ({
519+
propertyBaseType: PrimitivePropertyType
520+
): PrimitivePropertyTypeJsonStruct => ({
501521
type: propertyBaseType.type,
502522
}),
503-
create: (btype: PrimitivePropertyTypesType) =>
504-
new PropertyBaseType(btype.type),
523+
create: (btype: PrimitivePropertyTypeJsonStruct) =>
524+
new PrimitivePropertyType(btype.type),
505525
};
506526

507527
const propertyArrayType = {
508528
extract: (
509-
propertyArrayType: PropertyArrayType
510-
): PrimitivePropertyTypesArrayType => ({
529+
propertyArrayType: PrimitiveArrayPropertyType
530+
): PrimitiveArrayPropertyTypeJsonStruct => ({
511531
type: "array",
512532
items: propertyBaseType.extract(propertyArrayType.items),
513533
}),
514-
create: (type: PrimitivePropertyTypesArrayType["items"]["type"]) =>
515-
new PropertyArrayType(propertyBaseType.create({ type })),
534+
create: (type: PrimitiveArrayPropertyTypeJsonStruct["items"]["type"]) =>
535+
new PrimitiveArrayPropertyType(propertyBaseType.create({ type })),
516536
};
517537

518538
const nodeObjectType = {

packages/graph-schema-utils/src/formatters/json/parse.test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import path from "path";
33
import { readFile } from "../../../test/fs.utils.js";
44
import { describe, test } from "vitest";
55
import { fromJson } from "./index.js";
6-
import { PropertyTypes } from "../../model/index.js";
6+
import {
7+
PrimitiveArrayPropertyType,
8+
PrimitivePropertyType,
9+
} from "../../model/index.js";
710

811
import { validateSchema } from "../../validation.js";
912

@@ -66,7 +69,11 @@ describe("Parser tests", () => {
6669
"roles"
6770
);
6871
assert.strictEqual(
69-
(parsed.relationshipTypes[0].properties[0].type as PropertyTypes).type,
72+
(
73+
parsed.relationshipTypes[0].properties[0].type as
74+
| PrimitivePropertyType
75+
| PrimitiveArrayPropertyType
76+
).type,
7077
"array"
7178
);
7279
});

packages/graph-schema-utils/src/formatters/json/types.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
ConstraintType,
33
EntityType,
44
IndexType,
5+
PRIMITIVE_TYPE_OPTIONS,
56
PrimitivePropertyTypes,
67
} from "../../model/index.js";
78

@@ -105,21 +106,37 @@ export type LookupIndexJsonStruct = IndexJsonStruct & {
105106
export type PropertyJsonStruct = {
106107
$id: string;
107108
token: string;
108-
type: PropertyTypeJsonStructRecrsive;
109+
type: PropertyTypeJsonStruct | PropertyTypeJsonStruct[];
109110
nullable: boolean;
110111
};
111112

112-
export type PrimitivePropertyTypesType = { type: PrimitivePropertyTypes };
113-
export type PrimitivePropertyTypesArrayType = {
113+
export type PrimitivePropertyTypeJsonStruct = { type: PrimitivePropertyTypes };
114+
export type PrimitiveArrayPropertyTypeJsonStruct = {
114115
type: "array";
115116
items: { type: PrimitivePropertyTypes };
116117
};
117118

118119
export type PropertyTypeJsonStruct =
119-
| PrimitivePropertyTypesArrayType
120-
| PrimitivePropertyTypesType
121-
| (PrimitivePropertyTypesType | PrimitivePropertyTypesArrayType)[];
122-
123-
export type PropertyTypeJsonStructRecrsive =
124-
| PropertyTypeJsonStruct
125-
| Array<PropertyTypeJsonStruct | PropertyTypeJsonStructRecrsive[]>;
120+
| PrimitiveArrayPropertyTypeJsonStruct
121+
| PrimitivePropertyTypeJsonStruct;
122+
123+
export const isPrimitivePropertyTypeJsonStruct = (
124+
propertyType: PropertyTypeJsonStruct
125+
): propertyType is PrimitivePropertyTypeJsonStruct => {
126+
return (
127+
typeof propertyType === "object" &&
128+
"type" in propertyType &&
129+
PRIMITIVE_TYPE_OPTIONS.some((p) => propertyType.type === p)
130+
);
131+
};
132+
133+
export const isPrimitiveArrayPropertyTypeJsonStruct = (
134+
propertyType: PropertyTypeJsonStruct
135+
): propertyType is PrimitiveArrayPropertyTypeJsonStruct => {
136+
return (
137+
typeof propertyType === "object" &&
138+
"type" in propertyType &&
139+
propertyType.type === "array" &&
140+
propertyType.items !== undefined
141+
);
142+
};

packages/graph-schema-utils/src/formatters/llm-prompt/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import {
22
GraphSchema,
33
NodeObjectType,
4-
PropertyBaseType,
5-
PropertyTypeRecursive,
6-
PropertyTypes,
4+
PrimitivePropertyType,
5+
PropertyType,
76
RelationshipObjectType,
87
} from "../../model/index.js";
98
import { ElementPropertyObject } from "./types.js";
@@ -141,11 +140,11 @@ export function toOskars(schema: GraphSchema): string {
141140
return out.join("\n");
142141
}
143142

144-
function formatPropertyType(type: PropertyTypeRecursive): string {
143+
function formatPropertyType(type: PropertyType | PropertyType[]): string {
145144
if (Array.isArray(type)) {
146145
return type.map(formatPropertyType).join("|");
147146
}
148-
if (type instanceof PropertyBaseType) {
147+
if (type instanceof PrimitivePropertyType) {
149148
return type.type;
150149
}
151150
return `${type.items.type}[]`;

packages/graph-schema-utils/src/model/index.ts

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -295,20 +295,38 @@ export type IndexType =
295295

296296
export type EntityType = "node" | "relationship";
297297

298-
export type PropertyTypes = PropertyBaseType | PropertyArrayType;
299-
export type PropertyTypeRecursive =
300-
| PropertyTypes
301-
| Array<PropertyTypes | PropertyTypeRecursive[]>;
298+
export type PropertyType = PrimitivePropertyType | PrimitiveArrayPropertyType;
299+
300+
export const isPrimitiveArrayPropertyType = (
301+
property: PropertyType
302+
): property is PrimitiveArrayPropertyType => {
303+
return (
304+
property instanceof Object &&
305+
property.type === "array" &&
306+
property.items !== undefined
307+
);
308+
};
309+
310+
export const isPrimitivePropertyType = (
311+
property: PropertyType
312+
): property is PrimitivePropertyType => {
313+
return (
314+
property instanceof Object &&
315+
"type" in property &&
316+
PRIMITIVE_TYPE_OPTIONS.some((p) => property.type === p)
317+
);
318+
};
319+
302320
export class Property {
303321
$id: string;
304322
token: string;
305-
type: PropertyTypeRecursive;
323+
type: PropertyType | PropertyType[];
306324
nullable: boolean;
307325

308326
constructor(
309327
$id: string,
310328
token: string,
311-
type: PropertyTypeRecursive,
329+
type: PropertyType | PropertyType[],
312330
nullable: boolean
313331
) {
314332
this.$id = $id;
@@ -318,32 +336,35 @@ export class Property {
318336
}
319337
}
320338

321-
export class PropertyBaseType {
339+
export class PrimitivePropertyType {
322340
type: PrimitivePropertyTypes;
323341
constructor(type: PrimitivePropertyTypes) {
324342
this.type = type;
325343
}
326344
}
327345

328-
export class PropertyArrayType {
329-
items: PropertyBaseType;
346+
export class PrimitiveArrayPropertyType {
347+
items: PrimitivePropertyType;
330348
type: "array";
331349

332-
constructor(items: PropertyBaseType) {
350+
constructor(items: PrimitivePropertyType) {
333351
this.type = "array";
334352
this.items = items;
335353
}
336354
}
337355

338-
export type PrimitivePropertyTypes =
339-
| "integer"
340-
| "string"
341-
| "float"
342-
| "boolean"
343-
| "point"
344-
| "date"
345-
| "datetime"
346-
| "time"
347-
| "localtime"
348-
| "localdatetime"
349-
| "duration";
356+
export const PRIMITIVE_TYPE_OPTIONS = [
357+
"integer",
358+
"string",
359+
"float",
360+
"boolean",
361+
"point",
362+
"date",
363+
"datetime",
364+
"time",
365+
"localtime",
366+
"localdatetime",
367+
"duration",
368+
] as const;
369+
370+
export type PrimitivePropertyTypes = (typeof PRIMITIVE_TYPE_OPTIONS)[number];

0 commit comments

Comments
 (0)