Skip to content

Commit

Permalink
Added basic support for Iri data type in resource schema
Browse files Browse the repository at this point in the history
  • Loading branch information
karelklima committed Oct 29, 2021
1 parent a1e620d commit d704ac8
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions library/resource/src/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import type { Iri } from "./iri";
import type { Property, Schema } from "@ldkit/schema";
import { fromRdf, Literal, Graph, NamedNode } from "@ldkit/rdf";
import { fromRdf, Graph, NamedNode, Term } from "@ldkit/rdf";

type EntityData = {
schema: Schema;
graph: Graph;
pointer: Iri;
};

const resolveTerm = (term: Term) => {
if (term.termType === "NamedNode") {
return term.value;
} else if (term.termType === "Literal") {
return fromRdf(term);
} else {
throw new Error(`Unsupported term type to resolve: ${term.termType}`);
}
};

const proxyHandler = {
get: (target: EntityData, propertyAlias: string): any => {
console.warn("PROXY GET", target.pointer, target.graph, propertyAlias);
Expand Down Expand Up @@ -64,14 +74,12 @@ const proxyHandler = {
}
if (property["@meta"].includes("@array")) {
// console.log("ARRAY", proxyValue);
return proxyValue.map((item) => fromRdf(item as Literal));
return proxyValue.map(resolveTerm);
} else {
// console.log("SINGLE", proxyValue);
return Array.isArray(proxyValue)
? fromRdf(proxyValue[0] as Literal)
: fromRdf(proxyValue);
//console.log(val.value)
//return fromRdf(val as Literal)
return resolveTerm(
Array.isArray(proxyValue) ? proxyValue[0] : proxyValue
);
}
},
};
Expand Down

0 comments on commit d704ac8

Please sign in to comment.