-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdata.ts
328 lines (297 loc) · 8.84 KB
/
data.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import { RelationshipWithComments } from "../../parsing";
import { RelationTuple as Relationship } from "../../protodefs/core/v1/core";
import { Struct } from "../../protodefs/google/protobuf/struct";
import { COLUMNS } from "./columns";
/**
* ColumnData holds raw column data for the grid.
*/
export type ColumnData = readonly string[];
/**
* Comment represents a comment in the grid.
*/
export type Comment = {
comment: string;
};
/**
* PartialRelationship represents a (possibly partial) relationship.
*/
export type PartialRelationship = {
resourceType: string;
resourceId: string;
relation: string;
subjectType: string;
subjectId: string;
subjectRelation: string;
caveatName: string;
caveatContext: string;
expiration: string;
};
/**
* RelationshipDatum represents a single row in the grid.
*/
export type RelationshipDatum = PartialRelationship | Comment;
/**
* RelationshipDatumAndMetadata is a type which holds both the datum, as well as the metadata for
* the rel grid.
*/
export interface RelationshipDatumAndMetadata {
/**
* datum is the datum forming the row in the grid.
*/
datum: RelationshipDatum;
/**
* dataRowIndex is the index of the datum in the array from which this
* item was constructed.
*/
dataRowIndex: number;
/**
* columnData holds the raw column data for the row.
*/
columnData: ColumnData;
}
/**
* AnnotatedData represents the data found in the grid, as an array of RelationshipDatumAndMetadata,
* one per row.
*/
export type AnnotatedData = RelationshipDatumAndMetadata[];
/**
* toExternalData converts the annotated data into a simple RelationshipDatum array.
*/
export function toExternalData(data: AnnotatedData): RelationshipDatum[] {
return data.map((datum: RelationshipDatumAndMetadata) => datum.datum);
}
/**
* fromExternalData converts a simple RelationshipDatum array into the annotated data.
*/
export function fromExternalData(
externalData: RelationshipDatum[] | undefined,
): AnnotatedData {
return (externalData ?? []).map(datumToAnnotated);
}
/**
* emptyAnnotatedDatum returns an empty annotated datum for the grid, at the given index.
*/
export function emptyAnnotatedDatum(
index: number,
): RelationshipDatumAndMetadata {
return datumToAnnotated(
{
resourceType: "",
resourceId: "",
relation: "",
subjectType: "",
subjectId: "",
subjectRelation: "",
caveatName: "",
caveatContext: "",
expiration: "",
},
index,
);
}
/**
* toRelationshipString converts the given annotated datum into a relationship string. If the
* datum is a comment or not a full relationship, returns undefined.
*/
export function toRelationshipString(
annotated: RelationshipDatumAndMetadata,
): string | undefined {
if ("comment" in annotated.datum) {
return undefined;
}
return toFullRelationshipString(annotated.datum);
}
/**
* toFullRelationshipString returns the full relationship found, or undefined if none.
*/
export function toFullRelationshipString(
annotated: PartialRelationship,
): string | undefined {
if (
!annotated.resourceType ||
!annotated.resourceId ||
!annotated.relation ||
!annotated.subjectType ||
!annotated.subjectId
) {
return undefined;
}
return toPartialRelationshipString(annotated);
}
/**
* toPartialRelationshipString returns a relationship string with the given relationship's data.
*/
export function toPartialRelationshipString(
annotated: PartialRelationship,
): string | undefined {
const caveatContext = annotated.caveatContext
? `:${annotated.caveatContext}`
: "";
const caveat = annotated.caveatName
? `[${annotated.caveatName}${caveatContext}]`
: "";
const expiration = annotated.expiration
? `[expiration:${annotated.expiration}]`
: "";
return `${annotated.resourceType}:${annotated.resourceId}#${
annotated.relation
}@${annotated.subjectType}:${annotated.subjectId}${
annotated.subjectRelation ? `#${annotated.subjectRelation}` : ""
}${caveat}${expiration}`;
}
/**
* datumToAnnotated returns an annotated datum with the given row index.
*/
export function datumToAnnotated(
datum: RelationshipDatum,
index: number,
): RelationshipDatumAndMetadata {
return {
datum: datum,
dataRowIndex: index,
columnData: getColumnData(datum),
};
}
/**
* getColumnData returns the column data for a datum.
*/
export function getColumnData(datum: RelationshipDatum) {
if ("comment" in datum) {
return [datum.comment];
}
const colData = [
datum.resourceType,
datum.resourceId,
datum.relation,
datum.subjectType,
datum.subjectId,
datum.subjectRelation ?? "",
datum.caveatName ?? "",
datum.caveatContext ?? "",
datum.expiration ?? "",
];
return colData;
}
/**
* relationshipToDatum converts a relationship into a datum row.
*/
export function relationshipToDatum(rel: Relationship): PartialRelationship {
let subRel = rel.subject?.relation;
if (subRel === "...") {
subRel = "";
}
return {
resourceType: rel.resourceAndRelation?.namespace ?? "",
resourceId: rel.resourceAndRelation?.objectId ?? "",
relation: rel.resourceAndRelation?.relation ?? "",
subjectType: rel.subject?.namespace ?? "",
subjectId: rel.subject?.objectId ?? "",
subjectRelation: subRel ?? "",
caveatName: rel.caveat?.caveatName ?? "",
caveatContext: rel.caveat?.context
? Struct.toJsonString(rel.caveat?.context)
: "",
expiration: rel.optionalExpirationTime
? new Date(parseFloat(rel.optionalExpirationTime.seconds) * 1000)
.toISOString()
.replace(".000", "")
: "",
};
}
/**
* fromColumnData converts column data into a partial relationship.
*/
function fromColumnData(columnData: ColumnData): PartialRelationship | Comment {
if (columnData[0].startsWith("// ")) {
return {
comment: columnData[0],
};
}
return {
resourceType: columnData[0],
resourceId: columnData[1],
relation: columnData[2],
subjectType: columnData[3],
subjectId: columnData[4],
subjectRelation: columnData[5] ?? "",
caveatName: columnData[6] ?? "",
caveatContext: columnData[7] ?? "",
expiration: columnData[8] ?? "",
};
}
// relationshipToColumnData converts the given relationship into column data.
export function relationshipToColumnData(
rel: RelationshipWithComments,
): ColumnData | undefined {
const relationship = rel.relationship;
if (relationship === undefined) {
return undefined;
}
let userRel = relationship.subject?.relation ?? "";
if (userRel.trim() === "...") {
userRel = "";
}
const caveatContext = relationship.caveat?.context
? JSON.stringify(relationship.caveat.context)
: "";
const columnData = [
relationship.resourceAndRelation?.namespace ?? "",
relationship.resourceAndRelation?.objectId ?? "",
relationship.resourceAndRelation?.relation ?? "",
relationship.subject?.namespace ?? "",
relationship.subject?.objectId ?? "",
userRel,
relationship.caveat?.caveatName ?? "",
caveatContext,
relationship.optionalExpirationTime
? new Date(parseFloat(relationship.optionalExpirationTime.seconds) * 1000)
.toISOString()
.replace(".000", "")
: "",
];
if (columnData.length !== COLUMNS.length) {
throw Error("Missing column");
}
return columnData;
}
/**
* updateRowInData updates the grid data for a specific set of new column data.
* @param inFlightGridData The current *in flight* annotated grid data.
* @param dataRowIndex The index of the row to be updated in the grid data.
* @param newColumnData The new column data for the row.
* @param startingColIndex If specified, the column at which the newColumnData starts.
*/
export function updateRowInData(
inFlightGridData: AnnotatedData,
dataRowIndex: number,
newColumnData: ColumnData,
startingColIndex?: number,
): AnnotatedData {
const adjustedData = Array.from(inFlightGridData);
if (dataRowIndex === adjustedData.length) {
// Add a new row.
adjustedData.push(emptyAnnotatedDatum(dataRowIndex));
} else if (dataRowIndex > adjustedData.length) {
// Skip any outside of the immediate range.
return adjustedData;
}
// If startingColIndex is given, build an adjusted fullNewColumnData.
let fullNewColumnData: string[] = Array.from(newColumnData);
if (startingColIndex !== undefined) {
const existingColumnData = adjustedData[dataRowIndex].columnData;
fullNewColumnData = [
...existingColumnData.slice(0, startingColIndex),
...newColumnData,
...existingColumnData.slice(startingColIndex + newColumnData.length),
];
for (let i = fullNewColumnData.length; i < COLUMNS.length; ++i) {
fullNewColumnData.push("");
}
}
adjustedData[dataRowIndex] = {
datum: fromColumnData(fullNewColumnData),
columnData: fullNewColumnData,
dataRowIndex: dataRowIndex,
};
return adjustedData;
}