-
Notifications
You must be signed in to change notification settings - Fork 23
/
decoder.py
220 lines (157 loc) · 5.93 KB
/
decoder.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import re
from helpers import *
KNOWN_DECODERS = [
'maybe',
'list' ,
'int' ,
'float' ,
'bool',
'string' ]
JSON_DECODE = 'Json.Decode.'
JSON_ENCODE = 'Json.Encode.'
def get_type_alias_name(string):
grab_type_name = re.search('type alias(.+)\=', string)
if grab_type_name is None or len(grab_type_name.groups()) == 0:
raise Exception("Can't find type alias declaration")
groups = grab_type_name.groups()
if len(groups) > 1:
raise "Please only give me one type alias at a time"
return groups[0].strip()
def get_union_type_name(string):
grab_type_name = re.search('type (.+)\=', string)
if grab_type_name is None or len(grab_type_name.groups()) == 0:
raise Exception("Can't find type declaration")
groups = grab_type_name.groups()
if len(groups) > 1:
raise "Please only give me one type at a time"
return groups[0].strip()
def get_fields(string):
grab_fields = re.match(".*{(.+)\}", string)
if grab_fields is None or len(grab_fields.groups()) == 0:
raise Exception("Please give me a field I can work with")
groups = grab_fields.groups()
return groups[0].split(',')
def get_constructors(string):
string = string[string.index('=') + 1:]
return [part.strip() for part in string.split('|')]
def field_name_and_type(string):
splitted = string.split(':')
return (splitted[0].strip(), splitted[1].strip())
def make_guess_at_codec(string):
return string.lower()
def prefix_codec(prefix, value, default_module=JSON_DECODE):
parts = value.split()
return ' '.join(
default_module + value
if value in KNOWN_DECODERS
else prefix + value.capitalize()
for value in parts
)
def suffix_codec(suffix, value):
parts = value.split()
return ' '.join(
value
if value in KNOWN_DECODERS
else value + suffix
for value in parts
)
def create_decoder(string, has_snakecase=False, prefix=None, suffix=None):
string = re.sub('[\\n\\r]', '', string)
type_name = get_type_alias_name(string)
fields = [field_name_and_type(f) for f in get_fields(string)]
if has_snakecase:
fields = [
(convert_camelcase_to_underscores(name), value)
for name, value in fields
]
fields = [(name, make_guess_at_codec(type)) for name, type in fields]
if prefix is not None:
fields = [ (name, prefix_codec(prefix, value)) for name, value in fields ]
if suffix is not None:
fields = [ (name, suffix_codec(suffix, value)) for name, value in fields ]
formattedFields ='\n '.join(
'|: ("{name}" := {type})'
.format(
name=name,
type=type
)
for (name, type) in fields)
output = """
decode{type_name} : Json.Decode.Decoder {type_name}
decode{type_name} =
Json.Decode.succeed {type_name}
{fields}
""".format(type_name=type_name, fields=formattedFields)
return output.strip()
def create_encoder(string, has_snakecase=False, prefix=None, suffix=None):
string = re.sub('[\\n\\r]', '', string)
type_name = get_type_alias_name(string)
original_fields = [field_name_and_type(f) for f in get_fields(string)]
fields = original_fields[:]
if has_snakecase:
fields = [
(convert_camelcase_to_underscores(name), value)
for name, value in fields
]
fields = [(name, make_guess_at_codec(type)) for name, type in fields]
if prefix is not None:
fields = [ (name, prefix_codec(prefix, value, JSON_ENCODE)) for name, value in fields ]
if suffix is not None:
fields = [ (name, suffix_codec(suffix, value)) for name, value in fields ]
formatted_fields ='\n , '.join(
'("{name}", {type} record.{original_name})'
.format(
name=encoded_name,
type=' <| '.join(encoder.split(' ')),
original_name=original_name
)
for ((encoded_name, encoder), (original_name, _)) in zip(fields, original_fields))
output = """
encode{type_name} : {type_name} -> Json.Encode.Value
encode{type_name} record =
Json.Encode.object
[ {fields}
]
""".format(type_name=type_name, fields=formatted_fields)
return output.strip()
def create_union_type_decoder(string, has_snakecase=False):
"""
string is a union type that looks like
type Action = Noop | Run
"""
string = re.sub('[\\n\\r]', '', string)
type_name = get_union_type_name(string)
constructors = get_constructors(string)
formatted_constructors = '\n '.join(
'"{constructor}" -> Result.Ok {constructor}'.format(constructor=constructor) for constructor in constructors
)
output = """
decode{type_name} : Json.Decode.Decoder {type_name}
decode{type_name} =
let
decodeToType string =
case string of
{patterns}
_ -> Result.Err ("Not valid pattern for decoder to {type_name}. Pattern: " ++ (toString string))
in
Json.Decode.customDecoder Json.Decode.string decodeToType
""".format(type_name=type_name, patterns=formatted_constructors)
return output.strip()
def create_union_type_encoder(string, has_snakecase=False):
"""
string is a union type that looks like
type Action = Noop | Run
"""
string = re.sub('[\\n\\r]', '', string)
type_name = get_union_type_name(string)
constructors = get_constructors(string)
formatted_constructors = '\n '.join(
'{constructor} -> Json.Encode.string "{constructor}"'.format(constructor=constructor) for constructor in constructors
)
output = """
encode{type_name} : {type_name} -> Json.Encode.Value
encode{type_name} item =
case item of
{patterns}
""".format(type_name=type_name, patterns=formatted_constructors)
return output.strip()