API

Types

Functions and macros

Documentation

PropDicts.MissingProperty — Type
struct MissingProperty

An instance MissingProperty(parent, key::Union{Symbol,Int}) represents the fact that key is missing in parent.

Instances of MissingProperty support setindex! and setproperty!, this will create the key in parent as a PropDict.

source
PropDicts.PropDict — Type
PropDict <: AbstractDict{Union{Symbol,Int},Any}

A dictionary with Symbol and Int keys that supports property-based access and deep merging.

Constructors:

PropDict(dict::AbstractDict)

PropDict(key1 => value1, key2 => value2, ...)

PropDict(key1 = value1, key2 = value2, ...)

Also see @propdict for construction from named-tuple syntax.

Keys are automatically converted to Symbols and Ints: string keys that represent an integer become Ints, other string keys become Symbols. The same conversion is applied when getting, setting or deleting entries. Values that are dicts are converted to PropDicts.

PropDicts support deep merging:

x = PropDict(:a => PropDict(:b => 7, :c => 5, :d => 2), :e => "foo")
y = PropDict(:a => PropDict(:c => 42, :d => nothing), :f => "bar")

z = merge(x, y)
@assert z == PropDict(
    :a => PropDict(:b=>7, :d => nothing, :c => 42),
    :e => "foo", :f => "bar"
)

PropDicts.trim_null!(z)
@assert z == PropDict(
    :a => PropDict(:b=>7, :c => 42),
    :e => "foo", :f => "bar"
)

Accessing non-existing properties will return instances of PropDicts.MissingProperty. When setting the value of missing properties, parent PropDicts are created automatically:

z.foo.bar isa PropDicts.MissingProperty
z.foo.bar = 42
z.foo.bar == 42

PropDicts can be read/written to/from JSON and YAML files using readprops and writeprops. Note that the Julia packages JSON resp. YAML need to be loaded first.

Note

Like with Base.Dict, mutating a PropDict is not thread safe.

source
PropDicts.@propdict — Macro
@propdict (key1 = value1, key2 = (key3 = value3,), ...)

Construct a PropDict from named-tuple syntax.

Nested named-tuple literals become nested PropDicts:

p = @propdict (a = (b = 7, c = 5), e = "foo")
p.a.b == 7

Like in named tuples, a nested single-entry literal needs a trailing comma ((b = 7,)) or semicolon form ((; b = 7)).

source
PropDicts.deepmerge! — Method
deepmerge!(d::AbstractDict, others::AbstractDict...)

Merge others into d recursively: entries whose values are dicts on both sides are deep-merged, all other entries are overwritten.

Dict values of others are copied. Other values, like arrays and immutable dicts like Base.ImmutableDict, are shared, including any dicts inside them.

source
PropDicts.deepmerge — Method
deepmerge(d::AbstractDict, others::AbstractDict...)

Non-mutating version of deepmerge!: returns a new dict, the inputs are left unchanged. As immutable dicts are shared, merging into one still changes the mutable dicts inside it.

source
PropDicts.readprops — Function
readprops(filename::AbstractString; subst_pathvar::Bool = true, subst_env::Bool = true, trim_null::Bool = true)
readprops(filenames::Vector{<:AbstractString}; ...)

Read a PropDict from a single or multiple files.

readprops supports JSON and YAML files. Note that the Julia packages JSON resp. YAML need to be loaded first.

If the top level of a file is a sequence instead of a mapping, an array is returned instead of a PropDict, with the dicts in it converted to PropDicts. Nested arrays are returned as read: dicts in them are not converted, and variable substitution and trim_null do not apply to them.

If multiple files are given, they are merged into a single PropDict using merge, all files must contain a mapping at the top level.

subst_pathvar controls whether $_ should be substituted with the directory path of the/each file within string values (but not field names).

subst_env controls whether $ENVVAR should be substituted with the value of the each environment variable ENVVAR within string values (but not field names).

trim_null controls whether JSON/YAML null values should be removed entirely.

source
PropDicts.trim_null! — Function
trim_null!(d::AbstractDict; recursive::Bool = true)
trim_null!(A::AbstractArray; recursive::Bool = true)

Remove entries with a value of nothing from d, resp. from the dicts in A.

Operates recursively on nested dicts if recursive == true.

source
PropDicts.writeprops — Function
writeprops(io::IO, p; format::Symbol = :JSON, multiline::Bool = true, indent::Int = -1)
writeprops(filename, p; format::Symbol = _format_from_filename(filename), multiline::Bool = true, indent::Int = -1)

Write a PropDict or an array of properties p to a file or an I/O stream.

writeprops supports JSON and YAML files. Note that the Julia packages JSON resp. YAML need to be loaded first.

multiline = false is not supported for YAML files.

Use indent = -1 for default indentation in multiline mode.

source