Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NoSuchPropsDBEntry #14

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 50 additions & 6 deletions src/props_db.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,43 @@
end


"""
struct LegendDataManagement.NoSuchPropsDBEntry

Indicates that a given property (path) of a
`LegendDataManagementPropsDB`[@ref] does not exist.

Supports
`PropDicts.writeprops(missing_props::NoSuchPropsDBEntry, props::PropDicts.PropDict)`
to create the missing directories and file for the property path.
"""
struct NoSuchPropsDBEntry
_base_path::String

Check warning on line 118 in src/props_db.jl

View check run for this annotation

Codecov / codecov/patch

src/props_db.jl#L118

Added line #L118 was not covered by tests
_rel_path::Vector{String}
end

_base_path(@nospecialize(pd::NoSuchPropsDBEntry)) = getfield(pd, :_base_path)
_rel_path(@nospecialize(pd::NoSuchPropsDBEntry)) = getfield(pd, :_rel_path)

Check warning on line 123 in src/props_db.jl

View check run for this annotation

Codecov / codecov/patch

src/props_db.jl#L122-L123

Added lines #L122 - L123 were not covered by tests

function _get_md_property(missing_props::NoSuchPropsDBEntry, s::Symbol)
NoSuchPropsDBEntry(_base_path(missing_props), push!(copy(_rel_path(missing_props)), string(s)))

Check warning on line 126 in src/props_db.jl

View check run for this annotation

Codecov / codecov/patch

src/props_db.jl#L125-L126

Added lines #L125 - L126 were not covered by tests
end

function PropDicts.writeprops(@nospecialize(missing_props::NoSuchPropsDBEntry), @nospecialize(props::PropDict))
rp = _rel_path(missing_props)
dir = joinpath(_base_path(missing_props), rp[begin:end-1]...)
maybe_dirpath = joinpath(dir, rp[end])
if isdir(maybe_dirpath)
throw(ErrorException("Cannot write properties to existing directory \"$maybe_dirpath\", target must be a file"))

Check warning on line 134 in src/props_db.jl

View check run for this annotation

Codecov / codecov/patch

src/props_db.jl#L129-L134

Added lines #L129 - L134 were not covered by tests
end
file = "$(rp[end]).json"
mkpath(dir)
writeprops(joinpath(dir, file), props)
nothing

Check warning on line 139 in src/props_db.jl

View check run for this annotation

Codecov / codecov/patch

src/props_db.jl#L136-L139

Added lines #L136 - L139 were not covered by tests
end



"""
LegendDataManagement.AnyProps = Union{LegendDataManagement.PropsDB,PropDicts.PropDict}

Expand Down Expand Up @@ -207,24 +244,27 @@
(@nospecialize(pd::PropsDB{Nothing}))(filekey::FileKey) = pd(ValiditySelection(filekey))


function Base.getindex(@nospecialize(pd::PropsDB), a, b, cs...)
const MaybePropsDB = Union{PropsDB,NoSuchPropsDBEntry}


function Base.getindex(@nospecialize(pd::MaybePropsDB), a, b, cs...)

Check warning on line 250 in src/props_db.jl

View check run for this annotation

Codecov / codecov/patch

src/props_db.jl#L250

Added line #L250 was not covered by tests
getindex(getindex(pd, a), b, cs...)
end

function Base.getindex(@nospecialize(pd::PropsDB), s::Symbol)
function Base.getindex(@nospecialize(pd::MaybePropsDB), s::Symbol)
_get_md_property(pd, s)
end

function Base.getindex(@nospecialize(pd::PropsDB), s::DataSelector)
function Base.getindex(@nospecialize(pd::MaybePropsDB), s::DataSelector)

Check warning on line 258 in src/props_db.jl

View check run for this annotation

Codecov / codecov/patch

src/props_db.jl#L258

Added line #L258 was not covered by tests
getindex(pd, Symbol(string(s)))
end

function Base.getindex(@nospecialize(pd::PropsDB), S::AbstractArray{<:Symbol})
function Base.getindex(@nospecialize(pd::MaybePropsDB), S::AbstractArray{<:Symbol})

Check warning on line 262 in src/props_db.jl

View check run for this annotation

Codecov / codecov/patch

src/props_db.jl#L262

Added line #L262 was not covered by tests
_get_md_property.(Ref(pd), S)
end


function Base.getproperty(@nospecialize(pd::PropsDB), s::Symbol)
function Base.getproperty(@nospecialize(pd::MaybePropsDB), s::Symbol)
# Include internal fields:
if s == :_base_path
_base_path(pd)
Expand Down Expand Up @@ -264,7 +304,11 @@
elseif isfile(json_filename)
readprops(json_filename)
else
throw(ArgumentError("Metadata entry doesn't have a property $s"))
if !_needs_vsel(pd) && (isnothing(_validity_sel(pd)) || isempty(_validity_sel(pd)))
NoSuchPropsDBEntry(_base_path(pd), push!(copy(_rel_path(pd)), string(s)))

Check warning on line 308 in src/props_db.jl

View check run for this annotation

Codecov / codecov/patch

src/props_db.jl#L307-L308

Added lines #L307 - L308 were not covered by tests
else
throw(ArgumentError("Metadata entry doesn't have a property $s"))

Check warning on line 310 in src/props_db.jl

View check run for this annotation

Codecov / codecov/patch

src/props_db.jl#L310

Added line #L310 was not covered by tests
end
end
end

Expand Down
Loading