Skip to content

Commit

Permalink
fighting the jsonforms
Browse files Browse the repository at this point in the history
  • Loading branch information
stan-dot committed Feb 24, 2025
1 parent e532bad commit 17ad041
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 24 deletions.
14 changes: 9 additions & 5 deletions apps/gda-scan-definition/app/forms-editor/schemaToUiSchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ export function convertSchemaToUiSchema(schema: JsonSchema): UiSchema {
elements: [],
};

console.dir(schema)

// Recursive helper to handle nested properties
function processProperties(schema: JsonSchema, parentPath = '#/properties'): any {
const elements: any[] = [];

Object.keys(schema.properties || {}).forEach((property) => {
const propertySchema = schema.properties[property];
console.log(`noew another property: ${property}`)
console.dir(propertySchema);

// Check if the property is a nested object (with its own properties)
if (propertySchema.type === 'object' && propertySchema.properties) {
Expand All @@ -50,7 +54,7 @@ export function convertSchemaToUiSchema(schema: JsonSchema): UiSchema {
},
},
};
console.log(`trying to render array: ${property}, with ${nestedArrayUiSchema.type}, ${nestedArrayUiSchema.scope}`);
// console.log(`trying to render array: ${property}, with ${nestedArrayUiSchema.type}, ${nestedArrayUiSchema.scope}`);
elements.push(nestedArrayUiSchema);
} else {
// Add a control element for simple types
Expand All @@ -63,8 +67,8 @@ export function convertSchemaToUiSchema(schema: JsonSchema): UiSchema {
}
});

console.log(`elements: ${elements}`);
console.dir(elements, {depth: null});
// console.log(`elements: ${elements}`);
// console.dir(elements, {depth: null});
return elements;
}

Expand Down Expand Up @@ -97,5 +101,5 @@ const schema = {
required: ['name', 'due_date'],
};

const uischema = convertSchemaToUiSchema(schema);
console.log(JSON.stringify(uischema, null, 2));
// const uischema = convertSchemaToUiSchema(schema);
// console.log(JSON.stringify(uischema, null, 2));
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React, { useEffect, useState } from "react";
import { JsonForms } from "@jsonforms/react";
import { materialRenderers } from "@jsonforms/material-renderers";
import { materialRenderers, MaterialArrayControlRenderer } from "@jsonforms/material-renderers";
import { fullQexafsSchema, fullQexafsUiSchema, FullQexafsSchemaType, fullQexafsJson } from "../../schemas/qexafs";
import { useQexafsState, useQexafsDispatch, startConfigRead, startConfigUpdate } from "./QexafsContextProvider";

Expand Down
60 changes: 50 additions & 10 deletions apps/gda-scan-definition/app/qexafs/server-xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,39 @@ import fs from "fs";
import { detectorParametersSchema, DetectorsSchema, motorPositionSchema, outputParametersSchema, OutputParametersType, qexafsParametersSchema, QexafsParametersType, sampleParametersSchema, SampleParametersType } from "../schemas/qexafs";
import { rootDirectory } from "./server-safety";

/**
* todo this might need deletion as the auto parser does ok and it's the jsonform that fails
* @param xml
* @param tagName
* @param listName
* @returns
*/
function fixXmlToWrapInList(xml: string, tagName:string, listName:string): string {
// Find the first occurrence of <myTagItem>
const openingTag = `<${tagName}>`;
const closingTag = `</${tagName}>`;
const listOpeningTag = `<${listName}>`;
const listClosingTag = `</${listName}>`;

const firstIndex = xml.indexOf(openingTag);
if (firstIndex === -1) {
// No <myTagItem> found, return the original XML
return xml;
}

// Insert <myTagList> before the first <myTagItem>
let fixedXml = xml.substring(0, firstIndex) + listOpeningTag + xml.substring(firstIndex);

// Find the last occurrence of </myTagItem> to close the list
const lastIndex = fixedXml.lastIndexOf(closingTag);
if (lastIndex !== -1) {
// Insert </myTagList> after the last </myTagItem>
fixedXml = fixedXml.substring(0, lastIndex + closingTag.length) + listClosingTag + fixedXml.substring(lastIndex + closingTag.length);
}

return fixedXml;
}

