Skip to content

Commit e8fddad

Browse files
committed
Generate types for single object data sources
1 parent 084165d commit e8fddad

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

packages/pluggable-widgets-tools/src/typings-generator/generate.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const importableModules = [
2525
"ListWidgetValue",
2626
"NativeIcon",
2727
"NativeImage",
28+
"ObjectItem",
2829
"Option",
2930
"ReferenceSetValue",
3031
"ReferenceValue",

packages/pluggable-widgets-tools/src/typings-generator/generateClientTypes.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ export function hasOptionalDataSource(prop: Property, resolveProp: (key: string)
100100
return prop.$.dataSource && resolveProp(prop.$.dataSource)?.$.required === "false";
101101
}
102102

103+
function isLinkedToListDataSource(prop: Property, resolveProp: (key: string) => Property | undefined): boolean {
104+
return prop.$.dataSource != null && resolveProp(prop.$.dataSource)?.$.isList === "true";
105+
}
106+
103107
function toActionVariablesOutputType(actionVariables?: ActionVariableTypes[]) {
104108
const types = actionVariables?.flatMap(av => av.actionVariable)
105109
.map(avt => `${avt.$.key}: ${toOption(toAttributeClientType(avt.$.type))}`)
@@ -121,9 +125,9 @@ function toClientPropType(
121125
return "string";
122126
case "action":
123127
const variableTypes = toActionVariablesOutputType(prop.actionVariables);
124-
return (prop.$.dataSource ? "ListActionValue" : "ActionValue") + variableTypes;
128+
return (isLinkedToListDataSource(prop, resolveProp) ? "ListActionValue" : "ActionValue") + variableTypes;
125129
case "textTemplate":
126-
return prop.$.dataSource ? "ListExpressionValue<string>" : "DynamicValue<string>";
130+
return isLinkedToListDataSource(prop, resolveProp) ? "ListExpressionValue<string>" : "DynamicValue<string>";
127131
case "integer":
128132
return "number";
129133
case "decimal":
@@ -135,7 +139,7 @@ function toClientPropType(
135139
case "file":
136140
return prop.$.allowUpload ? "EditableFileValue" : "DynamicValue<FileValue>";
137141
case "datasource":
138-
return "ListValue";
142+
return prop.$.isList === "true" ? "ListValue" : "DynamicValue<ObjectItem>";
139143
case "attribute": {
140144
if (!prop.attributeTypes?.length) {
141145
throw new Error("[XML] Attribute property requires attributeTypes element");
@@ -144,22 +148,22 @@ function toClientPropType(
144148
.flatMap(ats => ats.attributeType)
145149
.map(at => toAttributeClientType(at.$.name));
146150
const unionType = toUniqueUnionType(types);
147-
const linkedToDataSource = !!prop.$.dataSource;
151+
const linkedToListDS = isLinkedToListDataSource(prop, resolveProp);
148152

149153
if (prop.$.isMetaData === "true") {
150-
if (!linkedToDataSource) {
154+
if (!prop.$.dataSource) {
151155
throw new Error(`[XML] Attribute property can only have isMetaData="true" when linked to a datasource`);
152156
}
153157
return `AttributeMetaData<${unionType}>`;
154158
}
155159

156160
if (!prop.associationTypes?.length) {
157-
return toAttributeOutputType("Reference", linkedToDataSource, unionType);
161+
return toAttributeOutputType("Reference", linkedToListDS, unionType);
158162
}
159163
else {
160164
const reftypes = prop.associationTypes
161165
.flatMap(ats => ats.associationType)
162-
.map(at => toAttributeOutputType(at.$.name, linkedToDataSource, unionType));
166+
.map(at => toAttributeOutputType(at.$.name, linkedToListDS, unionType));
163167
return toUniqueUnionType(reftypes);
164168
}
165169
}
@@ -168,25 +172,25 @@ function toClientPropType(
168172
throw new Error("[XML] Association property requires associationTypes element");
169173
}
170174

171-
const linkedToDataSource = !!prop.$.dataSource;
175+
const linkedToListDS = isLinkedToListDataSource(prop, resolveProp);
172176
if (prop.$.isMetaData === "true") {
173-
if (!linkedToDataSource) {
177+
if (!prop.$.dataSource) {
174178
throw new Error(`[XML] Association property can only have isMetaData="true" when linked to a datasource`);
175179
}
176180
return "AssociationMetaData";
177181
}
178182

179183
const types = prop.associationTypes
180184
.flatMap(ats => ats.associationType)
181-
.map(at => toAssociationOutputType(at.$.name, linkedToDataSource));
185+
.map(at => toAssociationOutputType(at.$.name, linkedToListDS));
182186
return toUniqueUnionType(types);
183187
}
184188
case "expression":
185189
if (!prop.returnType || prop.returnType.length === 0) {
186190
throw new Error("[XML] Expression property requires returnType element");
187191
}
188192
const type = toExpressionClientType(prop.returnType[0], resolveProp);
189-
return prop.$.dataSource ? `ListExpressionValue<${type}>` : `DynamicValue<${type}>`;
193+
return isLinkedToListDataSource(prop, resolveProp) ? `ListExpressionValue<${type}>` : `DynamicValue<${type}>`;
190194
case "enumeration":
191195
const typeName = capitalizeFirstLetter(prop.$.key) + "Enum";
192196
generatedTypes.push(generateEnum(typeName, prop));
@@ -208,7 +212,7 @@ ${generateClientTypeBody(childProperties, isNative, generatedTypes, resolveChild
208212
);
209213
return prop.$.isList === "true" ? `${childType}[]` : childType;
210214
case "widgets":
211-
return prop.$.dataSource ? "ListWidgetValue" : "ReactNode";
215+
return isLinkedToListDataSource(prop, resolveProp) ? "ListWidgetValue" : "ReactNode";
212216
case "selection":
213217
if (!prop.selectionTypes?.length) {
214218
throw new Error("[XML] Selection property requires selectionTypes element");

0 commit comments

Comments
 (0)