slightning-coco-widget
Version:
SLIGHTNING 的 CoCo 控件框架。
164 lines (163 loc) • 5.61 kB
JavaScript
import * as stringify from "@slightning/anything-to-string";
import { betterToString, XMLEscape } from "../../utils";
import { TypeValidateError } from "./type-validate-error";
import { inlineTypeToString, typeToString, validate } from "./utils";
export class ObjectType {
constructor({ propertiesType, defaultValue } = {}) {
this.propertiesType = propertiesType;
this.defaultValue = defaultValue !== null && defaultValue !== void 0 ? defaultValue : inlineTypeToString(this);
}
validate(value) {
if (value == null || typeof value != "object") {
throw new TypeValidateError(`不能将 ${betterToString(value)} 分配给 ${typeToString(this)}`, value, this);
}
if (this.propertiesType != null) {
const errors = [];
for (const [key, type] of Object.entries(this.propertiesType)) {
try {
if (type.isVoid()) {
continue;
}
if (key in value) {
validate(`属性 ${key}`, value[key], type);
}
else {
throw new TypeValidateError(`属性 ${key} 缺失`, value, this);
}
}
catch (error) {
if (!(error instanceof TypeValidateError)) {
throw error;
}
errors.push(error);
}
}
if (errors.length != 0) {
throw new TypeValidateError(`不能将 ${betterToString(value)} 分配给 ${typeToString(this)}:\n` +
errors.map((error) => error.message
.split("\n")
.map((line) => ` ${line}`)
.join("\n")).join("\n"), value, this);
}
}
return true;
}
getSameDirectionChildren() {
return this.propertiesType == null ? [] : Object.entries(this.propertiesType).map(([key, type]) => ({
key: `__slightning_coco_widget_object_property__${key}`,
label: `字典属性·${key}`,
type: type
}));
}
getReverseDirectionChildren() {
return [];
}
isVoid() {
return false;
}
typeToStringPrepare(config, context) {
if (this.propertiesType != null) {
for (const propertyType of Object.values(this.propertiesType)) {
new stringify.AnythingRule().prepare(propertyType, config, context);
}
}
}
typeToString(config, context) {
let result = "字典";
if (this.propertiesType != null) {
const properties = Object.entries(this.propertiesType)
.map(([key, type]) => `${key}: ${new stringify.AnythingRule().toString(type, config, context)}`)
.join("\n")
.split("\n")
.map((line) => ` ${line}`)
.join("\n");
result += ` {\n${properties}\n}`;
}
return result;
}
inlineTypeToStringPrepare(config, context) {
if (this.propertiesType != null) {
for (const propertyType of Object.values(this.propertiesType)) {
new stringify.AnythingRule().prepare(propertyType, config, context);
}
}
}
inlineTypeToString(config, context) {
let result = "字典";
if (this.propertiesType != null) {
const properties = Object.entries(this.propertiesType)
.map(([key, type]) => `${key}: ${new stringify.AnythingRule().toString(type, config, context)}`)
.join("; ");
result += ` { ${properties} }`;
}
return result;
}
toCoCoPropertyValueTypes() {
return {
valueType: ["string", "object"],
checkType: "string",
defaultValue: XMLEscape(typeof this.defaultValue == "string" ? this.defaultValue : JSON.stringify(this.defaultValue))
};
}
toCoCoMethodParamValueTypes() {
return {
valueType: ["string", "object"],
checkType: "string",
defaultValue: XMLEscape(typeof this.defaultValue == "string" ? this.defaultValue : JSON.stringify(this.defaultValue))
};
}
toCoCoMethodValueTypes() {
return {
valueType: "object"
};
}
toCoCoEventParamValueTypes() {
return {
valueType: "object"
};
}
toCreationProject1PropValueTypes() {
return {
valueType: "object",
defaultValue: this.defaultValue
};
}
toCreationProject1MethodParamValueTypes() {
return {
valueType: "object",
defaultValue: this.defaultValue
};
}
toCreationProject1MethodValueTypes() {
return {
valueType: "object"
};
}
toCreationProject1EmitParamValueTypes() {
return {
valueType: "object"
};
}
toCreationProject2PropValueTypes() {
return {
valueType: "object",
defaultValue: this.defaultValue
};
}
toCreationProject2MethodParamValueTypes() {
return {
valueType: "object",
defaultValue: this.defaultValue
};
}
toCreationProject2MethodValueTypes() {
return {
valueType: "object"
};
}
toCreationProject2EmitParamValueTypes() {
return {
valueType: "object"
};
}
}