Skip to content

Commit 3749a17

Browse files
RSDK-9505: accept bson queries in mql function (viamrobotics#318)
* change query to be dynamic * add comment * change test to use list of maps
1 parent 8fc978f commit 3749a17

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

lib/src/app/data.dart

+11-3
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,21 @@ class DataClient {
152152
return response.rawData.map((e) => BsonCodec.deserialize(BsonBinary.from(e))).toList();
153153
}
154154

155-
/// Obtain unified tabular data and metadata, queried with MQL.
155+
/// Obtain unified tabular data and metadata, queried with MQL. The query should be of type List<Map<String, dynamic>>.
156156
///
157157
/// For more information, see [Data Client API](https://docs.viam.com/appendix/apis/data-client/).
158-
Future<List<Map<String, dynamic>>> tabularDataByMql(String organizationId, List<Uint8List> query) async {
158+
Future<List<Map<String, dynamic>>> tabularDataByMql(String organizationId, dynamic query) async {
159+
List<Uint8List> binary;
160+
if (query is List<Map<String, dynamic>>) {
161+
binary = query.map((q) => BsonCodec.serialize(q).byteList).toList();
162+
} else if (query is List<Uint8List>) {
163+
binary = query;
164+
} else {
165+
throw TypeError();
166+
}
159167
final request = TabularDataByMQLRequest()
160168
..organizationId = organizationId
161-
..mqlBinary.addAll(query);
169+
..mqlBinary.addAll(binary);
162170
final response = await _dataClient.tabularDataByMQL(request);
163171
return response.rawData.map((e) => BsonCodec.deserialize(BsonBinary.from(e))).toList();
164172
}

test/unit_test/app/data_client_test.dart

+4-1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ void main() {
162162
});
163163

164164
test('tabularDataByMql', () async {
165+
final List<Map<String, dynamic>> query = [
166+
{'key': 'value'}
167+
];
165168
final startDate = DateTime.utc(2020, 12, 31);
166169
final List<Map<String, dynamic>> data = [
167170
{
@@ -175,7 +178,7 @@ void main() {
175178
when(serviceClient.tabularDataByMQL(any)).thenAnswer(
176179
(_) => MockResponseFuture.value(TabularDataByMQLResponse()..rawData.addAll(data.map((e) => BsonCodec.serialize(e).byteList))));
177180

178-
final response = await dataClient.tabularDataByMql('some_org_id', [Uint8List.fromList('some_query'.codeUnits)]);
181+
final response = await dataClient.tabularDataByMql('some_org_id', query);
179182
expect(response[0]['key1'], equals(data[0]['key1']));
180183
expect(response, equals(data));
181184
});

0 commit comments

Comments
 (0)