mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-02-28 21:20:28 +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 fields = Object.keys(state).map<string>((stateName) => {
|
||||
// TODO: 这里用什么 handlers?
|
||||
const [isString, value] = generateCompositeType(state[stateName], {});
|
||||
return `${stateName}: ${isString ? `'${value}'` : value},`;
|
||||
const value = generateCompositeType(state[stateName], {});
|
||||
return `${stateName}: ${value}`;
|
||||
});
|
||||
|
||||
if (cfg.implementType === 'inConstructor') {
|
||||
@ -43,7 +43,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
|
||||
type: ChunkType.STRING,
|
||||
fileType: cfg.fileType,
|
||||
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]],
|
||||
});
|
||||
} else if (cfg.implementType === 'insMember') {
|
||||
@ -51,7 +51,7 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
|
||||
type: ChunkType.STRING,
|
||||
fileType: cfg.fileType,
|
||||
name: CLASS_DEFINE_CHUNK_NAME.InsVar,
|
||||
content: `state = { ${fields.join('')} };`,
|
||||
content: `state = { ${fields.join(',')} };`,
|
||||
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 value = (rest as Record<string, CompositeValue>)[extConfigName];
|
||||
const [isString, valueStr] = generateCompositeType(value);
|
||||
return `${extConfigName}: ${isString ? `'${valueStr}'` : valueStr}`;
|
||||
const valueStr = generateCompositeType(value);
|
||||
return `${extConfigName}: ${valueStr}`;
|
||||
});
|
||||
|
||||
attrs = [...attrs, ...extConfigs];
|
||||
|
||||
@ -186,13 +186,8 @@ export interface CompositeValueCustomHandlerSet {
|
||||
function?: CompositeValueCustomHandler;
|
||||
}
|
||||
|
||||
export interface CompositeTypeContainerHandlerSet {
|
||||
default?: CompositeTypeContainerHandler;
|
||||
string?: CompositeTypeContainerHandler;
|
||||
}
|
||||
|
||||
export type CompositeValueGeneratorOptions = {
|
||||
handlers?: CompositeValueCustomHandlerSet;
|
||||
containerHandlers?: CompositeTypeContainerHandlerSet;
|
||||
containerHandler?: (value: string, isString: boolean, valStr: string) => string;
|
||||
nodeGenerator?: NodeGenerator;
|
||||
};
|
||||
|
||||
@ -6,17 +6,14 @@ import {
|
||||
isJSFunction,
|
||||
isJSSlot,
|
||||
} from '@ali/lowcode-types';
|
||||
import _ from 'lodash';
|
||||
|
||||
import { generateExpression, generateFunction } from './jsExpression';
|
||||
import { generateJsSlot } from './jsSlot';
|
||||
import { isValidIdentifier } from './validate';
|
||||
import { camelize } from './common';
|
||||
|
||||
import { CompositeValueGeneratorOptions, CompositeTypeContainerHandlerSet, CodeGeneratorError } from '../types';
|
||||
|
||||
const defaultContainerHandlers: CompositeTypeContainerHandlerSet = {
|
||||
default: (v) => v,
|
||||
string: (v) => `'${v}'`,
|
||||
};
|
||||
import { CompositeValueGeneratorOptions, CompositeTypeContainerHandler, CodeGeneratorError } from '../types';
|
||||
|
||||
function generateArray(value: CompositeArray, options: CompositeValueGeneratorOptions = {}): string {
|
||||
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 {
|
||||
if (Array.isArray(value)) {
|
||||
if (options.handlers && options.handlers.array) {
|
||||
if (_.isUndefined(value)) {
|
||||
return 'undefined';
|
||||
}
|
||||
|
||||
if (_.isNull(value)) {
|
||||
return 'null';
|
||||
}
|
||||
|
||||
if (_.isArray(value)) {
|
||||
if (options.handlers?.array) {
|
||||
return options.handlers.array(value);
|
||||
}
|
||||
return generateArray(value, options);
|
||||
} else if (typeof value === 'object') {
|
||||
if (value === null) {
|
||||
return 'null';
|
||||
}
|
||||
}
|
||||
|
||||
if (isJSExpression(value)) {
|
||||
if (options.handlers && options.handlers.expression) {
|
||||
return options.handlers.expression(value);
|
||||
}
|
||||
return generateExpression(value);
|
||||
if (isJSExpression(value)) {
|
||||
if (options.handlers?.expression) {
|
||||
return options.handlers.expression(value);
|
||||
}
|
||||
return generateExpression(value);
|
||||
}
|
||||
|
||||
if (isJSFunction(value)) {
|
||||
if (options.handlers && options.handlers.function) {
|
||||
return options.handlers.function(value);
|
||||
}
|
||||
return generateFunction(value, { isArrow: true });
|
||||
if (isJSFunction(value)) {
|
||||
if (options.handlers?.function) {
|
||||
return options.handlers.function(value);
|
||||
}
|
||||
return generateFunction(value, { isArrow: true });
|
||||
}
|
||||
|
||||
if (isJSSlot(value)) {
|
||||
if (options.nodeGenerator) {
|
||||
return generateJsSlot(value, options.nodeGenerator);
|
||||
}
|
||||
throw new CodeGeneratorError("Can't find Node Generator");
|
||||
if (isJSSlot(value)) {
|
||||
if (options.nodeGenerator) {
|
||||
return generateJsSlot(value, options.nodeGenerator);
|
||||
}
|
||||
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 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 `'${value}'`;
|
||||
} else if (typeof value === 'number' && options.handlers && options.handlers.number) {
|
||||
}
|
||||
|
||||
if (_.isNumber(value) && options.handlers?.number) {
|
||||
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);
|
||||
} else if (typeof value === 'undefined') {
|
||||
return 'undefined';
|
||||
}
|
||||
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
export function generateCompositeType(value: CompositeValue, options: CompositeValueGeneratorOptions = {}): string {
|
||||
const result = generateUnknownType(value, options);
|
||||
const containerHandlers = {
|
||||
...defaultContainerHandlers,
|
||||
...(options.containerHandlers || {}),
|
||||
};
|
||||
const defaultContainer: CompositeTypeContainerHandler = (v: string) => v;
|
||||
|
||||
const isStringType = result.substr(0, 1) === "'" && result.substr(-1, 1) === "'";
|
||||
if (isStringType) {
|
||||
return (containerHandlers.string && containerHandlers.string(result.substring(1, result.length - 1))) || '';
|
||||
export function generateCompositeType(value: CompositeValue, options: CompositeValueGeneratorOptions = {}): string {
|
||||
const isStringType = _.isString(value);
|
||||
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 [];
|
||||
}
|
||||
const valueStr = generateCompositeType(attrValue, {
|
||||
containerHandlers: {
|
||||
default: (v) => `{${v}}`,
|
||||
string: (v) => `"${v}"`,
|
||||
},
|
||||
containerHandler: (v, isStr, vStr) => (isStr ? `"${vStr}"` : `{${v}}`),
|
||||
nodeGenerator: ctx.generator,
|
||||
});
|
||||
return [
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user