Skip to content

Commit 736ce01

Browse files
authored
Merge pull request #140 from BabyGeek/feature/nested-fields
Nested fields and object type.
2 parents 676e3b4 + 7a7a7bd commit 736ce01

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

lib/src/models/field.dart

+2
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ enum Type {
248248
auto,
249249
stringify,
250250
geopoint,
251+
object,
251252
}
252253

253254
extension _Type on Type {
@@ -259,6 +260,7 @@ extension _Type on Type {
259260
case Type.float:
260261
case Type.bool:
261262
case Type.geopoint:
263+
case Type.object:
262264
final description = toString(),
263265
indexOfDot = description.indexOf('.'),
264266
value = description.substring(indexOfDot + 1);

lib/src/models/schema.dart

+9
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ class Schema extends BaseSchema {
2828
/// searching.
2929
final Field? defaultSortingField;
3030

31+
/// Boolean to enable nested fields on the schema, only available for typesense 0.24 or more
32+
final bool? enableNestedFields;
33+
3134
Schema(
3235
this.name,
3336
super.fields, {
3437
this.defaultSortingField,
3538
this.documentCount,
39+
this.enableNestedFields,
3640
});
3741

3842
factory Schema.fromMap(Map<String, dynamic> map) {
@@ -54,12 +58,14 @@ class Schema extends BaseSchema {
5458
),
5559
)
5660
: null;
61+
final bool? enableNestedFields = map["enable_nested_fields"];
5762

5863
return Schema(
5964
map['name'],
6065
fields,
6166
documentCount: map['num_documents'] ?? 0,
6267
defaultSortingField: defaultSortingField,
68+
enableNestedFields: enableNestedFields,
6369
);
6470
}
6571

@@ -73,6 +79,9 @@ class Schema extends BaseSchema {
7379
if (documentCount != null) {
7480
map['num_documents'] = documentCount;
7581
}
82+
if (enableNestedFields != null) {
83+
map['enable_nested_fields'] = enableNestedFields;
84+
}
7685
return map;
7786
}
7887
}

test/models/field_test.dart

+14
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,20 @@ void main() {
242242
});
243243
expect(field.type, equals(Type.geopoint));
244244
expect(field.isMultivalued, isTrue);
245+
246+
field = Field.fromMap({
247+
"name": "name",
248+
"type": "object",
249+
});
250+
expect(field.type, equals(Type.object));
251+
expect(field.isMultivalued, isFalse);
252+
253+
field = Field.fromMap({
254+
"name": "name",
255+
"type": "object[]",
256+
});
257+
expect(field.type, equals(Type.object));
258+
expect(field.isMultivalued, isTrue);
245259
});
246260
test('sets default values to fields when null', () {
247261
final field = Field.fromMap({"name": "num_employees", "type": "int32"});

test/models/schema_test.dart

+33
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ void main() {
1515
},
1616
defaultSortingField: Field('num_employees'),
1717
documentCount: 0,
18+
enableNestedFields: true,
1819
);
1920
s2 = Schema.fromMap({
2021
"name": "companies",
@@ -25,6 +26,7 @@ void main() {
2526
],
2627
"default_sorting_field": "num_employees",
2728
"num_documents": 0,
29+
"enable_nested_fields": true,
2830
});
2931
});
3032

@@ -54,6 +56,10 @@ void main() {
5456
expect(s2.defaultSortingField,
5557
equals(Field('num_employees', type: Type.int32)));
5658
});
59+
test('has a enableNestedFields field', () {
60+
expect(s1.enableNestedFields, equals(true));
61+
expect(s2.enableNestedFields, equals(true));
62+
});
5763
test('has a toMap method', () {
5864
final map = {
5965
"name": "companies",
@@ -64,6 +70,7 @@ void main() {
6470
],
6571
"default_sorting_field": "num_employees",
6672
"num_documents": 0,
73+
"enable_nested_fields": true,
6774
};
6875

6976
expect(s1.toMap(), equals(map));
@@ -151,6 +158,32 @@ void main() {
151158
),
152159
);
153160
});
161+
test("with null/empty enableNestedFields is successful", () {
162+
var schema = Schema.fromMap({
163+
"name": "companies",
164+
"fields": [
165+
{"name": "company_name", "type": "string"},
166+
{"name": "num_employees", "type": "int32"},
167+
{"name": "country", "type": "string", "facet": true}
168+
],
169+
"default_sorting_field": "",
170+
"num_documents": 0,
171+
});
172+
expect(schema.name, equals('companies'));
173+
expect(schema.enableNestedFields, isNull);
174+
175+
schema = Schema.fromMap({
176+
"name": "companies",
177+
"fields": [
178+
{"name": "company_name", "type": "string"},
179+
{"name": "num_employees", "type": "int32"},
180+
{"name": "country", "type": "string", "facet": true}
181+
],
182+
"num_documents": 0,
183+
});
184+
expect(schema.name, equals('companies'));
185+
expect(schema.enableNestedFields, isNull);
186+
});
154187
});
155188

156189
group('UpdateSchema', () {

0 commit comments

Comments
 (0)