Skip to content

Commit a4da39a

Browse files
committed
add brain custom statement
1 parent 3611e04 commit a4da39a

8 files changed

+47
-14
lines changed

craco.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
"@pages": path.resolve(__dirname, "src/pages"),
88
"@router": path.resolve(__dirname, "src/router"),
99
"@views": path.resolve(__dirname, "src/views"),
10+
"@port": path.resolve(__dirname, "src/port"),
1011
},
1112
configure: {
1213
module: {

src/base/Json2Ts.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { BrainStatementImpl } from "../port/StatementCustom/BrainStatementImpl";
2+
import { DefaultStatementImpl } from "../port/StatementCustom/DefaultStatementImpl";
3+
import { StatementCustom } from "../port/StatementCustom/StatementCustom";
4+
15
type Partial<T> = {
26
[P in keyof T]?: T[P];
37
};
@@ -7,12 +11,14 @@ interface IJson2TsConfigPrivate {
711
optionalFields: boolean;
812
rootObjectName: string;
913
addPrefix: boolean;
14+
brainCustom: boolean;
1015
}
1116

1217
export type IJson2TsConfig = Partial<IJson2TsConfigPrivate>;
1318

1419
export class Json2Ts {
1520
private config: IJson2TsConfigPrivate;
21+
private statementCustom: StatementCustom;
1622

1723
private interfaces: {
1824
[name: string]: {
@@ -26,8 +32,10 @@ export class Json2Ts {
2632
optionalFields: false,
2733
rootObjectName: "RootObject",
2834
addPrefix: false,
35+
brainCustom: true,
2936
...config,
3037
};
38+
this.statementCustom = config.brainCustom ? new BrainStatementImpl() : new DefaultStatementImpl();
3139
}
3240

3341
convert(json: {}) {
@@ -136,12 +144,13 @@ export class Json2Ts {
136144
}
137145
fields.forEach(field => {
138146
const type = this.interfaces[name][field];
139-
const defaultValue = optionalFields ? "" : " = " + this.getDefaultValue(type);
147+
const defaultTypeValue = this.getDefaultValue(type);
148+
const defaultValue = optionalFields ? "" : " = " + defaultTypeValue;
140149

141150
const customType = type.replace("[]", "");
142151
const customTypeCheck = Object.keys(this.interfaces).includes(customType);
143152
if (customTypeCheck) {
144-
interfaceStr.push(` @Type(() => ${customType})`);
153+
interfaceStr.push(this.statementCustom.typeStatementGenerator(customType, defaultTypeValue));
145154
}
146155
const rewriteFiled = this.hasSpaceInMiddle(field) ? `"${field}"` : field;
147156
interfaceStr.push(` ${rewriteFiled}${optionalFields ? "?" : ""}: ${type}${defaultValue};`);
@@ -151,7 +160,7 @@ export class Json2Ts {
151160
})
152161
.join("\n");
153162

154-
if (outout.includes("@Type")) outout = 'import { Type } from "class-transformer";\n\n' + outout;
163+
if (outout.includes("@Type")) outout = this.statementCustom.importTypeStatement + "\n\n" + outout;
155164
return outout;
156165
}
157166

src/pages/default/DefaultPage.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,17 @@ export function DefaultPage(props: {}): React.JSX.Element {
5252
<Form.Item label="添加前缀">
5353
<Switch value={bloc.state.value.options.addPrefix} onChange={e => bloc.handleAddPrefixChange(e)} />
5454
</Form.Item>
55+
<Form.Item label="Brain 自定义">
56+
<Switch value={bloc.state.value.options.brainCustom} onChange={e => bloc.handleBrainCustomChange(e)} />
57+
</Form.Item>
5558
<Form.Item label="类名">
5659
<Input
5760
placeholder=""
5861
value={bloc.state.value.options.rootObjectName}
5962
onChange={e => bloc.handleRootObjectNameChange(e)}
6063
/>
6164
</Form.Item>
65+
6266
<Form.Item label="&nbsp;" noStyle={false}>
6367
<Button type="primary" onClick={() => bloc.transform()}>
6468
生成

src/pages/default/DefaultPageBloc.ts

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class DefaultPageState {
1313
optionalFields: false,
1414
rootObjectName: "RootObject",
1515
addPrefix: false,
16+
brainCustom: true,
1617
};
1718

1819
copyWith(params: {
@@ -69,4 +70,9 @@ export class DefaultPageBloc {
6970
this.state.value.options.addPrefix = e;
7071
this.state.value = this.state.value.copyWith({});
7172
}
73+
74+
handleBrainCustomChange(e: boolean) {
75+
this.state.value.options.brainCustom = e;
76+
this.state.value = this.state.value.copyWith({});
77+
}
7278
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { StatementCustom } from "./StatementCustom";
2+
3+
export class BrainStatementImpl extends StatementCustom {
4+
public importTypeStatement: string = 'import { Type } from "@base/cutil/ClassTransformerDecorators";';
5+
public typeStatementGenerator(customType: string, defaultTypeValue?: string): string {
6+
return ` @Type(() => ${customType}${defaultTypeValue === "[]" ? ", []" : ""})`;
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { StatementCustom } from "./StatementCustom";
2+
3+
export class DefaultStatementImpl extends StatementCustom {
4+
public importTypeStatement: string = 'import { Type } from "class-transformer";';
5+
public typeStatementGenerator(customType: string): string {
6+
return ` @Type(() => ${customType})`;
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export abstract class StatementCustom {
2+
public abstract importTypeStatement: string;
3+
public abstract typeStatementGenerator(customType: string, defaultTypeValue?: string): string;
4+
}

tsconfig.json

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
{
22
"compilerOptions": {
3-
"types": [
4-
"reflect-metadata"
5-
],
3+
"types": ["reflect-metadata"],
64
"experimentalDecorators": true,
75
"emitDecoratorMetadata": true,
86
"target": "es5",
9-
"lib": [
10-
"dom",
11-
"dom.iterable",
12-
"esnext"
13-
],
7+
"lib": ["dom", "dom.iterable", "esnext"],
148
"allowJs": true,
159
"skipLibCheck": true,
1610
"esModuleInterop": true,
@@ -31,9 +25,8 @@
3125
"@pages/*": ["pages/*"],
3226
"@router/*": ["router/*"],
3327
"@views/*": ["views/*"],
28+
"@port/*": ["port/*"]
3429
}
3530
},
36-
"include": [
37-
"src"
38-
]
31+
"include": ["src"]
3932
}

0 commit comments

Comments
 (0)