Complete setRefactor pre-test

main
Jackson Morgan 7 months ago
parent f799b155e2
commit da6438c926
  1. 2
      packages/jsonld-dataset-proxy/src/ContextUtil.ts
  2. 16
      packages/jsonld-dataset-proxy/src/JsonldDatasetProxyBuilder.ts
  3. 16
      packages/jsonld-dataset-proxy/src/ProxyContext.ts
  4. 2
      packages/jsonld-dataset-proxy/src/language/languagesOf.ts
  5. 64
      packages/jsonld-dataset-proxy/src/setProxy/createNewSetProxy.ts
  6. 7
      packages/jsonld-dataset-proxy/src/subjectProxy/getValueForKey.ts
  7. 6
      packages/jsonld-dataset-proxy/src/util/addObjectToDataset.ts
  8. 10
      packages/jsonld-dataset-proxy/src/util/isProxy.ts

@ -144,7 +144,7 @@ export class ContextUtil {
/**
* Returns true if the object is a collection
*/
public isArray(key: string, typeName: NamedNode[]): boolean {
public isSet(key: string, typeName: NamedNode[]): boolean {
const relevantContext = this.getRelevantContext(key, typeName);
return !!(
relevantContext[key] &&

@ -57,14 +57,14 @@ export class JsonldDatasetProxyBuilder {
* @param graph The graph to match
*/
matchSubject<T extends ObjectLike>(
predicate: QuadMatch[1],
object?: QuadMatch[2],
graph?: QuadMatch[3],
predicate: QuadMatch[1] | undefined | null,
object: QuadMatch[2] | undefined | null,
graph: QuadMatch[3] | undefined | null,
): LdSet<T> {
return this.proxyContext.createSetProxy(
[null, predicate, object, graph],
true,
) as LdSet<T>;
) as unknown as LdSet<T>;
}
/**
@ -75,16 +75,16 @@ export class JsonldDatasetProxyBuilder {
* @param graph The graph to match
*/
matchObject<T extends ObjectLike>(
subject?: QuadMatch[0],
subject: QuadMatch[0] | undefined | null,
predicate: QuadMatch[1],
graph?: QuadMatch[3],
): T[] {
graph: QuadMatch[3] | undefined | null,
): LdSet<T> {
return this.proxyContext.createSetProxy([
subject,
predicate,
null,
graph,
]) as unknown as T[];
]) as unknown as LdSet<T>;
}
/**

@ -2,11 +2,12 @@ import type { GraphNode, QuadMatch, SubjectNode } from "@ldo/rdf-utils";
import type { BlankNode, Dataset, NamedNode } from "@rdfjs/types";
import { createSubjectHandler } from "./subjectProxy/createSubjectHandler";
import type { SubjectProxy } from "./subjectProxy/SubjectProxy";
import { SetProxy } from "./setProxy/setProxy";
import type { SetProxy } from "./setProxy/setProxy";
import type { ContextUtil } from "./ContextUtil";
import type { LanguageOrdering } from "./language/languageTypes";
import { namedNode } from "@rdfjs/data-model";
import type { RawObject } from "./util/RawObject";
import type { RawValue } from "./util/RawObject";
import { createNewSetProxy } from "./setProxy/createNewSetProxy";
export interface ProxyContextOptions {
dataset: Dataset;
@ -25,7 +26,7 @@ const rdfType = namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
*/
export class ProxyContext {
private subjectMap: Map<string, SubjectProxy> = new Map();
private setMap: Map<string, SetProxy<RawObject>> = new Map();
private setMap: Map<string, SetProxy<NonNullable<RawValue>>> = new Map();
readonly dataset: Dataset;
readonly contextUtil: ContextUtil;
@ -66,11 +67,16 @@ export class ProxyContext {
public createSetProxy(
quadMatch: QuadMatch,
isLangStringSet?: boolean,
isSubjectOriented?: boolean,
_isLangStringSet?: boolean,
): SetProxy {
const key = this.getSetKey(...quadMatch);
if (!this.setMap.has(key)) {
const proxy = new SetProxy(this, quadMatch, isLangStringSet);
const proxy = createNewSetProxy(
quadMatch,
isSubjectOriented ?? false,
this,
);
this.setMap.set(key, proxy);
}
return this.setMap.get(key)!;

@ -59,6 +59,6 @@ export function languagesOf<
subject,
predicate,
proxyContext,
proxyContext.contextUtil.isArray(key as string, rdfTypes),
proxyContext.contextUtil.isSet(key as string, rdfTypes),
) as LanguageOfConditionalReturn<SubjectObject, Key>;
}

@ -0,0 +1,64 @@
import type { QuadMatch } from "@ldo/rdf-utils";
import type { ProxyContext } from "../ProxyContext";
import type { RawObject, RawValue } from "../util/RawObject";
import type { ObjectSetProxyQuadMatch } from "./ObjectSetProxy";
import { ObjectSetProxy } from "./ObjectSetProxy";
import type { SubjectSetProxyQuadMatch } from "./SubjectSetProxy";
import { SubjectSetProxy } from "./SubjectSetProxy";
import type { WildcardObjectSetProxyQuadMatch } from "./WildcardObjectSetProxy";
import { WildcardObjectSetProxy } from "./WildcardObjectSetProxy";
import type { WildcardSubjectSetProxyQuadMatch } from "./WildcardSubjectSetProxy";
import { WildcardSubjectSetProxy } from "./WildcardSubjectSetProxy";
import type { SetProxy } from "./setProxy";
// export function createNewSetProxy<T extends NonNullable<RawValue>>(
// quadMatch: ObjectSetProxyQuadMatch,
// isSubjectOriented: false,
// proxyContext: ProxyContext,
// ): ObjectSetProxy<T>;
// export function createNewSetProxy<T extends RawObject>(
// quadMatch: SubjectSetProxyQuadMatch,
// isSubjectOriented: true,
// proxyContext: ProxyContext,
// ): SubjectSetProxy<T>;
// export function createNewSetProxy<T extends NonNullable<RawValue>>(
// quadMatch: WildcardObjectSetProxyQuadMatch,
// isSubjectOriented: false,
// proxyContext: ProxyContext,
// ): WildcardObjectSetProxy<T>;
// export function createNewSetProxy<T extends RawObject>(
// quadMatch: WildcardSubjectSetProxyQuadMatch,
// isSubjectOriented: true,
// proxyContext: ProxyContext,
// ): WildcardSubjectSetProxy<T>;
export function createNewSetProxy<T extends NonNullable<RawValue>>(
quadMatch: QuadMatch,
isSubjectOriented: boolean,
proxyContext: ProxyContext,
): SetProxy<T> {
if (!isSubjectOriented) {
if (quadMatch[0] && quadMatch[1]) {
return new ObjectSetProxy<T>(
proxyContext,
quadMatch as ObjectSetProxyQuadMatch,
);
} else {
return new WildcardObjectSetProxy<T>(
proxyContext,
quadMatch as WildcardObjectSetProxyQuadMatch,
);
}
} else {
if (quadMatch[1] && quadMatch[2]) {
return new SubjectSetProxy<T & RawObject>(
proxyContext,
quadMatch as SubjectSetProxyQuadMatch,
);
} else {
return new WildcardSubjectSetProxy<T & RawObject>(
proxyContext,
quadMatch as WildcardSubjectSetProxyQuadMatch,
);
}
}
}

@ -35,11 +35,10 @@ export function getValueForKey(
const subject = target["@id"];
const rdfType = proxyContext.getRdfType(subject);
const predicate = namedNode(contextUtil.keyToIri(key, rdfType));
if (contextUtil.isArray(key, rdfType)) {
const arrayProxy = proxyContext.createArrayProxy(
if (contextUtil.isSet(key, rdfType)) {
const arrayProxy = proxyContext.createSetProxy(
[subject, predicate, null, null],
false,
undefined,
contextUtil.isLangString(key, rdfType),
);
return arrayProxy;
@ -60,6 +59,6 @@ export function getValueForKey(
);
return thing;
} else {
return proxyContext.createArrayProxy([subject, predicate, null, null]);
return proxyContext.createSetProxy([subject, predicate, null, null]);
}
}

@ -25,7 +25,11 @@ export function addRawValueToDatasetRecursive(
const rdfType = proxyContext.getRdfType(subject);
const predicate = namedNode(contextUtil.keyToIri(key, rdfType));
// Get the Object Node
const object = getNodeFromRawValue(key, value, rdfType, proxyContext);
const object = getNodeFromRawValue(
value,
proxyContext,
contextUtil.getDataType(key, rdfType),
);
if (object == undefined) {
dataset.deleteMatches(subject, predicate);
} else if (object.termType === "Literal") {

@ -1,18 +1,18 @@
import type { ArrayProxy } from "../arrayProxy/ArrayProxy";
import { isArrayProxy } from "../arrayProxy/isArrayProxy";
import { isSetProxy } from "../setProxy/isSetProxy";
import type { SetProxy } from "../setProxy/setProxy";
import { isSubjectProxy } from "../subjectProxy/isSubjectProxy";
import type { SubjectProxy } from "../subjectProxy/SubjectProxy";
import type { ObjectLike } from "../types";
export function isProxy(
someObject?: unknown,
): someObject is ArrayProxy | SubjectProxy {
return isSubjectProxy(someObject) || isArrayProxy(someObject);
): someObject is SetProxy | SubjectProxy {
return isSubjectProxy(someObject) || isSetProxy(someObject);
}
export function getProxyFromObject(
object: ObjectLike | ObjectLike[],
): SubjectProxy | ArrayProxy {
): SubjectProxy | SetProxy {
if (!isProxy(object)) {
throw new Error(`${object} is not a Jsonld Dataset Proxy`);
}

Loading…
Cancel
Save