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

View File

@ -1,5 +1,6 @@
'use strict';
const ts = require('typescript');
const { factory } = require('typescript');
const _ = require('lodash/fp');
@ -185,8 +186,34 @@ const mappers = {
decimal() {
return ['DecimalAttribute'];
},
uid() {
return ['UIDAttribute'];
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'];
}
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() {
return ['EnumerationAttribute'];