-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFakeDB.hh
65 lines (58 loc) · 1.77 KB
/
FakeDB.hh
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
<?hh // strict
type DBResultExtra = shape('age' => int);
type DBResult = shape(
'id' => int,
'name' => string,
'extra' => DBResultExtra,
);
final class FakeDB {
public function getRawRows(): array<array<string, mixed>> {
$good_extra = json_encode(array('age' => 40));
$bad_extra = 'corrupt data';
// Real code would query a DB, but for now let's hardcode it
return array(
array(
'id' => 123,
'name' => 'Alice',
'extra' => $good_extra,
),
array(
'id' => 456,
'name' => 'Bob',
'extra' => $bad_extra,
),
);
}
/**
* When processing untyped data you need to check each piece of data and
* figure out whether to give up or recover when the data is bad
*/
public function processRow(array<string, mixed> $row): ?DBResult {
$row = Map::fromArray($row);
$id = $row->contains('id') ? $row['id'] : null;
$name = $row->contains('name') ? $row['name'] : null;
$extra = $row->contains('extra') ? json_decode($row['extra'], true) : null;
// Ignore rows with invalid IDs or names
if (!is_int($id) || !is_string($name)) {
return null;
}
// Try to recover from a bad extra column
if (!is_array($extra)) {
$extra = shape('age' => 0);
} else {
$extra = Map::fromArray($extra);
$extra = shape('age' => $extra->contains('age') ? $extra['age'] : 0);
}
return shape('id' => $id, 'name' => $name, 'extra' => $extra);
}
public function getDBResults(): Vector<DBResult> {
$ret = Vector {};
foreach ($this->getRawRows() as $raw_row) {
$row = $this->processRow($raw_row);
if ($row !== null) {
$ret->add($row);
}
}
return $ret;
}
}