-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathNamedTupleUtilities.jl
More file actions
203 lines (191 loc) · 7.18 KB
/
NamedTupleUtilities.jl
File metadata and controls
203 lines (191 loc) · 7.18 KB
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
module NamedTupleUtilities
"""
select(a::NamedTuple, v::Val{n})
Select a field `n` from `a` if it is in `a`.
```jldoctest
julia> QueryOperators.NamedTupleUtilities.select((a=1,b=2,c=3),Val(:a))
(a = 1,)
```
"""
@generated function select(a::NamedTuple{an}, ::Val{bn}) where {an, bn}
names = ((i for i in an if i == bn)...,)
types = Tuple{(fieldtype(a, n) for n in names)...}
vals = Expr[:(getfield(a, $(QuoteNode(n)))) for n in names]
return :(NamedTuple{$names,$types}(($(vals...),)))
end
"""
remove(a::NamedTuple, v::Val{n})
Remove a field `n` from the `a` if it is in `a`.
```jldoctest
julia> QueryOperators.NamedTupleUtilities.remove((a=1,b=2,c=3),Val(:c))
(a = 1, b = 2)
```
"""
@generated function remove(a::NamedTuple{an}, ::Val{bn}) where {an, bn}
names = ((i for i in an if i != bn)...,)
types = Tuple{(fieldtype(a, n) for n in names)...}
vals = Expr[:(getfield(a, $(QuoteNode(n)))) for n in names]
return :(NamedTuple{$names,$types}(($(vals...),)))
end
"""
range(a::NamedTuple, b::Val{n}, c::Val{n})
Return a NamedTuple which retains the fields from `b` to `c` in `a`.
If `b` is not in `a`, then it will return the empty NamedTuple.
If `c` is not in `a`, then it will return everything starting with `b`.
```jldoctest
julia> QueryOperators.NamedTupleUtilities.range((a=1,b=2,c=3),Val(:a),Val(:b))
(a = 1, b = 2)
```
"""
@generated function range(a::NamedTuple{an}, ::Val{bn}, ::Val{cn}) where {an, bn, cn}
rangeStarted = false
names = Symbol[]
for n in an
if n == bn
rangeStarted = true
end
if rangeStarted
push!(names, n)
end
if n == cn
rangeStarted = false
break
end
end
types = Tuple{(fieldtype(a, n) for n in names)...}
vals = Expr[:(getfield(a, $(QuoteNode(n)))) for n in names]
return :( NamedTuple{$(names...,),$types}(($(vals...),)) )
end
"""
rename(a::NamedTuple, b::Val{n}, c::Val{n})
Return a NamedTuple derived from `a` in which the the field from `b` is renamed to `c`.
If `b` is not in `a`, then it will return the original NamedTuple.
If `c` is in `a`, then `ERROR: duplicate field name in NamedTuple: "c" is not unique` will occur.
```jldoctest
julia> QueryOperators.NamedTupleUtilities.rename((a = 1, b = 2, c = 3),Val(:a),Val(:d))
(d = 1, b = 2, c = 3)
julia> QueryOperators.NamedTupleUtilities.rename((a = 1, b = 2, c = 3),Val(:m),Val(:d))
(a = 1, b = 2, c = 3)
julia> QueryOperators.NamedTupleUtilities.rename((a = 1, b = 2, c = 3),Val(:a),Val(:c))
ERROR: duplicate field name in NamedTuple: "c" is not unique
```
"""
@generated function rename(a::NamedTuple{an}, ::Val{bn}, ::Val{cn}) where {an, bn, cn}
names = Symbol[]
typesArray = DataType[]
vals = Expr[]
for n in an
if n == bn
push!(names, cn)
else
push!(names, n)
end
push!(typesArray, fieldtype(a, n))
push!(vals, :(getfield(a, $(QuoteNode(n)))))
end
types = Tuple{typesArray...}
return :(NamedTuple{$(names...,),$types}(($(vals...),)))
end
"""
startswith(a::NamedTuple, b::Val{n})
Return a NamedTuple which retains the fields with names starting with `b` in `a`.
```jldoctest
julia> QueryOperators.NamedTupleUtilities.startswith((abc=1,bcd=2,cde=3),Val(:a))
(abc = 1,)
```
"""
@generated function startswith(a::NamedTuple{an}, ::Val{bn}) where {an, bn}
names = ((i for i in an if Base.startswith(String(i), String(bn)))...,)
types = Tuple{(fieldtype(a, n) for n in names)...}
vals = Expr[:(getfield(a, $(QuoteNode(n)))) for n in names]
return :(NamedTuple{$names,$types}(($(vals...),)))
end
"""
not_startswith(a::NamedTuple, b::Val{n})
Return a NamedTuple which retains the fields with names that do not start with `b` in `a`.
```jldoctest
julia> QueryOperators.NamedTupleUtilities.not_startswith((abc=1,bcd=2,cde=3),Val(:a))
(bcd = 2, cde = 3)
```
"""
@generated function not_startswith(a::NamedTuple{an}, ::Val{bn}) where {an, bn}
names = ((i for i in an if !Base.startswith(String(i), String(bn)))...,)
types = Tuple{(fieldtype(a, n) for n in names)...}
vals = Expr[:(getfield(a, $(QuoteNode(n)))) for n in names]
return :(NamedTuple{$names,$types}(($(vals...),)))
end
"""
endswith(a::NamedTuple, b::Val{n})
Return a NamedTuple which retains the fields with names that do not end with `b` in `a`.
```jldoctest
julia> QueryOperators.NamedTupleUtilities.not_endswith((abc=1,bcd=2,cde=3),Val(:d))
(abc = 1, cde = 3)
```
"""
@generated function endswith(a::NamedTuple{an}, ::Val{bn}) where {an, bn}
names = ((i for i in an if Base.endswith(String(i), String(bn)))...,)
types = Tuple{(fieldtype(a, n) for n in names)...}
vals = Expr[:(getfield(a, $(QuoteNode(n)))) for n in names]
return :(NamedTuple{$names,$types}(($(vals...),)))
end
"""
not_endswith(a::NamedTuple, b::Val{n})
Return a NamedTuple which retains the fields with names ending with `b` in `a`.
```jldoctest
julia> QueryOperators.NamedTupleUtilities.endswith((abc=1,bcd=2,cde=3),Val(:d))
(bcd = 2,)
```
"""
@generated function not_endswith(a::NamedTuple{an}, ::Val{bn}) where {an, bn}
names = ((i for i in an if !Base.endswith(String(i), String(bn)))...,)
types = Tuple{(fieldtype(a, n) for n in names)...}
vals = Expr[:(getfield(a, $(QuoteNode(n)))) for n in names]
return :(NamedTuple{$names,$types}(($(vals...),)))
end
"""
occursin(a::NamedTuple, b::Val{n})
Return a NamedTuple which retains the fields with names containing `b` as a substring.
```jldoctest
julia> QueryOperators.NamedTupleUtilities.occursin((abc=1,bcd=2,cde=3),Val(:d))
(bcd = 2, cde = 3)
```
"""
@generated function occursin(a::NamedTuple{an}, ::Val{bn}) where {an, bn}
names = ((i for i in an if Base.occursin(String(bn), String(i)))...,)
types = Tuple{(fieldtype(a, n) for n in names)...}
vals = Expr[:(getfield(a, $(QuoteNode(n)))) for n in names]
return :(NamedTuple{$names,$types}(($(vals...),)))
end
"""
not_occursin(a::NamedTuple, b::Val{n})
Return a NamedTuple which retains the fields without names containing `b` as a substring.
```jldoctest
julia> QueryOperators.NamedTupleUtilities.not_occursin((abc=1,bcd=2,cde=3),Val(:d))
(abc = 1,)
```
"""
@generated function not_occursin(a::NamedTuple{an}, ::Val{bn}) where {an, bn}
names = ((i for i in an if !Base.occursin(String(bn), String(i)))...,)
types = Tuple{(fieldtype(a, n) for n in names)...}
vals = Expr[:(getfield(a, $(QuoteNode(n)))) for n in names]
return :(NamedTuple{$names,$types}(($(vals...),)))
end
"""
oftype(a::NamedTuple, b::DataType)
Returns a NamedTuple which retains the fields whose elements have type `b`.
```jldoctest
julia> QueryOperators.NamedTupleUtilities.oftype((a = 4, b = 3., c = "He"), Val(Int64))
(a = 4,)
julia> QueryOperators.NamedTupleUtilities.oftype((a = 4, b = 3., c = "He"), Val(Number))
(a = 4, b = 3.0)
julia> QueryOperators.NamedTupleUtilities.oftype((a = 4, b = 3., c = "He"), Val(Float32))
NamedTuple()
```
"""
@generated function oftype(a::NamedTuple{an}, ::Val{b}) where {an, b}
names = ((i for i in an if fieldtype(a, i) <: b)...,)
types = Tuple{(fieldtype(a, n) for n in names)...}
vals = Expr[:(getfield(a, $(QuoteNode(n)))) for n in names]
return :(NamedTuple{$names,$types}(($(vals...),)))
end
end