lbl-srg / modelica-json

Modelica to JSON Parser
Other
21 stars 17 forks source link

FE • Refactor getJsons and getSimpleJson to return JS object #233

Closed AntoineGautier closed 1 month ago

AntoineGautier commented 2 months ago

This ticket is to refactor the following functions from lib/parser.js so they return an actual JS object with the parsed data.

⚠︎ This object is an array containing the data for all Modelica classes used by the Modelica file passed as argument — even if a class is not actually parsed during the current invocation.

getJsons

The function currently declares const jsonData = [] and returns jsonData without never populating the array.

This ticket will populate jsonData so getJsons does not return an empty array.

getSimpleJson

The function returns either null or the absolute path of the Modelica file that is parsed — not including the absolute path of the classes instantiated in this file, which are only pushed to parsedFile (to avoid parsing them again).

This ticket will make getSimpleJson return an array containing the data for all Modelica classes used by the Modelica file:

The current parsedFile array can then simply be created by fetching all fullMoFilePath properties from getSimpleJson() output.

AntoineGautier commented 2 months ago

@anandkp92 Could you confirm that this is not currently implemented in a feature branch? And tell me if I'm not missing something here. I don't understand why there is often read access to the JSON files on disk instead of relying on in-memory objects. Even for simple use cases such as https://github.com/lbl-srg/modelica-json/blob/master/lib/parser.js#L114-L121

  // write the simplified json output to file
  const out = (prettyPrint === 'false') ? JSON.stringify(da) : JSON.stringify(da, null, 2)
  ut.writeFile(tempJsonPath, out)
  let allObjectsJson2
  if (outputFormat === 'semantic' || outputFormat === 'cxf') {
    const jsonOp = JSON.parse(fs.readFileSync(tempJsonPath, 'utf8'))
    allObjectsJson2 = generateAllObjectsJson(jsonOp, moFile, tempDir, prettyPrint)

Why don't we directly use allObjectsJson2 = generateAllObjectsJson(da, ...) in the code snippet above (since JSON.parse(JSON.stringify(da)) and da are equal)?

anandkp92 commented 2 months ago

Returning the json object is something that we have discussed and its in the plan of implementation. However, we write files to disk because outputs like semantic, require reading multiple json files at the same time and putting them together.

AntoineGautier commented 2 months ago

This FE will be implemented in the branch issue233_returnObject.

AntoineGautier commented 1 month ago

Closed with #234