-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproperty.jl
313 lines (250 loc) · 8.4 KB
/
property.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
_property(ex::Symbol) = PropertyGenerator(ex)
get_propname(ex::Symbol) = ex
function get_propname(ex::Expr)
if ex.head === :(::)
return get_propname(ex.args[1]))
elseif ex.head === :call
return get_propname(ex.args[1])
end
end
get_propargs(ex::Symbol) = esc(:self), esc(:val)
function get_propargs(ex::Expr)
if ex.head === :(::)
return get_propname(ex.args[1]))
elseif ex.head === :call
if length(ex.args) == 1
return esc(:self), esc(:val)
elseif length(ex.args) == 2
return esc(x.args[2]), esc(:val)
else
return esc(x.args[2]), esc(x.args[3])
end
end
end
is_getter_call(x::Expr) = x.head === :(->)
is_getter_call(x) = false
is_setter_call(x::Expr) = x.head === :(-->)
is_setter_call(x) = false
function get_proptype(ex::Expr)
if ex.head === :(::)
return esc(ex.args[2])
else
return nothing
end
end
function get_getproperty(getname, pname, self, blk::Expr)
get_expr = callexpr(dotexpr(:FieldProperties, :propconvert),
getname,
self,
callexpr(esc(:getproperty), self, QuoteNode(pname)))
for line_i in blk.args
if is_getter_call(line_i)
get_expr = fxnbody(line_i)
end
end
return fxnexpr(callexpr(getname, self), get_expr)
end
function get_setproperty(setname, getname, pname, self, val, blk::Expr)
set_expr = callexpr(esc(:setproperty!),
self,
QuoteNode(pname),
callexpr(dotexpr(:FieldProperties, :propconvert), getname, self, val))
for line_i in blk.args
if is_setter_call(line_i)
set_expr = fxnbody(line_i)
end
end
return fxnexpr(callexpr(setname, self), set_expr)
end
function _property(ex, blk)
pname = get_propname(ex)
getname = esc(pname)
setname = esc(Symbol(pname, :!))
ptype = get_proptype(ex)
self, val = get_propargs(pname, self, ex)
setexpr = get_setproperty(setname, getname, pname, self, val, blk)
getexpr = get_setproperty(getname, pname, self, blk)
for line_i in blk.args
if is_macro_expr(line_i)
push!(out.args, macro_to_call(pname, line_i))
end
end
push!(out.args, :(const $getname = $struct_name{$(esc(:getproperty))}()))
push!(out.args, :(const $setname = $struct_name{$(esc(:setproperty!))}()))
return :(struct $(struct_name){$(esc(:T))} <: $(dotexpr(:FieldProperties, :AbstractProperty)){$(QuoteNode(pname)),$(esc(:T))} end), out
end
_property(ex) = _property(ex, Expr(:block))
"""
@property
```jldoctest
@macroexpand @property prop1
@macroexpand @property prop2::Int
@macroexpand @property prop2::(x -> eltype(x)) x =>
```
"""
macro property(expr)
expr = macroexpand(__module__, expr)
getexpr, setexpr, typeexpr = _property(expr)
quote
Base.@__doc__($(getexpr))
$setexpr
$typeexpr
end
end
macro property(expr, blk)
expr = macroexpand(__module__, expr)
getexpr, setexpr, typeexpr = _property(expr, blk)
quote
Base.@__doc__($(getexpr))
$setexpr
$typeexpr
end
end
#=
# get property defaults
if x isa Expr && x.head === :(=)
if x.args[2] isa Expr && x.args[2].head == :->
default_arg = esc(x.args[2].args[1])
default = esc(x.args[2].args[2])
x2 = x.args[1]
else # TODO don't know what to do here
default_arg = default = nothing
x2 = x
end
else
default_arg = default = nothing
x2 = x
end
# get property type enforcement
if x2 isa Expr && x2.head === :(::)
if x2.args[2] isa Expr && x2.args[2].head == :->
ptype_arg = esc(x2.args[2].args[1])
ptype = esc(x2.args[2].args[2])
getname = x2.args[1]
else
ptype_arg = esc(:x)
ptype = esc(x2.args[2])
getname = x2.args[1]
end
else
ptype = nothing
ptype_arg = esc(:x)
getname = x2
end
setname = esc(Symbol(getname, :!))
pname = QuoteNode(getname)
getname = esc(getname)
blk = Expr(:block)
push!(blk.args, Expr(:function,
callexpr(setname, esc(:x), esc(:val)),
callexpr(esc(:setproperty!),
esc(:x),
pname,
callexpr(dotexpr(:FieldProperties, :propconvert),
getname,
esc(:x),
esc(:val)))))
if isnothing(default)
getfxn = fxnexpr(callexpr(getname, esc(:x)),
callexpr(dotexpr(:FieldProperties, :propconvert),
getname,
esc(:x),
callexpr(esc(:getproperty),
esc(:x),
pname)))
else
getfxn = fxnexpr(callexpr(getname, default_arg), default)
end
if !isnothing(ptype)
push!(blk.args, fxnexpr(callexpr(dotexpr(:FieldProperties, :proptype),
Expr(:(::), Expr(:call, esc(:typeof), getname)),
ptype_arg),
ptype))
end
end
=#
"""
AbstractMetadata
"""
abstract type AbstractMetadata{D<:AbstractDict{Symbol,Any}} <: AbstractDict{Symbol,Any} end
Base.empty!(m::AbstractMetadata) = empty!(dictextension(m))
function Base.get(m::AbstractMetadata, p, default)
if hasproperty(m, p)
return getproperty(m, p)
else
return default
end
end
function Base.get!(m::AbstractMetadata, k, default)
if hasproperty(m, p)
return getproperty(m, p)
else
setproperty!(m, p, default)
return default
end
end
# TODO
#Base.in(k, m::AbstractMetadata) = in(k, propname(m))
#Base.pop!(m::AbstractMetadata, k) = pop!(dictextension(m), k)
#Base.pop!(m::AbstractMetadata, k, default) = pop!(dictextension(m), k, default)
Base.isempty(m::AbstractMetadata) = isempty(dictextension(m))
Base.delete!(m::AbstractMetadata, k) = delete!(dictextension(m), k)
@inline Base.getindex(x::AbstractMetadata, s::Symbol) = getindex(dictextension(x), s)
@inline function Base.setindex!(x::AbstractMetadata, val, s::Symbol)
return setindex!(dictextension(x), val, s)
end
Base.length(m::AbstractMetadata) = length(propertynames(m))
Base.getkey(m::AbstractMetadata, k, default) = getkey(dictextension(m), k, default)
Base.keys(m::AbstractMetadata) = propertynames(m)
suppress(m::AbstractMetadata) = get(m, :suppress, ())
Base.show(io::IO, m::AbstractMetadata) = showdictlines(io, m, suppress(m))
Base.show(io::IO, ::MIME"text/plain", m::AbstractMetadata) = showdictlines(io, m, suppress(m))
function showdictlines(io::IO, m, suppress)
print(io, summary(m))
for (k, v) in m
if !in(k, suppress)
print(io, "\n ", k, ": ")
print(IOContext(io, :compact => true), v)
else
print(io, "\n ", k, ": <suppressed>")
end
end
end
#Base.iterate(m::AbstractMetadata, state=1) = _iterate_properties(m, state)
"""
NoopMetadata
Empty dictionary that indicates there is no metadata.
"""
struct NoopMetadata <: AbstractMetadata{Dict{Symbol,Any}} end
Base.isempty(::NoopMetadata) = true
Base.get(::NoopMetadata, k, default) = default
Base.length(::NoopMetadata) = 0
Base.haskey(::NoopMetadata, k) = false
Base.in(k, ::NoopMetadata) = false
Base.propertynames(::NoopMetadata) = ()
Base.iterate(m::NoopMetadata) = nothing
Base.iterate(m::NoopMetadata, state) = nothing
function Base.setindex!(m::NoopMetadata, val, s::Symbol)
error("Cannot set property for NoopMetadata.")
end
"""
Metadata{D}
Subtype of `AbstractMetadata` that provides `getproperty` syntax for accessing
the values of a dictionary.
"""
struct Metadata{D} <: AbstractMetadata{D}
dictextension::D
end
function Metadata(; kwargs...)
out = Metadata(Dict{Symbol,Any}())
for (k,v) in kwargs
out[k] = v
end
return out
end
@properties Metadata begin
Any(self) -> (dictextension)
Any!(self, val) -> (dictextension)
end
Base.iterate(m::Metadata) = iterate(getfield(m, :dictextension))
Base.iterate(m::Metadata, state) = iterate(getfield(m, :dictextension), state)