Skip to content

Commit 771d5c9

Browse files
committed
fix: working internals
Signed-off-by: Guillaume Hivert <[email protected]>
1 parent ec4c2e6 commit 771d5c9

File tree

2 files changed

+138
-43
lines changed

2 files changed

+138
-43
lines changed

apps/backend/src/api/signatures.gleam

+48-7
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ fn upsert_package_module(
8383

8484
fn upsert_type_definitions(
8585
db: pgo.Connection,
86+
package_interface: Package,
8687
module_id: Int,
8788
module: Module,
8889
gleam_toml: Dict(String, Toml),
@@ -97,7 +98,13 @@ fn upsert_type_definitions(
9798
|> json.to_string()
9899
let signature = type_definition_to_string(type_name, type_def)
99100
let type_def_json =
100-
type_definition_to_json(db, type_name, type_def, gleam_toml)
101+
type_definition_to_json(
102+
db,
103+
package_interface,
104+
type_name,
105+
type_def,
106+
gleam_toml,
107+
)
101108
use #(json_signature, parameters) <- result.try(type_def_json)
102109

103110
"INSERT INTO package_type_fun_signature (
@@ -145,6 +152,7 @@ fn upsert_type_definitions(
145152

146153
fn upsert_type_aliases(
147154
db: pgo.Connection,
155+
package_interface: Package,
148156
module_id: Int,
149157
module: Module,
150158
gleam_toml: Dict(String, Toml),
@@ -159,7 +167,13 @@ fn upsert_type_aliases(
159167
|> json.to_string()
160168
let signature = type_alias_to_string(type_name, type_alias)
161169
let type_alias_json =
162-
type_alias_to_json(db, type_name, type_alias, gleam_toml)
170+
type_alias_to_json(
171+
db,
172+
package_interface,
173+
type_name,
174+
type_alias,
175+
gleam_toml,
176+
)
163177
use #(json_signature, parameters) <- result.try(type_alias_json)
164178

165179
"INSERT INTO package_type_fun_signature (
@@ -218,6 +232,7 @@ fn implementations_pgo(implementations: package_interface.Implementations) {
218232

219233
fn upsert_constants(
220234
db: pgo.Connection,
235+
package_interface: Package,
221236
module_id: Int,
222237
module: Module,
223238
gleam_toml: Dict(String, Toml),
@@ -236,7 +251,13 @@ fn upsert_constants(
236251
|> json.to_string()
237252
let signature = constant_to_string(constant_name, constant)
238253
let constant_json =
239-
constant_to_json(db, constant_name, constant, gleam_toml)
254+
constant_to_json(
255+
db,
256+
package_interface,
257+
constant_name,
258+
constant,
259+
gleam_toml,
260+
)
240261
use #(json_signature, parameters) <- result.try(constant_json)
241262

242263
"INSERT INTO package_type_fun_signature (
@@ -289,6 +310,7 @@ fn upsert_constants(
289310

290311
fn upsert_functions(
291312
db: pgo.Connection,
313+
package_interface: Package,
292314
module_id: Int,
293315
module: Module,
294316
gleam_toml: Dict(String, Toml),
@@ -307,7 +329,13 @@ fn upsert_functions(
307329
|> json.to_string()
308330
let signature = function_to_string(function_name, function)
309331
let function_json =
310-
function_to_json(db, function_name, function, gleam_toml)
332+
function_to_json(
333+
db,
334+
package_interface,
335+
function_name,
336+
function,
337+
gleam_toml,
338+
)
311339
use #(json_signature, parameters) <- result.try(function_json)
312340

313341
"INSERT INTO package_type_fun_signature (
@@ -379,16 +407,29 @@ pub fn extract_signatures(
379407
wisp.log_info("Extracting " <> qualified_name <> " type definitions")
380408
use _ <- result.try(upsert_type_definitions(
381409
db,
410+
package,
382411
module_id,
383412
module,
384413
gleam_toml,
385414
))
386415
wisp.log_info("Extracting " <> qualified_name <> " type aliases")
387-
use _ <- result.try(upsert_type_aliases(db, module_id, module, gleam_toml))
416+
use _ <- result.try(upsert_type_aliases(
417+
db,
418+
package,
419+
module_id,
420+
module,
421+
gleam_toml,
422+
))
388423
wisp.log_info("Extracting " <> qualified_name <> " constants")
389-
use _ <- result.try(upsert_constants(db, module_id, module, gleam_toml))
424+
use _ <- result.try(upsert_constants(
425+
db,
426+
package,
427+
module_id,
428+
module,
429+
gleam_toml,
430+
))
390431
wisp.log_info("Extracting " <> qualified_name <> " functions")
391-
upsert_functions(db, module_id, module, gleam_toml)
432+
upsert_functions(db, package, module_id, module, gleam_toml)
392433
|> function.tap(fn(r) {
393434
use <- bool.guard(when: result.is_error(r), return: Nil)
394435
wisp.log_info("Extracting " <> qualified_name <> " finished")

apps/backend/src/gleam/generate/types.gleam

+90-36
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import gleam/list
88
import gleam/option
99
import gleam/order
1010
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,
1314
}
1415
import gleam/pair
1516
import gleam/pgo
@@ -35,11 +36,12 @@ fn reduce_components(
3536

3637
pub fn type_definition_to_json(
3738
db: pgo.Connection,
39+
package_interface: Package,
3840
type_name: String,
3941
type_def: TypeDefinition,
4042
toml: GleamToml,
4143
) -> 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, _)
4345
use gen <- result.map(reduce_components(type_def.constructors, mapper))
4446
use constructors <- pair.map_first(pair.map_second(gen, set.to_list))
4547
json.object([
@@ -54,10 +56,11 @@ pub fn type_definition_to_json(
5456

5557
fn type_constructor_to_json(
5658
db: pgo.Connection,
59+
package_interface: Package,
5760
gleam_toml: GleamToml,
5861
constructor: TypeConstructor,
5962
) {
60-
let mapper = parameters_to_json(db, gleam_toml, _)
63+
let mapper = parameters_to_json(db, package_interface, gleam_toml, _)
6164
use gen <- result.map(reduce_components(constructor.parameters, mapper))
6265
use parameters <- pair.map_first(gen)
6366
json.object([
@@ -70,10 +73,16 @@ fn type_constructor_to_json(
7073

7174
fn parameters_to_json(
7275
db: pgo.Connection,
76+
package_interface: Package,
7377
gleam_toml: GleamToml,
7478
parameter: Parameter,
7579
) {
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+
))
7786
use type_ <- pair.map_first(gen)
7887
json.object([
7988
#("type", json.string("parameter")),
@@ -82,10 +91,15 @@ fn parameters_to_json(
8291
])
8392
}
8493

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+
) {
86100
case type_ {
87101
package_interface.Tuple(elements) -> {
88-
let mapper = type_to_json(db, gleam_toml, _)
102+
let mapper = type_to_json(db, package_interface, gleam_toml, _)
89103
use gen <- result.map(reduce_components(elements, mapper))
90104
use elements <- pair.map_first(gen)
91105
json.object([
@@ -94,9 +108,14 @@ fn type_to_json(db: pgo.Connection, gleam_toml: GleamToml, type_: Type) {
94108
])
95109
}
96110
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, _)
98112
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+
))
100119
let new_params = set.union(of: params, and: gen.1)
101120
json.object([
102121
#("type", json.string("fn")),
@@ -111,19 +130,23 @@ fn type_to_json(db: pgo.Connection, gleam_toml: GleamToml, type_: Type) {
111130
Ok(#(json, set.new()))
112131
}
113132
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, _)
115134
use gen <- result.try(reduce_components(parameters, mapper))
116135
use ref <- result.map(extract_parameters_relation(
117136
db,
137+
package_interface,
118138
gleam_toml,
119139
name,
120140
package,
121141
module,
122142
))
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+
}
124147
json.object([
125148
#("type", json.string("named")),
126-
#("ref", json.int(ref)),
149+
#("ref", json.nullable(ref, json.int)),
127150
#("name", json.string(name)),
128151
#("package", json.string(package)),
129152
#("module", json.string(module)),
@@ -175,13 +198,13 @@ fn keep_matching_releases(rows: List(#(Int, String)), requirement: String) {
175198

176199
fn find_type_signature(
177200
db: pgo.Connection,
201+
package_interface: Package,
178202
name: String,
179203
package: String,
180-
requirement: String,
181204
module: String,
182205
releases: List(Int),
183-
) {
184-
use response <- result.try({
206+
) -> Result(option.Option(Int), error.Error) {
207+
case
185208
list.fold(releases, option.None, fn(acc, release) {
186209
use <- bool.guard(when: option.is_some(acc), return: acc)
187210
case
@@ -198,36 +221,49 @@ fn find_type_signature(
198221
dynamic.element(0, dynamic.int),
199222
)
200223
{
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+
}
202229
Error(_) -> option.None
203230
}
204231
})
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+
}
218253
}
219254

220255
fn extract_parameters_relation(
221256
db: pgo.Connection,
257+
package_interface: Package,
222258
gleam_toml: GleamToml,
223259
name: String,
224260
package: String,
225261
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))
228264
use requirement <- result.try(get_toml_requirement(gleam_toml, package))
229265
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)
231267
}
232268

233269
fn get_toml_requirement(gleam_toml: GleamToml, package: String) {
@@ -249,11 +285,17 @@ fn is_prelude(package: String, module: String) {
249285

250286
pub fn type_alias_to_json(
251287
db: pgo.Connection,
288+
package_interface: Package,
252289
type_name: String,
253290
type_alias: TypeAlias,
254291
gleam_toml: GleamToml,
255292
) {
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+
))
257299
use alias <- pair.map_first(pair.map_second(gen, set.to_list))
258300
json.object([
259301
#("type", json.string("type-alias")),
@@ -278,11 +320,17 @@ pub fn implementations_to_json(implementations: Implementations) {
278320

279321
pub fn constant_to_json(
280322
db: pgo.Connection,
323+
package_interface: Package,
281324
constant_name: String,
282325
constant: Constant,
283326
gleam_toml: GleamToml,
284327
) {
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+
))
286334
use type_ <- pair.map_first(pair.map_second(gen, set.to_list))
287335
json.object([
288336
#("type", json.string("constant")),
@@ -296,13 +344,19 @@ pub fn constant_to_json(
296344

297345
pub fn function_to_json(
298346
db: pgo.Connection,
347+
package_interface: Package,
299348
function_name: String,
300349
function: Function,
301350
gleam_toml: GleamToml,
302351
) {
303-
let mapper = parameters_to_json(db, gleam_toml, _)
352+
let mapper = parameters_to_json(db, package_interface, gleam_toml, _)
304353
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+
))
306360
gen
307361
|> pair.map_second(fn(s) { set.to_list(set.union(s, ret.1)) })
308362
|> pair.map_first(fn(parameters) {

0 commit comments

Comments
 (0)