Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WTF-1967]: Generate widget property types for metadata associations #106

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/pluggable-widgets-tools/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Added

- We added the property type AssociationMetaData which exposes useful metadata for filtering and sorting datasources.

## [10.15.0] - 2024-09-24

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
export const associationMetaDataWebInput = `<?xml version="1.0" encoding="utf-8"?>
<widget id="mendix.mywidget.MyWidget" needsEntityContext="true" offlineCapable="true"
pluginWidget="true" supportedPlatform="Web"
xmlns="http://www.mendix.com/widget/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mendix.com/widget/1.0/ ../xsd/widget.xsd">
<properties>
<propertyGroup caption="General">
<property key="data" type="datasource" isList="true">
<caption>Reference</caption>
<description />
</property>
<property key="metaReference" type="association" isMetaData="true" dataSource="data">
<caption>Reference</caption>
<description />
<associationTypes>
<associationType name="Reference" />
</associationTypes>
</property>
<property key="metaReferenceSet" type="association" isMetaData="true" dataSource="data">
<caption>Reference</caption>
<description />
<associationTypes>
<associationType name="ReferenceSet" />
</associationTypes>
</property>
</propertyGroup>
</properties>
</widget>`;
export const associationMetaDataNativeInput = `<?xml version="1.0" encoding="utf-8"?>
<widget id="mendix.mywidget.MyWidget" needsEntityContext="true" offlineCapable="true"
pluginWidget="true" supportedPlatform="Native"
xmlns="http://www.mendix.com/widget/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mendix.com/widget/1.0/ ../xsd/widget.xsd">
<properties>
<propertyGroup caption="General">
<property key="data" type="datasource" isList="true">
<caption>Reference</caption>
<description />
</property>
<property key="metaReference" type="association" isMetaData="true" dataSource="data">
<caption>Reference</caption>
<description />
<associationTypes>
<associationType name="Reference" />
</associationTypes>
</property>
<property key="metaReferenceSet" type="association" isMetaData="true" dataSource="data">
<caption>Reference</caption>
<description />
<associationTypes>
<associationType name="ReferenceSet" />
</associationTypes>
</property>
</propertyGroup>
</properties>
</widget>`;
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const attributeMetaDataWebInput = `<?xml version="1.0" encoding="utf-8"?>
</propertyGroup>
</properties>
</widget>`;

export const attributeMetaDataNativeInput = `<?xml version="1.0" encoding="utf-8"?>
<widget id="mendix.mywidget.MyWidget" needsEntityContext="true" offlineCapable="true"
pluginWidget="true" supportedPlatform="Native"
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Style> {
name: string;
style: Style[];
data: ListValue;
metaReference: AssociationMetaData;
metaReferenceSet: AssociationMetaData;
}`;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { WidgetXml } from "./WidgetXml";

const mxExports = [
"ActionValue",
"AssociationMetaData",
"AttributeMetaData",
"DynamicValue",
"EditableValue",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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":
Expand Down
Loading