const options: XmlBuilderOptions = {
// processEntities: false,
// preserveOrder: true,
Expand All @@ -17,8 +50,8 @@ const parserOptions = {
ignoreAttributes: false, // Keep attributes if present
// alwaysCreateTextNode: true, // Ensures text nodes are explicitly stored
isArray: (name: string, jpath) => {
return false;
// Define elements that should be arrays when duplicated
console.log(`arrach check for : ${name}, ${jpath}`);
const arrayFields = ["sampleParameterMotorPosition", "detectorConfiguration"]; // Example duplicated elements
return arrayFields.includes(name); // Only convert these to arrays
},
Expand Down Expand Up @@ -80,7 +113,6 @@ export function readQexafsParameters(): QexafsParametersType {
const content = fs.readFileSync(qexafsPath);
const parsedResult = parser.parse(content.toString());

console.log("Parsed result", parsedResult);
const p = parsedResult.QEXAFSParameters;
try {
const params: QexafsParametersType = qexafsParametersSchema.parse(p);
Expand All @@ -93,9 +125,14 @@ export function readQexafsParameters(): QexafsParametersType {

export function readDetectorParameters(): DetectorsSchema {
const content = fs.readFileSync(detectorsPath);
const parsedResult = parser.parse(content.toString());

const p = parsedResult.DetectorParameters;
const fixed = fixXmlToWrapInList(content.toString(), "detectorConfiguration", "detectorConfigurationList" )
const parsedResult = parser.parse(fixed);
console.dir(parsedResult);
let p = parsedResult.DetectorParameters;
const r = parsedResult.DetectorParameters.detectorConfigurationList.detectorConfiguration;
console.dir(r);
delete p.detectorConfigurationList;
p.detectorConfiguration = r;
console.log("Parsed result", parsedResult);
try {
const params: DetectorsSchema = detectorParametersSchema.parse(p);
Expand All @@ -108,15 +145,18 @@ export function readDetectorParameters(): DetectorsSchema {

export function readSampleParameters(): SampleParametersType {
const content = fs.readFileSync(samplePath);
const parsedResult = parser.parse(content.toString());
const fixed = fixXmlToWrapInList(content.toString(), "sampleParameterMotorPosition", "sampleParameterMotorPositionList" )
const parsedResult = parser.parse(fixed);

console.log("Parsed result", parsedResult);
const p = parsedResult.B18SampleParameters;
let p = parsedResult.B18SampleParameters;
const r = parsedResult.B18SampleParameters.sampleParameterMotorPositionList.sampleParameterMotorPosition;
delete p.sampleParameterMotorPositionList
p.sampleParameterMotorPosition = r;
try {
const sampleParameters: SampleParametersType = sampleParametersSchema.parse(p);
const motors = sampleParameters.sampleParameterMotorPosition.map(i => motorPositionSchema.parse(i));
console.log(`motor params: ${Object.keys(sampleParameters.sampleParameterMotorPosition[0])}`)
sampleParameters.sampleParameterMotorPosition = motors;
console.log(sampleParameters.sampleParameterMotorPosition);
return sampleParameters
} catch (e) {
console.log("Error", e);
Expand All @@ -133,7 +173,7 @@ export function readOutputParameters(): OutputParametersType {
const params: OutputParametersType = outputParametersSchema.parse(p);
return params
} catch (e) {
console.log("Error", e);
console.error("Error", e);
}
throw new Error("Failed to parse output parameters at path " + outputPath);
}
12 changes: 6 additions & 6 deletions apps/gda-scan-definition/app/schemas/qexafs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,18 @@ export const detectorConfigurationSchema = z.object({
});


export const detectorConfigurationJson: JsonSchema = zodToJsonSchema(detectorConfigurationSchema) as unknown as JsonSchema;
export const detectorConfigurationUiSchema = convertSchemaToUiSchema(detectorConfigurationJson);
export type DetectorConfiguration = z.infer<typeof detectorConfigurationSchema>;
export type DetectorsSchema = z.infer<typeof detectorParametersSchema>;

// Main schema for DetectorParameters
// todo missing the definition for the full detector parameters schema
export const detectorParametersSchema = z.object({
shouldValidate: z.boolean(),
detectorConfiguration: z.array(detectorConfigurationSchema),
});

export const detectorConfigurationJson: JsonSchema = zodToJsonSchema(detectorConfigurationSchema) as unknown as JsonSchema;
export const detectorConfigurationUiSchema = convertSchemaToUiSchema(detectorConfigurationJson);
export type DetectorConfiguration = z.infer<typeof detectorConfigurationSchema>;
export type DetectorsSchema = z.infer<typeof detectorParametersSchema>;

export const outputParametersSchema = z.object({
shouldValidate: z.boolean(),
asciiFileName: z.string(),
Expand Down Expand Up @@ -342,7 +342,7 @@ export const detectorsDefinition: FormFileDefinition = {
uiSchema: detectorConfigurationUiSchema,
}

console.dir(detectorConfigurationJson, {depth: null})
// console.dir(detectorConfigurationJson, {depth: null})

export const qexafsDefinition: FormFileDefinition = {
fileName: "QEXAFS_Parameters",
Expand Down
2 changes: 0 additions & 2 deletions apps/gda-scan-definition/app/scripting/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Container } from "@mui/material";
import FileExplorer from "../components/FileExplorer";
import RunScanForm from "../components/forms/RunScanForm";
import IDE from "../components/IDE";
import { IDEProvider } from "../components/ideReducer";
import styles from "../page.module.css";
import PythonEditor from "./components/PythonEditor";
Expand Down

0 comments on commit 17ad041

Please sign in to comment.