forked from finos/architecture-as-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-models.ts
More file actions
159 lines (134 loc) · 5.63 KB
/
template-models.ts
File metadata and controls
159 lines (134 loc) · 5.63 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
export type CalmCoreCanonicalModel = {
nodes: CalmNodeCanonicalModel[];
relationships: CalmRelationshipCanonicalModel[];
metadata?: CalmMetadataCanonicalModel;
controls?: CalmControlsCanonicalModel;
flows?: CalmFlowCanonicalModel[];
adrs?: string[];
};
export type CalmNodeCanonicalModel = {
'unique-id': string;
'node-type': string;
name: string;
description: string;
details?: CalmCoreCanonicalModel;
interfaces?: CalmInterfaceCanonicalModel[];
controls?: CalmControlsCanonicalModel;
metadata?: CalmMetadataCanonicalModel;
} & Record<string, unknown>;
export type CalmTimelineCanonicalModel = {
'current-moment'?: string;
moments: CalmMomentCanonicalModel[];
metadata?: CalmMetadataCanonicalModel;
}
export type CalmMomentCanonicalModel =
CalmNodeCanonicalModel & {
'valid-from'?: string;
adrs?: string[];
}
export type CalmDecisionCanonicalModel = {
description: string;
nodes: string[];
relationships: string[];
controls?: string[];
metadata?: string[];
};
export type CalmRelationshipTypeCanonicalModel =
| { interacts: { actor: string; nodes: string[] } }
| { connects: { source: CalmNodeInterfaceCanonicalModel; destination: CalmNodeInterfaceCanonicalModel } }
| { 'deployed-in': { container: string; nodes: string[] } }
| { 'composed-of': { container: string; nodes: string[] } }
| { options: CalmDecisionCanonicalModel[] };
export type CalmRelationshipCanonicalModel = {
'unique-id': string;
'relationship-type': CalmRelationshipTypeCanonicalModel;
description?: string;
protocol?: string;
metadata?: CalmMetadataCanonicalModel;
controls?: CalmControlsCanonicalModel;
} & Record<string, unknown>;
export type CalmFlowCanonicalModel = {
'unique-id': string;
name: string;
description: string;
transitions: CalmFlowTransitionCanonicalModel[];
'requirement-url'?: string;
controls?: CalmControlsCanonicalModel;
metadata?: CalmMetadataCanonicalModel;
};
export type CalmFlowTransitionCanonicalModel = {
'relationship-unique-id': string;
'sequence-number': number;
description: string;
direction?: 'source-to-destination' | 'destination-to-source';
};
export type CalmMetadataCanonicalModel = Record<string, unknown>;
export type CalmControlsCanonicalModel = {
[controlId: string]: CalmControlCanonicalModel;
};
export type CalmControlCanonicalModel = {
description: string;
requirements: CalmControlDetailCanonicalModel[];
};
export type CalmControlDetailCanonicalModel = {
'requirement-url': string;
} & Record<string, unknown>;
export type CalmNodeInterfaceCanonicalModel = {
node: string;
interfaces?: string[];
};
export type CalmInterfaceCanonicalModel = {
'unique-id': string;
} & Record<string, unknown>;
export type CalmRelationshipKey = 'interacts' | 'connects' | 'composed-of' | 'deployed-in' | 'options';
export type ExtractRel<K extends CalmRelationshipKey> =
Extract<CalmRelationshipTypeCanonicalModel, Record<K, unknown>>;
export function isInteracts(rel: CalmRelationshipTypeCanonicalModel): rel is ExtractRel<'interacts'> {
return 'interacts' in rel;
}
export function isConnects(rel: CalmRelationshipTypeCanonicalModel): rel is ExtractRel<'connects'> {
return 'connects' in rel;
}
export function isComposedOf(rel: CalmRelationshipTypeCanonicalModel): rel is ExtractRel<'composed-of'> {
return 'composed-of' in rel;
}
export function isDeployedIn(rel: CalmRelationshipTypeCanonicalModel): rel is ExtractRel<'deployed-in'> {
return 'deployed-in' in rel;
}
export function isOptions(rel: CalmRelationshipTypeCanonicalModel): rel is ExtractRel<'options'> {
return 'options' in rel;
}
export function visitRelationship<T>(
rel: CalmRelationshipTypeCanonicalModel,
fns: {
interacts?: (r: ExtractRel<'interacts'>) => T;
connects?: (r: ExtractRel<'connects'>) => T;
composedOf?: (r: ExtractRel<'composed-of'>) => T;
deployedIn?: (r: ExtractRel<'deployed-in'>) => T;
options?: (r: ExtractRel<'options'>) => T;
default: () => T;
}
): T {
if (isInteracts(rel) && fns.interacts) return fns.interacts(rel);
if (isConnects(rel) && fns.connects) return fns.connects(rel);
if (isComposedOf(rel) && fns.composedOf) return fns.composedOf(rel);
if (isDeployedIn(rel) && fns.deployedIn) return fns.deployedIn(rel);
if (isOptions(rel) && fns.options) return fns.options(rel);
return fns.default();
}
export type CalmRelationshipTypeKindView =
| { kind: 'interacts'; actor: string; nodes: string[] }
| { kind: 'connects'; source: CalmNodeInterfaceCanonicalModel; destination: CalmNodeInterfaceCanonicalModel }
| { kind: 'deployed-in'; container: string; nodes: string[] }
| { kind: 'composed-of'; container: string; nodes: string[] }
| { kind: 'options'; options: CalmDecisionCanonicalModel[] };
export function toKindView(rel: CalmRelationshipTypeCanonicalModel): CalmRelationshipTypeKindView {
return visitRelationship<CalmRelationshipTypeKindView>(rel, {
interacts: r => ({ kind: 'interacts', actor: r.interacts.actor, nodes: r.interacts.nodes }),
connects: r => ({ kind: 'connects', source: r.connects.source, destination: r.connects.destination }),
deployedIn: r => ({ kind: 'deployed-in', container: r['deployed-in'].container, nodes: r['deployed-in'].nodes }),
composedOf: r => ({ kind: 'composed-of', container: r['composed-of'].container, nodes: r['composed-of'].nodes }),
options: r => ({ kind: 'options', options: r.options }),
default: () => { throw new Error('Unknown relationship type'); },
});
}