Skip to content

Commit b2558f8

Browse files
authored
feat: make DatasetVersion repr valid Julia code (#84)
1 parent c9a711d commit b2558f8

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
88

99
* All the public API names are now correctly marked `public` in Julia 1.11 and above. ([#83])
1010

11+
### Changed
12+
13+
* The string `repr` of `DatasetVersion` (e.g. `dataset.versions`) is now valid Julia code. ([#84])
14+
1115
### Fixed
1216

1317
* The `JuliaHub.update_dataset` function now correctly accepts the `license=(:fulltext, ...)` argument. ([#74])
@@ -146,3 +150,4 @@ Initial package release.
146150
[#58]: https://github.com/JuliaComputing/JuliaHub.jl/issues/58
147151
[#74]: https://github.com/JuliaComputing/JuliaHub.jl/issues/74
148152
[#83]: https://github.com/JuliaComputing/JuliaHub.jl/issues/83
153+
[#84]: https://github.com/JuliaComputing/JuliaHub.jl/issues/84

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ docs-manifest:
1717
docs: docs/Manifest.toml
1818
${JULIA} --project=docs/ docs/make.jl
1919

20+
fix-doctests: docs/Manifest.toml
21+
${JULIA} --project=docs/ docs/make.jl --fix-doctests
22+
2023
changelog:
2124
${JULIA} --project=docs/ docs/changelog.jl
2225

docs/src/guides/datasets.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ When downloading, you can also specify the version you wish to download (with th
8787
```jldoctest example-dataset; filter = r"\"/.+/mydata\""
8888
julia> ds.versions
8989
2-element Vector{JuliaHub.DatasetVersion}:
90-
JuliaHub.DatasetVersion(dataset = ("username", "example-dataset"), version = 1)
91-
JuliaHub.DatasetVersion(dataset = ("username", "example-dataset"), version = 2)
90+
JuliaHub.dataset(("username", "example-dataset")).versions[1]
91+
JuliaHub.dataset(("username", "example-dataset")).versions[2]
9292
9393
julia> ds.versions[1]
9494
DatasetVersion: example-dataset @ v1

src/datasets.jl

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,25 @@ Objects have the following properties:
2525
- `.size :: Int`: size of the dataset version in bytes
2626
- `.timestamp :: ZonedDateTime`: dataset version timestamp
2727
28-
```
29-
julia> JuliaHub.datasets()
28+
```jldoctest
29+
julia> dataset = JuliaHub.dataset("example-dataset")
30+
Dataset: example-dataset (Blob)
31+
owner: username
32+
description: An example dataset
33+
versions: 2
34+
size: 388 bytes
35+
tags: tag1, tag2
36+
37+
julia> dataset.versions
38+
2-element Vector{JuliaHub.DatasetVersion}:
39+
JuliaHub.dataset(("username", "example-dataset")).versions[1]
40+
JuliaHub.dataset(("username", "example-dataset")).versions[2]
41+
42+
julia> dataset.versions[end]
43+
DatasetVersion: example-dataset @ v2
44+
owner: username
45+
timestamp: 2022-10-14T01:39:43.237-04:00
46+
size: 331 bytes
3047
```
3148
3249
See also: [`Dataset`](@ref), [`datasets`](@ref), [`dataset`](@ref).
@@ -46,28 +63,25 @@ struct DatasetVersion
4663
size = _get_json(json, "size", Int; msg)
4764
timestamp = _parse_tz(_get_json(json, "date", String; msg); msg)
4865
blobstore_path = _get_json(json, "blobstore_path", String; msg)
49-
new((owner, name), version, size, timestamp, blobstore_path)
66+
return new((owner, name), version, size, timestamp, blobstore_path)
5067
end
5168
end
5269

5370
function Base.show(io::IO, dsv::DatasetVersion)
5471
owner, name = dsv._dsref
55-
print(
56-
io,
57-
"JuliaHub.DatasetVersion(dataset = (\"",
58-
owner,
59-
"\", \"",
60-
name,
61-
"\"), version = $(dsv.id))",
62-
)
72+
dsref = string("(\"", owner, "\", \"", name, "\")")
73+
print(io, "JuliaHub.dataset($dsref).versions[$(dsv.id)]")
74+
return nothing
6375
end
76+
6477
function Base.show(io::IO, ::MIME"text/plain", dsv::DatasetVersion)
6578
owner, name = dsv._dsref
6679
printstyled(io, "DatasetVersion:"; bold=true)
6780
print(io, " ", name, " @ v", dsv.id)
6881
print(io, "\n owner: ", owner)
6982
print(io, "\n timestamp: ", dsv.timestamp)
7083
print(io, "\n size: ", dsv.size, " bytes")
84+
return nothing
7185
end
7286

7387
"""

test/datasets.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ end
5656
@test ds.versions[2].id == 2
5757
@test ds.versions[2].size == 331
5858

59+
# Test that .versions repr()-s to a valid array
60+
# and DatasetVersion reprs to a valid JuliaHub.dataset().versions[...]
61+
# call.
62+
let versions = eval(Meta.parse(string(ds.versions)))
63+
@test versions isa Vector{JuliaHub.DatasetVersion}
64+
@test length(versions) == 2
65+
@test versions == ds.versions
66+
end
67+
let expr = Meta.parse(string(ds.versions[1]))
68+
@test expr == :((JuliaHub.dataset(("username", "example-dataset"))).versions[1])
69+
version = eval(expr)
70+
@test version == ds.versions[1]
71+
end
72+
5973
ds_updated = JuliaHub.dataset("example-dataset")
6074
@test ds_updated isa JuliaHub.Dataset
6175
@test ds_updated.name == ds.name

0 commit comments

Comments
 (0)