Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
NodeJS = "2bd173c7-0d6d-553b-b6af-13a54713934c"
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
TableTraits = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c"
URIParser = "30578b45-9adc-5946-b283-645ec420af67"

Expand All @@ -27,6 +28,7 @@ IteratorInterfaceExtensions = "^0.1.1, ^1"
JSON = "≥ 0.19.0"
MacroTools = "≥ 0.4.4"
NodeJS = "≥ 0.5.1"
Setfield = "≥ 0.4.0"
TableTraits = "^0.3.1, ^0.4, ^1"
URIParser = "≥ 0.4.0"
julia = "1"
Expand Down
1 change: 1 addition & 0 deletions src/VegaLite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ using FilePaths
using REPL, Dates
using Random
import JSONSchema
using Setfield: Setfield, PropertyLens, @lens, @set

# This import can eventually be removed, it currently just makes sure
# that the iterable tables integration for DataFrames and friends
Expand Down
12 changes: 12 additions & 0 deletions src/spec_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,15 @@ function Base.getproperty(spec::ObjectLike, name::Symbol)
return value
end
end

Setfield.set(spec::ObjectLike, ::typeof(@lens getparams(_)), params) =
typeof(spec)(params)

function Setfield.set(spec::ObjectLike, ::PropertyLens{name}, value) where name
params = copy(getparams(spec))
params[keytype(params)(name)] = _maybeparams(value)
@set getparams(spec) = params
end
Comment on lines +39 to +43
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This could be written as @set getparams(spec)[keytype(params)(name)] = value if Base had setindex for Dict...: JuliaLang/julia#33495


_maybeparams(value) = value
_maybeparams(value::ObjectLike) = getparams(value)
4 changes: 4 additions & 0 deletions test/test_properties.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Test
using VegaLite
using Setfield

@testset "properties" begin

Expand All @@ -14,4 +15,7 @@ vlp = getvlplot()
@test vlp.encoding.x.field isa String
@test_deprecated vlp.params["mark"] isa String

@test (@set vlp.mark = :point).mark == :point
@test vlp.mark == "bar" # not mutated

end