Unverified Commit c4016aad authored by adazh's avatar adazh Committed by GitHub

[Flutter Driver] Simplified the serialization/deserialization logic of the Descendant/… (#40715)

* Simplified the serialization/deserialization logic of the Descendant/Ancestor matchers
parent d95adf99
......@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:convert';
import 'package:meta/meta.dart';
import 'error.dart';
......@@ -338,9 +340,9 @@ class Descendant extends SerializableFinder {
@override
Map<String, String> serialize() {
return super.serialize()
..addAll(of.serialize().map((String key, String value) => MapEntry<String, String>('of_$key', value)))
..addAll(matching.serialize().map((String key, String value) => MapEntry<String, String>('matching_$key', value)))
..addAll(<String, String>{
'of': jsonEncode(of.serialize()),
'matching': jsonEncode(matching.serialize()),
'matchRoot': matchRoot ? 'true' : 'false',
'firstMatchOnly': firstMatchOnly ? 'true' : 'false',
});
......@@ -348,23 +350,15 @@ class Descendant extends SerializableFinder {
/// Deserializes the finder from JSON generated by [serialize].
static Descendant deserialize(Map<String, String> json) {
final Map<String, String> of = <String, String>{};
final Map<String, String> matching = <String, String>{};
final Map<String, String> other = <String, String>{};
for (String key in json.keys) {
if (key.startsWith('of_')) {
of[key.substring('of_'.length)] = json[key];
} else if (key.startsWith('matching_')) {
matching[key.substring('matching_'.length)] = json[key];
} else {
other[key] = json[key];
}
}
final Map<String, String> jsonOfMatcher =
Map<String, String>.from(jsonDecode(json['of']));
final Map<String, String> jsonMatchingMatcher =
Map<String, String>.from(jsonDecode(json['matching']));
return Descendant(
of: SerializableFinder.deserialize(of),
matching: SerializableFinder.deserialize(matching),
matchRoot: other['matchRoot'] == 'true',
firstMatchOnly: other['firstMatchOnly'] == 'true',
of: SerializableFinder.deserialize(jsonOfMatcher),
matching: SerializableFinder.deserialize(jsonMatchingMatcher),
matchRoot: json['matchRoot'] == 'true',
firstMatchOnly: json['firstMatchOnly'] == 'true',
);
}
}
......@@ -401,9 +395,9 @@ class Ancestor extends SerializableFinder {
@override
Map<String, String> serialize() {
return super.serialize()
..addAll(of.serialize().map((String key, String value) => MapEntry<String, String>('of_$key', value)))
..addAll(matching.serialize().map((String key, String value) => MapEntry<String, String>('matching_$key', value)))
..addAll(<String, String>{
'of': jsonEncode(of.serialize()),
'matching': jsonEncode(matching.serialize()),
'matchRoot': matchRoot ? 'true' : 'false',
'firstMatchOnly': firstMatchOnly ? 'true' : 'false',
});
......@@ -411,23 +405,15 @@ class Ancestor extends SerializableFinder {
/// Deserializes the finder from JSON generated by [serialize].
static Ancestor deserialize(Map<String, String> json) {
final Map<String, String> of = <String, String>{};
final Map<String, String> matching = <String, String>{};
final Map<String, String> other = <String, String>{};
for (String key in json.keys) {
if (key.startsWith('of_')) {
of[key.substring('of_'.length)] = json[key];
} else if (key.startsWith('matching_')) {
matching[key.substring('matching_'.length)] = json[key];
} else {
other[key] = json[key];
}
}
final Map<String, String> jsonOfMatcher =
Map<String, String>.from(jsonDecode(json['of']));
final Map<String, String> jsonMatchingMatcher =
Map<String, String>.from(jsonDecode(json['matching']));
return Ancestor(
of: SerializableFinder.deserialize(of),
matching: SerializableFinder.deserialize(matching),
matchRoot: other['matchRoot'] == 'true',
firstMatchOnly: other['firstMatchOnly'] == 'true',
of: SerializableFinder.deserialize(jsonOfMatcher),
matching: SerializableFinder.deserialize(jsonMatchingMatcher),
matchRoot: json['matchRoot'] == 'true',
firstMatchOnly: json['firstMatchOnly'] == 'true',
);
}
}
......
......@@ -19,11 +19,8 @@ void main() {
);
expect(a.serialize(), <String, String>{
'finderType': 'Ancestor',
'of_finderType': 'ByType',
'of_type': 'Text',
'matching_finderType': 'ByValueKey',
'matching_keyValueString': 'hello',
'matching_keyValueType': 'String',
'of': '{"finderType":"ByType","type":"Text"}',
'matching': '{"finderType":"ByValueKey","keyValueString":"hello","keyValueType":"String"}',
'matchRoot': 'true',
'firstMatchOnly': 'true',
});
......@@ -32,11 +29,8 @@ void main() {
test('Ancestor finder deserialize', () {
final Map<String, String> serialized = <String, String>{
'finderType': 'Ancestor',
'of_finderType': 'ByType',
'of_type': 'Text',
'matching_finderType': 'ByValueKey',
'matching_keyValueString': 'hello',
'matching_keyValueType': 'String',
'of': '{"finderType":"ByType","type":"Text"}',
'matching': '{"finderType":"ByValueKey","keyValueString":"hello","keyValueType":"String"}',
'matchRoot': 'true',
'firstMatchOnly': 'true',
};
......@@ -60,11 +54,8 @@ void main() {
);
expect(a.serialize(), <String, String>{
'finderType': 'Descendant',
'of_finderType': 'ByType',
'of_type': 'Text',
'matching_finderType': 'ByValueKey',
'matching_keyValueString': 'hello',
'matching_keyValueType': 'String',
'of': '{"finderType":"ByType","type":"Text"}',
'matching': '{"finderType":"ByValueKey","keyValueString":"hello","keyValueType":"String"}',
'matchRoot': 'true',
'firstMatchOnly': 'true',
});
......@@ -73,11 +64,8 @@ void main() {
test('Descendant finder deserialize', () {
final Map<String, String> serialized = <String, String>{
'finderType': 'Descendant',
'of_finderType': 'ByType',
'of_type': 'Text',
'matching_finderType': 'ByValueKey',
'matching_keyValueString': 'hello',
'matching_keyValueType': 'String',
'of': '{"finderType":"ByType","type":"Text"}',
'matching': '{"finderType":"ByValueKey","keyValueString":"hello","keyValueType":"String"}',
'matchRoot': 'true',
'firstMatchOnly': 'true',
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment