Skip to content

Commit ac30135

Browse files
committed
Allow interpolation of properties
1 parent bff69d5 commit ac30135

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/sugar.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,17 @@ function parse_obj_lenses(ex)
123123
lens = :($IndexLens($index))
124124
end
125125
elseif @capture(ex, front_.property_)
126-
property isa Union{Symbol,String} || throw(ArgumentError(
127-
string("Error while parsing :($ex). Second argument to `getproperty` can only be",
128-
"a `Symbol` or `String` literal, received `$property` instead.")
129-
))
130126
obj, frontlens = parse_obj_lenses(front)
131-
lens = :($PropertyLens{$(QuoteNode(property))}())
127+
if property isa Union{Symbol,String}
128+
lens = :($PropertyLens{$(QuoteNode(property))}())
129+
elseif is_interpolation(property)
130+
lens = :($PropertyLens{$(esc(property.args[1]))}())
131+
else
132+
throw(ArgumentError(
133+
string("Error while parsing :($ex). Second argument to `getproperty` can only be",
134+
"a `Symbol` or `String` literal, received `$property` instead.")
135+
))
136+
end
132137
elseif @capture(ex, f_(front_))
133138
obj, frontlens = parse_obj_lenses(front)
134139
lens = :($FunctionLens($(esc(f))))

test/test_core.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,9 @@ end
509509
@test @lens($lbc)== lbc
510510
@test @lens(_.a $lbc) == @lens(_.a) lbc
511511
@test @lens(_.a $lbc _[1] $lbc) == @lens(_.a) lbc @lens(_[1]) lbc
512+
513+
name = :a
514+
@test @lens(_.$name) == @lens(_.a)
512515
end
513516

514517
end

0 commit comments

Comments
 (0)