Skip to content

Commit

Permalink
Add some methods to make JLSOFile more Dict-like (#108)
Browse files Browse the repository at this point in the history
* Define `keys/haskey/get/get!` for JLSOFile

* Fix no-arg constructor for JLSOFile

- Before: gave an error
- Now: returns a `JLSOFile` with no objects

* Allow `save` to take a JLSOFile

* fixup! Allow `save` to take a JLSOFile

* Bump version
  • Loading branch information
nickrobinson251 authored Jun 2, 2021
1 parent dbf7be6 commit aa6c1f7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "JLSO"
uuid = "9da8a3cd-07a3-59c0-a743-3fdc52c30d11"
license = "MIT"
authors = ["Invenia Technical Computing Corperation"]
version = "2.5.0"
version = "2.6.0"

[deps]
BSON = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0"
Expand Down
18 changes: 17 additions & 1 deletion src/JLSOFile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ function JLSOFile(;
image=_image(),
kwargs...
)
data = isempty(kwargs) ? Dict{Symbol,Any}() : Dict(kwargs)
return JLSOFile(
Dict(kwargs);
data;
version=version,
julia=julia,
format=format,
Expand Down Expand Up @@ -117,3 +118,18 @@ function Base.:(==)(a::JLSOFile, b::JLSOFile)
end

Base.names(jlso::JLSOFile) = collect(keys(jlso.objects))

Base.keys(jlso::JLSOFile) = keys(jlso.objects)

Base.haskey(jlso::JLSOFile, key) = haskey(jlso.objects, key)

Base.get(jlso::JLSOFile, key, default) = haskey(jlso, key) ? jlso[key] : default

Base.get!(jlso::JLSOFile, key, default) = get!(() -> default, jlso, key)
function Base.get!(func, jlso::JLSOFile, key)
return if haskey(jlso, key)
jlso[key]
else
jlso[key] = func()
end
end
3 changes: 2 additions & 1 deletion src/file_io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ end
Creates a JLSOFile with the specified data and kwargs and writes it back to the io.
"""
save(io::IO, data; kwargs...) = write(io, JLSOFile(data; kwargs...))
save(io::IO, data::JLSOFile) = write(io, data)
save(io::IO, data; kwargs...) = save(io, JLSOFile(data; kwargs...))
save(io::IO, data::Pair...; kwargs...) = save(io, Dict(data...); kwargs...)
function save(path::Union{AbstractPath, AbstractString}, args...; kwargs...)
return open(io -> save(io, args...; kwargs...), path, "w")
Expand Down
31 changes: 31 additions & 0 deletions test/JLSOFile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
@test jlso[:b] == "hello"
@test haskey(jlso.manifest, "BSON")
end

@testset "no-arg constructor" begin
jlso = JLSOFile()
@test jlso isa JLSOFile
@test isempty(jlso.objects)
@test haskey(jlso.manifest, "BSON")
end
end

@testset "unknown format" begin
Expand Down Expand Up @@ -55,3 +62,27 @@ end
@test Pkg.TOML.parsefile(joinpath(d, "Manifest.toml")) == jlso.manifest
end
end

@testset "keys/haskey" begin
jlso = JLSOFile(:string => datas[:String])
@test collect(keys(jlso)) == [:string]
@test haskey(jlso, :string)
@test !haskey(jlso, :other)
end

@testset "get/get!" begin
v = datas[:String]
jlso = JLSOFile(:str => v)
@test get(jlso, :str, "fail") == v
@test get!(jlso, :str, "fail") == v

@test get(jlso, :other, v) == v
@test !haskey(jlso, :other)

@test get!(jlso, :other, v) == v
@test jlso[:other] == v

# key must be a Symbol
@test get(jlso, "str", 999) == 999
@test_throws MethodError get!(jlso, "str", 999)
end

2 comments on commit aa6c1f7

@nickrobinson251
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/38040

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.6.0 -m "<description of version>" aa6c1f793567ac262864053fd725aed20b41f5d7
git push origin v2.6.0

Please sign in to comment.