From 47eacd3a4b4b0d6210dd50494fa732879f92d7b6 Mon Sep 17 00:00:00 2001 From: Nick Robinson Date: Wed, 2 Jun 2021 16:40:52 +0100 Subject: [PATCH 1/5] Define `keys/haskey/get/get!` for JLSOFile --- src/JLSOFile.jl | 15 +++++++++++++++ test/JLSOFile.jl | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/JLSOFile.jl b/src/JLSOFile.jl index 1bcba1d..cefa3f7 100644 --- a/src/JLSOFile.jl +++ b/src/JLSOFile.jl @@ -117,3 +117,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 diff --git a/test/JLSOFile.jl b/test/JLSOFile.jl index f5bff60..7fb132e 100644 --- a/test/JLSOFile.jl +++ b/test/JLSOFile.jl @@ -55,3 +55,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(jlso1)) == [: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 From 2da791718551e2b5ba51c420da866e85a225fb7d Mon Sep 17 00:00:00 2001 From: Nick Robinson Date: Wed, 2 Jun 2021 16:41:21 +0100 Subject: [PATCH 2/5] Fix no-arg constructor for JLSOFile - Before: gave an error - Now: returns a `JLSOFile` with no objects --- src/JLSOFile.jl | 3 ++- test/JLSOFile.jl | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/JLSOFile.jl b/src/JLSOFile.jl index cefa3f7..fcd3b9e 100644 --- a/src/JLSOFile.jl +++ b/src/JLSOFile.jl @@ -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, diff --git a/test/JLSOFile.jl b/test/JLSOFile.jl index 7fb132e..7c279fb 100644 --- a/test/JLSOFile.jl +++ b/test/JLSOFile.jl @@ -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 From 009904293b038d1b7d69e40cfc25741b566899f3 Mon Sep 17 00:00:00 2001 From: Nick Robinson Date: Wed, 2 Jun 2021 16:42:42 +0100 Subject: [PATCH 3/5] Allow `save` to take a JLSOFile --- src/file_io.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/file_io.jl b/src/file_io.jl index 56b18d5..823624d 100644 --- a/src/file_io.jl +++ b/src/file_io.jl @@ -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") From 0a0804788cd57fb9577d12d459db83da0dd0cb28 Mon Sep 17 00:00:00 2001 From: Nick Robinson Date: Wed, 2 Jun 2021 17:21:46 +0100 Subject: [PATCH 4/5] fixup! Allow `save` to take a JLSOFile --- test/JLSOFile.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/JLSOFile.jl b/test/JLSOFile.jl index 7c279fb..cb659a6 100644 --- a/test/JLSOFile.jl +++ b/test/JLSOFile.jl @@ -65,7 +65,7 @@ end @testset "keys/haskey" begin jlso = JLSOFile(:string => datas[:String]) - @test collect(keys(jlso1)) == [:string] + @test collect(keys(jlso)) == [:string] @test haskey(jlso, :string) @test !haskey(jlso, :other) end From 7c75b90ad5bfa70020d5568f144d160aec011c1b Mon Sep 17 00:00:00 2001 From: Nick Robinson Date: Wed, 2 Jun 2021 18:56:13 +0100 Subject: [PATCH 5/5] Bump version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 268274b..b8a5449 100644 --- a/Project.toml +++ b/Project.toml @@ -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"