From cc64489d7fedc99f5f383fe85d93c5851217f70e Mon Sep 17 00:00:00 2001 From: Guillaume Hivert Date: Sun, 12 May 2024 19:48:11 +0200 Subject: [PATCH] fix: change type to nature Signed-off-by: Guillaume Hivert --- .../src/backend/gleam/generate/types.gleam | 8 +- .../src/backend/postgres/queries.gleam | 82 +++++++++++++------ apps/backend/src/backend/router.gleam | 8 +- 3 files changed, 67 insertions(+), 31 deletions(-) diff --git a/apps/backend/src/backend/gleam/generate/types.gleam b/apps/backend/src/backend/gleam/generate/types.gleam index e9007a0..87b4534 100644 --- a/apps/backend/src/backend/gleam/generate/types.gleam +++ b/apps/backend/src/backend/gleam/generate/types.gleam @@ -40,7 +40,7 @@ pub fn type_definition_to_json( use gen <- result.map(reduce_components(type_def.constructors, mapper)) use constructors <- pair.map_first(pair.map_second(gen, set.to_list)) json.object([ - #("type", json.string("type-definition")), + #("nature", json.string("type-definition")), #("name", json.string(type_name)), #("documentation", json.nullable(type_def.documentation, json.string)), #("deprecation", json.nullable(type_def.documentation, json.string)), @@ -266,7 +266,7 @@ pub fn type_alias_to_json( use gen <- result.map(type_to_json(ctx, type_alias.alias)) use alias <- pair.map_first(pair.map_second(gen, set.to_list)) json.object([ - #("type", json.string("type-alias")), + #("nature", json.string("type-alias")), #("name", json.string(type_name)), #("documentation", json.nullable(type_alias.documentation, json.string)), #("deprecation", json.nullable(type_alias.documentation, json.string)), @@ -288,7 +288,7 @@ pub fn constant_to_json(ctx: Context, constant_name: String, constant: Constant) use gen <- result.map(type_to_json(ctx, constant.type_)) use type_ <- pair.map_first(pair.map_second(gen, set.to_list)) json.object([ - #("type", json.string("constant")), + #("nature", json.string("constant")), #("name", json.string(constant_name)), #("documentation", json.nullable(constant.documentation, json.string)), #("deprecation", json.nullable(constant.documentation, json.string)), @@ -305,7 +305,7 @@ pub fn function_to_json(ctx: Context, function_name: String, function: Function) |> pair.map_second(fn(s) { set.to_list(set.union(s, ret.1)) }) |> pair.map_first(fn(parameters) { json.object([ - #("type", json.string("function")), + #("nature", json.string("function")), #("name", json.string(function_name)), #("documentation", json.nullable(function.documentation, json.string)), #("deprecation", json.nullable(function.documentation, json.string)), diff --git a/apps/backend/src/backend/postgres/queries.gleam b/apps/backend/src/backend/postgres/queries.gleam index 6a78227..b271fe6 100644 --- a/apps/backend/src/backend/postgres/queries.gleam +++ b/apps/backend/src/backend/postgres/queries.gleam @@ -382,6 +382,61 @@ pub fn upsert_package_type_fun_signature( |> result.replace(Nil) } +pub fn name_search(db: pgo.Connection, query: String) { + let query = pgo.text(query) + "SELECT DISTINCT ON (type_name, nature, module_name) * + FROM ( + SELECT + s.name type_name, + s.documentation, + s.nature, + s.metadata, + s.json_signature, + m.name module_name, + p.name, + r.version, + string_to_array(r.version, '.')::int[] AS ordering + FROM package_type_fun_signature s + JOIN package_module m + ON m.id = s.package_module_id + JOIN package_release r + ON m.package_release_id = r.id + JOIN package p + ON p.id = r.package_id + WHERE s.name = $1 + ORDER BY m.name ASC, ordering DESC + LIMIT 100 + ) i" + |> pgo.execute(db, [query], decode_type_search) + |> result.map_error(error.DatabaseError) + |> result.map(fn(r) { r.rows }) +} + +fn decode_type_search(dyn) { + dynamic.decode8( + fn(a, b, c, d, e, f, g, h) { + json.object([ + #("name", json.string(a)), + #("documentation", json.string(b)), + #("nature", json.string(c)), + #("metadata", dynamic.unsafe_coerce(d)), + #("json_signature", dynamic.unsafe_coerce(e)), + #("module_name", json.string(f)), + #("package_name", json.string(g)), + #("version", json.string(h)), + ]) + }, + dynamic.element(0, dynamic.string), + dynamic.element(1, dynamic.string), + dynamic.element(2, dynamic.string), + dynamic.element(3, dynamic.dynamic), + dynamic.element(4, dynamic.dynamic), + dynamic.element(5, dynamic.string), + dynamic.element(6, dynamic.string), + dynamic.element(7, dynamic.string), + )(dyn) +} + pub fn search(db: pgo.Connection, q: String) { let query = pgo.text("'" <> q <> "'") "SELECT @@ -402,32 +457,7 @@ pub fn search(db: pgo.Connection, q: String) { ON p.id = r.package_id WHERE to_tsvector(s.signature_) @@ to_tsquery($1) LIMIT 100" - |> pgo.execute( - db, - [query], - dynamic.decode8( - fn(a, b, c, d, e, f, g, h) { - json.object([ - #("name", json.string(a)), - #("documentation", json.string(b)), - #("nature", json.string(c)), - #("metadata", dynamic.unsafe_coerce(d)), - #("json_signature", dynamic.unsafe_coerce(e)), - #("module_name", json.string(f)), - #("package_name", json.string(g)), - #("version", json.string(h)), - ]) - }, - dynamic.element(0, dynamic.string), - dynamic.element(1, dynamic.string), - dynamic.element(2, dynamic.string), - dynamic.element(3, dynamic.dynamic), - dynamic.element(4, dynamic.dynamic), - dynamic.element(5, dynamic.string), - dynamic.element(6, dynamic.string), - dynamic.element(7, dynamic.string), - ), - ) + |> pgo.execute(db, [query], decode_type_search) |> result.map_error(error.DatabaseError) |> result.map(fn(r) { r.rows }) } diff --git a/apps/backend/src/backend/router.gleam b/apps/backend/src/backend/router.gleam index e0633f7..74e63ea 100644 --- a/apps/backend/src/backend/router.gleam +++ b/apps/backend/src/backend/router.gleam @@ -20,13 +20,19 @@ fn empty_json() { |> wisp.json_response(200) } +fn search(query: String, ctx: Context) { + let a = result.unwrap(queries.name_search(ctx.db, query), []) + let b = result.unwrap(queries.search(ctx.db, query), []) + list.append(a, b) +} + pub fn handle_get(req: Request, ctx: Context) { case wisp.path_segments(req) { ["search"] -> { wisp.get_query(req) |> list.find(fn(item) { item.0 == "q" }) |> result.replace_error(error.EmptyError) - |> result.try(fn(item) { queries.search(ctx.db, item.1) }) + |> result.map(fn(item) { search(item.1, ctx) }) |> result.unwrap([]) |> json.preprocessed_array() |> json.to_string_builder()