Skip to content

Commit 62047a2

Browse files
16-bit char: change PropertyType.char to a 16-bit unsigned integer #70
1 parent b584c3f commit 62047a2

File tree

7 files changed

+19
-19
lines changed

7 files changed

+19
-19
lines changed

generator/lib/src/code_chunks.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class CodeChunks {
254254
OBXPropertyType.Bool: 'Bool',
255255
OBXPropertyType.Byte: 'Int8',
256256
OBXPropertyType.Short: 'Int16',
257-
OBXPropertyType.Char: 'Int8',
257+
OBXPropertyType.Char: 'Uint16',
258258
OBXPropertyType.Int: 'Int32',
259259
OBXPropertyType.Long: 'Int64',
260260
OBXPropertyType.Float: 'Float32',
@@ -433,10 +433,7 @@ class CodeChunks {
433433
return readListCodeString(p, "int", OBXPropertyType.Byte);
434434
}
435435
case OBXPropertyType.CharVector:
436-
// OBXPropertyType.Char currently incorrectly mapped to Int8,
437-
// so explicitly use Uint16Reader for now.
438-
return readFieldCodeString(
439-
p, "fb.ListReader<int>(fb.Uint16Reader(), lazy: false)");
436+
return readListCodeString(p, "int", OBXPropertyType.Char);
440437
case OBXPropertyType.ShortVector:
441438
// FlatBuffers has Uint16ListReader, but it does not use Uint16List
442439
// internally. Use implementation of objectbox package.

objectbox/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
}
3535
```
3636
Note: for queries currently only the `isNull` and `notNull` conditions are supported.
37+
* Changed `PropertyType.char` from a 8-bit signed integer to a 16-bit unsigned integer to match the
38+
ObjectBox database type.
3739
* Fix put returning an incorrect error message in a rare case.
3840
* Require at least Dart SDK 2.16 (shipped with Flutter 2.10.0).
3941
* Let `Store.awaitQueueCompletion` actually wait on the async queue to become idle. It previously

objectbox/lib/src/annotations.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ enum PropertyType {
9999
/// size: 2-bytes/16-bits
100100
short,
101101

102-
/// size: 1-byte/8-bits
102+
/// Use with [Property.type] to store [int] as 2 bytes (16-bit unsigned integer).
103103
char,
104104

105105
/// size: 4-bytes/32-bits

objectbox/lib/src/modelinfo/enums.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ abstract class OBXPropertyType {
168168
/// < 2 bytes
169169
static const int Short = 3;
170170

171-
/// < 1 byte
171+
/// < 2 bytes
172172
static const int Char = 4;
173173

174174
/// < 4 bytes

objectbox/lib/src/native/query/property.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ extension IntegerPropertyQuery on PropertyQuery<int> {
122122
List<int> find({int? replaceNullWith}) {
123123
switch (_type) {
124124
case OBXPropertyType.Bool:
125-
case OBXPropertyType.Byte:
126-
case OBXPropertyType.Char: // Int8
125+
case OBXPropertyType.Byte: // Int8
127126
final cDefault = replaceNullWith == null
128127
? null
129128
: (malloc<Int8>()..value = replaceNullWith);
@@ -133,6 +132,7 @@ extension IntegerPropertyQuery on PropertyQuery<int> {
133132
(Pointer<OBX_int8_array> cItems) =>
134133
cItems.ref.items.asTypedList(cItems.ref.count).toList(),
135134
C.int8_array_free);
135+
case OBXPropertyType.Char:
136136
case OBXPropertyType.Short: // Int16
137137
final cDefault = replaceNullWith == null
138138
? null

objectbox_test/test/box_test.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -511,26 +511,27 @@ void main() {
511511
final int8Max = 127;
512512
final int16Min = -32768;
513513
final int16Max = 32767;
514+
final uint16Max = 65535;
514515
final int32Min = -2147483648;
515516
final int32Max = 2147483647;
516517
final int64Min = -9223372036854775808;
517518
final int64Max = 9223372036854775807;
518519
final List<TestEntity> items = [
519-
...[int8Min, int8Max].map((n) => TestEntity(tChar: n)).toList(),
520520
...[int8Min, int8Max].map((n) => TestEntity(tByte: n)).toList(),
521521
...[int16Min, int16Max].map((n) => TestEntity(tShort: n)).toList(),
522+
...[0, uint16Max].map((n) => TestEntity(tChar: n)).toList(),
522523
...[int32Min, int32Max].map((n) => TestEntity(tInt: n)).toList(),
523524
...[int64Min, int64Max].map((n) => TestEntity(tLong: n)).toList()
524525
];
525526
expect('${items[8].tLong}', equals('$int64Min'));
526527
expect('${items[9].tLong}', equals('$int64Max'));
527528
final List<TestEntity?> fetchedItems = box.getMany(box.putMany(items));
528-
expect(fetchedItems[0]!.tChar, equals(int8Min));
529-
expect(fetchedItems[1]!.tChar, equals(int8Max));
530-
expect(fetchedItems[2]!.tByte, equals(int8Min));
531-
expect(fetchedItems[3]!.tByte, equals(int8Max));
532-
expect(fetchedItems[4]!.tShort, equals(int16Min));
533-
expect(fetchedItems[5]!.tShort, equals(int16Max));
529+
expect(fetchedItems[0]!.tByte, equals(int8Min));
530+
expect(fetchedItems[1]!.tByte, equals(int8Max));
531+
expect(fetchedItems[2]!.tShort, equals(int16Min));
532+
expect(fetchedItems[3]!.tShort, equals(int16Max));
533+
expect(fetchedItems[4]!.tChar, equals(0));
534+
expect(fetchedItems[5]!.tChar, equals(uint16Max));
534535
expect(fetchedItems[6]!.tInt, equals(int32Min));
535536
expect(fetchedItems[7]!.tInt, equals(int32Max));
536537
expect(fetchedItems[8]!.tLong, equals(int64Min));
@@ -634,7 +635,7 @@ void main() {
634635
tBool: true,
635636
tByte: 123,
636637
tShort: -4567,
637-
tChar: 'x'.codeUnitAt(0),
638+
tChar: 'Ā'.codeUnitAt(0), // U+0100
638639
tInt: 789012,
639640
tFloat: -2.71);
640641
final fetchedItem = box.get(box.put(item))!;
@@ -644,7 +645,7 @@ void main() {
644645
expect(fetchedItem.tBool, equals(true));
645646
expect(fetchedItem.tByte, equals(123));
646647
expect(fetchedItem.tShort, equals(-4567));
647-
expect(fetchedItem.tChar, equals('x'.codeUnitAt(0)));
648+
expect(fetchedItem.tChar, equals('Ā'.codeUnitAt(0)));
648649
expect(fetchedItem.tInt, equals(789012));
649650
expect((fetchedItem.tFloat! - (-2.71)).abs(), lessThan(0.0000001));
650651
});

objectbox_test/test/entity.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class TestEntity {
4141
@Property(type: PropertyType.short)
4242
int? tShort;
4343

44-
// OBXPropertyType.Char | 1 byte
44+
// OBXPropertyType.Char | 16-bit unsigned integer
4545
@Property(type: PropertyType.char)
4646
int? tChar;
4747

0 commit comments

Comments
 (0)