/
runtime.go
135 lines (114 loc) · 2.68 KB
/
runtime.go
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
package main
func (fg *FileGenerator) GenerateRuntime() {
// Applicative-style decoders. This is fine as long as this is the only Applicative in the
// package, otherwise operator will clash, since Elm does not have support to generalise
// them via HKTs.
fg.P("")
fg.P("")
fg.P("(<$>) : (a -> b) -> JD.Decoder a -> JD.Decoder b")
fg.P("(<$>) =")
fg.In()
fg.P("JD.map")
fg.Out()
fg.P("")
fg.P("")
fg.P("(<*>) : JD.Decoder (a -> b) -> JD.Decoder a -> JD.Decoder b")
fg.P("(<*>) f v =")
fg.In()
fg.P("f |> JD.andThen (\\x -> x <$> v)")
fg.Out()
fg.P("")
fg.P("")
fg.P("optionalDecoder : JD.Decoder a -> JD.Decoder (Maybe a)")
fg.P("optionalDecoder decoder =")
fg.In()
fg.P("JD.oneOf")
fg.In()
fg.P("[ JD.map Just decoder")
fg.P(", JD.succeed Nothing")
fg.P("]")
fg.Out()
fg.Out()
fg.P("")
fg.P("")
fg.P("requiredFieldDecoder : String -> a -> JD.Decoder a -> JD.Decoder a")
fg.P("requiredFieldDecoder name default decoder =")
fg.In()
fg.P("withDefault default (JD.field name decoder)")
fg.Out()
fg.P("")
fg.P("")
fg.P("optionalFieldDecoder : String -> JD.Decoder a -> JD.Decoder (Maybe a)")
fg.P("optionalFieldDecoder name decoder =")
fg.In()
fg.P("optionalDecoder (JD.field name decoder)")
fg.Out()
fg.P("")
fg.P("")
fg.P("repeatedFieldDecoder : String -> JD.Decoder a -> JD.Decoder (List a)")
fg.P("repeatedFieldDecoder name decoder =")
fg.In()
fg.P("withDefault [] (JD.field name (JD.list decoder))")
fg.Out()
fg.P("")
fg.P("")
fg.P("withDefault : a -> JD.Decoder a -> JD.Decoder a")
fg.P("withDefault default decoder =")
fg.In()
fg.P("JD.oneOf")
fg.In()
fg.P("[ decoder")
fg.P(", JD.succeed default")
fg.P("]")
fg.Out()
fg.Out()
fg.P("")
fg.P("")
fg.P("optionalEncoder : String -> (a -> JE.Value) -> Maybe a -> Maybe (String, JE.Value)")
fg.P("optionalEncoder name encoder v =")
fg.In()
fg.P("case v of")
fg.In()
fg.P("Just x ->")
fg.In()
fg.P("Just ( name, encoder x )")
fg.Out()
fg.P("")
fg.P("Nothing ->")
fg.In()
fg.P("Nothing")
fg.Out()
fg.Out()
fg.Out()
fg.P("")
fg.P("")
fg.P("requiredFieldEncoder : String -> (a -> JE.Value) -> a -> a -> Maybe ( String, JE.Value )")
fg.P("requiredFieldEncoder name encoder default v =")
fg.In()
fg.P("if v == default then")
fg.In()
fg.P("Nothing")
fg.Out()
fg.P("else")
fg.In()
fg.P("Just ( name, encoder v )")
fg.Out()
fg.Out()
fg.P("")
fg.P("")
fg.P("repeatedFieldEncoder : String -> (a -> JE.Value) -> List a -> Maybe (String, JE.Value)")
fg.P("repeatedFieldEncoder name encoder v =")
fg.In()
fg.P("case v of")
fg.In()
fg.P("[] ->")
fg.In()
fg.P("Nothing")
fg.Out()
fg.P("_ ->")
fg.In()
fg.P("Just (name, JE.list <| List.map encoder v)")
fg.Out()
fg.Out()
fg.Out()
}