Skip to content

Commit

Permalink
fix: change type to nature
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Hivert <[email protected]>
  • Loading branch information
ghivert committed May 12, 2024
1 parent 6643bb7 commit cc64489
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 31 deletions.
8 changes: 4 additions & 4 deletions apps/backend/src/backend/gleam/generate/types.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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)),
Expand All @@ -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)),
Expand All @@ -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)),
Expand Down
82 changes: 56 additions & 26 deletions apps/backend/src/backend/postgres/queries.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 })
}
8 changes: 7 additions & 1 deletion apps/backend/src/backend/router.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit cc64489

Please sign in to comment.