forked from ECharts-Java/ECharts-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypescript-to-config.py
More file actions
98 lines (90 loc) · 3.03 KB
/
typescript-to-config.py
File metadata and controls
98 lines (90 loc) · 3.03 KB
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
import json
typescript_dir = "typescript"
config_dir = "config"
type_ts_to_java = {
"number": "Number",
"string": "String",
"boolean": "Boolean",
"ZRColor": "String",
"ColorString": "String",
"unknown": "Object",
}
def convert_single_type(ts_type):
if ts_type in type_ts_to_java:
java_type = type_ts_to_java[ts_type]
elif "'" in ts_type:
java_type = "String"
elif ts_type in ["true", "false"]:
java_type = "Boolean"
else:
java_type = ts_type
return java_type
def convert(file):
lines = open(file).readlines()
i = 0
definition = ""
while "{" not in lines[i]:
definition += lines[i]
i += 1
definition += lines[i]
i += 1
definition = definition.replace("export", " ")
definition = definition.replace("extends", " ")
definition = definition.replace(",", " ")
definition = definition.replace("{", " ")
type, name, *extends = definition.split()
fields = []
while i < len(lines):
line = lines[i]
i += 1
if "//" in line or "}" == line.strip() or "};" == line.strip() or "*" in line or line.strip() == "":
continue
line = line.replace("?:", " ")
line = line.replace(";", " ")
line = line.replace("|", " ")
line = line.replace(",", " ")
field_name, *field_types = line.split()
field_java_types = set()
j = 0
while j < len(field_types):
field_type = field_types[j]
if "[]" in field_type:
field_java_types.add("{}[]".format(
convert_single_type(field_type.replace("(", "").replace(")", "").replace("[]", ""))))
elif "(" in field_type:
multitypes = set()
while ")[]" not in field_types[j]:
field_type = field_types[j].replace("(", "")
multitypes.add(convert_single_type(field_type))
j += 1
field_type = field_types[j].replace(")[]", "")
multitypes.add(convert_single_type(field_type))
for singletype in multitypes:
field_java_types.add("{}[]".format(singletype))
elif "[" in field_type:
field_java_types.add("{}[]".format(
convert_single_type(field_type.replace("[", ""))))
while "]" not in field_types[j]:
j += 1
elif "{" in field_type:
field_java_types.add("Object")
while "}" not in lines[i]:
i += 1
i += 1
break
else:
field_java_types.add(convert_single_type(field_type))
j += 1
fields.append({
"name": field_name,
"types": list(field_java_types)
})
obj = {
"name": name,
"type": type,
"extends": extends,
"fields": fields
}
json.dump(obj, open("config-out.json", "w"))
if __name__ == "__main__":
convert("ts-in.ts")