Skip to content

Commit ebdf72c

Browse files
Merge pull request #157 from objectbox/type-support
Type support
2 parents 23f30c2 + e552b48 commit ebdf72c

File tree

17 files changed

+584
-122
lines changed

17 files changed

+584
-122
lines changed

generator/integration-tests/basics/1.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,22 @@ void main() {
3535
expect(entity(model, 'D').flags, equals(OBXEntityFlags.SYNC_ENABLED));
3636
expect(entity(jsonModel, 'D').flags, equals(OBXEntityFlags.SYNC_ENABLED));
3737
});
38+
39+
test('types', () {
40+
expect(property(model, 'T.tBool').type, OBXPropertyType.Bool);
41+
expect(property(model, 'T.tByte').type, OBXPropertyType.Byte);
42+
expect(property(model, 'T.tShort').type, OBXPropertyType.Short);
43+
expect(property(model, 'T.tChar').type, OBXPropertyType.Char);
44+
expect(property(model, 'T.tInt').type, OBXPropertyType.Int);
45+
expect(property(model, 'T.tLong').type, OBXPropertyType.Long);
46+
expect(property(model, 'T.tFloat').type, OBXPropertyType.Float);
47+
expect(property(model, 'T.tDouble').type, OBXPropertyType.Double);
48+
expect(property(model, 'T.tString').type, OBXPropertyType.String);
49+
expect(property(model, 'T.tDate').type, OBXPropertyType.Date);
50+
expect(property(model, 'T.tDateNano').type, OBXPropertyType.DateNano);
51+
expect(property(model, 'T.tListInt').type, OBXPropertyType.ByteVector);
52+
expect(property(model, 'T.tInt8List').type, OBXPropertyType.ByteVector);
53+
expect(property(model, 'T.tUint8List').type, OBXPropertyType.ByteVector);
54+
expect(property(model, 'T.tListString').type, OBXPropertyType.StringVector);
55+
});
3856
}

generator/integration-tests/basics/lib/lib.dart

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:typed_data';
2+
13
import 'package:objectbox/objectbox.dart';
24
import 'objectbox.g.dart';
35
export 'other.dart';
@@ -27,3 +29,51 @@ class D {
2729

2830
D();
2931
}
32+
33+
@Entity()
34+
class T {
35+
@Id()
36+
int id;
37+
38+
// implicit PropertyType.bool
39+
bool tBool;
40+
41+
@Property(type: PropertyType.byte)
42+
int tByte;
43+
44+
@Property(type: PropertyType.short)
45+
int tShort;
46+
47+
@Property(type: PropertyType.char)
48+
int tChar;
49+
50+
@Property(type: PropertyType.int)
51+
int tInt;
52+
53+
// implicit PropertyType.long
54+
int tLong;
55+
56+
@Property(type: PropertyType.float)
57+
double tFloat;
58+
59+
// implicit PropertyType.double
60+
double tDouble;
61+
62+
// implicitly determined types
63+
String tString;
64+
65+
@Property(type: PropertyType.date)
66+
int tDate;
67+
68+
@Property(type: PropertyType.dateNano)
69+
int tDateNano;
70+
71+
@Property(type: PropertyType.byteVector)
72+
List<int> tListInt; // truncates int to 8-bits
73+
74+
Int8List tInt8List;
75+
76+
Uint8List tUint8List;
77+
78+
List<String> tListString;
79+
}

