Skip to content

Commit ccaf460

Browse files
committed
refactoring the names of property types
1 parent 8702c18 commit ccaf460

File tree

9 files changed

+134
-116
lines changed

9 files changed

+134
-116
lines changed

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

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {
66
NodeLabelIndex,
77
NodeObjectType,
88
Property,
9-
PropertyArrayType,
10-
PropertyBaseType,
9+
PrimitiveArrayPropertyType,
10+
PrimitivePropertyType,
1111
PropertyType,
1212
RelationshipObjectType,
1313
RelationshipType,
@@ -18,23 +18,21 @@ import {
1818
isNodeLabelIndex,
1919
isRelationshipTypeConstraint,
2020
isRelationshipTypeIndex,
21-
isPropertyTypeList,
22-
isPropertyBaseType,
23-
isPropertyArrayType,
21+
isPrimitivePropertyType,
22+
isPrimitiveArrayPropertyType,
2423
} from "../../model/index.js";
2524
import {
2625
ConstraintJsonStruct,
2726
IndexJsonStruct,
28-
isPrimitivePropertyTypesArrayTypeJsonStruct,
29-
isPrimitivePropertyTypesTypeJsonStruct,
30-
isPropertyTypeListJsonStruct,
27+
isPrimitiveArrayPropertyTypeJsonStruct,
28+
isPrimitivePropertyTypeJsonStruct,
3129
LookupIndexJsonStruct,
3230
NodeLabelConstraintJsonStruct,
3331
NodeLabelIndexJsonStruct,
3432
NodeLabelJsonStruct,
3533
NodeObjectTypeJsonStruct,
36-
PrimitivePropertyTypesArrayType,
37-
PrimitivePropertyTypesType,
34+
PrimitiveArrayPropertyTypeJsonStruct,
35+
PrimitivePropertyTypeJsonStruct,
3836
PropertyJsonStruct,
3937
PropertyTypeJsonStruct,
4038
RelationshipObjectTypeJsonStruct,
@@ -476,36 +474,40 @@ const property = {
476474
};
477475

478476
const propertyType = {
479-
extract: (pt: PropertyType): PropertyTypeJsonStruct => {
480-
if (isPropertyTypeList(pt)) {
477+
extract: (
478+
pt: PropertyType | PropertyType[]
479+
): PropertyTypeJsonStruct | PropertyTypeJsonStruct[] => {
480+
if (Array.isArray(pt)) {
481481
return pt.map((p) => {
482-
if (isPropertyBaseType(p)) {
482+
if (isPrimitivePropertyType(p)) {
483483
return propertyBaseType.extract(p);
484-
} else if (isPropertyArrayType(p)) {
484+
} else if (isPrimitiveArrayPropertyType(p)) {
485485
return propertyArrayType.extract(p);
486486
}
487487
throw Error(`Unknown property type in list ${p}`);
488488
});
489489
}
490-
if (isPropertyBaseType(pt)) {
490+
if (isPrimitivePropertyType(pt)) {
491491
return propertyBaseType.extract(pt);
492-
} else if (isPropertyArrayType(pt)) {
492+
} else if (isPrimitiveArrayPropertyType(pt)) {
493493
return propertyArrayType.extract(pt);
494494
}
495495
throw new Error(`Unknown property type ${pt}`);
496496
},
497-
create: (propertyTypeJson: PropertyTypeJsonStruct): PropertyType => {
498-
if (isPropertyTypeListJsonStruct(propertyTypeJson)) {
497+
create: (
498+
propertyTypeJson: PropertyTypeJsonStruct | PropertyTypeJsonStruct[]
499+
): PropertyType | PropertyType[] => {
500+
if (Array.isArray(propertyTypeJson)) {
499501
return propertyTypeJson.map((pt) => {
500-
if (isPrimitivePropertyTypesTypeJsonStruct(pt)) {
502+
if (isPrimitivePropertyTypeJsonStruct(pt)) {
501503
return propertyBaseType.create(pt);
502-
} else if (isPrimitivePropertyTypesArrayTypeJsonStruct(pt)) {
504+
} else if (isPrimitiveArrayPropertyTypeJsonStruct(pt)) {
503505
return propertyArrayType.create(pt.items.type);
504506
}
505507
throw new Error(`Unknown property type in list ${pt}`);
506508
});
507509
}
508-
if (isPrimitivePropertyTypesArrayTypeJsonStruct(propertyTypeJson)) {
510+
if (isPrimitiveArrayPropertyTypeJsonStruct(propertyTypeJson)) {
509511
return propertyArrayType.create(propertyTypeJson.items.type);
510512
}
511513
return propertyBaseType.create(propertyTypeJson);
@@ -514,23 +516,23 @@ const propertyType = {
514516

515517
const propertyBaseType = {
516518
extract: (
517-
propertyBaseType: PropertyBaseType
518-
): PrimitivePropertyTypesType => ({
519+
propertyBaseType: PrimitivePropertyType
520+
): PrimitivePropertyTypeJsonStruct => ({
519521
type: propertyBaseType.type,
520522
}),
521-
create: (btype: PrimitivePropertyTypesType) =>
522-
new PropertyBaseType(btype.type),
523+
create: (btype: PrimitivePropertyTypeJsonStruct) =>
524+
new PrimitivePropertyType(btype.type),
523525
};
524526

525527
const propertyArrayType = {
526528
extract: (
527-
propertyArrayType: PropertyArrayType
528-
): PrimitivePropertyTypesArrayType => ({
529+
propertyArrayType: PrimitiveArrayPropertyType
530+
): PrimitiveArrayPropertyTypeJsonStruct => ({
529531
type: "array",
530532
items: propertyBaseType.extract(propertyArrayType.items),
531533
}),
532-
create: (type: PrimitivePropertyTypesArrayType["items"]["type"]) =>
533-
new PropertyArrayType(propertyBaseType.create({ type })),
534+
create: (type: PrimitiveArrayPropertyTypeJsonStruct["items"]["type"]) =>
535+
new PrimitiveArrayPropertyType(propertyBaseType.create({ type })),
534536
};
535537

536538
const nodeObjectType = {

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

Lines changed: 6 additions & 3 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 { PropertyArrayType, PropertyBaseType } from "../../model/index.js";
6+
import {
7+
PrimitiveArrayPropertyType,
8+
PrimitivePropertyType,
9+
} from "../../model/index.js";
710

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

@@ -68,8 +71,8 @@ describe("Parser tests", () => {
6871
assert.strictEqual(
6972
(
7073
parsed.relationshipTypes[0].properties[0].type as
71-
| PropertyBaseType
72-
| PropertyArrayType
74+
| PrimitivePropertyType
75+
| PrimitiveArrayPropertyType
7376
).type,
7477
"array"
7578
);

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

Lines changed: 23 additions & 22 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,37 +106,37 @@ export type LookupIndexJsonStruct = IndexJsonStruct & {
105106
export type PropertyJsonStruct = {
106107
$id: string;
107108
token: string;
108-
type: PropertyTypeJsonStruct;
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

118-
export type PropertyTypeListJsonStruct = (PrimitivePropertyTypesType | PrimitivePropertyTypesArrayType)[]
119-
120119
export type PropertyTypeJsonStruct =
121-
| PrimitivePropertyTypesArrayType
122-
| PrimitivePropertyTypesType
123-
| PropertyTypeListJsonStruct;
124-
125-
export const isPropertyTypeListJsonStruct = (
126-
propertyType: PropertyTypeJsonStruct
127-
): propertyType is PropertyTypeListJsonStruct => {
128-
return Array.isArray(propertyType);
129-
}
120+
| PrimitiveArrayPropertyTypeJsonStruct
121+
| PrimitivePropertyTypeJsonStruct;
130122

131-
export const isPrimitivePropertyTypesTypeJsonStruct = (
123+
export const isPrimitivePropertyTypeJsonStruct = (
132124
propertyType: PropertyTypeJsonStruct
133-
): propertyType is PrimitivePropertyTypesType => {
134-
return typeof propertyType === "object" && "type" in propertyType && propertyType.type !== "array";
135-
}
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+
};
136132

137-
export const isPrimitivePropertyTypesArrayTypeJsonStruct = (
133+
export const isPrimitiveArrayPropertyTypeJsonStruct = (
138134
propertyType: PropertyTypeJsonStruct
139-
): propertyType is PrimitivePropertyTypesArrayType => {
140-
return typeof propertyType === "object" && "type" in propertyType && propertyType.type === "array" && propertyType.items !== undefined;
141-
}
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
GraphSchema,
33
NodeObjectType,
4-
PropertyBaseType,
4+
PrimitivePropertyType,
55
PropertyType,
66
RelationshipObjectType,
77
} from "../../model/index.js";
@@ -140,11 +140,11 @@ export function toOskars(schema: GraphSchema): string {
140140
return out.join("\n");
141141
}
142142

143-
function formatPropertyType(type: PropertyType): string {
143+
function formatPropertyType(type: PropertyType | PropertyType[]): string {
144144
if (Array.isArray(type)) {
145145
return type.map(formatPropertyType).join("|");
146146
}
147-
if (type instanceof PropertyBaseType) {
147+
if (type instanceof PrimitivePropertyType) {
148148
return type.type;
149149
}
150150
return `${type.items.type}[]`;

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

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -295,35 +295,38 @@ export type IndexType =
295295

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

298-
export type PropertyType =
299-
| PropertyBaseType
300-
| PropertyArrayType
301-
| PropertyTypeList;
298+
export type PropertyType = PrimitivePropertyType | PrimitiveArrayPropertyType;
302299

303-
export type PropertyTypeList = (PropertyBaseType | PropertyArrayType)[];
304-
305-
export const isPropertyBaseType = (p: PropertyType): p is PropertyBaseType => {
306-
return p instanceof PropertyBaseType;
307-
}
308-
309-
export const isPropertyArrayType = (p: PropertyType): p is PropertyArrayType => {
310-
return p instanceof PropertyArrayType;
311-
}
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+
};
312309

313-
export const isPropertyTypeList = (p: PropertyType): p is PropertyTypeList => {
314-
return Array.isArray(p);
315-
}
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+
};
316319

317320
export class Property {
318321
$id: string;
319322
token: string;
320-
type: PropertyType;
323+
type: PropertyType | PropertyType[];
321324
nullable: boolean;
322325

323326
constructor(
324327
$id: string,
325328
token: string,
326-
type: PropertyType,
329+
type: PropertyType | PropertyType[],
327330
nullable: boolean
328331
) {
329332
this.$id = $id;
@@ -333,32 +336,35 @@ export class Property {
333336
}
334337
}
335338

336-
export class PropertyBaseType {
339+
export class PrimitivePropertyType {
337340
type: PrimitivePropertyTypes;
338341
constructor(type: PrimitivePropertyTypes) {
339342
this.type = type;
340343
}
341344
}
342345

343-
export class PropertyArrayType {
344-
items: PropertyBaseType;
346+
export class PrimitiveArrayPropertyType {
347+
items: PrimitivePropertyType;
345348
type: "array";
346349

347-
constructor(items: PropertyBaseType) {
350+
constructor(items: PrimitivePropertyType) {
348351
this.type = "array";
349352
this.items = items;
350353
}
351354
}
352355

353-
export type PrimitivePropertyTypes =
354-
| "integer"
355-
| "string"
356-
| "float"
357-
| "boolean"
358-
| "point"
359-
| "date"
360-
| "datetime"
361-
| "time"
362-
| "localtime"
363-
| "localdatetime"
364-
| "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)