Skip to content

Commit

Permalink
[refactor] Migrate to JSONSchema7, add typechecks, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
amivanoff committed Aug 23, 2024
1 parent 1511542 commit 9b85667
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 167 deletions.
37 changes: 15 additions & 22 deletions src/ObjectProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* SPDX-License-Identifier: GPL-3.0-only
********************************************************************************/
import uuid62 from 'uuid62';
import { JSONSchema6 } from 'json-schema';
import { JSONSchema7, JSONSchema7TypeName } from 'json-schema';

export function json2str(data: any): string {
if (data) return JSON.stringify(data, null, 2);
Expand All @@ -35,60 +35,53 @@ export function idComparator(a: JsObject, b: JsObject): number {
return 0;
}

export type JSONSchema6DefinitionForRdfProperty = JSONSchema6forRdfProperty;
export interface JSONSchema6forRdfProperty extends JSONSchema6 {
//uri: string;
//formatters?: { [key: string]: (value: any) => any };

export type JSONSchema7PropertyDefinition_LD = JSONSchema7Property_LD;
export interface JSONSchema7Property_LD extends JSONSchema7 {
/**
* json-ld Property
* https://github.com/json-ld/json-ld.org/blob/master/schemas/jsonld-schema.json
*/
'@id'?: string;
'@type'?: string;

// permissions extension
valueModifiability?: string; // user or non -- system
shapeModifiability?: string; // user or non -- system

contentMediaType?: string;
contentEncoding?: string;

properties?: {
[key: string]: JSONSchema6DefinitionForRdfProperty;
[key: string]: JSONSchema7PropertyDefinition_LD;
};
}

export type JSONSchema6DefinitionForRdf = JSONSchema6forRdf;
export interface JSONSchema6forRdf extends JSONSchema6, JsObject {
//uri: string;
//formatField?: (field: JSONSchema6DefinitionForRdfProperty, value: any, format?: string) => any;
export type JSONSchema7Definition_LD = JSONSchema7_LD;
export interface JSONSchema7_LD extends Omit<JSONSchema7, 'allOf'>, JsObject {
allOf?: { $ref: string }[] | undefined; // override from this: "allOf?: JSONSchema6Definition[] | undefined;"
type: JSONSchema7TypeName; // restrict from this: "type?: JSONSchema6TypeName | JSONSchema6TypeName[] | undefined;"

/**
* json-ld Node
* json-ld Node extensions
* https://github.com/json-ld/json-ld.org/blob/master/schemas/jsonld-schema.json
*/
'@context'?: JsStrObjObj; // json-ld
'@id': string; // json-ld
//'@included' // json-ld
//'@graph'?: [] | {}; // json-ld
//'@nest' // json-ld
'@type'?: string; // json-ld
'@type'?: string; // json-ld (SHACL Shape IRI -- shape IRI itself, not shaped class IRI)
//'@reverse'
//'@index'

targetClass: string;
targetClass: string; // SHACL Shape Class IRI (shaped class IRI)

// Artifact extensions
//inCreationMenu?: boolean;
//defaultFormat?: string;
//iconReference?: string;

properties: {
[key: string]: JSONSchema6DefinitionForRdfProperty;
[key: string]: JSONSchema7PropertyDefinition_LD;
};
}
export type JSONSchema6forRdf2 = JSONSchema6forRdf & {
'@id'?: string;
};

export function copyObjectProps(objTo: JsObject, objFrom: JsObject): void {
Object.keys(objFrom).forEach((objFromKey) => {
Expand Down Expand Up @@ -147,7 +140,7 @@ export function copyUniqueArrayElements(arrTo: any[], arrFrom: any[]): void {
});
}

export function getPropKeysByFormat(schema: JSONSchema6forRdf, format: string): string[] {
export function getPropKeysByFormat(schema: JSONSchema7_LD, format: string): string[] {
const props: string[] = [];
for (const key in schema.properties) {
if (schema.properties[key].format === format) {
Expand Down
14 changes: 7 additions & 7 deletions src/ObjectProviderImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
* SPDX-License-Identifier: GPL-3.0-only
********************************************************************************/
import {
JSONSchema6forRdf,
JSONSchema7_LD,
JsObject,
JSONSchema6DefinitionForRdfProperty,
JSONSchema7PropertyDefinition_LD,
copyObjectProps,
copyUniqueArrayElements,
} from './ObjectProvider';

export const schemaNonPrimitivePropsKeys = ['@context', 'properties', 'required'];

function combineProperties(oldObj: any, newObj: any, schema: JSONSchema6forRdf): any {
function combineProperties(oldObj: any, newObj: any, schema: JSONSchema7_LD): any {
const newData: any = {};
Object.keys(oldObj).forEach((key) => {
if (schema.properties && oldObj[key] !== newObj[key]) {
Expand All @@ -31,7 +31,7 @@ function combineProperties(oldObj: any, newObj: any, schema: JSONSchema6forRdf):
return newData;
}

export function createObjectWithoutRepetitions(objects: any[], schema: JSONSchema6forRdf): any[] {
export function createObjectWithoutRepetitions(objects: any[], schema: JSONSchema7_LD): any[] {
const newData = new Map();
const usedUri: any[] = [];
objects.forEach((object) => {
Expand Down Expand Up @@ -231,8 +231,8 @@ function propertyShapeToUiSchema(

export function propertyShapesToSchemaProperties(
shapeProps: any[] | undefined,
): [{ [key: string]: JSONSchema6DefinitionForRdfProperty }, JsObject, string[], JsObject] {
const schemaProps: { [key: string]: JSONSchema6DefinitionForRdfProperty } = {};
): [{ [key: string]: JSONSchema7PropertyDefinition_LD }, JsObject, string[], JsObject] {
const schemaProps: { [key: string]: JSONSchema7PropertyDefinition_LD } = {};
const schemaContexts: JsObject = {};
const schemaReqs: string[] = [];
const uiSchema: JsObject = {};
Expand Down Expand Up @@ -261,7 +261,7 @@ export function propertyShapesToSchemaProperties(
* @param schema
* @param parentSchema
*/
export function addToSchemaParentSchema(schema: JSONSchema6forRdf, parentSchema: JSONSchema6forRdf): JSONSchema6forRdf {
export function addToSchemaParentSchema(schema: JSONSchema7_LD, parentSchema: JSONSchema7_LD): JSONSchema7_LD {
const parentCtx = parentSchema['@context'];
if (parentCtx && typeof parentCtx !== 'string' && !Array.isArray(parentCtx)) {
if (!schema['@context']) schema['@context'] = {};
Expand Down
16 changes: 8 additions & 8 deletions src/SparqlGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import { BgpPattern, Generator, OptionalPattern } from 'sparqljs';

import { Bindings } from './SparqlClient';
import {
JSONSchema6forRdf,
JSONSchema6forRdfProperty,
JSONSchema7_LD,
JSONSchema7Property_LD,
JsObject,
copyObjectPropsWithRenameOrFilter,
JsStrObj,
Expand Down Expand Up @@ -69,11 +69,11 @@ export function propsToSparqlVars(entConstr: Pick<EntConstrInternal, 'props2vars
return variables;
}

export function getSchemaPropUri(schema: JSONSchema6forRdf, propertyKey: string): string | string[] | undefined {
export function getSchemaPropUri(schema: JSONSchema7_LD, propertyKey: string): string | string[] | undefined {
const properties = schema.properties;
const context = schema['@context'];
if (properties && context && typeof context !== 'string') {
const prop: JSONSchema6forRdfProperty | undefined = properties[propertyKey];
const prop: JSONSchema7Property_LD | undefined = properties[propertyKey];
if (prop !== undefined && propertyKey !== '@id') {
const propContext = (context as any)[propertyKey];
if (propContext) {
Expand All @@ -91,11 +91,11 @@ export function getSchemaPropUri(schema: JSONSchema6forRdf, propertyKey: string)
}

export function getSchemaPropType(
properties: { [key: string]: JSONSchema6forRdfProperty },
properties: { [key: string]: JSONSchema7Property_LD },
context: JsObject,
propertyKey: string,
): string | undefined {
const prop: JSONSchema6forRdfProperty | undefined = properties[propertyKey];
const prop: JSONSchema7Property_LD | undefined = properties[propertyKey];
const propContext = context[propertyKey];
if (prop && prop.type && propContext && propContext['@type']) {
if (prop.type === 'object') {
Expand Down Expand Up @@ -188,7 +188,7 @@ export interface IEntConstrJsOpt {
'@type'?: string;
'@parent'?: string;
// external properties from Sparql EntConstr could be changed by client-code only (GUI code, etc.), immutable within SPARQL generation
schema: JSONSchema6forRdf;
schema: JSONSchema7_LD;
conditions?: JsObject;
variables?: JsObject;
data?: JsObject;
Expand Down Expand Up @@ -225,7 +225,7 @@ export interface IEntConstrJs extends EntConstrData {
}
export interface EntConstrData {
// external properties from Sparql EntConstr could be changed by client-code only (GUI code, etc.), immutable within SPARQL generation
schema: JSONSchema6forRdf;
schema: JSONSchema7_LD;
conditions: JsObject;
variables?: JsObject;
data: JsObject;
Expand Down
6 changes: 3 additions & 3 deletions src/SparqlGenSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
copyObjectPropsWithRenameOrFilter,
copyUniqueObjectProps,
JsStrObjObj,
JSONSchema6forRdf,
JSONSchema7_LD,
JsStrObj,
} from './ObjectProvider';
import {
Expand Down Expand Up @@ -79,7 +79,7 @@ export async function selectObjectsArrayProperties(
for (const key of Object.keys(entConstr.schemaPropsWithArrays)) {
for (const object of objects) {
const prop = entConstr.schemaPropsWithArrays[key];
const schemaWithArrayProperty: JSONSchema6forRdf = {
const schemaWithArrayProperty: JSONSchema7_LD = {
...schema,
'@id': '_' + uuid62.v4(),
'@context': {
Expand All @@ -95,7 +95,7 @@ export async function selectObjectsArrayProperties(
if (prop && prop.type && propType) {
//let schemaUri: string | undefined;
let constrWithSchema2: EntConstrInternal | undefined;
let schema2: JSONSchema6forRdf;
let schema2: JSONSchema7_LD;
if ((prop.type === 'array' && prop.items) || prop.type === 'object') {
//schemaUri = repository.schemas.getByClassId(propType);
constrWithSchema2 = entConstrs.find((c) => c.schema.targetClass === propType);
Expand Down
8 changes: 3 additions & 5 deletions src/models/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ import { ArtifactShapeSchema, PropertyShapeSchema } from '../schema/ArtifactShap
import { JsObject } from '../ObjectProvider';
import { SparqlClient } from '../SparqlClient';

import { MstRepository } from './MstRepository';
import { MstRepository, TMstRepositorySnapshotIn } from './MstRepository';

export const rootModelInitialState: any = {
export const rootModelInitialState: TMstRepositorySnapshotIn = {
repId: '',
user: {
login: 'guest@example.com',
login: 'guest@acme.com',
name: 'Guest',
},
processArea: 'projects:defaultProject',
ns: {
current: {
rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
Expand All @@ -49,7 +48,6 @@ export const rootModelInitialState: any = {
[PropertyShapeSchema.targetClass]: PropertyShapeSchema['@id'],
},
},
//collsConstr: {},
colls: {},
};

Expand Down
2 changes: 1 addition & 1 deletion src/models/MstCollConstr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export const MstCollConstr = types
for (const key of Object.keys(entConstr.schemaPropsWithArrays)) {
for (const object of objects) {
const prop = entConstr.schemaPropsWithArrays[key];
const schemaWithArrayProperty: JSONSchema6forRdf = {
const schemaWithArrayProperty: JSONSchema7_LD = {
...schema,
'@id': '_' + uuid62.v4(),
'@context': {
Expand Down
65 changes: 4 additions & 61 deletions src/models/MstRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* SPDX-License-Identifier: GPL-3.0-only
********************************************************************************/
import { values } from 'mobx';
import { types, getSnapshot, applySnapshot, getEnv, Instance } from 'mobx-state-tree';
import { types, getSnapshot, applySnapshot, getEnv, Instance, SnapshotIn, SnapshotOut } from 'mobx-state-tree';

import { addMissingId, JsObject } from '../ObjectProvider';
import { abbreviateIri } from '../SparqlGen';
Expand All @@ -33,7 +33,6 @@ export const MstRepository = types
.model('MstRepository', {
repId: types.string,
user: User,
//processArea: types.string,
ns: MstNamespaces,
schemas: MstSchemas,

Expand All @@ -42,9 +41,6 @@ export const MstRepository = types
* Use <code>rep.getColl(iri)</code> instead of <code>rep.colls.get(iri)</code>
*/
colls: types.map(MstColl),
editingData: types.map(types.boolean),

selectedData: types.map(types.frozen<any>()),
})
/**
* Views
Expand All @@ -61,18 +57,6 @@ export const MstRepository = types
const coll = self.colls.get(iri);
return coll;
},

getSelectedDataJs(iri: string) {
if (!iri) return undefined;
const coll = this.getColl(iri);
if (!coll) return undefined;
const id = self.selectedData.get(iri);
if (id) {
const data = coll.dataByIri(id);
if (data) return getSnapshot<JsObject>(data);
}
return undefined;
},
};
})
/**
Expand Down Expand Up @@ -221,49 +205,6 @@ export const MstRepository = types
},

saveData(schemaUri: string) {},
////////// Selection Service ///////////

setSelectedData(iri: string, data: any) {
//console.log('setSelectedData START', { iri, data });
if (iri /*&& */) {
//let id: string;
//if (typeof data === 'string') {
// id = data;
//} else {
// id = data['@id'];
//
//if (id && typeof id === 'string') {
// self.selectedData.set(iri, id);
// console.log('setSelectedData UPDATE', { iri, id });
//}
self.selectedData.set(iri, data);
}
//console.log('setSelectedData END', { iri, data });
},
////////// FORM ///////////
onSaveFormData(formId: string) {},
onCancelForm(id: string) {},
setEditing(schemaUri: string, state: boolean, reset = false) {
if (self.editingData.get(schemaUri) !== state) {
self.editingData.set(schemaUri, state);
//if (schemaUri === 'root' && this.setParentEditing) {
// self.setParentEditing(state);
//}
//if (this.saveLogicTree[schemaUri] && this.saveLogicTree[schemaUri].parent) {
// this.setEditingChanges(this.saveLogicTree[schemaUri].parent as string, state, schemaUri);
//}
//if (reset) {
// this.resetEditingChanges(schemaUri);
//}
}
},
setModalVisible(uri: string, state: boolean) {},
setOnValidate(form: string, id: string, state: boolean) {},
onChangeData(path: string, data: any) {},
setSaveLogic(parent: string, child: string) {},
setEditingChanges(parentUri: string, state: boolean, childUri: string) {},
onChangeFormData(form: string, path: string, data: any) {},
resetEditingChanges(schemaUri: string) {},

/**
* Edit Connection
Expand Down Expand Up @@ -541,4 +482,6 @@ export const MstRepository = types
};
});

export type IRepository = Instance<typeof MstRepository>;
export type TMstRepository = Instance<typeof MstRepository>;
export type TMstRepositorySnapshotIn = SnapshotIn<typeof MstRepository>;
export type TMstRepositorySnapshotOut = SnapshotOut<typeof MstRepository>;
Loading

0 comments on commit 9b85667

Please sign in to comment.