@@ -10,6 +10,79 @@ Base.size(x::TestArray) = size(x.x)
10
10
Base. getindex (x:: TestArray , i) = x. x[i]
11
11
DataAPI. levels (x:: TestArray ) = reverse (DataAPI. levels (x. x))
12
12
13
+ # An example implementation of metadata
14
+ # For simplicity Int col indexing is not implemented
15
+ # and no checking if col is a column of a table is performed
16
+
17
+ struct TestMeta
18
+ table:: Dict{String, Any}
19
+ col:: Dict{Symbol, Dict{String, Any}}
20
+
21
+ TestMeta () = new (Dict {String, Any} (), Dict {Symbol, Dict{String, Any}} ())
22
+ end
23
+
24
+ function DataAPI. metadata (x:: TestMeta , key:: AbstractString ; style:: Bool = false )
25
+ return style ? x. table[key] : x. table[key][1 ]
26
+ end
27
+
28
+ DataAPI. metadatakeys (x:: TestMeta ) = keys (x. table)
29
+
30
+ function DataAPI. metadata! (x:: TestMeta , key:: AbstractString , value; style)
31
+ x. table[key] = (value, style)
32
+ return x
33
+ end
34
+
35
+ function DataAPI. metadata! (x:: TestMeta , key:: AbstractString , value; style)
36
+ x. table[key] = (value, style)
37
+ return x
38
+ end
39
+
40
+ DataAPI. deletemetadata! (x:: TestMeta , key:: AbstractString ) = delete! (x. table, key)
41
+ DataAPI. emptymetadata! (x:: TestMeta ) = empty! (x. table)
42
+
43
+ function DataAPI. colmetadata (x:: TestMeta , col:: Symbol , key:: AbstractString ; style:: Bool = false )
44
+ return style ? x. col[col][key] : x. col[col][key][1 ]
45
+ end
46
+
47
+ function DataAPI. colmetadatakeys (x:: TestMeta , col:: Symbol )
48
+ haskey (x. col, col) && return keys (x. col[col])
49
+ return ()
50
+ end
51
+
52
+ function DataAPI. colmetadatakeys (x:: TestMeta )
53
+ isempty (x. col) && return ()
54
+ return (col => keys (x. col[col]) for col in keys (x. col))
55
+ end
56
+
57
+ function DataAPI. colmetadata! (x:: TestMeta , col:: Symbol , key:: AbstractString , value; style)
58
+ if haskey (x. col, col)
59
+ x. col[col][key] = (value, style)
60
+ else
61
+ x. col[col] = Dict {Any, Any} (key => (value, style))
62
+ end
63
+ return x
64
+ end
65
+
66
+ function DataAPI. deletecolmetadata! (x:: TestMeta , col:: Symbol , key:: AbstractString )
67
+ if haskey (x. col, col)
68
+ delete! (x. col[col], key)
69
+ else
70
+ throw (ArgumentError (" column $col not found" ))
71
+ end
72
+ return x
73
+ end
74
+
75
+ function DataAPI. emptycolmetadata! (x:: TestMeta , col:: Symbol )
76
+ if haskey (x. col, col)
77
+ delete! (x. col, col)
78
+ else
79
+ throw (ArgumentError (" column $col not found" ))
80
+ end
81
+ return x
82
+ end
83
+
84
+ DataAPI. emptycolmetadata! (x:: TestMeta ) = empty! (x. col)
85
+
13
86
@testset " DataAPI" begin
14
87
15
88
@testset " defaultarray" begin
173
246
@test DataAPI. unwrap (missing ) === missing
174
247
end
175
248
249
+ @testset " metadata" begin
250
+ @test_throws ArgumentError DataAPI. metadata! (1 , " a" , 10 , style= :default )
251
+ @test_throws ArgumentError DataAPI. deletemetadata! (1 , " a" )
252
+ @test_throws ArgumentError DataAPI. emptymetadata! (1 )
253
+ @test_throws ArgumentError DataAPI. metadata (1 , " a" )
254
+ @test_throws ArgumentError DataAPI. metadata (1 , " a" , style= true )
255
+ @test DataAPI. metadatakeys (1 ) == ()
256
+
257
+ @test_throws ArgumentError DataAPI. colmetadata! (1 , :col , " a" , 10 , style= :default )
258
+ @test_throws ArgumentError DataAPI. deletecolmetadata! (1 , :col , " a" )
259
+ @test_throws ArgumentError DataAPI. emptycolmetadata! (1 , :col )
260
+ @test_throws ArgumentError DataAPI. deletecolmetadata! (1 , 1 , " a" )
261
+ @test_throws ArgumentError DataAPI. emptycolmetadata! (1 , 1 )
262
+ @test_throws ArgumentError DataAPI. emptycolmetadata! (1 )
263
+ @test_throws ArgumentError DataAPI. colmetadata (1 , :col , " a" )
264
+ @test_throws ArgumentError DataAPI. colmetadata (1 , :col , " a" , style= true )
265
+ @test_throws ArgumentError DataAPI. colmetadata! (1 , 1 , " a" , 10 , style= :default )
266
+ @test_throws ArgumentError DataAPI. colmetadata (1 , 1 , " a" )
267
+ @test_throws ArgumentError DataAPI. colmetadata (1 , 1 , " a" , style= true )
268
+ @test DataAPI. colmetadatakeys (1 , :col ) == ()
269
+ @test DataAPI. colmetadatakeys (1 , 1 ) == ()
270
+ @test DataAPI. colmetadatakeys (1 ) == ()
271
+
272
+ tm = TestMeta ()
273
+ @test isempty (DataAPI. metadatakeys (tm))
274
+ @test DataAPI. metadata! (tm, " a" , " 100" , style= :note ) == tm
275
+ @test collect (DataAPI. metadatakeys (tm)) == [" a" ]
276
+ @test_throws KeyError DataAPI. metadata (tm, " b" )
277
+ @test_throws KeyError DataAPI. metadata (tm, " b" , style= true )
278
+ @test DataAPI. metadata (tm, " a" ) == " 100"
279
+ @test DataAPI. metadata (tm, " a" , style= true ) == (" 100" , :note )
280
+ DataAPI. deletemetadata! (tm, " a" )
281
+ @test isempty (DataAPI. metadatakeys (tm))
282
+ @test DataAPI. metadata! (tm, " a" , " 100" , style= :note ) == tm
283
+ DataAPI. emptymetadata! (tm)
284
+ @test isempty (DataAPI. metadatakeys (tm))
285
+
286
+ @test DataAPI. colmetadatakeys (tm) == ()
287
+ @test DataAPI. colmetadatakeys (tm, :col ) == ()
288
+ @test DataAPI. colmetadata! (tm, :col , " a" , " 100" , style= :note ) == tm
289
+ @test [k => collect (v) for (k, v) in DataAPI. colmetadatakeys (tm)] == [:col => [" a" ]]
290
+ @test collect (DataAPI. colmetadatakeys (tm, :col )) == [" a" ]
291
+ @test_throws KeyError DataAPI. colmetadata (tm, :col , " b" )
292
+ @test_throws KeyError DataAPI. colmetadata (tm, :col , " b" , style= true )
293
+ @test_throws KeyError DataAPI. colmetadata (tm, :col2 , " a" )
294
+ @test_throws KeyError DataAPI. colmetadata (tm, :col2 , " a" , style= true )
295
+ @test DataAPI. colmetadata (tm, :col , " a" ) == " 100"
296
+ @test DataAPI. colmetadata (tm, :col , " a" , style= true ) == (" 100" , :note )
297
+ DataAPI. deletecolmetadata! (tm, :col , " a" )
298
+ @test isempty (DataAPI. colmetadatakeys (tm, :col ))
299
+ @test DataAPI. colmetadata! (tm, :col , " a" , " 100" , style= :note ) == tm
300
+ DataAPI. emptycolmetadata! (tm, :col )
301
+ @test isempty (DataAPI. colmetadatakeys (tm, :col ))
302
+ @test DataAPI. colmetadata! (tm, :col , " a" , " 100" , style= :note ) == tm
303
+ DataAPI. emptycolmetadata! (tm)
304
+ @test isempty (DataAPI. colmetadatakeys (tm))
305
+ end
306
+
176
307
end # @testset "DataAPI"
0 commit comments