Skip to content

Commit

Permalink
Made type optional for schema definition (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
karelklima authored Nov 19, 2023
1 parent 43f00ed commit 40a9c1e
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 107 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"deno.enable": true,
"deno.importMap": ".vscode/import_map.json",
"deno.documentPreloadLimit": 0,
"editor.defaultFormatter": "denoland.vscode-deno"
"editor.defaultFormatter": "denoland.vscode-deno",
"editor.formatOnSave": true
}
20 changes: 4 additions & 16 deletions library/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ class Decoder {
throw new Error(`Error decoding graph, <${nodeIri}> node not found.`);
}

output.$type = this.decodeNodeType(node);

Object.keys(schema).forEach((key) => {
if (key === "@type") {
return;
Expand All @@ -97,26 +95,16 @@ class Decoder {
return output;
}

decodeNodeType(node: Node) {
const typeTerms = node.get(rdf.type);
if (!typeTerms) {
return [];
}
return typeTerms.reduce((acc, term) => {
if (term.value !== ldkit.Resource) {
acc.push(term.value);
}
return acc;
}, [] as Iri[]);
}

decodeNodeProperty(
nodeIri: Iri,
node: Node,
propertyKey: string,
property: Property,
) {
const terms = node.get(property["@id"]);
const allTerms = node.get(property["@id"]);
const terms = property["@id"] !== rdf.type
? allTerms
: allTerms?.filter((term) => term.value !== ldkit.Resource);

if (!terms) {
if (!property["@optional"]) {
Expand Down
8 changes: 2 additions & 6 deletions library/lens/query_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class QueryBuilder {
}

getQuery(where?: string | RDF.Quad[], limit = 1000) {
const selectSubQuery = SELECT`
const selectSubQuery = SELECT.DISTINCT`
${this.df.variable!("iri")}
`.WHERE`
${this.getShape(false, true)}
Expand All @@ -123,10 +123,8 @@ export class QueryBuilder {

const query = CONSTRUCT`
${this.getResourceSignature()}
${this.getTypesSignature()}
${this.getShape(true, false, true)}
`.WHERE`
${this.getTypesSignature()}
${this.getShape(true, true, true)}
{
${selectSubQuery}
Expand All @@ -139,11 +137,9 @@ export class QueryBuilder {
getByIrisQuery(iris: Iri[]) {
const query = CONSTRUCT`
${this.getResourceSignature()}
${this.getTypesSignature()}
${this.getShape(true, false, true)}
`.WHERE`
${this.getTypesSignature()}
${this.getShape(true, true, true)}
${this.getShape(true, true, false)}
VALUES ?iri {
${iris.map(this.df.namedNode)}
}
Expand Down
3 changes: 1 addition & 2 deletions library/schema/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,4 @@ export type SchemaInterface<T extends SchemaPrototype> =
? ConvertProperty<T[X]>
: never;
}
& SchemaInterfaceIdentity
& SchemaInterfaceType;
& SchemaInterfaceIdentity;
2 changes: 1 addition & 1 deletion library/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type SchemaPrototypeProperties = {
};

export type SchemaPrototypeType = {
"@type": string | readonly string[];
"@type"?: string | readonly string[];
};

export type SchemaPrototype = SchemaPrototypeProperties & SchemaPrototypeType;
Expand Down
21 changes: 19 additions & 2 deletions library/schema/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import rdf from "../namespaces/rdf.ts";
import xsd from "../namespaces/xsd.ts";

import type {
Expand All @@ -8,6 +9,20 @@ import type {
} from "./schema.ts";

export const expandSchema = (schemaPrototype: SchemaPrototype) => {
if (typeof schemaPrototype !== "object") {
throw new Error(`Invalid schema, expected object`);
}

if (Object.keys(schemaPrototype).length === 0) {
throw new Error(
`Invalid schema, empty object, expected "@type" key or property definition`,
);
}

const expandShortcut = (value: string) => {
return value === "@type" ? rdf.type : value;
};

const expandArray = <T extends string>(stringOrStrings: T | readonly T[]) => {
return Array.isArray(stringOrStrings) ? stringOrStrings : [stringOrStrings];
};
Expand All @@ -17,7 +32,7 @@ export const expandSchema = (schemaPrototype: SchemaPrototype) => {
) => {
if (typeof stringOrProperty === "string") {
return {
"@id": stringOrProperty,
"@id": expandShortcut(stringOrProperty),
"@type": xsd.string,
};
}
Expand All @@ -44,6 +59,8 @@ export const expandSchema = (schemaPrototype: SchemaPrototype) => {
const expandedProperty = Object.keys(property).reduce((acc, key) => {
if (key === "@context") {
acc[key] = expandSchema(property[key]!);
} else if (key === "@id") {
acc[key] = expandShortcut(property[key]);
} else if (validKeys.includes(key as keyof PropertyPrototype)) {
acc[key] = property[key as keyof PropertyPrototype] as unknown;
}
Expand All @@ -63,7 +80,7 @@ export const expandSchema = (schemaPrototype: SchemaPrototype) => {

return Object.keys(schemaPrototype).reduce((acc, key) => {
if (key === "@type") {
acc[key] = expandArray(schemaPrototype[key]);
acc[key] = expandArray(schemaPrototype[key]!);
} else {
acc[key] = expandSchemaProperty(
schemaPrototype[key] as string | PropertyPrototype,
Expand Down
Loading

0 comments on commit 40a9c1e

Please sign in to comment.