From 3212db1c5b72734c5566ee024f430c663e36602e Mon Sep 17 00:00:00 2001 From: Mert Karaca Date: Mon, 30 Sep 2024 11:47:36 +0200 Subject: [PATCH 1/6] Generate types for metadata association properties --- .../src/typings-generator/generate.ts | 1 + .../src/typings-generator/generateClientTypes.ts | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/pluggable-widgets-tools/src/typings-generator/generate.ts b/packages/pluggable-widgets-tools/src/typings-generator/generate.ts index d17c8381..d26e1e30 100644 --- a/packages/pluggable-widgets-tools/src/typings-generator/generate.ts +++ b/packages/pluggable-widgets-tools/src/typings-generator/generate.ts @@ -5,6 +5,7 @@ import { WidgetXml } from "./WidgetXml"; const mxExports = [ "ActionValue", + "AssociationMetaData", "AttributeMetaData", "DynamicValue", "EditableValue", diff --git a/packages/pluggable-widgets-tools/src/typings-generator/generateClientTypes.ts b/packages/pluggable-widgets-tools/src/typings-generator/generateClientTypes.ts index 1e00cf9f..f1405148 100644 --- a/packages/pluggable-widgets-tools/src/typings-generator/generateClientTypes.ts +++ b/packages/pluggable-widgets-tools/src/typings-generator/generateClientTypes.ts @@ -158,9 +158,18 @@ function toClientPropType( if (!prop.associationTypes?.length) { throw new Error("[XML] Association property requires associationTypes element"); } + + const linkedToDataSource = !!prop.$.dataSource; + if (prop.$.isMetaData === "true") { + if (!linkedToDataSource) { + throw new Error(`[XML] Association property can only have isMetaData="true" when linked to a datasource`); + } + return "AssociationMetaData"; + } + const types = prop.associationTypes .flatMap(ats => ats.associationType) - .map(at => toAssociationOutputType(at.$.name, !!prop.$.dataSource)); + .map(at => toAssociationOutputType(at.$.name, linkedToDataSource)); return toUniqueUnionType(types); } case "expression": From 0dfcefcf1a3fde2f970c9fe51245774a5a93bb2f Mon Sep 17 00:00:00 2001 From: Mert Karaca Date: Mon, 30 Sep 2024 11:50:11 +0200 Subject: [PATCH 2/6] Bonus fix: add missing statement terminator --- .../src/typings-generator/generateClientTypes.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pluggable-widgets-tools/src/typings-generator/generateClientTypes.ts b/packages/pluggable-widgets-tools/src/typings-generator/generateClientTypes.ts index f1405148..c4d6bd2d 100644 --- a/packages/pluggable-widgets-tools/src/typings-generator/generateClientTypes.ts +++ b/packages/pluggable-widgets-tools/src/typings-generator/generateClientTypes.ts @@ -141,7 +141,7 @@ function toClientPropType( if (!linkedToDataSource) { throw new Error(`[XML] Attribute property can only have isMetaData="true" when linked to a datasource`); } - return `AttributeMetaData<${unionType}>` + return `AttributeMetaData<${unionType}>`; } if (!prop.associationTypes?.length) { From a5aeacc803458a6e5a34162ddda8fda73d37688e Mon Sep 17 00:00:00 2001 From: Mert Karaca Date: Mon, 30 Sep 2024 12:18:51 +0200 Subject: [PATCH 3/6] Bonus fix: separate metadata attribute inputs with a blank line --- .../src/typings-generator/__tests__/inputs/metadata-attribute.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/pluggable-widgets-tools/src/typings-generator/__tests__/inputs/metadata-attribute.ts b/packages/pluggable-widgets-tools/src/typings-generator/__tests__/inputs/metadata-attribute.ts index 4d79de82..5a194ee3 100644 --- a/packages/pluggable-widgets-tools/src/typings-generator/__tests__/inputs/metadata-attribute.ts +++ b/packages/pluggable-widgets-tools/src/typings-generator/__tests__/inputs/metadata-attribute.ts @@ -27,6 +27,7 @@ export const attributeMetaDataWebInput = ` `; + export const attributeMetaDataNativeInput = ` Date: Mon, 30 Sep 2024 13:50:21 +0200 Subject: [PATCH 4/6] Add tests for AssociationMetaData generation --- .../typings-generator/__tests__/index.spec.ts | 12 ++++ .../__tests__/inputs/metadata-association.ts | 56 +++++++++++++++++++ .../__tests__/outputs/metadata-association.ts | 41 ++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 packages/pluggable-widgets-tools/src/typings-generator/__tests__/inputs/metadata-association.ts create mode 100644 packages/pluggable-widgets-tools/src/typings-generator/__tests__/outputs/metadata-association.ts diff --git a/packages/pluggable-widgets-tools/src/typings-generator/__tests__/index.spec.ts b/packages/pluggable-widgets-tools/src/typings-generator/__tests__/index.spec.ts index ee493c7e..c9e63fef 100644 --- a/packages/pluggable-widgets-tools/src/typings-generator/__tests__/index.spec.ts +++ b/packages/pluggable-widgets-tools/src/typings-generator/__tests__/index.spec.ts @@ -41,6 +41,8 @@ import { } from "./outputs/non-linked-list-attribute-refset"; import { attributeMetaDataNativeInput, attributeMetaDataWebInput } from "./inputs/metadata-attribute"; import { attributeMetaDataNativeOutput, attributeMetaDataWebOutput } from "./outputs/metadata-attribute"; +import { associationMetaDataNativeInput, associationMetaDataWebInput } from "./inputs/metadata-association"; +import { associationMetaDataNativeOutput, associationMetaDataWebOutput } from "./outputs/metadata-association"; describe("Generating tests", () => { it("Generates a parsed typing from XML for native", () => { @@ -212,6 +214,16 @@ describe("Generating tests", () => { const newContent = generateNativeTypesFor(attributeMetaDataNativeInput); expect(newContent).toBe(attributeMetaDataNativeOutput); }); + + it("Generates a parsed typing from XML for web using metadata association", () => { + const newContent = generateFullTypesFor(associationMetaDataWebInput); + expect(newContent).toBe(associationMetaDataWebOutput); + }); + + it("Generates a parsed typing from XML for native using metadata association", () => { + const newContent = generateNativeTypesFor(associationMetaDataNativeInput); + expect(newContent).toBe(associationMetaDataNativeOutput); + }); }); function generateFullTypesFor(xml: string) { diff --git a/packages/pluggable-widgets-tools/src/typings-generator/__tests__/inputs/metadata-association.ts b/packages/pluggable-widgets-tools/src/typings-generator/__tests__/inputs/metadata-association.ts new file mode 100644 index 00000000..874e66ed --- /dev/null +++ b/packages/pluggable-widgets-tools/src/typings-generator/__tests__/inputs/metadata-association.ts @@ -0,0 +1,56 @@ +export const associationMetaDataWebInput = ` + + + + + Reference + + + + Reference + + + + + + + Reference + + + + + + + +`; +export const associationMetaDataNativeInput = ` + + + + + Reference + + + + Reference + + + + + + + Reference + + + + + + + +`; diff --git a/packages/pluggable-widgets-tools/src/typings-generator/__tests__/outputs/metadata-association.ts b/packages/pluggable-widgets-tools/src/typings-generator/__tests__/outputs/metadata-association.ts new file mode 100644 index 00000000..0a093214 --- /dev/null +++ b/packages/pluggable-widgets-tools/src/typings-generator/__tests__/outputs/metadata-association.ts @@ -0,0 +1,41 @@ +export const associationMetaDataWebOutput = `/** + * This file was generated from MyWidget.xml + * WARNING: All changes made to this file will be overwritten + * @author Mendix Widgets Framework Team + */ +import { CSSProperties } from "react"; +import { AssociationMetaData, ListValue } from "mendix"; + +export interface MyWidgetContainerProps { + name: string; + class: string; + style?: CSSProperties; + tabIndex?: number; + data: ListValue; + metaReference: AssociationMetaData; + metaReferenceSet: AssociationMetaData; +} + +export interface MyWidgetPreviewProps { + /** + * @deprecated Deprecated since version 9.18.0. Please use class property instead. + */ + className: string; + class: string; + style: string; + styleObject?: CSSProperties; + readOnly: boolean; + renderMode?: "design" | "xray" | "structure"; + data: {} | { caption: string } | { type: string } | null; + metaReference: string; + metaReferenceSet: string; +} +`; + +export const associationMetaDataNativeOutput = `export interface MyWidgetProps