22
22
////
23
23
24
24
import gleam/dict . { type Dict }
25
- import gleam/dynamic . { type DecodeErrors , type Decoder , type Dynamic }
25
+ import gleam/dynamic . { type Dynamic }
26
+ import gleam/dynamic/decode . { type DecodeError , type Decoder }
26
27
import gleam/json . { type Json }
27
28
import gleam/list
28
29
import gleam/result
@@ -34,6 +35,8 @@ pub type JsObject {
34
35
JsObject
35
36
}
36
37
38
+ pub type DecodeErrors = List ( DecodeError )
39
+
37
40
/// error constructors
38
41
///
39
42
pub type WechatError {
@@ -160,7 +163,7 @@ pub fn literal(ls: List(#(k, v))) -> JsObject {
160
163
pub fn int ( o : JsObject ) -> Result ( Int , WechatError ) {
161
164
o
162
165
|> dynamic
163
- |> dynamic . int
166
+ |> decode . run ( decode . int )
164
167
|> result . map_error ( WechatDecodeError ( _) )
165
168
}
166
169
@@ -169,7 +172,7 @@ pub fn int(o: JsObject) -> Result(Int, WechatError) {
169
172
pub fn float ( o : JsObject ) -> Result ( Float , WechatError ) {
170
173
o
171
174
|> dynamic
172
- |> dynamic . float
175
+ |> decode . run ( decode . float )
173
176
|> result . map_error ( WechatDecodeError ( _) )
174
177
}
175
178
@@ -178,7 +181,7 @@ pub fn float(o: JsObject) -> Result(Float, WechatError) {
178
181
pub fn bool ( o : JsObject ) -> Result ( Bool , WechatError ) {
179
182
o
180
183
|> dynamic
181
- |> dynamic . bool
184
+ |> decode . run ( decode . bool )
182
185
|> result . map_error ( WechatDecodeError ( _) )
183
186
}
184
187
@@ -187,7 +190,7 @@ pub fn bool(o: JsObject) -> Result(Bool, WechatError) {
187
190
pub fn string ( o : JsObject ) -> Result ( String , WechatError ) {
188
191
o
189
192
|> dynamic
190
- |> dynamic . string
193
+ |> decode . run ( decode . string )
191
194
|> result . map_error ( WechatDecodeError ( _) )
192
195
}
193
196
@@ -199,20 +202,24 @@ pub fn field(
199
202
name a : name,
200
203
of b : Decoder ( t) ,
201
204
) -> Result ( t, WechatError ) {
205
+ let decoder = {
206
+ use value <- decode . field ( a , b )
207
+ decode . success ( value )
208
+ }
202
209
o
203
210
|> dynamic
204
- |> dynamic . field ( a , b )
211
+ |> decode . run ( decoder )
205
212
|> result . map_error ( WechatDecodeError ( _) )
206
213
}
207
214
208
215
/// convert to `list(t)` with `gleam/dynamic`
209
216
///
210
217
pub fn list (
211
218
o : JsObject ,
212
- of f : fn ( Dynamic ) -> Result ( t , DecodeErrors ) ,
219
+ of f : Decoder ( t ) ,
213
220
) -> Result ( List ( t) , WechatError ) {
214
221
o
215
222
|> dynamic
216
- |> dynamic . list ( f )
223
+ |> decode . run ( decode . list ( f ) )
217
224
|> result . map_error ( WechatDecodeError ( _) )
218
225
}
0 commit comments