Typings and initial test case for type-based context

main
Jackson Morgan 2 years ago
parent 74bcfd3c40
commit 0c2f981356
  1. 5
      packages/jsonld-dataset-proxy/src/ContextUtil.ts
  2. 4
      packages/jsonld-dataset-proxy/src/LdoJsonldContext.ts
  3. 3
      packages/jsonld-dataset-proxy/src/jsonldDatasetProxy.ts
  4. 91
      packages/jsonld-dataset-proxy/test/scopedExampleData.ts

@ -1,4 +1,5 @@
import type { ContextDefinition, ExpandedTermDefinition } from "jsonld"; import type { ContextDefinition, ExpandedTermDefinition } from "jsonld";
import type { LdoJsonldContext } from "./LdoJsonldContext";
// Create JSONLD Shorthands // Create JSONLD Shorthands
const shorthandToIriMap: Record<string, string> = { const shorthandToIriMap: Record<string, string> = {
@ -10,10 +11,10 @@ const shorthandToIriMap: Record<string, string> = {
* Handles the JSON-LD context and allows conversion between IRIs and terms * Handles the JSON-LD context and allows conversion between IRIs and terms
*/ */
export class ContextUtil { export class ContextUtil {
public readonly context: ContextDefinition; public readonly context: ContextDefinition | LdoJsonldContext;
private iriToKeyMap: Record<string, string>; private iriToKeyMap: Record<string, string>;
constructor(context: ContextDefinition) { constructor(context: ContextDefinition | LdoJsonldContext) {
this.context = context; this.context = context;
this.iriToKeyMap = {}; this.iriToKeyMap = {};
Object.entries(context).forEach(([contextKey, contextValue]) => { Object.entries(context).forEach(([contextKey, contextValue]) => {

@ -9,6 +9,6 @@ export interface LdoJsonldContext extends ContextDefinition {
} }
export type LdoJsonldContextExpandedTermDefinition = ExpandedTermDefinition & { export type LdoJsonldContextExpandedTermDefinition = ExpandedTermDefinition & {
"@isCollection": boolean; "@context"?: LdoJsonldContext | undefined;
"@isOptional": boolean; "@isCollection"?: boolean | undefined;
}; };

@ -4,6 +4,7 @@ import type { ContextDefinition } from "jsonld";
import { ContextUtil } from "./ContextUtil"; import { ContextUtil } from "./ContextUtil";
import { JsonldDatasetProxyBuilder } from "./JsonldDatasetProxyBuilder"; import { JsonldDatasetProxyBuilder } from "./JsonldDatasetProxyBuilder";
import { ProxyContext } from "./ProxyContext"; import { ProxyContext } from "./ProxyContext";
import type { LdoJsonldContext } from "./LdoJsonldContext";
/** /**
* Creates a JSON-LD Dataset Proxy * Creates a JSON-LD Dataset Proxy
@ -14,7 +15,7 @@ import { ProxyContext } from "./ProxyContext";
*/ */
export function jsonldDatasetProxy( export function jsonldDatasetProxy(
inputDataset: Dataset, inputDataset: Dataset,
context: ContextDefinition, context: ContextDefinition | LdoJsonldContext,
): JsonldDatasetProxyBuilder { ): JsonldDatasetProxyBuilder {
const contextUtil = new ContextUtil(context); const contextUtil = new ContextUtil(context);
const proxyContext = new ProxyContext({ const proxyContext = new ProxyContext({

@ -39,11 +39,6 @@ export type Element =
"@id": "CreativeWork"; "@id": "CreativeWork";
}; };
// No need to fully define the schema because this library doesn't use it
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
export const patientSchema: Schema = {};
export const patientContext: LdoJsonldContext = { export const patientContext: LdoJsonldContext = {
Bender: { Bender: {
"@id": "https://example.com/Bender", "@id": "https://example.com/Bender",
@ -56,46 +51,37 @@ export const patientContext: LdoJsonldContext = {
}, },
friend: { friend: {
"@id": "https://example.com/friend", "@id": "https://example.com/friend",
} "@isCollection": true,
}, },
}, },
Patient: "http://hl7.org/fhir/Patient",
subject: { "@id": "http://hl7.org/fhir/subject", "@type": "@id" },
name: {
"@id": "http://hl7.org/fhir/name",
"@type": "http://www.w3.org/2001/XMLSchema#string",
"@container": "@set",
}, },
langName: { Avatar: {
"@id": "http://hl7.org/fhir/langName", "@id": "https://example.com/Bender",
"@type": "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString", "@context": {
"@container": "@set", type: {
"@id": "@type",
},
element: {
"@id": "https://example.com/element",
"@isCollection": true,
},
friend: {
"@id": "https://example.com/friend",
"@isCollection": true,
}, },
birthdate: {
"@id": "http://hl7.org/fhir/birthdate",
"@type": "http://www.w3.org/2001/XMLSchema#date",
}, },
age: {
"@id": "http://hl7.org/fhir/age",
"@type": "http://www.w3.org/2001/XMLSchema#integer",
}, },
isHappy: { NonBender: {
"@id": "http://hl7.org/fhir/isHappy", "@id": "https://example.com/NonBender",
"@type": "http://www.w3.org/2001/XMLSchema#boolean", "@context": {
type: {
"@id": "@type",
}, },
roommate: { friend: {
"@id": "http://hl7.org/fhir/roommate", "@id": "https://example.com/friend",
"@type": "@id", "@isCollection": true,
"@container": "@set",
}, },
notes: {
"@id": "http://hl7.org/fhir/notes",
"@type": "http://www.w3.org/2001/XMLSchema#string",
}, },
langNotes: {
"@id": "http://hl7.org/fhir/langNotes",
"@type": "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString",
}, },
}; };
@ -105,32 +91,17 @@ export const patientData = `
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
example:Observation1 example:Aang a example:Avatar
fhir:notes "Cool Notes"^^xsd:string ; example:element example:Air, example:Water ;
fhir:subject example:Patient1 . example:friend example:Sokka, example:Katara .
example:Patient1
rdf:type fhir:Patient ;
fhir:name "Garrett"^^xsd:string, "Bobby"^^xsd:string, "Ferguson"^^xsd:string ;
fhir:birthdate "1986-01-01"^^xsd:date ;
fhir:age "35"^^xsd:integer ;
fhir:isHappy "true"^^xsd:boolean ;
fhir:roommate example:Patient2, example:Patient3 .
example:Patient2 example:Katara a example:Avatar
rdf:type fhir:Patient ; example:element example:Water ;
fhir:name "Rob"^^xsd:string ; example:friend example:Sokka, example:Aang .
fhir:birthdate "1987-01-01"^^xsd:date ;
fhir:age "34"^^xsd:integer ;
fhir:isHappy "false"^^xsd:boolean ;
fhir:roommate example:Patient1, example:Patient3 .
example:Patient3 example:Sokka a example:Avatar
rdf:type fhir:Patient ; example:element example:Water ;
fhir:name "Amy"^^xsd:string ; example:friend example:Sokka, example:Aang .
fhir:birthdate "1988-01-01"^^xsd:date ;
fhir:age "33"^^xsd:integer ;
fhir:isHappy "true"^^xsd:boolean .
`; `;
export const patientDataWithBlankNodes = ` export const patientDataWithBlankNodes = `

Loading…
Cancel
Save