mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-03-10 09:56:20 +00:00
fix: enhance api design
This commit is contained in:
parent
dd97a0c92a
commit
95d67c1d99
@ -34,8 +34,8 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
|
|||||||
const state = ir.state;
|
const state = ir.state;
|
||||||
const fields = Object.keys(state).map<string>((stateName) => {
|
const fields = Object.keys(state).map<string>((stateName) => {
|
||||||
// TODO: 这里用什么 handlers?
|
// TODO: 这里用什么 handlers?
|
||||||
const [isString, value] = generateCompositeType(state[stateName], {});
|
const value = generateCompositeType(state[stateName], {});
|
||||||
return `${stateName}: ${isString ? `'${value}'` : value},`;
|
return `${stateName}: ${value}`;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (cfg.implementType === 'inConstructor') {
|
if (cfg.implementType === 'inConstructor') {
|
||||||
@ -43,7 +43,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
|
|||||||
type: ChunkType.STRING,
|
type: ChunkType.STRING,
|
||||||
fileType: cfg.fileType,
|
fileType: cfg.fileType,
|
||||||
name: CLASS_DEFINE_CHUNK_NAME.ConstructorContent,
|
name: CLASS_DEFINE_CHUNK_NAME.ConstructorContent,
|
||||||
content: `this.state = { ${fields.join('')} };`,
|
content: `this.state = { ${fields.join(',')} };`,
|
||||||
linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.ConstructorContent]],
|
linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.ConstructorContent]],
|
||||||
});
|
});
|
||||||
} else if (cfg.implementType === 'insMember') {
|
} else if (cfg.implementType === 'insMember') {
|
||||||
@ -51,7 +51,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
|
|||||||
type: ChunkType.STRING,
|
type: ChunkType.STRING,
|
||||||
fileType: cfg.fileType,
|
fileType: cfg.fileType,
|
||||||
name: CLASS_DEFINE_CHUNK_NAME.InsVar,
|
name: CLASS_DEFINE_CHUNK_NAME.InsVar,
|
||||||
content: `state = { ${fields.join('')} };`,
|
content: `state = { ${fields.join(',')} };`,
|
||||||
linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsVar]],
|
linkAfter: [...DEFAULT_LINK_AFTER[CLASS_DEFINE_CHUNK_NAME.InsVar]],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,8 +35,8 @@ const pluginFactory: BuilderComponentPluginFactory<unknown> = () => {
|
|||||||
|
|
||||||
const extConfigs = Object.keys(rest).map((extConfigName) => {
|
const extConfigs = Object.keys(rest).map((extConfigName) => {
|
||||||
const value = (rest as Record<string, CompositeValue>)[extConfigName];
|
const value = (rest as Record<string, CompositeValue>)[extConfigName];
|
||||||
const [isString, valueStr] = generateCompositeType(value);
|
const valueStr = generateCompositeType(value);
|
||||||
return `${extConfigName}: ${isString ? `'${valueStr}'` : valueStr}`;
|
return `${extConfigName}: ${valueStr}`;
|
||||||
});
|
});
|
||||||
|
|
||||||
attrs = [...attrs, ...extConfigs];
|
attrs = [...attrs, ...extConfigs];
|
||||||
|
|||||||
@ -186,13 +186,8 @@ export interface CompositeValueCustomHandlerSet {
|
|||||||
function?: CompositeValueCustomHandler;
|
function?: CompositeValueCustomHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CompositeTypeContainerHandlerSet {
|
|
||||||
default?: CompositeTypeContainerHandler;
|
|
||||||
string?: CompositeTypeContainerHandler;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type CompositeValueGeneratorOptions = {
|
export type CompositeValueGeneratorOptions = {
|
||||||
handlers?: CompositeValueCustomHandlerSet;
|
handlers?: CompositeValueCustomHandlerSet;
|
||||||
containerHandlers?: CompositeTypeContainerHandlerSet;
|
containerHandler?: (value: string, isString: boolean, valStr: string) => string;
|
||||||
nodeGenerator?: NodeGenerator;
|
nodeGenerator?: NodeGenerator;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -6,17 +6,14 @@ import {
|
|||||||
isJSFunction,
|
isJSFunction,
|
||||||
isJSSlot,
|
isJSSlot,
|
||||||
} from '@ali/lowcode-types';
|
} from '@ali/lowcode-types';
|
||||||
|
import _ from 'lodash';
|
||||||
|
|
||||||
import { generateExpression, generateFunction } from './jsExpression';
|
import { generateExpression, generateFunction } from './jsExpression';
|
||||||
import { generateJsSlot } from './jsSlot';
|
import { generateJsSlot } from './jsSlot';
|
||||||
import { isValidIdentifier } from './validate';
|
import { isValidIdentifier } from './validate';
|
||||||
import { camelize } from './common';
|
import { camelize } from './common';
|
||||||
|
|
||||||
import { CompositeValueGeneratorOptions, CompositeTypeContainerHandlerSet, CodeGeneratorError } from '../types';
|
import { CompositeValueGeneratorOptions, CompositeTypeContainerHandler, CodeGeneratorError } from '../types';
|
||||||
|
|
||||||
const defaultContainerHandlers: CompositeTypeContainerHandlerSet = {
|
|
||||||
default: (v) => v,
|
|
||||||
string: (v) => `'${v}'`,
|
|
||||||
};
|
|
||||||
|
|
||||||
function generateArray(value: CompositeArray, options: CompositeValueGeneratorOptions = {}): string {
|
function generateArray(value: CompositeArray, options: CompositeValueGeneratorOptions = {}): string {
|
||||||
const body = value.map((v) => generateUnknownType(v, options)).join(',');
|
const body = value.map((v) => generateUnknownType(v, options)).join(',');
|
||||||
@ -52,67 +49,77 @@ function generateObject(value: CompositeObject, options: CompositeValueGenerator
|
|||||||
}
|
}
|
||||||
|
|
||||||
function generateUnknownType(value: CompositeValue, options: CompositeValueGeneratorOptions = {}): string {
|
function generateUnknownType(value: CompositeValue, options: CompositeValueGeneratorOptions = {}): string {
|
||||||
if (Array.isArray(value)) {
|
if (_.isUndefined(value)) {
|
||||||
if (options.handlers && options.handlers.array) {
|
return 'undefined';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_.isNull(value)) {
|
||||||
|
return 'null';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_.isArray(value)) {
|
||||||
|
if (options.handlers?.array) {
|
||||||
return options.handlers.array(value);
|
return options.handlers.array(value);
|
||||||
}
|
}
|
||||||
return generateArray(value, options);
|
return generateArray(value, options);
|
||||||
} else if (typeof value === 'object') {
|
}
|
||||||
if (value === null) {
|
|
||||||
return 'null';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isJSExpression(value)) {
|
if (isJSExpression(value)) {
|
||||||
if (options.handlers && options.handlers.expression) {
|
if (options.handlers?.expression) {
|
||||||
return options.handlers.expression(value);
|
return options.handlers.expression(value);
|
||||||
}
|
|
||||||
return generateExpression(value);
|
|
||||||
}
|
}
|
||||||
|
return generateExpression(value);
|
||||||
|
}
|
||||||
|
|
||||||
if (isJSFunction(value)) {
|
if (isJSFunction(value)) {
|
||||||
if (options.handlers && options.handlers.function) {
|
if (options.handlers?.function) {
|
||||||
return options.handlers.function(value);
|
return options.handlers.function(value);
|
||||||
}
|
|
||||||
return generateFunction(value, { isArrow: true });
|
|
||||||
}
|
}
|
||||||
|
return generateFunction(value, { isArrow: true });
|
||||||
|
}
|
||||||
|
|
||||||
if (isJSSlot(value)) {
|
if (isJSSlot(value)) {
|
||||||
if (options.nodeGenerator) {
|
if (options.nodeGenerator) {
|
||||||
return generateJsSlot(value, options.nodeGenerator);
|
return generateJsSlot(value, options.nodeGenerator);
|
||||||
}
|
|
||||||
throw new CodeGeneratorError("Can't find Node Generator");
|
|
||||||
}
|
}
|
||||||
|
throw new CodeGeneratorError("Can't find Node Generator");
|
||||||
|
}
|
||||||
|
|
||||||
if (options.handlers && options.handlers.object) {
|
if (_.isObject(value)) {
|
||||||
|
if (options.handlers?.object) {
|
||||||
return options.handlers.object(value);
|
return options.handlers.object(value);
|
||||||
}
|
}
|
||||||
return generateObject(value as CompositeObject, options);
|
return generateObject(value as CompositeObject, options);
|
||||||
} else if (typeof value === 'string') {
|
}
|
||||||
if (options.handlers && options.handlers.string) {
|
|
||||||
|
if (_.isString(value)) {
|
||||||
|
if (options.handlers?.string) {
|
||||||
return options.handlers.string(value);
|
return options.handlers.string(value);
|
||||||
}
|
}
|
||||||
return `'${value}'`;
|
return `'${value}'`;
|
||||||
} else if (typeof value === 'number' && options.handlers && options.handlers.number) {
|
}
|
||||||
|
|
||||||
|
if (_.isNumber(value) && options.handlers?.number) {
|
||||||
return options.handlers.number(value);
|
return options.handlers.number(value);
|
||||||
} else if (typeof value === 'boolean' && options.handlers && options.handlers.boolean) {
|
}
|
||||||
|
|
||||||
|
if (_.isBoolean(value) && options.handlers?.boolean) {
|
||||||
return options.handlers.boolean(value);
|
return options.handlers.boolean(value);
|
||||||
} else if (typeof value === 'undefined') {
|
|
||||||
return 'undefined';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return JSON.stringify(value);
|
return JSON.stringify(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function generateCompositeType(value: CompositeValue, options: CompositeValueGeneratorOptions = {}): string {
|
const defaultContainer: CompositeTypeContainerHandler = (v: string) => v;
|
||||||
const result = generateUnknownType(value, options);
|
|
||||||
const containerHandlers = {
|
|
||||||
...defaultContainerHandlers,
|
|
||||||
...(options.containerHandlers || {}),
|
|
||||||
};
|
|
||||||
|
|
||||||
const isStringType = result.substr(0, 1) === "'" && result.substr(-1, 1) === "'";
|
export function generateCompositeType(value: CompositeValue, options: CompositeValueGeneratorOptions = {}): string {
|
||||||
if (isStringType) {
|
const isStringType = _.isString(value);
|
||||||
return (containerHandlers.string && containerHandlers.string(result.substring(1, result.length - 1))) || '';
|
const result = generateUnknownType(value, options);
|
||||||
|
const handler: CompositeTypeContainerHandler = options.containerHandler || defaultContainer;
|
||||||
|
|
||||||
|
if (isStringType && result.length >= 2) {
|
||||||
|
return handler(result, true, result.substring(1, result.length - 1));
|
||||||
}
|
}
|
||||||
return (containerHandlers.default && containerHandlers.default(result)) || '';
|
|
||||||
|
return handler(result, false, result);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -114,10 +114,7 @@ export function generateAttr(ctx: INodeGeneratorContext, attrName: string, attrV
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
const valueStr = generateCompositeType(attrValue, {
|
const valueStr = generateCompositeType(attrValue, {
|
||||||
containerHandlers: {
|
containerHandler: (v, isStr, vStr) => (isStr ? `"${vStr}"` : `{${v}}`),
|
||||||
default: (v) => `{${v}}`,
|
|
||||||
string: (v) => `"${v}"`,
|
|
||||||
},
|
|
||||||
nodeGenerator: ctx.generator,
|
nodeGenerator: ctx.generator,
|
||||||
});
|
});
|
||||||
return [
|
return [
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user