Handle UID attributes when generating attributes types definition

This commit is contained in:
Convly 2022-07-06 15:58:46 +02:00
parent dde36c88f1
commit d587de6b19
2 changed files with 37 additions and 10 deletions

View File

@ -18,31 +18,31 @@ export interface UIDAttributeOptions {
} }
export interface UIDAttributeProperties< export interface UIDAttributeProperties<
// UID options
S extends UIDAttributeOptions = UIDAttributeOptions,
// Own Schema Reference // Own Schema Reference
T extends SchemaUID | undefined = undefined, T extends SchemaUID | undefined = undefined,
// Target attribute // Target attribute
U extends T extends SchemaUID U extends T extends SchemaUID
? GetAttributesKeysByType<T, 'string' | 'text'> ? GetAttributesKeysByType<T, 'string' | 'text'>
: undefined = undefined : undefined = undefined,
// UID options
S extends UIDAttributeOptions = UIDAttributeOptions
> { > {
targetField?: U; targetField?: U;
options?: S; options?: UIDAttributeOptions & S;
} }
export type UIDAttribute< export type UIDAttribute<
// UID options
S extends UIDAttributeOptions = UIDAttributeOptions,
// Own Schema Reference // Own Schema Reference
T extends SchemaUID | undefined = undefined, T extends SchemaUID | undefined = undefined,
// Target attribute // Target attribute
U extends T extends SchemaUID U extends T extends SchemaUID
? GetAttributesKeysByType<T, 'string' | 'text'> ? GetAttributesKeysByType<T, 'string' | 'text'>
: undefined = undefined : undefined = undefined,
// UID options
S extends UIDAttributeOptions = UIDAttributeOptions
> = Attribute<'uid'> & > = Attribute<'uid'> &
// Properties // Properties
UIDAttributeProperties<S, T, U> & UIDAttributeProperties<T, U, S> &
// Options // Options
ConfigurableOption & ConfigurableOption &
DefaultOption<UIDValue> & DefaultOption<UIDValue> &

View File

@ -1,5 +1,6 @@
'use strict'; 'use strict';
const ts = require('typescript');
const { factory } = require('typescript'); const { factory } = require('typescript');
const _ = require('lodash/fp'); const _ = require('lodash/fp');
@ -185,8 +186,34 @@ const mappers = {
decimal() { decimal() {
return ['DecimalAttribute']; return ['DecimalAttribute'];
}, },
uid() { uid({ attribute, uid }) {
const { targetField, options } = attribute;
// If there are no params to compute, then return the attribute type alone
if (targetField === undefined && options === undefined) {
return ['UIDAttribute']; return ['UIDAttribute'];
}
const params = [];
// If the targetField property is defined, then reference it,
// otherwise, put `undefined` keyword type nodes as placeholders
const targetFieldParams = _.isUndefined(targetField)
? [
factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword),
factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword),
]
: [factory.createStringLiteral(uid), factory.createStringLiteral(targetField)];
params.push(...targetFieldParams);
// If the options property is defined, transform it to
// a type literral node and add it to the params list
if (_.isObject(options)) {
params.push(toTypeLitteral(options));
}
return ['UIDAttribute', params];
}, },
enumeration() { enumeration() {
return ['EnumerationAttribute']; return ['EnumerationAttribute'];