Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made type optional for schema definition #58

Merged
merged 5 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading