schema ts 类型调整

This commit is contained in:
2betop 2020-09-16 11:05:33 +08:00
parent c94a2fab77
commit 238f055ec6
12 changed files with 96 additions and 49 deletions

View File

@ -1,16 +1,41 @@
import {
BaseType,
Context,
IntersectionNodeParser as BaseIntersectionNodeParser
IntersectionNodeParser as BaseIntersectionNodeParser,
IntersectionType,
NodeParser,
ReferenceType
} from 'ts-json-schema-generator';
import ts from 'typescript';
export class IntersectionNodeParser extends BaseIntersectionNodeParser {
protected readonly childParser: NodeParser;
public constructor(typeChecker: ts.TypeChecker, childNodeParser: NodeParser) {
super(typeChecker, childNodeParser);
this.childParser = childNodeParser;
}
public createType(
node: ts.IntersectionTypeNode,
context: Context
): BaseType | undefined {
console.log('here');
// 这两个只能用 allOf 来了,提取不了。
const shouldBeReference = (type: any) =>
type.typeName?.escapedText === 'SchemaObject' ||
type.typeName?.escapedText === 'FormControlSchema';
const matched = node.types.some(shouldBeReference);
// 跟 SchemaObject and 一般都有问题,改成 allOf 不要支持 additional props false 了
if (matched) {
const types: BaseType[] = node.types
.map(type => this.childParser.createType(type, context)!)
.filter(item => item);
return new IntersectionType(types);
}
return super.createType(node, context);
}
}

View File

@ -1,14 +1,48 @@
import {
BaseType,
Definition,
IntersectionType,
IntersectionTypeFormatter as BaseIntersectionTypeFormatter
IntersectionTypeFormatter as BaseIntersectionTypeFormatter,
ReferenceType,
TypeFormatter
} from 'ts-json-schema-generator';
import {getAllOfDefinitionReducer} from 'ts-json-schema-generator/dist/src/Utils/allOfDefinition';
export class IntersectionTypeFormatter extends BaseIntersectionTypeFormatter {
protected readonly childFormatter: TypeFormatter;
public constructor(childTypeFormatter: TypeFormatter) {
super(childTypeFormatter);
this.childFormatter = childTypeFormatter;
}
getDefinition(type: any): Definition {
if (type.types[1]?.name === 'SchemaObject') {
console.log(type);
process.exit(1);
const hasReferenceType = type.types.some(
(t: BaseType) => t instanceof ReferenceType
);
if (hasReferenceType) {
const references: Array<any> = [];
const rest: Array<any> = [];
type.types.forEach((t: any) => {
if (t instanceof ReferenceType) {
references.push(t);
} else {
rest.push(t);
}
});
return {
allOf: references
.map((type: any) => this.childFormatter.getDefinition(type))
.concat(
rest.reduce(getAllOfDefinitionReducer(this.childFormatter), {
type: 'object',
additionalProperties: true // 这里只能是 true 了
} as Definition)
)
.filter((item: any) => item)
} as any;
}
return super.getDefinition(type);

View File

@ -1,12 +0,0 @@
import {
Definition,
IntersectionType,
ObjectType,
ObjectTypeFormatter as BaseObjectTypeFormatter
} from 'ts-json-schema-generator';
export class ObjectTypeFormatter extends BaseObjectTypeFormatter {
getDefinition(type: ObjectType): Definition {
return super.getDefinition(type);
}
}

View File

@ -17,7 +17,6 @@ import {
SubNodeParser
} from 'ts-json-schema-generator';
import {IntersectionTypeFormatter as MyIntersectionTypeFormatter} from './TypeFormatter/IntersectionTypeFormatter';
import {ObjectTypeFormatter as MyObjectTypeFormatter} from './TypeFormatter/ObjectTypeFormatter';
import {IntersectionNodeParser as MyIntersectionNodeParser} from './NodeParser/IntersectionNodeParser';
/**
@ -54,12 +53,6 @@ function hackIt(generator: any) {
new MyIntersectionTypeFormatter(circularReferenceTypeFormatter)
);
replaceTypeFormatter(
typeFormatters,
ObjectTypeFormatter,
new MyObjectTypeFormatter(circularReferenceTypeFormatter)
);
const chainNodeParser = generator.nodeParser.childNodeParser;
const typeChecker = generator.program.getTypeChecker();

View File

@ -18,12 +18,13 @@ import {
SchemaClassName,
SchemaCollection,
SchemaExpression,
SchemaObject,
SchemaTpl,
SchemaUrlPath
} from '../Schema';
import {ActionSchema} from './Action';
export type CardBodyField = BaseSchema & {
export type CardBodyField = SchemaObject & {
/**
*
*/

View File

@ -40,6 +40,13 @@ export type ComboCondition = {
mode?: string;
};
export type ComboSubControl = FormControlSchema & {
/**
* , combo
*/
unique?: boolean;
};
/**
* Combo
* https://baidu.gitee.io/amis/docs/components/form/combo
@ -106,7 +113,7 @@ export interface ComboControlSchema extends FormBaseControl {
/**
*
*/
controls?: Array<FormControlSchema>;
controls?: Array<ComboSubControl>;
/**
*

View File

@ -11,6 +11,18 @@ import {FormBaseControl, FormControlSchema, FormItemWrap} from './Item';
import getExprProperties from '../../utils/filter-schema';
import {SchemaClassName} from '../../Schema';
export type GroupSubControl = FormControlSchema & {
/**
* , group
*/
columnClassName?: SchemaClassName;
/**
* group
*/
columnRatio?: number;
};
/**
* Group
* https://baidu.gitee.io/amis/docs/components/form/group
@ -21,7 +33,7 @@ export interface GroupControlSchema extends FormBaseControl {
/**
* FormItem
*/
controls: Array<FormControlSchema>;
controls: Array<GroupSubControl>;
/**
*

View File

@ -483,21 +483,6 @@ export interface FormBaseControl extends Omit<BaseSchema, 'type'> {
* name
*/
value?: any;
/**
* , combo
*/
unique?: boolean;
/**
* , group
*/
columnClassName?: SchemaClassName;
/**
* group
*/
columnRatio?: number;
}
export interface FormItemBasicConfig extends Partial<RendererConfig> {

View File

@ -3,7 +3,7 @@ import {Renderer, RendererProps} from '../factory';
import {Api, SchemaNode, Schema, Action} from '../types';
import cx from 'classnames';
import {isVisible} from '../utils/helper';
import {BaseSchema} from '../Schema';
import {BaseSchema, SchemaObject} from '../Schema';
export type HBoxColumnObject = {
columnClassName?: string;
@ -16,7 +16,7 @@ export type HBoxColumnObject = {
};
};
export type HBoxColumn = HBoxColumnObject & BaseSchema; // 不能用 SchemaObject 呢,会报错
export type HBoxColumn = HBoxColumnObject & SchemaObject; // 不能用 SchemaObject 呢,会报错
/**
* Hbox

View File

@ -28,6 +28,7 @@ import {
SchemaClassName,
SchemaCollection,
SchemaExpression,
SchemaObject,
SchemaTokenizeableString,
SchemaTpl,
SchemaUrlPath
@ -35,7 +36,7 @@ import {
import {ActionSchema} from './Action';
import {SchemaRemark} from './Remark';
export type ListBodyField = BaseSchema & {
export type ListBodyField = SchemaObject & {
/**
*
*/

View File

@ -30,6 +30,7 @@ import {TableContent} from './TableContent';
import {
BaseSchema,
SchemaClassName,
SchemaObject,
SchemaTokenizeableString
} from '../../Schema';
import {FormControlSchema, FormControlType} from '../Form/Item';
@ -37,7 +38,7 @@ import {SchemaPopOver} from '../PopOver';
import {SchemaQuickEdit} from '../QuickEdit';
import {SchemaCopyable} from '../Copyable';
export type TableColumn = BaseSchema & {
export type TableColumn = SchemaObject & {
/**
*
*/

View File

@ -2,9 +2,9 @@ import React from 'react';
import {Renderer, RendererProps} from '../factory';
import {Api, SchemaNode, Schema, Action} from '../types';
import cx from 'classnames';
import {BaseSchema} from '../Schema';
import {BaseSchema, SchemaObject} from '../Schema';
export type HboxRow = BaseSchema & {
export type HboxRow = SchemaObject & {
rowClassName?: string;
cellClassName?: string;
};