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
|
# File 'lib/jade/stdlib/decode/params.rb', line 22
def code
<<~JADE
module Decode.Params exposing (
Params(..),
accept,
bool,
collect,
default,
empty,
float,
int,
nested,
string,
)
import Decode exposing (Decoder)
type Params(a) = Params(List((String, Decoder(a))), List((String, a)))
def empty -> Params(a)
Params([], [])
end
def accept(p: Params(a), key: String, decoder: Decoder(a)) -> Params(a)
case p
in Params(accs, defs) then Params(accs ++ [(key, decoder)], defs)
end
end
def default(p: Params(a), key: String, value: a) -> Params(a)
case p
in Params(accs, defs) then Params(accs, defs ++ [(key, value)])
end
end
def string(p: Params(a), key: String, ctor: String -> a) -> Params(a)
accept(p, key, Decode.map(Decode.string, ctor))
end
def int(p: Params(a), key: String, ctor: Int -> a) -> Params(a)
accept(p, key, Decode.map(Decode.int, ctor))
end
def float(p: Params(a), key: String, ctor: Float -> a) -> Params(a)
accept(p, key, Decode.map(Decode.float, ctor))
end
def bool(p: Params(a), key: String, ctor: Bool -> a) -> Params(a)
accept(p, key, Decode.map(Decode.bool, ctor))
end
def nested(
p: Params(a),
key: String,
ctor: List(b) -> a,
sub: Params(b),
) -> Params(a)
accept(p, key, Decode.map(collect(sub), ctor))
end
def collect(p: Params(a)) -> Decoder(List(a))
case p
in Params(accs, defs)
accs
|> List.map((acc) -> { accept_to_decoder(defs, acc) })
|> Decode.sequence
|> Decode.map(filter_justs)
end
end
def accept_to_decoder(
defs: List((String, a)),
acc: (String, Decoder(a)),
) -> Decoder(Maybe(a))
key = Tuple.first(acc)
decoder = Tuple.second(acc)
raw = Decode.optional_field(key, decoder)
case lookup_default(defs, key)
in Just(v) then with_default(raw, v)
in Nothing then raw
end
end
def with_default(decoder: Decoder(Maybe(a)), v: a) -> Decoder(Maybe(a))
Decode.map(decoder, (m) -> { maybe_or_just(m, v) })
end
def maybe_or_just(m: Maybe(a), v: a) -> Maybe(a)
case m
in Just(_) then m
in Nothing then Just(v)
end
end
def filter_justs(maybes: List(Maybe(a))) -> List(a)
List.and_then(maybes, maybe_to_list)
end
def maybe_to_list(m: Maybe(a)) -> List(a)
case m
in Just(x) then [x]
in Nothing then []
end
end
def lookup_default(defs: List((String, a)), key: String) -> Maybe(a)
case defs
in [] then Nothing
in [(k, v) | rest] then k == key ? Just(v) : lookup_default(rest, key)
end
end
JADE
end
|