Skip to content

Commit

Permalink
ducktype instanceof DmnoDataType
Browse files Browse the repository at this point in the history
  • Loading branch information
theoephraim committed Sep 9, 2024
1 parent 2709e22 commit 6ba80a6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-planets-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"dmno": patch
---

fix for instanceof checks on DmnoDataType
15 changes: 9 additions & 6 deletions packages/core/src/config-engine/base-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ export type ExtractSettingsSchema<F> =


export class DmnoDataType<InstanceOptions = any> {
// NOTE - note quite sure about this setup yet...
// but the idea is to provide a wrapped version of the validate/coerce (the fns that need the type instance options)
// while providing transparent access to the rest. This is so the ConfigItem can just walk up the chain of types
// without having to understand the details... The other option is to revert that change and
/** use instead of `instanceof DmnoDataType`
* because there can be a different copy of dmno being used within vite from the dmno config loading process
* */
static checkInstanceOf(other: any) {
return other?.typeDef && other?.primitiveTypeFactory;
}


parentType?: DmnoDataType;
private _valueResolver?: ConfigValueResolver;
Expand Down Expand Up @@ -94,13 +97,13 @@ export class DmnoDataType<InstanceOptions = any> {
// deal with uninitialized case - `extends: DmnoBaseTypes.number`
} else if (_.isFunction(this.typeDef.extends)) {
const initializedDataType = this.typeDef.extends(typeInstanceOptions as any);
if (initializedDataType instanceof DmnoDataType) {
if (DmnoDataType.checkInstanceOf(initializedDataType)) {
this.parentType = initializedDataType;
} else {
throw new Error('found invalid parent (as result of fn) in extends chain');
}
// normal case - `extends: DmnoBaseTypes.number({ ... })`
} else if (this.typeDef.extends instanceof DmnoDataType) {
} else if (DmnoDataType.checkInstanceOf(this.typeDef.extends)) {
this.parentType = this.typeDef.extends;
// anything else is considered an error
} else if (this.typeDef.extends) {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/config-engine/config-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1200,15 +1200,15 @@ export class DmnoConfigItem extends DmnoConfigItemBase {
} else if (_.isFunction(defOrShorthand)) {
// in this case, we have no settings to pass through, so we pass an empty object
const shorthandFnResult = defOrShorthand({});
if (!(shorthandFnResult instanceof DmnoDataType)) {
if (!DmnoDataType.checkInstanceOf(shorthandFnResult)) {
// TODO: put this in schema error instead?
console.log(DmnoDataType, shorthandFnResult);
throw new Error('invalid schema as result of fn shorthand');
} else {
this.type = shorthandFnResult;
}
} else if (defOrShorthand instanceof DmnoDataType) {
this.type = defOrShorthand;
} else if (DmnoDataType.checkInstanceOf(defOrShorthand)) {
this.type = defOrShorthand as DmnoDataType;
} else if (_.isObject(defOrShorthand)) {
// this is the only real difference b/w the handling of extends...
// we create a DmnoDataType directly without a reusable type for the items defined in the schema directly
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/config-loader/vite-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ export async function setupViteServer(
// meaning we have 2 copies of classes and `instanceof` stops working
enforce: 'pre', // Run before the builtin 'vite:resolve' of Vite
async resolveId(source, importer, options) {
// console.log(kleur.bgCyan('PLUGIN RESOLVE!'), source, importer, options);
// console.log('PLUGIN RESOLVE!', source, importer, options);

if (source === 'dmno') {
// const resolution = await this.resolve(source, importer, options);
// console.log('resolution', resolution);
// console.log('dmno resolution', resolution);
// if (!resolution) return;

return {
// pointing at dist/index is hard-coded...
// we could extract the main entry point from the resolution instead?
id: '/node_modules/dmno/dist/index.js',
// I believe this path is appended to our "root" which is our workpace root
id: `${workspaceRootPath}/node_modules/dmno/dist/index.js`,
external: 'absolute',
};
}
},
Expand Down

0 comments on commit 6ba80a6

Please sign in to comment.