Skip to content

Commit

Permalink
Merge 9bc449f into 6bfcba2
Browse files Browse the repository at this point in the history
  • Loading branch information
andrelmlins committed Jul 7, 2020
2 parents 6bfcba2 + 9bc449f commit a84d8cd
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 12 deletions.
4 changes: 4 additions & 0 deletions example/src/models/CommentModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class CommentModel extends Publication {
export class AuthorModel {
name!: String;
book?: BookModel;
params?: {
a: string;
b: string;
};
}

@Type({ name: 'Journal' })
Expand Down
7 changes: 7 additions & 0 deletions src/compiler/TypeCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ class TypeCompiler {
}
}

compileObjectLiteral(node: ts.TypeLiteralNode, fieldName: string, path: string): Type {
const type = new Type(node, path, false, [], this.sourceFile, fieldName);
this.types.push(type);

return type;
}

private getFolder(): string {
const bar = os.platform() === 'win32' ? '\\' : '/';
return this.path.substring(0, this.path.lastIndexOf(bar));
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/token/Field.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as ts from 'typescript';
import Log from '../../log';
import InputCompiler from '../InputCompiler';
import TypeCompiler from '../TypeCompiler';
import FieldTypeEnum from '../enum/FieldTypeEnum';
import PrimitiveType from '../PrimitiveType';

Expand Down Expand Up @@ -66,6 +67,13 @@ class Field {
this.path
);
this.type = input.name;
} else if (fieldType === FieldTypeEnum.TYPE) {
const type = TypeCompiler.Instance.compileObjectLiteral(
node.type,
`${capitalize(this.parentName)}${capitalize(this.name)}`,
this.path
);
this.type = type.name;
}
} else {
this.type = PrimitiveType.getPrimitiveType(node.type.getText(sourceFile));
Expand Down
32 changes: 20 additions & 12 deletions src/compiler/token/Type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,42 @@ class Type {
public options: TypeOptions;

constructor(
node: ts.ClassDeclaration,
node: ts.ClassDeclaration | ts.TypeLiteralNode,
path: string,
isDefaultExternal: boolean,
imports: ImportDeclaration[],
sourceFile?: ts.SourceFile
sourceFile?: ts.SourceFile,
name?: string
) {
this.fields = [];
this.options = {};
this.path = path;
this.name = node.name!.getText(sourceFile).replace('Model', '');
this.nameModel = node.name!.getText(sourceFile);
if (ts.isTypeLiteralNode(node)) {
this.name = name!;
this.nameModel = name!;
} else {
this.name = node.name!.getText(sourceFile).replace('Model', '');
this.nameModel = node.name!.getText(sourceFile);
}

this.isExportDefaultModel = isDefaultExternal || isExportDefault(node);

node.members.forEach(member => {
if (ts.isPropertyDeclaration(member)) {
node.members.forEach((member: ts.Node) => {
if (ts.isPropertyDeclaration(member) || ts.isPropertySignature(member)) {
const field = new Field(member, this.path, this.name, imports, FieldTypeEnum.TYPE, sourceFile);
this.fields.push(field);
}
});

if (node.heritageClauses) {
node.heritageClauses.forEach((heritage: ts.HeritageClause) => {
this.heritageName = heritage.types[0].expression.getText(sourceFile);
});
}
if (ts.isClassDeclaration(node)) {
if (node.heritageClauses) {
node.heritageClauses.forEach((heritage: ts.HeritageClause) => {
this.heritageName = heritage.types[0].expression.getText(sourceFile);
});
}

this.getOptions(node, sourceFile);
this.getOptions(node, sourceFile);
}
}

private getOptions(node: ts.ClassDeclaration, sourceFile?: ts.SourceFile) {
Expand Down
14 changes: 14 additions & 0 deletions test/type-compiler/snapshot/with-object-type-in-field/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Type } from 'recife';

@Type()
class User {
name?: String;
exist?: Boolean;
emails: String[];
params?: {
a: string;
b: string;
};
}

export default User;
64 changes: 64 additions & 0 deletions test/type-compiler/snapshot/with-object-type-in-field/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use-strict';

module.exports = {
types: [
{
fields: [
{
visible: true,
parentName: 'UserParams',
name: 'a',
isRequired: true,
type: 'String'
},
{
visible: true,
parentName: 'UserParams',
name: 'b',
isRequired: true,
type: 'String'
}
],
options: {},
name: 'UserParams',
nameModel: 'UserParams',
isExportDefaultModel: false
},
{
fields: [
{
visible: true,
parentName: 'User',
name: 'name',
isRequired: false,
type: 'String'
},
{
visible: true,
parentName: 'User',
name: 'exist',
isRequired: false,
type: 'Boolean'
},
{
visible: true,
parentName: 'User',
name: 'emails',
isRequired: true,
type: '[String]'
},
{
visible: true,
parentName: 'User',
name: 'params',
isRequired: false,
type: 'UserParams'
}
],
options: {},
name: 'User',
nameModel: 'User',
isExportDefaultModel: true
}
]
};

0 comments on commit a84d8cd

Please sign in to comment.