Skip to content

Commit

Permalink
fix ts type gen bug with enums (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
theoephraim authored Feb 2, 2025
1 parent 0e794cd commit 5a32b23
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changeset/ten-socks-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@dmno/configraph": patch
"dmno": patch
---

fix typegen enum issue
7 changes: 7 additions & 0 deletions example-repo/.dmno/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ export default defineDmnoService({
extends: DmnoBaseTypes.md5,
value: 'd41d8cd98f00b204e9800998ecf8427e'
},

SOME_ENUM: {
extends: DmnoBaseTypes.enum({
opt1: { description: 'something' },
opt2: { description: 'something else' },
}),
}
}
});

16 changes: 8 additions & 8 deletions packages/configraph/src/data-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1087,20 +1087,20 @@ type EnumDataTypeSettings = (
| Record<string, Omit<ExtendedEnumDescription, 'value'>>
);

const EnumDataType = createConfigraphDataType((settings?: EnumDataTypeSettings) => ({
const EnumDataType = createConfigraphDataType((enumOptions?: EnumDataTypeSettings) => ({
typeLabel: 'dmno/enum',
extends: PrimitiveBaseType,
injectable: false,
ui: { icon: 'material-symbols-light:category' }, // a few shapes... not sure about this one
validate(val) {
let possibleValues: Array<any>;
if (_.isPlainObject(settings)) {
possibleValues = _.keys(settings);
} else if (_.isArray(settings)) {
if (_.isObject(settings[0]) && 'value' in settings[0]) {
possibleValues = _.map(settings, (i) => (i as any).value);
if (_.isPlainObject(enumOptions)) {
possibleValues = _.keys(enumOptions);
} else if (_.isArray(enumOptions)) {
if (_.isObject(enumOptions[0]) && 'value' in enumOptions[0]) {
possibleValues = _.map(enumOptions, (i) => (i as any).value);
} else {
possibleValues = settings;
possibleValues = enumOptions;
}
}
possibleValues ||= [];
Expand All @@ -1110,7 +1110,7 @@ const EnumDataType = createConfigraphDataType((settings?: EnumDataTypeSettings)
});
}
},
_rawEnumOptions: settings,
_rawEnumOptions: enumOptions,
}));

const EMAIL_REGEX = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
Expand Down
21 changes: 18 additions & 3 deletions packages/configraph/src/type-generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export async function getTsDefinitionForNode(item: ConfigraphNode, indentLevel =
itemTsType = 'boolean';
} else if (baseType === ConfigraphBaseTypes.enum) {
// enums have several different formats we need to handle
const rawEnumOptions = (item.type.primitiveType as any)._rawEnumOptions;
const rawEnumOptions = (item.type.primitiveType as any).typeDef._rawEnumOptions;
let enumOptions = [] as Array<any>;
if (_.isArray(rawEnumOptions)) {
// extended definition case
Expand All @@ -176,10 +176,25 @@ export async function getTsDefinitionForNode(item: ConfigraphNode, indentLevel =
enumOptions = _.keys(rawEnumOptions);
}

itemTsType = _.map(enumOptions, JSON.stringify).join(' | ');
if (!enumOptions.length) {
itemTsType = 'never'; // should it be any instead?
} else {
// we could spit out descriptions in comments here, although currently it does nothing
// see https://github.com/microsoft/TypeScript/issues/38106
itemTsType = _.map(enumOptions, JSON.stringify).join(' | ');
}
} else if (baseType === ConfigraphBaseTypes.object) {
// TODO: deal with object types here!
// TODO: deal with object children
itemTsType = '{}';
} else if (baseType === ConfigraphBaseTypes.array) {
// TODO: deal with array item type
itemTsType = 'Array<any>';
} else if (baseType === ConfigraphBaseTypes.dictionary) {
// TODO: deal with record child type
itemTsType = 'Record<string, any>';
} else {
// throw?
itemTsType = 'any';
}
// TODO: deal with array and map types here!

Expand Down

0 comments on commit 5a32b23

Please sign in to comment.