generator/lib/src/code_builder.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class CodeBuilder extends Builder {
140140
propInModel.name = prop.name;
141141
propInModel.type = prop.type;
142142
propInModel.flags = prop.flags;
143+
propInModel.dartFieldType = prop.dartFieldType;
143144

144145
if (!prop.hasIndexFlag()) {
145146
propInModel.removeIndex();

generator/lib/src/code_chunks.dart

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import "dart:convert";
2-
import "package:objectbox/src/modelinfo/index.dart";
3-
import "package:objectbox/src/bindings/bindings.dart" show OBXPropertyType;
4-
import "package:source_gen/source_gen.dart" show InvalidGenerationSourceError;
1+
import 'dart:convert';
2+
import 'package:objectbox/src/modelinfo/index.dart';
3+
import 'package:objectbox/src/bindings/bindings.dart';
4+
import 'package:source_gen/source_gen.dart' show InvalidGenerationSourceError;
55

66
class CodeChunks {
77
static String objectboxDart(ModelInfo model, List<String> imports) => """
88
// GENERATED CODE - DO NOT MODIFY BY HAND
99
1010
// Currently loading model from "JSON" which always encodes with double quotes
1111
// ignore_for_file: prefer_single_quotes
12-
12+
${typedDataImportIfNeeded(model)}
1313
import 'package:objectbox/objectbox.dart';
1414
export 'package:objectbox/objectbox.dart'; // so that callers only have to import this file
1515
import '${imports.join("';\n import '")}';
@@ -36,13 +36,34 @@ class CodeChunks {
3636
},
3737
writer: (Map<String, dynamic> members) {
3838
final r = $name();
39-
${entity.properties.map((p) => "r.${p.name} = members['${p.name}'];").join()}
39+
${entity.properties.map(propertyBinding).join()}
4040
return r;
4141
}
4242
)
4343
""";
4444
}
4545

46+
static String typedDataImportIfNeeded(ModelInfo model) {
47+
if (model.entities
48+
.any((ModelEntity entity) => entity.properties.any(isTypedDataList))) {
49+
return "import 'dart:typed_data';\n";
50+
}
51+
return '';
52+
}
53+
54+
static bool isTypedDataList(ModelProperty property) {
55+
return (property.dartFieldType == 'Uint8List' ||
56+
property.dartFieldType == 'Int8List');
57+
}
58+
59+
static String propertyBinding(ModelProperty property) {
60+
if (isTypedDataList(property)) {
61+
return "r.${property.name} = members['${property.name}'] == null ? null : ${property.dartFieldType}.fromList(members['${property.name}']);";
62+
} else {
63+
return "r.${property.name} = members['${property.name}'];";
64+
}
65+
}
66+
4667
static String _queryConditionBuilder(ModelEntity entity) {
4768
final ret = <String>[];
4869
for (var prop in entity.properties) {
@@ -52,46 +73,48 @@ class CodeChunks {
5273
String fieldType;
5374
switch (prop.type) {
5475
case OBXPropertyType.Bool:
55-
fieldType = "Boolean";
76+
fieldType = 'Boolean';
5677
break;
5778
case OBXPropertyType.String:
58-
fieldType = "String";
59-
break;
60-
float:
61-
case OBXPropertyType.Double:
62-
fieldType = "Double";
79+
fieldType = 'String';
6380
break;
6481
case OBXPropertyType.Float:
65-
continue float;
66-
integer:
67-
case OBXPropertyType.Int:
68-
fieldType = "Integer";
82+
case OBXPropertyType.Double:
83+
fieldType = 'Double';
6984
break;
7085
case OBXPropertyType.Byte:
71-
continue integer;
7286
case OBXPropertyType.Short:
73-
continue integer;
7487
case OBXPropertyType.Char:
75-
continue integer;
88+
case OBXPropertyType.Int:
7689
case OBXPropertyType.Long:
77-
continue integer;
90+
case OBXPropertyType.Date:
91+
case OBXPropertyType.DateNano:
92+
case OBXPropertyType.Relation:
93+
fieldType = 'Integer';
94+
break;
95+
case OBXPropertyType.ByteVector:
96+
fieldType = 'ByteVector';
97+
break;
98+
case OBXPropertyType.StringVector:
99+
fieldType = 'StringVector';
100+
break;
78101
default:
79102
throw InvalidGenerationSourceError(
80-
"Unsupported property type (${prop.type}): ${entity.name}.${name}");
103+
'Unsupported property type (${prop.type}): ${entity.name}.${name}');
81104
}
82105

83-
ret.add("""
84-
static final ${name} = Query${fieldType}Property(entityId:${entity.id.id}, propertyId:${prop.id.id}, obxType:${prop.type});
85-
""");
106+
ret.add('''
107+
static final ${prop.name} = Query${fieldType}Property(entityId:${entity.id.id}, propertyId:${prop.id.id}, obxType:${prop.type});
108+
''');
86109
}
87110
return ret.join();
88111
}
89112

90113
static String queryConditionClasses(ModelEntity entity) {
91114
// TODO add entity.id check to throw an error Box if the wrong entity.property is used
92-
return """
115+
return '''
93116
class ${entity.name}_ {
94117
${_queryConditionBuilder(entity)}
95-
}""";
118+
}''';
96119
}
97120
}

0 commit comments

Comments
 (0)