diff --git a/npm/ng-packs/packages/schematics/src/constants/volo.ts b/npm/ng-packs/packages/schematics/src/constants/volo.ts index 00fde72c79..12aa166d66 100644 --- a/npm/ng-packs/packages/schematics/src/constants/volo.ts +++ b/npm/ng-packs/packages/schematics/src/constants/volo.ts @@ -1 +1,22 @@ +import { Interface, Property } from '../models'; + export const VOLO_REGEX = /^Volo\.Abp\.(Application\.Dtos|ObjectExtending)/; + +export const VOLO_NAME_VALUE = new Interface({ + base: null, + identifier: 'NameValue', + ref: 'Volo.Abp.NameValue', + namespace: 'Volo.Abp', + properties: [ + new Property({ + name: 'name', + type: 'string', + refs: ['System.String'], + }), + new Property({ + name: 'value', + type: 'T', + refs: ['T'], + }), + ], +}); diff --git a/npm/ng-packs/packages/schematics/src/utils/model.ts b/npm/ng-packs/packages/schematics/src/utils/model.ts index 22e2b55347..2ac5949050 100644 --- a/npm/ng-packs/packages/schematics/src/utils/model.ts +++ b/npm/ng-packs/packages/schematics/src/utils/model.ts @@ -1,5 +1,5 @@ import { strings } from '@angular-devkit/core'; -import { VOLO_REGEX } from '../constants'; +import { VOLO_NAME_VALUE, VOLO_REGEX } from '../constants'; import { Interface, Model, Property, Type, TypeWithEnum } from '../models'; import { parseNamespace } from './namespace'; import { relativePathToModel } from './path'; @@ -40,8 +40,12 @@ export function createImportRefsToModelReducer(params: ModelGeneratorParams) { const index = models.findIndex(m => m.namespace === _interface.namespace); if (index > -1) { if (models[index].interfaces.some(i => i.identifier === _interface.identifier)) return; + if (_interface.ref.startsWith(VOLO_NAME_VALUE.ref)) return; + models[index].interfaces.push(_interface); } else { + if (_interface.ref.startsWith(VOLO_NAME_VALUE.ref)) _interface = VOLO_NAME_VALUE; + const { namespace } = _interface; models.push( @@ -59,6 +63,7 @@ export function createImportRefsToModelReducer(params: ModelGeneratorParams) { model.interfaces.forEach(_interface => { const { baseType } = types[_interface.ref]; + if (baseType && parseNamespace(solution, baseType) !== model.namespace) toBeImported.push({ type: baseType.split('<')[0], @@ -131,13 +136,7 @@ export function createImportRefToInterfaceReducerCreator(params: ModelGeneratorP prop.refs.forEach(type => !types[type]?.isEnum && refs.push(type)); return refs; }, []) - .concat( - base - ? parseGenerics(typeDef.baseType!) - .toGenerics() - .join('') - : [], - ) + .concat(base ? parseGenerics(typeDef.baseType!).toGenerics() : []) .reduce(reduceRefsToInterfaces, interfaces); } } diff --git a/npm/ng-packs/packages/schematics/src/utils/namespace.ts b/npm/ng-packs/packages/schematics/src/utils/namespace.ts index 78ee91a02a..88c574db8a 100644 --- a/npm/ng-packs/packages/schematics/src/utils/namespace.ts +++ b/npm/ng-packs/packages/schematics/src/utils/namespace.ts @@ -1,7 +1,7 @@ -import { createTypeParser } from './type'; +import { createTypeParser, removeGenerics } from './type'; export function parseNamespace(solution: string, type: string) { - const parseType = createTypeParser(); + const parseType = createTypeParser(removeGenerics); let namespace = parseType(type)[0] .split('.') .slice(0, -1) diff --git a/npm/ng-packs/packages/schematics/src/utils/type.ts b/npm/ng-packs/packages/schematics/src/utils/type.ts index d6e451f202..25751f475d 100644 --- a/npm/ng-packs/packages/schematics/src/utils/type.ts +++ b/npm/ng-packs/packages/schematics/src/utils/type.ts @@ -15,13 +15,18 @@ export function createTypeSimplifier() { ); return type.split('.').pop()!; }); - return (type: string) => parseType(type).join(' | '); + + return (type: string) => { + const parsed = parseType(type); + const last = parsed.pop()!; + return parsed.reduceRight((record, tKey) => `Record<${tKey}, ${record}>`, last); + }; } export function createTypeParser(replacerFn = (t: string) => t) { const normalizeType = createTypeNormalizer(replacerFn); - return (originalType: string) => flattenUnionTypes([], originalType).map(normalizeType); + return (originalType: string) => flattenDictionaryTypes([], originalType).map(normalizeType); } export function createTypeNormalizer(replacerFn = (t: string) => t) { @@ -32,14 +37,10 @@ export function createTypeNormalizer(replacerFn = (t: string) => t) { }; } -export function flattenUnionTypes(types: string[], type: string) { +export function flattenDictionaryTypes(types: string[], type: string) { type - .replace(/^{/, '') - .replace(/}$/, '') - .replace(/{/, '(') - .replace(/}/, ')') + .replace(/[}{]/g, '') .split(':') - .filter(Boolean) .forEach(t => types.push(t)); return types; @@ -50,6 +51,10 @@ export function normalizeTypeAnnotations(type: string) { return type.replace(/\?/g, ''); } +export function removeGenerics(type: string) { + return type.replace(/<.+>/g, ''); +} + export function removeTypeModifiers(type: string) { return type.replace(/\[\]/g, ''); }