From 04ca8cd1e181221b9135d4beaa74f769457e6d55 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Mon, 27 Jun 2022 12:04:45 +1200 Subject: [PATCH] Add helper methods for boolean arrays --- lib/src/dbus_dart_type.dart | 4 ++++ lib/src/dbus_value.dart | 15 +++++++++++++++ test/dbus_test.dart | 12 +++++++++++- .../method-multiple-inputs.client.out | 2 +- .../method-multiple-inputs.server.out | 2 +- .../signal-multiple-args.client.out | 2 +- .../signal-multiple-args.server.out | 2 +- 7 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/src/dbus_dart_type.dart b/lib/src/dbus_dart_type.dart index b4c89cc..ed7507c 100644 --- a/lib/src/dbus_dart_type.dart +++ b/lib/src/dbus_dart_type.dart @@ -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': @@ -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': diff --git a/lib/src/dbus_value.dart b/lib/src/dbus_value.dart index 73e53b2..c834b81 100644 --- a/lib/src/dbus_value.dart +++ b/lib/src/dbus_value.dart @@ -92,6 +92,9 @@ abstract class DBusValue { /// Extracts the bytes inside this array. Only works if [signature] is 'ay'. Iterable asByteArray() => (this as DBusArray).mapByte(); + /// Extracts the boolean values inside this array. Only works if [signature] is 'ab'. + Iterable asBooleanArray() => (this as DBusArray).mapBoolean(); + /// Extracts the 16 bit signed integers inside this array. Only works if [signature] is 'an'. Iterable asInt16Array() => (this as DBusArray).mapInt16(); @@ -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 values) { + return DBusArray( + DBusSignature('b'), values.map((value) => DBusBoolean(value))); + } + /// Creates a new array of signed 16 bit values. factory DBusArray.int16(Iterable values) { return DBusArray( @@ -990,6 +999,9 @@ class DBusArray extends DBusValue { /// Maps the contents of this array into native types. Only works if [childSignature] is 'y'. Iterable mapByte() => children.map((value) => value.asByte()); + /// Maps the contents of this array into native types. Only works if [childSignature] is 'b'. + Iterable mapBoolean() => children.map((value) => value.asBoolean()); + /// Maps the contents of this array into native types. Only works if [childSignature] is 'n'. Iterable mapInt16() => children.map((value) => value.asInt16()); @@ -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])'; diff --git a/test/dbus_test.dart b/test/dbus_test.dart index 333b625..2ce1bb3 100644 --- a/test/dbus_test.dart +++ b/test/dbus_test.dart @@ -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( @@ -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(), @@ -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]), diff --git a/test/generated-code/method-multiple-inputs.client.out b/test/generated-code/method-multiple-inputs.client.out index d8c739b..ab4153c 100644 --- a/test/generated-code/method-multiple-inputs.client.out +++ b/test/generated-code/method-multiple-inputs.client.out @@ -5,6 +5,6 @@ class ComExampleTest extends DBusRemoteObject { /// Invokes com.example.Test.Hello() Future 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 array_value_y, List array_value_b, List array_value_n, List array_value_q, List array_value_u, List array_value_n, List array_value_x, List array_value_t, List array_value_d, List array_value_s, List array_value_o, List array_value_g, List array_value_v, List array_value_struct, List> array_value_array, Map dict_value_sv, Map dict_value_us, Map> 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); } } diff --git a/test/generated-code/method-multiple-inputs.server.out b/test/generated-code/method-multiple-inputs.server.out index eacc636..b33a909 100644 --- a/test/generated-code/method-multiple-inputs.server.out +++ b/test/generated-code/method-multiple-inputs.server.out @@ -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(); } diff --git a/test/generated-code/signal-multiple-args.client.out b/test/generated-code/signal-multiple-args.client.out index 4ffb2a8..bb908e0 100644 --- a/test/generated-code/signal-multiple-args.client.out +++ b/test/generated-code/signal-multiple-args.client.out @@ -17,7 +17,7 @@ class ComExampleTestEvent extends DBusSignal { DBusValue get variant_value => values[12].asVariant(); DBusStruct get struct_value => values[13].asStruct(); List get array_value_y => values[14].asByteArray().toList(); - List get array_value_b => values[15].asArray().map((child) => child.asBoolean()).toList(); + List get array_value_b => values[15].asBooleanArray().toList(); List get array_value_n => values[16].asInt16Array().toList(); List get array_value_q => values[17].asUint16Array().toList(); List get array_value_u => values[18].asInt32Array().toList(); diff --git a/test/generated-code/signal-multiple-args.server.out b/test/generated-code/signal-multiple-args.server.out index 806c07f..1e2211f 100644 --- a/test/generated-code/signal-multiple-args.server.out +++ b/test/generated-code/signal-multiple-args.server.out @@ -6,7 +6,7 @@ class ComExampleTest extends DBusObject { /// Emits signal com.example.Test.Event Future 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 array_value_y, List array_value_b, List array_value_n, List array_value_q, List array_value_u, List array_value_n_, List array_value_x, List array_value_t, List array_value_d, List array_value_s, List array_value_o, List array_value_g, List array_value_v, List array_value_struct, List> array_value_array, Map dict_value_sv, Map dict_value_us, Map> 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