-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.elm
191 lines (157 loc) · 6.77 KB
/
Example.elm
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
module Example exposing (..)
import Dynamic.Build as D
import Dynamic as D
import Date exposing (Date)
import Dynamic.Json
import Json.Encode
import Json.Decode
type Example
= Constructor1 Bool Date
| Constructor2 { d : Char, f : Float }
| Constructor3 (List String)
| Constructor4 Example Example
| Constructor5 Example2
type Example2
= ConstructorX Int
test : Example
test =
Constructor4
(Constructor4
(Constructor2 { d = 'a', f = 1 })
(Constructor1 True (Date.fromTime 0))
)
(Constructor4
(Constructor3 [ "A" ])
(Constructor5 (ConstructorX 1))
)
run : Example -> Result String Bool
run t =
t
|> Dynamic.Json.encoder t_Example
|> Json.Encode.encode 0
|> Json.Decode.decodeString (Dynamic.Json.decoder t_Example)
|> Result.map ((==) t)
-- CODE BELOW WOULD BE GENERATED BY COMPILER by typing #Example --
t_Example : D.Dynamic Example
t_Example =
let
record_d_f =
let
types =
[ ( "d", D.typ D.t_Char )
, ( "f", D.typ D.t_Float )
]
fromElm r =
D.recordFromElm
[ ( "d", D.fromElm D.t_Char r.d )
, ( "f", D.fromElm D.t_Float r.f )
]
toElm data =
D.recordToElm types data
|> Result.andThen
(\values ->
case values of
[ data_d, data_f ] ->
D.toElm D.t_Char data_d
|> Result.andThen
(\d ->
D.toElm D.t_Float data_f
|> Result.andThen
(\f ->
Ok { d = d, f = f }
)
)
_ ->
D.internalError
)
in
D.buildRecord types [ D.unions D.t_Char, D.unions D.t_Float ] fromElm toElm
list_String =
D.t_List D.t_String
t_Example2 =
let
types =
[ ( "ConstructorX", [ D.typ D.t_Int ] ) ]
fromElm u =
D.unionFromElm <|
case u of
ConstructorX d1 ->
D.constructorFromElm "ConstructorX" [ D.fromElm D.t_Int d1 ]
toElm data =
D.unionToElm types data
|> Result.andThen
(\v ->
case v of
( "ConstructorX", [ d1 ] ) ->
D.toElm D.t_Int d1 |> Result.andThen (\v1 -> Ok (ConstructorX v1))
_ ->
D.internalError
)
in
D.buildUnion ( "Example2", [] ) types [ D.unions D.t_Int ] fromElm toElm
types =
[ ( "Constructor1", [ D.typ D.t_Bool, D.typ D.t_Date ] )
, ( "Constructor2", [ D.typ record_d_f ] )
, ( "Constructor3", [ D.typ list_String ] )
, ( "Constructor4", [ D.namedType "Example" [], D.namedType "Example" [] ] )
, ( "Constructor5", [ D.namedType "Example2" [] ] )
]
fromElm u =
D.unionFromElm <|
case u of
Constructor1 v1 v2 ->
D.constructorFromElm "Constructor1" [ D.fromElm D.t_Bool v1, D.fromElm D.t_Date v2 ]
Constructor2 v1 ->
D.constructorFromElm "Constructor2"
[ D.fromElm record_d_f v1 ]
Constructor3 v1 ->
D.constructorFromElm "Constructor3"
[ D.fromElm list_String v1 ]
Constructor4 v1 v2 ->
D.constructorFromElm "Constructor4"
[ fromElm v1
, fromElm v2
]
Constructor5 v1 ->
D.constructorFromElm "Constructor5"
[ D.fromElm t_Example2 v1 ]
toElm data =
D.unionToElm types data
|> Result.andThen
(\v ->
case v of
( "Constructor1", [ d1, d2 ] ) ->
D.toElm D.t_Bool d1
|> Result.andThen
(\v1 ->
D.toElm D.t_Date d2
|> Result.andThen
(\v2 -> Ok (Constructor1 v1 v2))
)
( "Constructor2", [ d1 ] ) ->
D.toElm record_d_f d1
|> Result.andThen
(\v1 -> Ok (Constructor2 v1))
( "Constructor3", [ d1 ] ) ->
D.toElm list_String d1
|> Result.andThen
(\v1 -> Ok (Constructor3 v1))
( "Constructor4", [ d1, d2 ] ) ->
toElm d1
|> Result.andThen
(\v1 ->
toElm d2
|> Result.andThen
(\v2 -> Ok (Constructor4 v1 v2))
)
( "Constructor5", [ d1 ] ) ->
D.toElm t_Example2 d1
|> Result.andThen
(\v1 ->
Ok (Constructor5 v1)
)
_ ->
D.internalError
)
in
D.buildUnion ( "Example", [] ) types [ D.unions D.t_Bool, D.unions D.t_Date, D.unions record_d_f, D.unions list_String, D.unions t_Example2 ] fromElm toElm