-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Node def props: added File name expression * Record.getFileName: add original file name extension to name * update file name as dependency (WIP) * code cleanup * added multiple attribute node index * code cleanup * code cleanup --------- Co-authored-by: Stefano Ricci <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
fc251f7
commit ecc1390
Showing
20 changed files
with
278 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { NodeValueComposite } from './nodeValueComposite' | ||
|
||
export interface NodeValueFile extends NodeValueComposite { | ||
fileName?: string | ||
fileNameCalculated?: string | ||
fileSize?: number | ||
fileUuid: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export type { NodeValueCode } from './code' | ||
export type { NodeValueCoordinate } from './coordinate' | ||
export type { NodeValueFile } from './file' | ||
export type { NodeValueTaxon } from './taxon' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/record/recordNodesUpdater/recordNodeDependentsFileNamesEvaluator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { Node, NodePointer, Nodes, NodeValueFile, NodeValues } from '../../node' | ||
import { NodeDefFile, NodeDefs } from '../../nodeDef' | ||
import { NodeDefExpressionFactory } from '../../nodeDef/nodeDef' | ||
import { Survey, Surveys } from '../../survey' | ||
import { SurveyDependencyType } from '../../survey/survey' | ||
import { Dates, FileNames, Objects } from '../../utils' | ||
import { NodePointers } from '../nodePointers' | ||
import { Record } from '../record' | ||
import { RecordExpressionEvaluator } from '../recordExpressionEvaluator' | ||
import { Records } from '../records' | ||
import { throwError } from './recordNodesDependentsUpdaterCommons' | ||
import { RecordUpdateResult } from './recordUpdateResult' | ||
|
||
const recordExpressionEvaluator = new RecordExpressionEvaluator() | ||
|
||
const fileNameWithPositionSuffixRegExp = /^.+\s\[\d+\]$/ // file name like "name [1].test" | ||
|
||
const addPositionSuffix = (params: { fileName: string; position: number }) => { | ||
const { fileName, position } = params | ||
return `${fileName} [${position}]` | ||
} | ||
|
||
const calculateFileName = (params: { survey: Survey; record: Record; node: Node }): string | undefined => { | ||
const { survey, record, node } = params | ||
|
||
const nodeDef: NodeDefFile = Surveys.getNodeDefByUuid({ survey, uuid: node.nodeDefUuid }) as NodeDefFile | ||
const fileNameExpression = NodeDefs.getFileNameExpression(nodeDef) | ||
if (!fileNameExpression) return undefined | ||
|
||
const { value } = node | ||
|
||
let fileNameCalculated = recordExpressionEvaluator.evalExpression({ survey, record, node, query: fileNameExpression }) | ||
if (!fileNameCalculated) return undefined | ||
|
||
fileNameCalculated = String(fileNameCalculated) | ||
if (NodeDefs.isMultiple(nodeDef) && !fileNameWithPositionSuffixRegExp.test(fileNameCalculated)) { | ||
const index = Records.getNodeIndex({ record, node }) | ||
fileNameCalculated = addPositionSuffix({ fileName: fileNameCalculated, position: index + 1 }) | ||
} | ||
|
||
// add extension from original file name | ||
const originalFilaName = value ? (value as NodeValueFile).fileName : undefined | ||
if (Objects.isNotEmpty(originalFilaName)) { | ||
const originalExtension = FileNames.getExtension(originalFilaName) | ||
fileNameCalculated = FileNames.addExtensionIfMissing(fileNameCalculated, originalExtension) | ||
} | ||
return fileNameCalculated | ||
} | ||
|
||
const updateFileNamesInNodes = (params: { | ||
survey: Survey | ||
nodePointer: NodePointer | ||
updateResult: RecordUpdateResult | ||
sideEffect?: boolean | ||
}) => { | ||
const { survey, nodePointer, updateResult, sideEffect = false } = params | ||
|
||
const { nodeCtx, nodeDef } = nodePointer | ||
|
||
if (nodeCtx.deleted) return | ||
|
||
const expressionToEvaluate = NodeDefs.getFileNameExpression(nodeDef as NodeDefFile) | ||
if (!expressionToEvaluate) return | ||
|
||
const { record } = updateResult | ||
|
||
const nodes = NodePointers.getNodesFromNodePointers({ record, nodePointers: [nodePointer] }) | ||
|
||
nodes.forEach((node) => { | ||
try { | ||
// evaluate file name expression | ||
const fileNameCalculated = calculateFileName({ survey, record: updateResult.record, node }) | ||
const oldFileNameCalculated = NodeValues.getFileNameCalculated(node) | ||
if (fileNameCalculated !== oldFileNameCalculated) { | ||
const nodeUpdated = Nodes.mergeNodes(node, { | ||
value: { [NodeValues.valuePropsFile.fileNameCalculated]: fileNameCalculated }, | ||
updated: true, | ||
dateModified: Dates.nowFormattedForStorage(), | ||
}) | ||
updateResult.addNode(nodeUpdated, { sideEffect }) | ||
} | ||
} catch (error) { | ||
const expressionsToEvaluate = [NodeDefExpressionFactory.createInstance({ expression: expressionToEvaluate })] | ||
throwError({ | ||
error, | ||
errorKey: 'record.updateSelfAndDependentsFileNames', | ||
expressionType: SurveyDependencyType.fileName, | ||
survey, | ||
nodeDef, | ||
expressionsToEvaluate, | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
export const updateSelfAndDependentsFileNames = (params: { | ||
survey: Survey | ||
record: Record | ||
node: Node | ||
sideEffect?: boolean | ||
}) => { | ||
const { survey, record, node, sideEffect = false } = params | ||
|
||
const updateResult = new RecordUpdateResult({ record }) | ||
|
||
// 1. get dependent node pointers | ||
|
||
const nodePointersToUpdate = Records.getDependentNodePointers({ | ||
survey, | ||
record, | ||
node, | ||
dependencyType: SurveyDependencyType.fileName, | ||
includeSelf: true, | ||
}) | ||
|
||
// 2. update expr to node and dependent nodes | ||
nodePointersToUpdate.forEach((nodePointer) => { | ||
updateFileNamesInNodes({ survey, nodePointer, updateResult, sideEffect }) | ||
}) | ||
|
||
return updateResult | ||
} |
25 changes: 25 additions & 0 deletions
25
src/record/recordNodesUpdater/recordNodesDependentsUpdaterCommons.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { SystemError } from '../../error' | ||
import { NodeDef, NodeDefExpression, NodeDefProps, NodeDefType } from '../../nodeDef' | ||
import { Survey, SurveyDependencyType } from '../../survey' | ||
|
||
export const throwError = (params: { | ||
error: any | ||
errorKey: string | ||
expressionType: SurveyDependencyType | ||
survey: Survey | ||
nodeDef: NodeDef<NodeDefType, NodeDefProps> | ||
expressionsToEvaluate: NodeDefExpression[] | ||
}) => { | ||
const { error, errorKey, expressionType, survey, nodeDef, expressionsToEvaluate } = params | ||
const nodeDefName = nodeDef.props.name | ||
const expressionsString = JSON.stringify(expressionsToEvaluate) | ||
|
||
throw new SystemError(errorKey, { | ||
surveyName: survey.props.name, | ||
nodeDefName, | ||
expressionType, | ||
expressionsString, | ||
error: error.toString(), | ||
errorJson: error instanceof SystemError ? error.toJSON() : null, | ||
}) | ||
} |
Oops, something went wrong.