-
Notifications
You must be signed in to change notification settings - Fork 1
/
GdprDeserializer.ts
125 lines (107 loc) · 3.16 KB
/
GdprDeserializer.ts
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
import { GdprManager, GdprManagerRaw } from "../GdprManager"
import { GdprStorage } from "../GdprStorage"
import { GdprGuardGroup, GdprGuardGroupRaw } from "../GdprGuardGroup"
import { GdprGuard, GdprGuardRaw, makeGuard } from "../GdprGuard"
/*
For retro-compatibility, we do not check for
the presence of the `bannerWasShown` key
*/
const managerKeys = ["enabled", "groups"];
const groupKeys = ["guards"];
const guardKeys = [
"name",
"enabled",
"required",
"description",
"storage"
];
const isManager = (raw: any): raw is GdprManagerRaw => {
const allKeys = managerKeys.every(key => key in raw);
return allKeys
&& typeof raw.enabled == "boolean"
&& Array.isArray(raw.groups);
};
const isGroup = (raw: GdprGuardRaw|any): raw is GdprGuardGroupRaw => {
const allKeys = groupKeys.every(key => key in raw);
return allKeys && Array.isArray(raw.guards);
};
const isGuard = (raw: any): raw is GdprGuardRaw => {
const allKeys = guardKeys.every(key => key in raw);
return allKeys
&& typeof raw.name == "string"
&& typeof raw.enabled == "boolean"
&& typeof raw.required == "boolean"
&& typeof raw.description == "string"
&& typeof raw.storage == "number"
&& raw.storage in GdprStorage;
};
/**
* Namespace-like class that allows deserialization from raw format
* @abstract
* @class GdprDeserializer
* @export
*/
export abstract class GdprDeserializer {
/**
* Deserialize a GdprManager from its raw representation
* @param raw The serialized manager
* @returns {?GdprManager}
* @static
* @memberof GdprDeserializer
*/
static manager(raw: GdprManagerRaw|any): GdprManager | null {
if (!isManager(raw))
return null;
const groups: GdprGuardGroup[] = raw.groups
.map(group => this.group(group))
.filter(group => group !== null) as GdprGuardGroup[];
const manager = GdprManager.create([]);
manager.enabled = !!raw.enabled;
manager.bannerWasShown = !!raw.bannerWasShown;
groups.forEach(group => manager.addGroup(group));
return manager;
}
/**
* Deserialize a GdprGuardGroup from its raw representation
* @param {any} raw The serialized group
* @returns {?GdprGuardGroup}
* @static
* @memberof GdprDeserializer
*/
static group(raw: GdprGuardGroupRaw|any): GdprGuardGroup | null {
const guard: GdprGuard | null = this.guard(raw);
if (guard === null)
return null;
if (!isGroup(raw))
return null;
const group = GdprGuardGroup.for(
guard.name,
guard.description,
guard.enabled,
guard.required
);
const guards: GdprGuard[] = raw.guards
.map((guard: GdprGuardRaw) => groupKeys.every(key => key in guard) ? this.group(guard) : this.guard(guard))
.filter((guard: GdprGuard|null): guard is GdprGuard => guard !== null);
// if (!guards.length)
// return null;
guards.forEach(guard => group.addGuard(guard));
return group;
}
/**
* Deserialize a GdprGuard from its raw representation
* @param {any} raw The serialized guard
* @returns {?GdprGuard}
* @static
* @memberof GdprDeserializer
*/
static guard(raw: GdprGuardRaw|any): GdprGuard | null {
return !isGuard(raw) ? null : makeGuard(
raw.name,
raw.description,
raw.storage as GdprStorage,
!!raw.required,
!!raw.enabled
);
}
}