-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform_.py
62 lines (48 loc) · 1.74 KB
/
platform_.py
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
from enum import Enum
class Platform(Enum):
Swift = 'swift'
Kotlin = 'kotlin'
def format_array(self, class_name: str):
return f'[{class_name}]' if self is Platform.Swift else f'List<{class_name}>'
def format(self, value, use_class=False) -> str:
return _format_for_swift(value, use_class) if self is Platform.Swift else _format_for_kotlin(value, use_class)
def _format_for_kotlin(value, use_class=False) -> str:
class_name = f'{"class" if use_class else "data class"} {value.name}'
attrs: list = value.attrs
content = '\n'.join(
[
f'{_format_annotation_for_kotlin(item.description, item.enumDescription)}\tval {item.name}: {item.type}?,'
for item in attrs]
)
return '%s (\n%s\n)' % (class_name, content)
def _format_for_swift(value, use_class=False) -> str:
"""
Swift 中Model
"""
classname = f'{"class" if use_class else "struct"} {value.name}: Decodable'
attrs: list = value.attrs
content = '\n'.join(
[f'{_format_annotation_for_swift(item.description, item.enumDescription)}\tlet {item.name}: {item.type}'
for item in attrs]
)
return '%s {\n%s\n}' % (classname, content)
def _format_annotation_for_swift(description, enum_description) -> str:
"""
Swift 中的注释
"""
result = ''
if description:
result += f'\t/// {description}\n'
if enum_description:
result += f'\t/// {enum_description}\n'
return result
def _format_annotation_for_kotlin(description, enum_description) -> str:
"""
Swift 中的注释
"""
result = ''
if description:
result += f'\t// {description}\n'
if enum_description:
result += f'\t// {enum_description}\n'
return result