-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy patharray.py
More file actions
63 lines (50 loc) · 1.72 KB
/
array.py
File metadata and controls
63 lines (50 loc) · 1.72 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
from __future__ import annotations
from typing import Any, Dict, Optional
import attrs
from ..schema import Field
@attrs.define(kw_only=True, repr=False)
class ArrayField(Field):
type = "array"
builtin = True
supported_constraints = [
"required",
"minLength",
"maxLength",
"enum",
]
array_item: Optional[Dict[str, Any]] = attrs.field(factory=dict)
"""
A dictionary that specifies the type and other constraints for the
data that will be read in this data type field.
"""
# Read
def create_cell_reader(self):
default_reader = super().create_cell_reader()
# Create field
field_reader = None
if self.array_item:
descriptor = self.array_item.copy()
descriptor.pop("arrayItem", None)
descriptor.setdefault("name", self.name)
descriptor.setdefault("type", "any")
field = Field.from_descriptor(descriptor)
field_reader = field.create_cell_reader()
# Create reader
def cell_reader(cell: Any):
cell, notes = default_reader(cell)
if cell is not None and not notes and field_reader:
for index, item in enumerate(cell):
item_cell, item_notes = field_reader(item)
if item_notes:
notes = notes or {}
for name, note in item_notes.items():
notes[name] = f"array item {note}"
cell[index] = item_cell
return cell, notes
return cell_reader
# Metadata
metadata_profile_patch = {
"properties": {
"arrayItem": {"type": "object"},
}
}