-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project.ts
110 lines (97 loc) · 2.84 KB
/
Project.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
export module Project {
export class Request {
name: string
vars: Variable[]
path:string
method:string
response_type:string
error_type:string
constructor(name:string,vars:Variable[],path:string,method:string, response_type:string,error_type:string) {
this.name = name
this.vars = vars
this.path = path
this.method = method
this.response_type = response_type
this.error_type = error_type
}
}
export function normalizeModelType(model:Model): Model {
model.vars.forEach(variable => {
normalizeVariableType(variable)
})
return model
};
export function normalizeRequestType(request:Request): Request {
request.vars.forEach(variable => {
normalizeVariableType(variable)
})
return request
};
function normalizeVariableType(variable:Variable): Variable {
variable.type = Variable.typeFrom(variable.type)
return variable
}
export class Model {
name: string
vars: Variable[]
constructor(name:string,vars:Variable[]) {
this.name = name
this.vars = vars
}
}
export class Variable {
name: string
type: string
optional:boolean
value?: string
constructor(name:string,type:string,optional:boolean,value?:string) {
this.name = name
this.type = type
this.optional = optional
this.value = value
}
public toJSON()
{
const type = this.typeFor(this.type)
return {
name:this.name,
type:type,
optional:this.optional,
value:this.value
};
}
typeFor(type:string): any {
if (type[0] == "[") {
return {Array:this.typeFor(type.substr(1,type.length - 2))}
}
if (["String","Bool","Int","Float"].includes(type)) {
return type
}
return {Complex:type}
}
public static fromJSON(json: any): Variable {
console.log("FROM CALLLED")
const type = Variable.typeFrom(json["type"])
console.log(type)
let variable = Object.create(Variable.prototype);
return Object.assign(variable, json, {
type: type
});
}
static typeFrom(type:any): string {
if (typeof type === 'string' || type instanceof String) {
return type as string
}
if (typeof type == 'object') {
const obj = type as object
if (obj["Complex"] != undefined) {
return obj["Complex"]
}
if (obj["Array"] != undefined) {
return "[" + this.typeFrom(obj["Array"]) + "]"
}
}
return "idk"
}
}
}