Skip to content

Commit

Permalink
Add helper methods for boolean arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-ancell committed Jun 27, 2022
1 parent 544b44f commit 04ca8cd
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/src/dbus_dart_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ class DBusArrayType extends DBusDartType {
switch (childSignature.value) {
case 'y':
return 'DBusArray.byte($name)';
case 'b':
return 'DBusArray.boolean($name)';
case 'n':
return 'DBusArray.int16($name)';
case 'q':
Expand Down Expand Up @@ -328,6 +330,8 @@ class DBusArrayType extends DBusDartType {
switch (childSignature.value) {
case 'y':
return '$name.asByteArray().toList()';
case 'b':
return '$name.asBooleanArray().toList()';
case 'n':
return '$name.asInt16Array().toList()';
case 'q':
Expand Down
15 changes: 15 additions & 0 deletions lib/src/dbus_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ abstract class DBusValue {
/// Extracts the bytes inside this array. Only works if [signature] is 'ay'.
Iterable<int> asByteArray() => (this as DBusArray).mapByte();

/// Extracts the boolean values inside this array. Only works if [signature] is 'ab'.
Iterable<bool> asBooleanArray() => (this as DBusArray).mapBoolean();

/// Extracts the 16 bit signed integers inside this array. Only works if [signature] is 'an'.
Iterable<int> asInt16Array() => (this as DBusArray).mapInt16();

Expand Down Expand Up @@ -913,6 +916,12 @@ class DBusArray extends DBusValue {
DBusSignature('y'), values.map((value) => DBusByte(value)));
}

/// Creates a new array of boolean values.
factory DBusArray.boolean(Iterable<bool> values) {
return DBusArray(
DBusSignature('b'), values.map((value) => DBusBoolean(value)));
}

/// Creates a new array of signed 16 bit values.
factory DBusArray.int16(Iterable<int> values) {
return DBusArray(
Expand Down Expand Up @@ -990,6 +999,9 @@ class DBusArray extends DBusValue {
/// Maps the contents of this array into native types. Only works if [childSignature] is 'y'.
Iterable<int> mapByte() => children.map((value) => value.asByte());

/// Maps the contents of this array into native types. Only works if [childSignature] is 'b'.
Iterable<bool> mapBoolean() => children.map((value) => value.asBoolean());

/// Maps the contents of this array into native types. Only works if [childSignature] is 'n'.
Iterable<int> mapInt16() => children.map((value) => value.asInt16());

Expand Down Expand Up @@ -1041,6 +1053,9 @@ class DBusArray extends DBusValue {
case 'y':
var values = children.map((child) => child.asByte()).join(', ');
return 'DBusArray.byte([$values])';
case 'b':
var values = children.map((child) => child.asBoolean()).join(', ');
return 'DBusArray.boolean([$values])';
case 'n':
var values = children.map((child) => child.asInt16()).join(', ');
return 'DBusArray.int16([$values])';
Expand Down
12 changes: 11 additions & 1 deletion test/dbus_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,10 @@ void main() {
DBusArray.byte([1, 2, 3]),
equals(DBusArray(
DBusSignature('y'), [DBusByte(1), DBusByte(2), DBusByte(3)])));
expect(
DBusArray.boolean([false, true]),
equals(DBusArray(
DBusSignature('b'), [DBusBoolean(false), DBusBoolean(true)])));
expect(
DBusArray.int16([1, 2, -3]),
equals(DBusArray(
Expand Down Expand Up @@ -771,6 +775,12 @@ void main() {
DBusArray.byte([1, 2, 3])
]).toString(),
equals("DBusArray(DBusSignature('ay'), [DBusArray.byte([1, 2, 3])])"));
expect(
DBusArray(DBusSignature('ab'), [
DBusArray.boolean([false, true])
]).toString(),
equals(
"DBusArray(DBusSignature('ab'), [DBusArray.boolean([false, true])])"));
expect(DBusArray.byte([1, 2, 3]).toString(),
equals('DBusArray.byte([1, 2, 3])'));
expect(DBusArray.int16([1, 2, -3]).toString(),
Expand Down Expand Up @@ -2593,7 +2603,7 @@ void main() {
DBusVariant(DBusString('variant')),
DBusStruct([]),
DBusArray.byte([0, 1, 255]),
DBusArray(DBusSignature('b'), [DBusBoolean(false), DBusBoolean(true)]),
DBusArray.boolean([false, true]),
DBusArray.int16([0, 1, -32768, 32767]),
DBusArray.uint16([0, 1, 65535]),
DBusArray.int32([0, 1, -2147483648, 2147483647]),
Expand Down
2 changes: 1 addition & 1 deletion test/generated-code/method-multiple-inputs.client.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ class ComExampleTest extends DBusRemoteObject {

/// Invokes com.example.Test.Hello()
Future<void> callHello(int byte_value, bool boolean_value, int int16_value, int uint16_value, int int32_value, int uint32_value, int int64_value, int uint64_value, double double_value, String string_value, String object_path_value, DBusSignature signature_value, DBusValue variant_value, DBusStruct struct_value, List<int> array_value_y, List<bool> array_value_b, List<int> array_value_n, List<int> array_value_q, List<int> array_value_u, List<int> array_value_n, List<int> array_value_x, List<int> array_value_t, List<double> array_value_d, List<String> array_value_s, List<String> array_value_o, List<DBusSignature> array_value_g, List<DBusValue> array_value_v, List<DBusStruct> array_value_struct, List<List<String>> array_value_array, Map<String, DBusValue> dict_value_sv, Map<int, String> dict_value_us, Map<String, Map<String, DBusValue>> dict_value_dict, {bool noAutoStart = false, bool allowInteractiveAuthorization = false}) async {
await callMethod('com.example.Test', 'Hello', [DBusByte(byte_value), DBusBoolean(boolean_value), DBusInt16(int16_value), DBusUint16(uint16_value), DBusInt32(int32_value), DBusUint32(uint32_value), DBusInt64(int64_value), DBusUint64(uint64_value), DBusDouble(double_value), DBusString(string_value), DBusObjectPath(object_path_value), signature_value, DBusVariant(variant_value), struct_value, DBusArray.byte(array_value_y), DBusArray(DBusSignature('b'), array_value_b.map((child) => DBusBoolean(child))), DBusArray.int16(array_value_n), DBusArray.uint16(array_value_q), DBusArray.int32(array_value_u), DBusArray.uint32(array_value_n), DBusArray.int64(array_value_x), DBusArray.uint64(array_value_t), DBusArray.double(array_value_d), DBusArray.string(array_value_s), DBusArray.objectPath(array_value_o), DBusArray.signature(array_value_g), DBusArray.variant(array_value_v), DBusArray(DBusSignature('(si)'), array_value_struct.map((child) => child)), DBusArray(DBusSignature('as'), array_value_array.map((child) => DBusArray.string(child))), DBusDict.stringVariant(dict_value_sv), DBusDict(DBusSignature('u'), DBusSignature('s'), dict_value_us.map((key, value) => MapEntry(DBusUint32(key), DBusString(value)))), DBusDict(DBusSignature('s'), DBusSignature('a{sv}'), dict_value_dict.map((key, value) => MapEntry(DBusString(key), DBusDict.stringVariant(value))))], replySignature: DBusSignature(''), noAutoStart: noAutoStart, allowInteractiveAuthorization: allowInteractiveAuthorization);
await callMethod('com.example.Test', 'Hello', [DBusByte(byte_value), DBusBoolean(boolean_value), DBusInt16(int16_value), DBusUint16(uint16_value), DBusInt32(int32_value), DBusUint32(uint32_value), DBusInt64(int64_value), DBusUint64(uint64_value), DBusDouble(double_value), DBusString(string_value), DBusObjectPath(object_path_value), signature_value, DBusVariant(variant_value), struct_value, DBusArray.byte(array_value_y), DBusArray.boolean(array_value_b), DBusArray.int16(array_value_n), DBusArray.uint16(array_value_q), DBusArray.int32(array_value_u), DBusArray.uint32(array_value_n), DBusArray.int64(array_value_x), DBusArray.uint64(array_value_t), DBusArray.double(array_value_d), DBusArray.string(array_value_s), DBusArray.objectPath(array_value_o), DBusArray.signature(array_value_g), DBusArray.variant(array_value_v), DBusArray(DBusSignature('(si)'), array_value_struct.map((child) => child)), DBusArray(DBusSignature('as'), array_value_array.map((child) => DBusArray.string(child))), DBusDict.stringVariant(dict_value_sv), DBusDict(DBusSignature('u'), DBusSignature('s'), dict_value_us.map((key, value) => MapEntry(DBusUint32(key), DBusString(value)))), DBusDict(DBusSignature('s'), DBusSignature('a{sv}'), dict_value_dict.map((key, value) => MapEntry(DBusString(key), DBusDict.stringVariant(value))))], replySignature: DBusSignature(''), noAutoStart: noAutoStart, allowInteractiveAuthorization: allowInteractiveAuthorization);
}
}
2 changes: 1 addition & 1 deletion test/generated-code/method-multiple-inputs.server.out
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ComExampleTest extends DBusObject {
if (methodCall.signature != DBusSignature('ybnqiuxtdsogv(si)ayabanaqaiauaxatadasaoagava(si)aasa{sv}a{us}a{sa{sv}}')) {
return DBusMethodErrorResponse.invalidArgs();
}
return doHello(methodCall.values[0].asByte(), methodCall.values[1].asBoolean(), methodCall.values[2].asInt16(), methodCall.values[3].asUint16(), methodCall.values[4].asInt32(), methodCall.values[5].asUint32(), methodCall.values[6].asInt64(), methodCall.values[7].asUint64(), methodCall.values[8].asDouble(), methodCall.values[9].asString(), methodCall.values[10].asObjectPath(), methodCall.values[11].asSignature(), methodCall.values[12].asVariant(), methodCall.values[13].asStruct(), methodCall.values[14].asByteArray().toList(), methodCall.values[15].asArray().map((child) => child.asBoolean()).toList(), methodCall.values[16].asInt16Array().toList(), methodCall.values[17].asUint16Array().toList(), methodCall.values[18].asInt32Array().toList(), methodCall.values[19].asUint32Array().toList(), methodCall.values[20].asInt64Array().toList(), methodCall.values[21].asUint64Array().toList(), methodCall.values[22].asDoubleArray().toList(), methodCall.values[23].asStringArray().toList(), methodCall.values[24].asObjectPathArray().toList(), methodCall.values[25].asSignatureArray().toList(), methodCall.values[26].asVariantArray().toList(), methodCall.values[27].asArray().map((child) => child.asStruct()).toList(), methodCall.values[28].asArray().map((child) => child.asStringArray().toList()).toList(), methodCall.values[29].asStringVariantDict(), methodCall.values[30].asDict().map((key, value) => MapEntry(key.asUint32(), value.asString())), methodCall.values[31].asDict().map((key, value) => MapEntry(key.asString(), value.asStringVariantDict())));
return doHello(methodCall.values[0].asByte(), methodCall.values[1].asBoolean(), methodCall.values[2].asInt16(), methodCall.values[3].asUint16(), methodCall.values[4].asInt32(), methodCall.values[5].asUint32(), methodCall.values[6].asInt64(), methodCall.values[7].asUint64(), methodCall.values[8].asDouble(), methodCall.values[9].asString(), methodCall.values[10].asObjectPath(), methodCall.values[11].asSignature(), methodCall.values[12].asVariant(), methodCall.values[13].asStruct(), methodCall.values[14].asByteArray().toList(), methodCall.values[15].asBooleanArray().toList(), methodCall.values[16].asInt16Array().toList(), methodCall.values[17].asUint16Array().toList(), methodCall.values[18].asInt32Array().toList(), methodCall.values[19].asUint32Array().toList(), methodCall.values[20].asInt64Array().toList(), methodCall.values[21].asUint64Array().toList(), methodCall.values[22].asDoubleArray().toList(), methodCall.values[23].asStringArray().toList(), methodCall.values[24].asObjectPathArray().toList(), methodCall.values[25].asSignatureArray().toList(), methodCall.values[26].asVariantArray().toList(), methodCall.values[27].asArray().map((child) => child.asStruct()).toList(), methodCall.values[28].asArray().map((child) => child.asStringArray().toList()).toList(), methodCall.values[29].asStringVariantDict(), methodCall.values[30].asDict().map((key, value) => MapEntry(key.asUint32(), value.asString())), methodCall.values[31].asDict().map((key, value) => MapEntry(key.asString(), value.asStringVariantDict())));
} else {
return DBusMethodErrorResponse.unknownMethod();
}
Expand Down
2 changes: 1 addition & 1 deletion test/generated-code/signal-multiple-args.client.out
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ComExampleTestEvent extends DBusSignal {
DBusValue get variant_value => values[12].asVariant();
DBusStruct get struct_value => values[13].asStruct();
List<int> get array_value_y => values[14].asByteArray().toList();
List<bool> get array_value_b => values[15].asArray().map((child) => child.asBoolean()).toList();
List<bool> get array_value_b => values[15].asBooleanArray().toList();
List<int> get array_value_n => values[16].asInt16Array().toList();
List<int> get array_value_q => values[17].asUint16Array().toList();
List<int> get array_value_u => values[18].asInt32Array().toList();
Expand Down
2 changes: 1 addition & 1 deletion test/generated-code/signal-multiple-args.server.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ComExampleTest extends DBusObject {

/// Emits signal com.example.Test.Event
Future<void> emitEvent(int byte_value, bool boolean_value, int int16_value, int uint16_value, int int32_value, int uint32_value, int int64_value, int uint64_value, double double_value, String string_value, String object_path_value, DBusSignature signature_value, DBusValue variant_value, DBusStruct struct_value, List<int> array_value_y, List<bool> array_value_b, List<int> array_value_n, List<int> array_value_q, List<int> array_value_u, List<int> array_value_n_, List<int> array_value_x, List<int> array_value_t, List<double> array_value_d, List<String> array_value_s, List<String> array_value_o, List<DBusSignature> array_value_g, List<DBusValue> array_value_v, List<DBusStruct> array_value_struct, List<List<String>> array_value_array, Map<String, DBusValue> dict_value_sv, Map<int, String> dict_value_us, Map<String, Map<String, DBusValue>> dict_value_dict) async {
await emitSignal('com.example.Test', 'Event', [DBusByte(byte_value), DBusBoolean(boolean_value), DBusInt16(int16_value), DBusUint16(uint16_value), DBusInt32(int32_value), DBusUint32(uint32_value), DBusInt64(int64_value), DBusUint64(uint64_value), DBusDouble(double_value), DBusString(string_value), DBusObjectPath(object_path_value), signature_value, DBusVariant(variant_value), struct_value, DBusArray.byte(array_value_y), DBusArray(DBusSignature('b'), array_value_b.map((child) => DBusBoolean(child))), DBusArray.int16(array_value_n), DBusArray.uint16(array_value_q), DBusArray.int32(array_value_u), DBusArray.uint32(array_value_n_), DBusArray.int64(array_value_x), DBusArray.uint64(array_value_t), DBusArray.double(array_value_d), DBusArray.string(array_value_s), DBusArray.objectPath(array_value_o), DBusArray.signature(array_value_g), DBusArray.variant(array_value_v), DBusArray(DBusSignature('(si)'), array_value_struct.map((child) => child)), DBusArray(DBusSignature('as'), array_value_array.map((child) => DBusArray.string(child))), DBusDict.stringVariant(dict_value_sv), DBusDict(DBusSignature('u'), DBusSignature('s'), dict_value_us.map((key, value) => MapEntry(DBusUint32(key), DBusString(value)))), DBusDict(DBusSignature('s'), DBusSignature('a{sv}'), dict_value_dict.map((key, value) => MapEntry(DBusString(key), DBusDict.stringVariant(value))))]);
await emitSignal('com.example.Test', 'Event', [DBusByte(byte_value), DBusBoolean(boolean_value), DBusInt16(int16_value), DBusUint16(uint16_value), DBusInt32(int32_value), DBusUint32(uint32_value), DBusInt64(int64_value), DBusUint64(uint64_value), DBusDouble(double_value), DBusString(string_value), DBusObjectPath(object_path_value), signature_value, DBusVariant(variant_value), struct_value, DBusArray.byte(array_value_y), DBusArray.boolean(array_value_b), DBusArray.int16(array_value_n), DBusArray.uint16(array_value_q), DBusArray.int32(array_value_u), DBusArray.uint32(array_value_n_), DBusArray.int64(array_value_x), DBusArray.uint64(array_value_t), DBusArray.double(array_value_d), DBusArray.string(array_value_s), DBusArray.objectPath(array_value_o), DBusArray.signature(array_value_g), DBusArray.variant(array_value_v), DBusArray(DBusSignature('(si)'), array_value_struct.map((child) => child)), DBusArray(DBusSignature('as'), array_value_array.map((child) => DBusArray.string(child))), DBusDict.stringVariant(dict_value_sv), DBusDict(DBusSignature('u'), DBusSignature('s'), dict_value_us.map((key, value) => MapEntry(DBusUint32(key), DBusString(value)))), DBusDict(DBusSignature('s'), DBusSignature('a{sv}'), dict_value_dict.map((key, value) => MapEntry(DBusString(key), DBusDict.stringVariant(value))))]);
}

@override
Expand Down

0 comments on commit 04ca8cd

Please sign in to comment.