UNPKG

@finos/legend-extension-dsl-data-quality

Version:
197 lines 8.45 kB
/** * Copyright (c) 2026-present, Goldman Sachs * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { observable, action, computed, makeObservable, flow } from 'mobx'; import { LambdaEditorState } from '@finos/legend-query-builder'; import { SUPPORTED_TYPES } from '../constants/DataQualityConstants.js'; import { getPrimitiveTypeEnumFromPrecisePrimitiveTypeEnum, isStubbed_RawLambda, RawLambda, } from '@finos/legend-graph'; import { dataQualityRelationValidation_setAssertion, dataQualityRelationValidation_setDescription, dataQualityRelationValidation_setName, } from '../../graph-manager/DSL_DataQuality_GraphModifierHelper.js'; import { assertErrorThrown, debounce, guaranteeNonNullable, } from '@finos/legend-shared'; import { DataQualityValidationLambdaFormState } from './DataQualityValidationLambdaFormState.js'; export var LambdaEditorType; (function (LambdaEditorType) { LambdaEditorType["TEXT"] = "TEXT"; LambdaEditorType["GUI"] = "GUI"; })(LambdaEditorType || (LambdaEditorType = {})); export class LambdaEditorWithGUIState extends LambdaEditorState { isGUISupportedLambda = false; editorType = LambdaEditorType.TEXT; isStub = false; dataQualityValidationLambdaFormState; disableEditorToggle = false; columnOptions = []; isCurrentNameSameAsComputed = false; debouncedHandleValidationFormChange; constructor(lambdaString, lambdaPrefix) { super(lambdaString, lambdaPrefix); makeObservable(this, { changeEditorType: action, canEditInGUI: computed, isTextEditor: computed, isGUIEditor: computed, toggleEditorMode: action, dataQualityValidationLambdaFormState: observable, disableEditorToggle: observable, setColumnOptions: action, columnOptions: observable, initializeGUIEditor: action, editorType: observable, isGUISupportedLambda: observable, handleValidationFormChange: flow, tryParsingPureLambdaToGUIFormat: action, isCurrentNameSameAsComputed: observable, checkIfNameIsComputed: action, convertStubLambdaToValidationLambdaFormRule: action, setDataQualityValidationLambdaFormState: action, }); this.debouncedHandleValidationFormChange = debounce(() => { this.handleValidationFormChange(); }, 2000); } *handleValidationFormChange() { try { const lambda = this.dataQualityValidationLambdaFormState?.toPureLambdaObject(); dataQualityRelationValidation_setAssertion(this.relationValidation, new RawLambda(lambda?.parameters, lambda?.body)); yield this.convertLambdaObjectToGrammarString(); yield this.convertLambdaGrammarStringToObject(); dataQualityRelationValidation_setDescription(this.relationValidation, guaranteeNonNullable(this.dataQualityValidationLambdaFormState?.getDescription())); if (this.isCurrentNameSameAsComputed) { dataQualityRelationValidation_setName(this.relationValidation, guaranteeNonNullable(this.dataQualityValidationLambdaFormState?.getSuggestedName())); } else { this.checkIfNameIsComputed(); } } catch (error) { assertErrorThrown(error); } } get canEditInGUI() { return this.isGUISupportedLambda && !!this.columnOptions.length; } setDataQualityValidationLambdaFormState() { this.dataQualityValidationLambdaFormState = new DataQualityValidationLambdaFormState(this.editorStore.graphManagerState, this.editorStore.changeDetectionState.observerContext); } initializeGUIEditor(columns) { this.setColumnOptions(columns); this.setDataQualityValidationLambdaFormState(); const isSuccessful = this.tryParsingPureLambdaToGUIFormat(); if (isSuccessful) { this.changeEditorType(LambdaEditorType.GUI); this.checkIfNameIsComputed(); } else { this.changeEditorType(LambdaEditorType.TEXT); } } checkIfNameIsComputed() { if (this.dataQualityValidationLambdaFormState) { this.isCurrentNameSameAsComputed = this.relationValidation.name === this.dataQualityValidationLambdaFormState.getSuggestedName(); } } tryParsingPureLambdaToGUIFormat() { try { if (this.columnOptions.length) { if (isStubbed_RawLambda(this.relationValidation.assertion)) { this.convertStubLambdaToValidationLambdaFormRule(); } else { this.convertLambdaToValidationLambdaFormRule(); } this.isGUISupportedLambda = true; return true; } else { throw Error('Could not initiate graphical validation editor, no columns found. Please make sure query is running properly'); } } catch (error) { assertErrorThrown(error); this.editorStore.applicationStore.notificationService.notifyError(error.message); this.isGUISupportedLambda = false; return false; } } changeEditorType(newType) { if (newType === LambdaEditorType.GUI && !this.canEditInGUI) { this.editorType = LambdaEditorType.TEXT; return; } this.editorType = newType; } get isTextEditor() { return this.editorType === LambdaEditorType.TEXT; } get isGUIEditor() { return this.editorType === LambdaEditorType.GUI; } getLambdaBody() { const lambda = this.relationValidation.assertion; return (Array.isArray(lambda.body) ? lambda.body[0] : lambda.body); } toggleEditorMode() { if (this.isTextEditor) { if (this.canEditInGUI) { this.changeEditorType(LambdaEditorType.GUI); this.convertLambdaToValidationLambdaFormRule(); this.checkIfNameIsComputed(); this.disableEditorToggle = false; } } else { this.changeEditorType(LambdaEditorType.TEXT); } } convertLambdaToValidationLambdaFormRule() { const lambdaBody = this.getLambdaBody(); if (!this.dataQualityValidationLambdaFormState) { return; } const processLambdaBody = (body) => { const { _type: type, parameters } = body; if (!this.dataQualityValidationLambdaFormState) { return; } if (type === SUPPORTED_TYPES.FUNCTION) { this.dataQualityValidationLambdaFormState.addRuleFunction(body); parameters .filter((param) => param._type === SUPPORTED_TYPES.FUNCTION) .forEach((param) => { processLambdaBody(param); }); } }; processLambdaBody(lambdaBody); this.dataQualityValidationLambdaFormState.setRootParameters(this.relationValidation.assertion.parameters); } convertStubLambdaToValidationLambdaFormRule() { this.dataQualityValidationLambdaFormState = new DataQualityValidationLambdaFormState(this.editorStore.graphManagerState, this.editorStore.changeDetectionState.observerContext); } setColumnOptions(columns) { this.columnOptions = columns.map(({ name, type, multiplicity }) => { return { value: name, label: name, type: getPrimitiveTypeEnumFromPrecisePrimitiveTypeEnum(type), isOptional: multiplicity.lowerBound === 0, }; }); } } //# sourceMappingURL=LambdaEditorWithGUIState.js.map