-
Notifications
You must be signed in to change notification settings - Fork 558
/
TypeHelpers.ts
363 lines (352 loc) · 11.4 KB
/
TypeHelpers.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
import {
BSON,
ClassHelpers,
Collection,
INTERNAL,
List,
ObjCreator,
REALM,
Realm,
RealmObject,
TypeAssertionError,
UpdateMode,
assert,
binding,
safeGlobalThis,
} from "./internal";
const TYPED_ARRAY_CONSTRUCTORS = new Set(
[
DataView,
Int8Array,
Uint8Array,
Uint8ClampedArray,
Int16Array,
Uint16Array,
Int32Array,
Uint32Array,
Float32Array,
Float64Array,
// These will not be present on old versions of JSC without BigInt support.
safeGlobalThis.BigInt64Array,
safeGlobalThis.BigUint64Array,
].filter((ctor) => ctor !== undefined),
);
export function toArrayBuffer(value: unknown, stringToBase64 = true) {
if (typeof value === "string" && stringToBase64) {
return binding.Helpers.base64Decode(value);
}
for (const TypedArray of TYPED_ARRAY_CONSTRUCTORS) {
if (value instanceof TypedArray) {
return value.buffer.slice(value.byteOffset, value.byteOffset + value.byteLength);
}
}
assert.instanceOf(value, ArrayBuffer);
return value;
}
/** @internal */
export type TypeHelpers<T = unknown> = {
toBinding(value: T, createObj?: ObjCreator): binding.MixedArg;
fromBinding(value: unknown): T;
};
export type TypeOptions = {
realm: Realm;
name: string;
optional: boolean;
objectType: string | undefined;
objectSchemaName: string | undefined;
getClassHelpers(nameOrTableKey: string | binding.TableKey): ClassHelpers;
};
// TODO: Consider testing for expected object instance types and throw something similar to the legacy SDK:
// "Only Realm instances are supported." (which should probably have been "RealmObject")
// instead of relying on the binding to throw.
export function mixedToBinding(realm: binding.Realm, value: unknown): binding.MixedArg {
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null) {
// Fast track pass through for the most commonly used types
return value;
} else if (value === undefined) {
return null;
} else if (value instanceof Date) {
return binding.Timestamp.fromDate(value);
} else if (value instanceof RealmObject) {
const otherRealm = value[REALM].internal;
assert.isSameRealm(realm, otherRealm, "Realm object is from another Realm");
return value[INTERNAL];
} else if (value instanceof Collection) {
throw new Error(`Using a ${value.constructor.name} as Mixed value, is not yet supported`);
} else if (Array.isArray(value)) {
throw new TypeError("A mixed property cannot contain an array of values.");
} else {
// Convert typed arrays to an `ArrayBuffer`
for (const TypedArray of TYPED_ARRAY_CONSTRUCTORS) {
if (value instanceof TypedArray) {
return value.buffer.slice(value.byteOffset, value.byteOffset + value.byteLength);
}
}
// Rely on the binding for any other value
return value as binding.MixedArg;
}
}
function defaultToBinding(value: unknown): binding.MixedArg {
return value as binding.MixedArg;
}
function defaultFromBinding(value: unknown) {
return value;
}
/**
* Adds a branch to a function, which checks for the argument to be null, in which case it returns early.
*/
/* eslint-disable-next-line @typescript-eslint/no-explicit-any -- Using `unknown` here breaks type inference in `binding.PropertyType.Object` `toBinding` from for some reason */
function nullPassthrough<T, R extends any[], F extends (value: unknown, ...rest: R) => unknown>(
this: T,
fn: F,
enabled: boolean,
): F {
if (enabled) {
return ((value, ...rest) =>
typeof value === "undefined" || value === null ? null : fn.call(this, value, ...rest)) as F;
} else {
return fn;
}
}
const TYPES_MAPPING: Record<binding.PropertyType, (options: TypeOptions) => TypeHelpers> = {
[binding.PropertyType.Int]({ optional }) {
return {
toBinding: nullPassthrough((value) => {
if (typeof value === "number") {
return binding.Int64.numToInt(value);
} else if (binding.Int64.isInt(value)) {
return value;
} else {
throw new TypeAssertionError("a number or bigint", value);
}
}, optional),
// TODO: Support returning bigints to end-users
fromBinding: nullPassthrough((value) => Number(value), optional),
};
},
[binding.PropertyType.Bool]({ optional }) {
return {
toBinding: nullPassthrough((value) => {
assert.boolean(value);
return value;
}, optional),
fromBinding: defaultFromBinding,
};
},
[binding.PropertyType.String]({ optional }) {
return {
toBinding: nullPassthrough((value) => {
assert.string(value);
return value;
}, optional),
fromBinding: defaultFromBinding,
};
},
[binding.PropertyType.Data]({ optional }) {
return {
toBinding: nullPassthrough((value) => {
return toArrayBuffer(value);
}, optional),
fromBinding: defaultFromBinding,
};
},
[binding.PropertyType.Date]({ optional }) {
return {
toBinding: nullPassthrough((value) => {
if (typeof value === "string") {
// TODO: Consider deprecating this undocumented type coercion
return binding.Timestamp.fromDate(new Date(value));
} else {
assert.instanceOf(value, Date);
return binding.Timestamp.fromDate(value);
}
}, optional),
fromBinding: nullPassthrough((value) => {
assert.instanceOf(value, binding.Timestamp);
return value.toDate();
}, optional),
};
},
[binding.PropertyType.Float]({ optional }) {
return {
toBinding: nullPassthrough((value) => {
assert.number(value);
return new binding.Float(value);
}, optional),
fromBinding: nullPassthrough((value) => {
assert.instanceOf(value, binding.Float);
return value.value;
}, optional),
};
},
[binding.PropertyType.Double]({ optional }) {
return {
toBinding: nullPassthrough((value) => {
assert.number(value);
return value;
}, optional),
fromBinding: defaultFromBinding,
};
},
[binding.PropertyType.Object]({ realm, name, objectType, optional, getClassHelpers }) {
assert(objectType);
const helpers = getClassHelpers(objectType);
const { wrapObject } = helpers;
return {
toBinding: nullPassthrough((value, createObj) => {
if (
value instanceof RealmObject &&
value.constructor.name === objectType &&
value[REALM].internal.$addr === realm.internal.$addr
) {
return value[INTERNAL];
} else {
// TODO: Consider exposing a way for calling code to disable object creation
assert.object(value, name);
// Some other object is assumed to be an unmanged object, that the user wants to create
const createdObject = RealmObject.create(realm, value, UpdateMode.Never, {
helpers,
createObj,
});
return createdObject[INTERNAL];
}
}, optional),
fromBinding: nullPassthrough((value) => {
if (value instanceof binding.ObjLink) {
const table = binding.Helpers.getTable(realm.internal, value.tableKey);
const linkedObj = table.getObject(value.objKey);
return wrapObject(linkedObj);
} else {
assert.instanceOf(value, binding.Obj);
return wrapObject(value);
}
}, optional),
};
},
[binding.PropertyType.LinkingObjects]({ objectType, getClassHelpers }) {
assert(objectType);
const { wrapObject } = getClassHelpers(objectType);
return {
toBinding: defaultToBinding,
fromBinding(value) {
assert.instanceOf(value, binding.Obj);
return wrapObject(value);
},
};
},
[binding.PropertyType.Mixed]({ realm, getClassHelpers }) {
return {
toBinding: mixedToBinding.bind(null, realm.internal),
fromBinding(value) {
if (binding.Int64.isInt(value)) {
return binding.Int64.intToNum(value);
} else if (value instanceof binding.Timestamp) {
return value.toDate();
} else if (value instanceof binding.Float) {
return value.value;
} else if (value instanceof binding.ObjLink) {
const table = binding.Helpers.getTable(realm.internal, value.tableKey);
const linkedObj = table.getObject(value.objKey);
const { wrapObject } = getClassHelpers(value.tableKey);
return wrapObject(linkedObj);
} else {
return value;
}
},
};
},
[binding.PropertyType.ObjectId]({ optional }) {
return {
toBinding: nullPassthrough((value) => {
assert.instanceOf(value, BSON.ObjectId);
return value;
}, optional),
fromBinding: defaultFromBinding,
};
},
[binding.PropertyType.Decimal]({ optional }) {
return {
toBinding: nullPassthrough((value) => {
assert.instanceOf(value, BSON.Decimal128);
return value;
}, optional),
fromBinding: defaultFromBinding,
};
},
[binding.PropertyType.Uuid]({ optional }) {
return {
toBinding: nullPassthrough((value) => {
assert.instanceOf(value, BSON.UUID);
return value;
}, optional),
fromBinding: defaultFromBinding,
};
},
[binding.PropertyType.Array]({ realm, getClassHelpers, name, objectSchemaName }) {
assert.string(objectSchemaName, "objectSchemaName");
const classHelpers = getClassHelpers(objectSchemaName);
return {
fromBinding(value: unknown) {
assert.instanceOf(value, binding.List);
const propertyHelpers = classHelpers.properties.get(name);
const collectionHelpers = propertyHelpers.collectionHelpers;
assert.object(collectionHelpers);
return new List(realm, value, collectionHelpers);
},
toBinding() {
throw new Error("Not supported");
},
};
},
[binding.PropertyType.Set]() {
return {
fromBinding() {
throw new Error("Not yet supported");
},
toBinding() {
throw new Error("Not yet supported");
},
};
},
[binding.PropertyType.Dictionary]() {
return {
fromBinding() {
throw new Error("Not supported");
},
toBinding() {
throw new Error("Not supported");
},
};
},
[binding.PropertyType.Nullable]() {
throw new Error("Not directly mappable");
},
[binding.PropertyType.Collection]() {
throw new Error("Not directly mappable");
},
[binding.PropertyType.Flags]() {
throw new Error("Not directly mappable");
},
};
export function getTypeHelpers(type: binding.PropertyType, options: TypeOptions): TypeHelpers {
const helpers = TYPES_MAPPING[type];
assert(helpers, `Unexpected type ${type}`);
return helpers(options);
}