diff --git a/Project.toml b/Project.toml index 3616177d..2ea2e4f1 100644 --- a/Project.toml +++ b/Project.toml @@ -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" @@ -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" diff --git a/src/VegaLite.jl b/src/VegaLite.jl index f46d200f..5f93d2bd 100644 --- a/src/VegaLite.jl +++ b/src/VegaLite.jl @@ -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 diff --git a/src/spec_utils.jl b/src/spec_utils.jl index 4371866c..a06523d0 100644 --- a/src/spec_utils.jl +++ b/src/spec_utils.jl @@ -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 + +_maybeparams(value) = value +_maybeparams(value::ObjectLike) = getparams(value) diff --git a/test/test_properties.jl b/test/test_properties.jl index b86a6e5b..2e22cd17 100644 --- a/test/test_properties.jl +++ b/test/test_properties.jl @@ -1,5 +1,6 @@ using Test using VegaLite +using Setfield @testset "properties" begin @@ -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