Skip to content

Commit 019ff81

Browse files
committed
Fix and centralize generic type validation.
And also document which type is compatible with which `DataType`.
1 parent 6d80069 commit 019ff81

File tree

6 files changed

+209
-51
lines changed

6 files changed

+209
-51
lines changed

sources/TileDB.CSharp/Array.cs

+2-10
Original file line numberDiff line numberDiff line change
@@ -501,15 +501,11 @@ static void GetDomain<T>(Array array, string dimName, uint i, NonEmptyDomain non
501501
/// <exception cref="ArgumentException"><typeparamref name="T"/> is not the dimension's type.</exception>
502502
public (T Start, T End, bool IsEmpty) NonEmptyDomain<T>(uint index) where T : struct
503503
{
504-
var datatype = EnumUtil.TypeToDataType(typeof(T));
505504
using (var schema = Schema())
506505
using (var domain = schema.Domain())
507506
using (var dimension = domain.Dimension(index))
508507
{
509-
if (datatype != dimension.Type())
510-
{
511-
throw new ArgumentException("Array.NonEmptyDomain, not valid datatype!");
512-
}
508+
ErrorHandling.CheckDataType<T>(dimension.Type());
513509
}
514510

515511
SequentialPair<T> data;
@@ -531,15 +527,11 @@ static void GetDomain<T>(Array array, string dimName, uint i, NonEmptyDomain non
531527
/// <exception cref="ArgumentException"><typeparamref name="T"/> is not the dimension's type.</exception>
532528
public (T Start, T End, bool IsEmpty) NonEmptyDomain<T>(string name) where T : struct
533529
{
534-
var datatype = EnumUtil.TypeToDataType(typeof(T));
535530
using (var schema = Schema())
536531
using (var domain = schema.Domain())
537532
using (var dimension = domain.Dimension(name))
538533
{
539-
if (datatype != dimension.Type())
540-
{
541-
throw new ArgumentException("Array.NonEmptyDomain, not valid datatype!");
542-
}
534+
ErrorHandling.CheckDataType<T>(dimension.Type());
543535
}
544536

545537
using var ms_name = new MarshaledString(name);

sources/TileDB.CSharp/Enums.cs

+162-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.ComponentModel;
3+
using System.Text;
34
using TileDB.Interop;
45

56
namespace TileDB.CSharp
@@ -185,156 +186,294 @@ public enum DataType : uint
185186
/// <summary>
186187
/// A signed 32-bit integer.
187188
/// </summary>
189+
/// <remarks>
190+
/// On generic methods operating to objects of this datatype,
191+
/// this datatype can be used with <see cref="int"/>.
192+
/// </remarks>
188193
Int32 = tiledb_datatype_t.TILEDB_INT32,
189194
/// <summary>
190195
/// A signed 64-bit integer.
191196
/// </summary>
197+
/// <remarks>
198+
/// On generic methods operating to objects of this datatype,
199+
/// this datatype can be used with <see cref="long"/>.
200+
/// </remarks>
192201
Int64 = tiledb_datatype_t.TILEDB_INT64,
193202
/// <summary>
194203
/// A 32-bit floating-point number.
195204
/// </summary>
205+
/// <remarks>
206+
/// On generic methods operating to objects of this datatype,
207+
/// this datatype can be used with <see cref="float"/>.
208+
/// </remarks>
196209
Float32 = tiledb_datatype_t.TILEDB_FLOAT32,
197210
/// <summary>
198211
/// A 64-bit floating-point number.
199212
/// </summary>
213+
/// <remarks>
214+
/// On generic methods operating to objects of this datatype,
215+
/// this datatype can be used with <see cref="double"/>.
216+
/// </remarks>
200217
Float64 = tiledb_datatype_t.TILEDB_FLOAT64,
201218
/// <summary>
202219
/// A signed 8-bit integer.
203220
/// </summary>
221+
/// <remarks>
222+
/// On generic methods operating to objects of this datatype,
223+
/// this datatype can be used with <see cref="sbyte"/>.
224+
/// </remarks>
204225
Int8 = tiledb_datatype_t.TILEDB_INT8,
205226
/// <summary>
206227
/// An unsigned 8-bit integer.
207228
/// </summary>
229+
/// <remarks>
230+
/// On generic methods operating to objects of this datatype,
231+
/// this datatype can be used with <see cref="byte"/>.
232+
/// </remarks>
208233
UInt8 = tiledb_datatype_t.TILEDB_UINT8,
209234
/// <summary>
210235
/// A signed 16-bit integer.
211236
/// </summary>
237+
/// <remarks>
238+
/// On generic methods operating to objects of this datatype,
239+
/// this datatype can be used with <see cref="short"/>.
240+
/// </remarks>
212241
Int16 = tiledb_datatype_t.TILEDB_INT16,
213242
/// <summary>
214243
/// An unsigned 16-bit integer.
215244
/// </summary>
245+
/// <remarks>
246+
/// On generic methods operating to objects of this datatype,
247+
/// this datatype can be used with <see cref="ushort"/>.
248+
/// </remarks>
216249
UInt16 = tiledb_datatype_t.TILEDB_UINT16,
217250
/// <summary>
218251
/// An unsigned 32-bit integer.
219252
/// </summary>
253+
/// <remarks>
254+
/// On generic methods operating to objects of this datatype,
255+
/// this datatype can be used with <see cref="uint"/>.
256+
/// </remarks>
220257
UInt32 = tiledb_datatype_t.TILEDB_UINT32,
221258
/// <summary>
222259
/// An unsigned 64-bit integer.
223260
/// </summary>
261+
/// <remarks>
262+
/// On generic methods operating to objects of this datatype,
263+
/// this datatype can be used with <see cref="ulong"/>.
264+
/// </remarks>
224265
UInt64 = tiledb_datatype_t.TILEDB_UINT64,
225266
/// <summary>
226267
/// An ASCII string.
227268
/// </summary>
269+
/// <remarks>
270+
/// On generic methods operating to objects of this datatype,
271+
/// this datatype can be used with <see cref="byte"/>.
272+
/// </remarks>
228273
StringAscii = tiledb_datatype_t.TILEDB_STRING_ASCII,
229274
/// <summary>
230275
/// A UTF-8 string.
231276
/// </summary>
277+
/// <remarks>
278+
/// On generic methods operating to objects of this datatype,
279+
/// this datatype can be used with <see cref="byte"/>.
280+
/// </remarks>
232281
StringUtf8 = tiledb_datatype_t.TILEDB_STRING_UTF8,
233282
/// <summary>
234283
/// A UTF-16 string.
235284
/// </summary>
285+
/// <remarks>
286+
/// On generic methods operating to objects of this datatype,
287+
/// this datatype can be used with <see cref="ushort"/> or <see cref="char"/>.
288+
/// </remarks>
236289
StringUtf16 = tiledb_datatype_t.TILEDB_STRING_UTF16,
237290
/// <summary>
238291
/// A UTF-32 string.
239292
/// </summary>
293+
/// <remarks>
294+
/// On generic methods operating to objects of this datatype,
295+
/// this datatype can be used with <see cref="uint"/>.
296+
/// </remarks>
240297
StringUtf32 = tiledb_datatype_t.TILEDB_STRING_UTF32,
241298
/// <summary>
242299
/// A date and time, counted as the signed 64-bit number of
243300
/// years since the Unix epoch (January 1 1970 at midnight).
244301
/// </summary>
302+
/// <remarks>
303+
/// On generic methods operating to objects of this datatype,
304+
/// this datatype can be used with <see cref="long"/>.
305+
/// </remarks>
245306
DateTimeYear = tiledb_datatype_t.TILEDB_DATETIME_YEAR,
246307
/// <summary>
247308
/// A date and time, counted as the signed 64-bit number of
248309
/// months since the Unix epoch (January 1 1970 at midnight).
249310
/// </summary>
311+
/// <remarks>
312+
/// On generic methods operating to objects of this datatype,
313+
/// this datatype can be used with <see cref="long"/>.
314+
/// </remarks>
250315
DateTimeMonth = tiledb_datatype_t.TILEDB_DATETIME_MONTH,
251316
/// <summary>
252317
/// A date and time, counted as the signed 64-bit number of
253318
/// weeks since the Unix epoch (January 1 1970 at midnight).
254319
/// </summary>
320+
/// <remarks>
321+
/// On generic methods operating to objects of this datatype,
322+
/// this datatype can be used with <see cref="long"/>.
323+
/// </remarks>
255324
DateTimeWeek = tiledb_datatype_t.TILEDB_DATETIME_WEEK,
256325
/// <summary>
257326
/// A date and time, counted as the signed 64-bit number of
258327
/// days since the Unix epoch (January 1 1970 at midnight).
259328
/// </summary>
329+
/// <remarks>
330+
/// On generic methods operating to objects of this datatype,
331+
/// this datatype can be used with <see cref="long"/>.
332+
/// </remarks>
260333
DateTimeDay = tiledb_datatype_t.TILEDB_DATETIME_DAY,
261334
/// <summary>
262335
/// A date and time, counted as the signed 64-bit number of
263336
/// hours since the Unix epoch (January 1 1970 at midnight).
264337
/// </summary>
338+
/// <remarks>
339+
/// On generic methods operating to objects of this datatype,
340+
/// this datatype can be used with <see cref="long"/>.
341+
/// </remarks>
265342
DateTimeHour = tiledb_datatype_t.TILEDB_DATETIME_HR,
266343
/// <summary>
267344
/// A date and time, counted as the signed 64-bit number of
268345
/// minutes since the Unix epoch (January 1 1970 at midnight).
269346
/// </summary>
347+
/// <remarks>
348+
/// On generic methods operating to objects of this datatype,
349+
/// this datatype can be used with <see cref="long"/>.
350+
/// </remarks>
270351
DateTimeMinute = tiledb_datatype_t.TILEDB_DATETIME_MIN,
271352
/// <summary>
272353
/// A date and time, counted as the signed 64-bit number of
273354
/// seconds since the Unix epoch (January 1 1970 at midnight).
274355
/// </summary>
356+
/// <remarks>
357+
/// On generic methods operating to objects of this datatype,
358+
/// this datatype can be used with <see cref="long"/>.
359+
/// </remarks>
275360
DateTimeSecond = tiledb_datatype_t.TILEDB_DATETIME_SEC,
276361
/// <summary>
277362
/// A date and time, counted as the signed 64-bit number of
278363
/// milliseconds since the Unix epoch (January 1 1970 at midnight).
279364
/// </summary>
365+
/// <remarks>
366+
/// On generic methods operating to objects of this datatype,
367+
/// this datatype can be used with <see cref="long"/>.
368+
/// </remarks>
280369
DateTimeMillisecond = tiledb_datatype_t.TILEDB_DATETIME_MS,
281370
/// <summary>
282371
/// A date and time, counted as the signed 64-bit number of
283372
/// microseconds since the Unix epoch (January 1 1970 at midnight).
284373
/// </summary>
374+
/// <remarks>
375+
/// On generic methods operating to objects of this datatype,
376+
/// this datatype can be used with <see cref="long"/>.
377+
/// </remarks>
285378
DateTimeMicrosecond = tiledb_datatype_t.TILEDB_DATETIME_US,
286379
/// <summary>
287380
/// A date and time, counted as the signed 64-bit number of
288381
/// nanoseconds since the Unix epoch (January 1 1970 at midnight).
289382
/// </summary>
383+
/// <remarks>
384+
/// On generic methods operating to objects of this datatype,
385+
/// this datatype can be used with <see cref="long"/>.
386+
/// </remarks>
290387
DateTimeNanosecond = tiledb_datatype_t.TILEDB_DATETIME_NS,
291388
/// <summary>
292389
/// A date and time, counted as the signed 64-bit number of
293390
/// picoseconds since the Unix epoch (January 1 1970 at midnight).
294391
/// </summary>
295392
/// <remarks>
393+
/// <para>
296394
/// One second consists of one trillion picoseconds.
395+
/// </para>
396+
/// <para>
397+
/// On generic methods operating to objects of this datatype,
398+
/// this datatype can be used with <see cref="long"/>.
399+
/// </para>
297400
/// </remarks>
298401
DateTimePicosecond = tiledb_datatype_t.TILEDB_DATETIME_PS,
299402
/// <summary>
300403
/// A date and time, counted as the signed 64-bit number of
301404
/// femtoseconds since the Unix epoch (January 1 1970 at midnight).
302405
/// </summary>
303406
/// <remarks>
407+
/// <para>
304408
/// One second consists of one quadrillion femtoseconds.
409+
/// </para>
410+
/// <para>
411+
/// On generic methods operating to objects of this datatype,
412+
/// this datatype can be used with <see cref="long"/>.
413+
/// </para>
305414
/// </remarks>
306415
DateTimeFemtosecond = tiledb_datatype_t.TILEDB_DATETIME_FS,
307416
/// <summary>
308417
/// A date and time, counted as the signed 64-bit number of
309418
/// attoseconds since the Unix epoch (January 1 1970 at midnight).
310419
/// </summary>
311420
/// <remarks>
421+
/// <para>
312422
/// One second consists of one quintillion attoseconds.
423+
/// </para>
424+
/// <para>
425+
/// On generic methods operating to objects of this datatype,
426+
/// this datatype can be used with <see cref="long"/>.
427+
/// </para>
313428
/// </remarks>
314429
DateTimeAttosecond = tiledb_datatype_t.TILEDB_DATETIME_AS,
315430
/// <summary>
316431
/// A time of day counted in hours.
317432
/// </summary>
433+
/// <remarks>
434+
/// On generic methods operating to objects of this datatype,
435+
/// this datatype can be used with <see cref="long"/>.
436+
/// </remarks>
318437
TimeHour = tiledb_datatype_t.TILEDB_TIME_HR,
319438
/// <summary>
320439
/// A time of day counted in minutes.
321440
/// </summary>
441+
/// <remarks>
442+
/// On generic methods operating to objects of this datatype,
443+
/// this datatype can be used with <see cref="long"/>.
444+
/// </remarks>
322445
TimeMinute = tiledb_datatype_t.TILEDB_TIME_MIN,
323446
/// <summary>
324447
/// A time of day counted in seconds.
325448
/// </summary>
449+
/// <remarks>
450+
/// On generic methods operating to objects of this datatype,
451+
/// this datatype can be used with <see cref="long"/>.
452+
/// </remarks>
326453
TimeSecond = tiledb_datatype_t.TILEDB_TIME_SEC,
327454
/// <summary>
328455
/// A time of day counted in milliseconds.
329456
/// </summary>
457+
/// <remarks>
458+
/// On generic methods operating to objects of this datatype,
459+
/// this datatype can be used with <see cref="long"/>.
460+
/// </remarks>
330461
TimeMillisecond = tiledb_datatype_t.TILEDB_TIME_MS,
331462
/// <summary>
332463
/// A time of day counted in microseconds.
333464
/// </summary>
465+
/// <remarks>
466+
/// On generic methods operating to objects of this datatype,
467+
/// this datatype can be used with <see cref="long"/>.
468+
/// </remarks>
334469
TimeMicrosecond = tiledb_datatype_t.TILEDB_TIME_US,
335470
/// <summary>
336471
/// A time of day counted in nanoseconds.
337472
/// </summary>
473+
/// <remarks>
474+
/// On generic methods operating to objects of this datatype,
475+
/// this datatype can be used with <see cref="long"/>.
476+
/// </remarks>
338477
TimeNanosecond = tiledb_datatype_t.TILEDB_TIME_NS,
339478
/// <summary>
340479
/// A time of day counted in picoseconds.
@@ -349,12 +488,20 @@ public enum DataType : uint
349488
/// </summary>
350489
TimeAttosecond = tiledb_datatype_t.TILEDB_TIME_AS,
351490
/// <summary>
352-
/// A binary blob.
491+
/// Binary data.
353492
/// </summary>
493+
/// <remarks>
494+
/// On generic methods operating to objects of this datatype,
495+
/// this datatype can be used with <see cref="byte"/>.
496+
/// </remarks>
354497
Blob = tiledb_datatype_t.TILEDB_BLOB,
355498
/// <summary>
356499
/// A boolean value.
357500
/// </summary>
501+
/// <remarks>
502+
/// On generic methods operating to objects of this datatype,
503+
/// this datatype can be used with <see cref="byte"/> or <see cref="bool"/>.
504+
/// </remarks>
358505
Boolean = tiledb_datatype_t.TILEDB_BOOL
359506
}
360507

@@ -1025,6 +1172,15 @@ public static DataType TypeToDataType(Type t)
10251172
}
10261173
}
10271174

1175+
/// <summary>
1176+
/// Gets the corresponding <see cref="Type"/> of a <see cref="DataType"/>.
1177+
/// </summary>
1178+
/// <param name="datatype">The datatype to convert.</param>
1179+
/// <remarks>
1180+
/// Some data types like <see cref="DataType.Boolean"/> and <see cref="DataType.StringUtf16"/>
1181+
/// correspond to both a numeric type like <see cref="ushort"/> and a non-numeric type like <see cref="char"/>.
1182+
/// This method will return the numeric type.
1183+
/// </remarks>
10281184
public static Type DataTypeToType(DataType datatype)
10291185
{
10301186
switch (datatype)
@@ -1056,10 +1212,12 @@ public static Type DataTypeToType(DataType datatype)
10561212
case DataType.Int8:
10571213
return typeof(sbyte);
10581214
case DataType.StringAscii:
1215+
case DataType.StringUtf8:
1216+
return typeof(byte);
10591217
case DataType.StringUtf16:
1218+
return typeof(ushort);
10601219
case DataType.StringUtf32:
1061-
case DataType.StringUtf8:
1062-
return typeof(sbyte);
1220+
return typeof(uint);
10631221
case DataType.TimeAttosecond:
10641222
case DataType.TimeFemtosecond:
10651223
case DataType.TimeHour:
@@ -1081,7 +1239,7 @@ public static Type DataTypeToType(DataType datatype)
10811239
case DataType.Blob:
10821240
return typeof(byte);
10831241
case DataType.Boolean:
1084-
return typeof(byte);
1242+
return typeof(bool);
10851243
default:
10861244
return typeof(byte);
10871245
}

0 commit comments

Comments
 (0)