@@ -8,8 +8,9 @@ import gleam/list
8
8
import gleam/option
9
9
import gleam/order
10
10
import gleam/package_interface . {
11
- type Constant , type Function , type Implementations , type Parameter , type Type ,
12
- type TypeAlias , type TypeConstructor , type TypeDefinition ,
11
+ type Constant , type Function , type Implementations , type Package ,
12
+ type Parameter , type Type , type TypeAlias , type TypeConstructor ,
13
+ type TypeDefinition ,
13
14
}
14
15
import gleam/pair
15
16
import gleam/pgo
@@ -35,11 +36,12 @@ fn reduce_components(
35
36
36
37
pub fn type_definition_to_json (
37
38
db : pgo . Connection ,
39
+ package_interface : Package ,
38
40
type_name : String ,
39
41
type_def : TypeDefinition ,
40
42
toml : GleamToml ,
41
43
) -> Result ( # ( Json , List ( Int ) ) , error . Error ) {
42
- let mapper = type_constructor_to_json ( db , toml , _)
44
+ let mapper = type_constructor_to_json ( db , package_interface , toml , _)
43
45
use gen <- result . map ( reduce_components ( type_def . constructors , mapper ) )
44
46
use constructors <- pair . map_first ( pair . map_second ( gen , set . to_list ) )
45
47
json . object ( [
@@ -54,10 +56,11 @@ pub fn type_definition_to_json(
54
56
55
57
fn type_constructor_to_json (
56
58
db : pgo . Connection ,
59
+ package_interface : Package ,
57
60
gleam_toml : GleamToml ,
58
61
constructor : TypeConstructor ,
59
62
) {
60
- let mapper = parameters_to_json ( db , gleam_toml , _)
63
+ let mapper = parameters_to_json ( db , package_interface , gleam_toml , _)
61
64
use gen <- result . map ( reduce_components ( constructor . parameters , mapper ) )
62
65
use parameters <- pair . map_first ( gen )
63
66
json . object ( [
@@ -70,10 +73,16 @@ fn type_constructor_to_json(
70
73
71
74
fn parameters_to_json (
72
75
db : pgo . Connection ,
76
+ package_interface : Package ,
73
77
gleam_toml : GleamToml ,
74
78
parameter : Parameter ,
75
79
) {
76
- use gen <- result . map ( type_to_json ( db , gleam_toml , parameter . type_ ) )
80
+ use gen <- result . map ( type_to_json (
81
+ db ,
82
+ package_interface ,
83
+ gleam_toml ,
84
+ parameter . type_ ,
85
+ ) )
77
86
use type_ <- pair . map_first ( gen )
78
87
json . object ( [
79
88
# ( "type" , json . string ( "parameter" ) ) ,
@@ -82,10 +91,15 @@ fn parameters_to_json(
82
91
] )
83
92
}
84
93
85
- fn type_to_json ( db : pgo . Connection , gleam_toml : GleamToml , type_ : Type ) {
94
+ fn type_to_json (
95
+ db : pgo . Connection ,
96
+ package_interface : Package ,
97
+ gleam_toml : GleamToml ,
98
+ type_ : Type ,
99
+ ) {
86
100
case type_ {
87
101
package_interface . Tuple ( elements ) -> {
88
- let mapper = type_to_json ( db , gleam_toml , _)
102
+ let mapper = type_to_json ( db , package_interface , gleam_toml , _)
89
103
use gen <- result . map ( reduce_components ( elements , mapper ) )
90
104
use elements <- pair . map_first ( gen )
91
105
json . object ( [
@@ -94,9 +108,14 @@ fn type_to_json(db: pgo.Connection, gleam_toml: GleamToml, type_: Type) {
94
108
] )
95
109
}
96
110
package_interface . Fn ( params , return ) -> {
97
- let mapper = type_to_json ( db , gleam_toml , _)
111
+ let mapper = type_to_json ( db , package_interface , gleam_toml , _)
98
112
use # ( elements , params ) <- result . try ( reduce_components ( params , mapper ) )
99
- use gen <- result . map ( type_to_json ( db , gleam_toml , return ) )
113
+ use gen <- result . map ( type_to_json (
114
+ db ,
115
+ package_interface ,
116
+ gleam_toml ,
117
+ return ,
118
+ ) )
100
119
let new_params = set . union ( of : params , and : gen . 1 )
101
120
json . object ( [
102
121
# ( "type" , json . string ( "fn" ) ) ,
@@ -111,19 +130,23 @@ fn type_to_json(db: pgo.Connection, gleam_toml: GleamToml, type_: Type) {
111
130
Ok ( # ( json , set . new ( ) ) )
112
131
}
113
132
package_interface . Named ( name , package , module , parameters ) -> {
114
- let mapper = type_to_json ( db , gleam_toml , _)
133
+ let mapper = type_to_json ( db , package_interface , gleam_toml , _)
115
134
use gen <- result . try ( reduce_components ( parameters , mapper ) )
116
135
use ref <- result . map ( extract_parameters_relation (
117
136
db ,
137
+ package_interface ,
118
138
gleam_toml ,
119
139
name ,
120
140
package ,
121
141
module ,
122
142
) )
123
- let new_ids = set . insert ( gen . 1 , ref )
143
+ let new_ids = case ref {
144
+ option . None -> gen . 1
145
+ option . Some ( ref ) -> set . insert ( gen . 1 , ref )
146
+ }
124
147
json . object ( [
125
148
# ( "type" , json . string ( "named" ) ) ,
126
- # ( "ref" , json . int ( ref ) ) ,
149
+ # ( "ref" , json . nullable ( ref , json . int ) ) ,
127
150
# ( "name" , json . string ( name ) ) ,
128
151
# ( "package" , json . string ( package ) ) ,
129
152
# ( "module" , json . string ( module ) ) ,
@@ -175,13 +198,13 @@ fn keep_matching_releases(rows: List(#(Int, String)), requirement: String) {
175
198
176
199
fn find_type_signature (
177
200
db : pgo . Connection ,
201
+ package_interface : Package ,
178
202
name : String ,
179
203
package : String ,
180
- requirement : String ,
181
204
module : String ,
182
205
releases : List ( Int ) ,
183
- ) {
184
- use response <- result . try ( {
206
+ ) -> Result ( option . Option ( Int ) , error . Error ) {
207
+ case
185
208
list . fold ( releases , option . None , fn ( acc , release ) {
186
209
use <- bool . guard ( when : option . is_some ( acc ) , return : acc )
187
210
case
@@ -198,36 +221,49 @@ fn find_type_signature(
198
221
dynamic . element ( 0 , dynamic . int ) ,
199
222
)
200
223
{
201
- Ok ( value ) -> option . Some ( value )
224
+ Ok ( value ) ->
225
+ case list . first ( value . rows ) {
226
+ Ok ( v ) -> option . Some ( v )
227
+ Error ( _ ) -> option . None
228
+ }
202
229
Error ( _ ) -> option . None
203
230
}
204
231
} )
205
- |> option . to_result ( error . UnknownError (
206
- "Release "
207
- <> package
208
- <> " with conditions "
209
- <> requirement
210
- <> " not found" ,
211
- ) )
212
- } )
213
- response . rows
214
- |> list . first ( )
215
- |> result . replace_error ( error . UnknownError (
216
- "No type found for " <> module <> "." <> name ,
217
- ) )
232
+ {
233
+ option . None -> {
234
+ case package_interface . name == package {
235
+ False -> Error ( error . UnknownError ( "No release found" ) )
236
+ True ->
237
+ case dict . get ( package_interface . modules , module ) {
238
+ Error ( _ ) -> Error ( error . UnknownError ( "No module found" ) )
239
+ Ok ( mod ) ->
240
+ case dict . get ( mod . type_aliases , name ) {
241
+ Ok ( _ ) -> Error ( error . UnknownError ( "No release found" ) )
242
+ Error ( _ ) ->
243
+ case dict . get ( mod . types , name ) {
244
+ Ok ( _ ) -> Error ( error . UnknownError ( "No release found" ) )
245
+ Error ( _ ) -> Ok ( option . None )
246
+ }
247
+ }
248
+ }
249
+ }
250
+ }
251
+ option . Some ( value ) -> Ok ( option . Some ( value ) )
252
+ }
218
253
}
219
254
220
255
fn extract_parameters_relation (
221
256
db : pgo . Connection ,
257
+ package_interface : Package ,
222
258
gleam_toml : GleamToml ,
223
259
name : String ,
224
260
package : String ,
225
261
module : String ,
226
- ) {
227
- use <- bool . guard ( when : is_prelude ( package , module ) , return : Ok ( - 1 ) )
262
+ ) -> Result ( option . Option ( Int ) , error . Error ) {
263
+ use <- bool . guard ( when : is_prelude ( package , module ) , return : Ok ( option . None ) )
228
264
use requirement <- result . try ( get_toml_requirement ( gleam_toml , package ) )
229
265
use releases <- result . try ( find_package_release ( db , package , requirement ) )
230
- find_type_signature ( db , name , package , requirement , module , releases )
266
+ find_type_signature ( db , package_interface , name , package , module , releases )
231
267
}
232
268
233
269
fn get_toml_requirement ( gleam_toml : GleamToml , package : String ) {
@@ -249,11 +285,17 @@ fn is_prelude(package: String, module: String) {
249
285
250
286
pub fn type_alias_to_json (
251
287
db : pgo . Connection ,
288
+ package_interface : Package ,
252
289
type_name : String ,
253
290
type_alias : TypeAlias ,
254
291
gleam_toml : GleamToml ,
255
292
) {
256
- use gen <- result . map ( type_to_json ( db , gleam_toml , type_alias . alias ) )
293
+ use gen <- result . map ( type_to_json (
294
+ db ,
295
+ package_interface ,
296
+ gleam_toml ,
297
+ type_alias . alias ,
298
+ ) )
257
299
use alias <- pair . map_first ( pair . map_second ( gen , set . to_list ) )
258
300
json . object ( [
259
301
# ( "type" , json . string ( "type-alias" ) ) ,
@@ -278,11 +320,17 @@ pub fn implementations_to_json(implementations: Implementations) {
278
320
279
321
pub fn constant_to_json (
280
322
db : pgo . Connection ,
323
+ package_interface : Package ,
281
324
constant_name : String ,
282
325
constant : Constant ,
283
326
gleam_toml : GleamToml ,
284
327
) {
285
- use gen <- result . map ( type_to_json ( db , gleam_toml , constant . type_ ) )
328
+ use gen <- result . map ( type_to_json (
329
+ db ,
330
+ package_interface ,
331
+ gleam_toml ,
332
+ constant . type_ ,
333
+ ) )
286
334
use type_ <- pair . map_first ( pair . map_second ( gen , set . to_list ) )
287
335
json . object ( [
288
336
# ( "type" , json . string ( "constant" ) ) ,
@@ -296,13 +344,19 @@ pub fn constant_to_json(
296
344
297
345
pub fn function_to_json (
298
346
db : pgo . Connection ,
347
+ package_interface : Package ,
299
348
function_name : String ,
300
349
function : Function ,
301
350
gleam_toml : GleamToml ,
302
351
) {
303
- let mapper = parameters_to_json ( db , gleam_toml , _)
352
+ let mapper = parameters_to_json ( db , package_interface , gleam_toml , _)
304
353
use gen <- result . try ( reduce_components ( function . parameters , mapper ) )
305
- use ret <- result . map ( type_to_json ( db , gleam_toml , function . return ) )
354
+ use ret <- result . map ( type_to_json (
355
+ db ,
356
+ package_interface ,
357
+ gleam_toml ,
358
+ function . return ,
359
+ ) )
306
360
gen
307
361
|> pair . map_second ( fn ( s ) { set . to_list ( set . union ( s , ret . 1 ) ) } )
308
362
|> pair . map_first ( fn ( parameters ) {
0 commit comments