Skip to content

Commit 09d1e1f

Browse files
committed
Store owners
1 parent 377ce5a commit 09d1e1f

File tree

2 files changed

+88
-10
lines changed

2 files changed

+88
-10
lines changed

src/packages/storage.gleam

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,36 @@ pub type Package {
8282
downloads_day: Int,
8383
repository_url: Option(String),
8484
latest_version: String,
85+
owners: List(String),
8586
)
8687
}
8788

8889
fn package_to_json(package: Package) -> Json {
90+
let Package(
91+
name:,
92+
description:,
93+
inserted_in_hex_at:,
94+
updated_in_hex_at:,
95+
downloads_all:,
96+
downloads_recent:,
97+
downloads_week:,
98+
downloads_day:,
99+
repository_url:,
100+
latest_version:,
101+
owners:,
102+
) = package
89103
json.object([
90-
#("name", json.string(package.name)),
91-
#("description", json.string(package.description)),
92-
#("inserted_in_hex_at", json_timestamp(package.inserted_in_hex_at)),
93-
#("updated_in_hex_at", json_timestamp(package.updated_in_hex_at)),
94-
#("latest_version", json.string(package.latest_version)),
95-
#("downloads_all", json.int(package.downloads_all)),
96-
#("downloads_recent", json.int(package.downloads_recent)),
97-
#("downloads_week", json.int(package.downloads_week)),
98-
#("downloads_day", json.int(package.downloads_day)),
99-
#("repository_url", json.nullable(package.repository_url, json.string)),
104+
#("name", json.string(name)),
105+
#("description", json.string(description)),
106+
#("inserted_in_hex_at", json_timestamp(inserted_in_hex_at)),
107+
#("updated_in_hex_at", json_timestamp(updated_in_hex_at)),
108+
#("latest_version", json.string(latest_version)),
109+
#("downloads_all", json.int(downloads_all)),
110+
#("downloads_recent", json.int(downloads_recent)),
111+
#("downloads_week", json.int(downloads_week)),
112+
#("downloads_day", json.int(downloads_day)),
113+
#("repository_url", json.nullable(repository_url, json.string)),
114+
#("owners", json.array(owners, json.string)),
100115
])
101116
}
102117

@@ -114,6 +129,7 @@ fn package_decoder() -> Decoder(Package) {
114129
decode.optional(decode.string),
115130
)
116131
use latest_version <- decode.field("latest_version", decode.string)
132+
use owners <- decode.optional_field("owners", [], decode.list(decode.string))
117133
decode.success(Package(
118134
name:,
119135
description:,
@@ -125,6 +141,7 @@ fn package_decoder() -> Decoder(Package) {
125141
downloads_day:,
126142
repository_url:,
127143
latest_version:,
144+
owners:,
128145
))
129146
}
130147

@@ -225,6 +242,10 @@ fn hex_package_to_storage_package(
225242
}
226243
let repository_url =
227244
dict.get(package.meta.links, "Repository") |> option.from_result
245+
let owners =
246+
package.owners
247+
|> option.unwrap([])
248+
|> list.map(fn(owner) { owner.username })
228249

229250
Package(
230251
name: package.name,
@@ -237,6 +258,7 @@ fn hex_package_to_storage_package(
237258
downloads_day: downloads_count("day"),
238259
repository_url:,
239260
latest_version:,
261+
owners:,
240262
)
241263
}
242264

test/packages/storage_test.gleam

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,62 @@ pub fn insert_package_test() {
6767
downloads_week: 0,
6868
latest_version: "1.0.0",
6969
repository_url: Some("https://github.com/gleam-lang/stdlib"),
70+
owners: [],
71+
)
72+
}
73+
74+
pub fn insert_package_with_owners_test() {
75+
use db <- tests.with_database
76+
77+
let assert Ok(_) =
78+
storage.upsert_package_from_hex(
79+
db,
80+
hexpm.Package(
81+
downloads: dict.from_list([#("all", 5), #("recent", 2)]),
82+
docs_html_url: Some("https://hexdocs.pm/gleam_stdlib/"),
83+
html_url: Some("https://hex.pm/packages/gleam_stdlib"),
84+
meta: hexpm.PackageMeta(
85+
description: Some("Standard library for Gleam"),
86+
licenses: ["Apache-2.0"],
87+
links: dict.from_list([
88+
#("Website", "https://gleam.run/"),
89+
#("Repository", "https://github.com/gleam-lang/stdlib"),
90+
]),
91+
),
92+
name: "gleam_stdlib",
93+
owners: Some([
94+
hexpm.PackageOwner(
95+
username: "user1",
96+
email: Some("[email protected]"),
97+
url: "https://hex.pm/api/users/user1",
98+
),
99+
hexpm.PackageOwner(
100+
username: "user2",
101+
email: None,
102+
url: "https://hex.pm/api/users/user2",
103+
),
104+
]),
105+
releases: [],
106+
inserted_at: timestamp.from_unix_seconds(100),
107+
updated_at: timestamp.from_unix_seconds(2000),
108+
),
109+
"1.0.0",
110+
)
111+
112+
let assert Ok(package) = storage.get_package(db, "gleam_stdlib")
113+
assert package
114+
== Package(
115+
description: "Standard library for Gleam",
116+
name: "gleam_stdlib",
117+
inserted_in_hex_at: timestamp.from_unix_seconds(100),
118+
updated_in_hex_at: timestamp.from_unix_seconds(2000),
119+
downloads_all: 5,
120+
downloads_recent: 2,
121+
downloads_day: 0,
122+
downloads_week: 0,
123+
latest_version: "1.0.0",
124+
repository_url: Some("https://github.com/gleam-lang/stdlib"),
125+
owners: ["user1", "user2"],
70126
)
71127
}
72128

0 commit comments

Comments
 (0)