Skip to content

Commit

Permalink
feat: rename nature to kind
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 d1d3457 commit 58a8d4c
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- migrate:up
alter table package_release
add column retirement jsonb;

-- migrate:down
alter table package_release
drop column retirement;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- migrate:up
alter table package_type_fun_signature
rename column nature to kind;

-- migrate:down
alter table package_type_fun_signature
rename column kind to nature;
9 changes: 6 additions & 3 deletions apps/backend/db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ CREATE TABLE public.package_release (
created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
package_interface text,
gleam_toml text
gleam_toml text,
retirement jsonb
);


Expand Down Expand Up @@ -218,7 +219,7 @@ CREATE TABLE public.package_type_fun_signature (
documentation text NOT NULL,
signature_ text NOT NULL,
json_signature jsonb NOT NULL,
nature public.type_nature NOT NULL,
kind public.type_nature NOT NULL,
parameters integer[] NOT NULL,
deprecation text,
implementations text,
Expand Down Expand Up @@ -456,4 +457,6 @@ INSERT INTO public.schema_migrations (version) VALUES
('20240412155056'),
('20240412155057'),
('20240413164020'),
('20240506110519');
('20240506110519'),
('20240512211227'),
('20240512214036');
12 changes: 6 additions & 6 deletions apps/backend/src/api/signatures.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn upsert_type_definitions(ctx: Context, module: context.Module) {
let _ =
queries.upsert_package_type_fun_signature(
db: ctx.db,
nature: queries.TypeDefinition,
kind: queries.TypeDefinition,
name: type_name,
documentation: option.None,
metadata: json.null(),
Expand All @@ -49,7 +49,7 @@ fn upsert_type_definitions(ctx: Context, module: context.Module) {
use gen <- result.try(type_definition_to_json(ctx, type_name, type_def))
queries.upsert_package_type_fun_signature(
db: ctx.db,
nature: queries.TypeDefinition,
kind: queries.TypeDefinition,
name: type_name,
documentation: type_def.documentation,
metadata: metadata.generate(type_def.deprecation, None),
Expand All @@ -73,7 +73,7 @@ fn upsert_type_aliases(ctx: Context, module: context.Module) {
let _ =
queries.upsert_package_type_fun_signature(
db: ctx.db,
nature: queries.TypeAlias,
kind: queries.TypeAlias,
name: type_name,
documentation: option.None,
metadata: json.null(),
Expand All @@ -88,7 +88,7 @@ fn upsert_type_aliases(ctx: Context, module: context.Module) {
queries.upsert_package_type_fun_signature(
db: ctx.db,
name: type_name,
nature: queries.TypeAlias,
kind: queries.TypeAlias,
documentation: type_alias.documentation,
metadata: metadata.generate(type_alias.deprecation, None),
signature: type_alias_to_string(type_name, type_alias),
Expand All @@ -111,7 +111,7 @@ fn upsert_constants(ctx: Context, module: context.Module) {
queries.upsert_package_type_fun_signature(
db: ctx.db,
name: constant_name,
nature: queries.Constant,
kind: queries.Constant,
documentation: constant.documentation,
metadata: Some(constant.implementations)
|> metadata.generate(constant.deprecation, _),
Expand All @@ -135,7 +135,7 @@ fn upsert_functions(ctx: Context, module: context.Module) {
queries.upsert_package_type_fun_signature(
db: ctx.db,
name: function_name,
nature: queries.Function,
kind: queries.Function,
documentation: function.documentation,
metadata: Some(function.implementations)
|> metadata.generate(function.deprecation, _),
Expand Down
22 changes: 11 additions & 11 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([
#("nature", json.string("type-definition")),
#("kind", 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 All @@ -54,7 +54,7 @@ fn type_constructor_to_json(ctx: Context, constructor: TypeConstructor) {
use gen <- result.map(reduce_components(constructor.parameters, mapper))
use parameters <- pair.map_first(gen)
json.object([
#("type", json.string("type-constructor")),
#("kind", json.string("type-constructor")),
#("documentation", json.nullable(constructor.documentation, json.string)),
#("name", json.string(constructor.name)),
#("parameters", json.preprocessed_array(parameters)),
Expand All @@ -65,9 +65,9 @@ fn parameters_to_json(ctx: Context, parameter: Parameter) {
use gen <- result.map(type_to_json(ctx, parameter.type_))
use type_ <- pair.map_first(gen)
json.object([
#("type", json.string("parameter")),
#("kind", json.string("parameter")),
#("label", json.nullable(parameter.label, json.string)),
#("params_type", type_),
#("type", type_),
])
}

Expand All @@ -78,7 +78,7 @@ fn type_to_json(ctx: Context, type_: Type) {
use gen <- result.map(reduce_components(elements, mapper))
use elements <- pair.map_first(gen)
json.object([
#("type", json.string("tuple")),
#("kind", json.string("tuple")),
#("elements", json.preprocessed_array(elements)),
])
}
Expand All @@ -88,15 +88,15 @@ fn type_to_json(ctx: Context, type_: Type) {
use gen <- result.map(type_to_json(ctx, return))
let new_params = set.union(of: params, and: gen.1)
json.object([
#("type", json.string("fn")),
#("kind", json.string("fn")),
#("params", json.preprocessed_array(elements)),
#("return", gen.0),
])
|> pair.new(new_params)
}
package_interface.Variable(id) -> {
let json =
json.object([#("type", json.string("variable")), #("id", json.int(id))])
json.object([#("kind", json.string("variable")), #("id", json.int(id))])
Ok(#(json, set.new()))
}
package_interface.Named(name, package, module, parameters) -> {
Expand All @@ -109,7 +109,7 @@ fn type_to_json(ctx: Context, type_: Type) {
option.Some(ref) -> set.insert(gen.1, ref.1)
}
json.object([
#("type", json.string("named")),
#("kind", json.string("named")),
#("ref", json.nullable(option.map(ref, fn(r) { r.0 }), json.string)),
#("name", json.string(name)),
#("package", json.string(package)),
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([
#("nature", json.string("type-alias")),
#("kind", 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([
#("nature", json.string("constant")),
#("kind", 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([
#("nature", json.string("function")),
#("kind", json.string("function")),
#("name", json.string(function_name)),
#("documentation", json.nullable(function.documentation, json.string)),
#("deprecation", json.nullable(function.documentation, json.string)),
Expand Down
77 changes: 57 additions & 20 deletions apps/backend/src/backend/postgres/queries.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import gleam/result
import gleam/string
import helpers

pub type SignatureNature {
pub type SignatureKind {
TypeAlias
TypeDefinition
Constant
Expand Down Expand Up @@ -195,18 +195,27 @@ pub fn upsert_release(
let gleam_toml = pgo.nullable(pgo.text, gleam_toml)
let args = [package_id, version, url, package_interface, gleam_toml]
"INSERT INTO package_release (
package_id,
version,
url,
package_interface,
gleam_toml
) VALUES ($1, $2, $3, $4, $5)
package_id,
version,
url,
package_interface,
gleam_toml
) VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (package_id, version) DO UPDATE
SET
url = $3,
package_interface = $4,
gleam_toml = $5"
|> pgo.execute(db, args, dynamic.dynamic)
gleam_toml = $5
RETURNING id, package_interface, gleam_toml"
|> pgo.execute(
db,
args,
dynamic.tuple3(
dynamic.int,
dynamic.optional(dynamic.string),
dynamic.optional(dynamic.string),
),
)
|> result.map_error(error.DatabaseError)
}

Expand All @@ -218,13 +227,14 @@ pub fn lookup_release(
let package_id = pgo.int(package_id)
let version = pgo.text(release.version)
let args = [package_id, version]
"SELECT package_interface, gleam_toml
"SELECT id, package_interface, gleam_toml
FROM package_release
WHERE package_id = $1 AND version = $2"
|> pgo.execute(
db,
args,
dynamic.tuple2(
dynamic.tuple3(
dynamic.int,
dynamic.optional(dynamic.string),
dynamic.optional(dynamic.string),
),
Expand All @@ -250,6 +260,33 @@ pub fn add_package_gleam_constraint(
|> result.map_error(error.DatabaseError)
}

pub fn add_package_gleam_retirement(
db: pgo.Connection,
retirement: hexpm.ReleaseRetirement,
release_id: Int,
) {
let retirement =
json.object([
#("message", json.nullable(retirement.message, json.string)),
#("reason", {
json.string(case retirement.reason {
hexpm.OtherReason -> "other-reason"
hexpm.Invalid -> "invalid"
hexpm.Security -> "security"
hexpm.Deprecated -> "deprecated"
hexpm.Renamed -> "renamed"
})
}),
])
|> json.to_string()
|> pgo.text()
let release_id = pgo.int(release_id)
"UPDATE package_release SET retirement = $1 WHERE id = $2"
|> pgo.execute(db, [retirement, release_id], dynamic.dynamic)
|> result.replace(Nil)
|> result.map_error(error.DatabaseError)
}

pub fn get_package_release_ids(
db: pgo.Connection,
package: package_interface.Package,
Expand Down Expand Up @@ -311,7 +348,7 @@ fn implementations_pgo(implementations: package_interface.Implementations) {

pub fn upsert_package_type_fun_signature(
db db: pgo.Connection,
nature nature: SignatureNature,
kind kind: SignatureKind,
name name: String,
documentation documentation: Option(String),
metadata metadata: json.Json,
Expand All @@ -322,7 +359,7 @@ pub fn upsert_package_type_fun_signature(
deprecation deprecation: Option(package_interface.Deprecation),
implementations implementations: Option(package_interface.Implementations),
) {
let nature = case nature {
let kind = case kind {
Function -> "function"
TypeAlias -> "type_alias"
TypeDefinition -> "type_definition"
Expand All @@ -333,7 +370,7 @@ pub fn upsert_package_type_fun_signature(
documentation,
signature_,
json_signature,
nature,
kind,
parameters,
metadata,
package_module_id,
Expand All @@ -345,7 +382,7 @@ pub fn upsert_package_type_fun_signature(
documentation = $2,
signature_ = $3,
json_signature = $4,
nature = $5,
kind = $5,
parameters = $6,
metadata = $7,
deprecation = $9,
Expand All @@ -362,7 +399,7 @@ pub fn upsert_package_type_fun_signature(
json_signature
|> json.to_string()
|> pgo.text(),
pgo.text(nature),
pgo.text(kind),
dynamic.unsafe_coerce(dynamic.from(parameters)),
metadata
|> json.to_string()
Expand All @@ -384,12 +421,12 @@ pub fn upsert_package_type_fun_signature(

pub fn name_search(db: pgo.Connection, query: String) {
let query = pgo.text(query)
"SELECT DISTINCT ON (type_name, nature, module_name) *
"SELECT DISTINCT ON (type_name, kind, module_name) *
FROM (
SELECT
s.name type_name,
s.documentation,
s.nature,
s.kind,
s.metadata,
s.json_signature,
m.name module_name,
Expand Down Expand Up @@ -418,7 +455,7 @@ fn decode_type_search(dyn) {
json.object([
#("name", json.string(a)),
#("documentation", json.string(b)),
#("nature", json.string(c)),
#("kind", json.string(c)),
#("metadata", dynamic.unsafe_coerce(d)),
#("json_signature", dynamic.unsafe_coerce(e)),
#("module_name", json.string(f)),
Expand All @@ -442,7 +479,7 @@ pub fn search(db: pgo.Connection, q: String) {
"SELECT
s.name,
s.documentation,
s.nature,
s.kind,
s.metadata,
s.json_signature,
m.name,
Expand Down
Loading

0 comments on commit 58a8d4c

Please sign in to comment.