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 @@ ...@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:convert';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'error.dart'; import 'error.dart';
...@@ -338,9 +340,9 @@ class Descendant extends SerializableFinder { ...@@ -338,9 +340,9 @@ class Descendant extends SerializableFinder {
@override @override
Map<String, String> serialize() { Map<String, String> serialize() {
return super.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>{ ..addAll(<String, String>{
'of': jsonEncode(of.serialize()),
'matching': jsonEncode(matching.serialize()),
'matchRoot': matchRoot ? 'true' : 'false', 'matchRoot': matchRoot ? 'true' : 'false',
'firstMatchOnly': firstMatchOnly ? 'true' : 'false', 'firstMatchOnly': firstMatchOnly ? 'true' : 'false',
}); });
...@@ -348,23 +350,15 @@ class Descendant extends SerializableFinder { ...@@ -348,23 +350,15 @@ class Descendant extends SerializableFinder {
/// Deserializes the finder from JSON generated by [serialize]. /// Deserializes the finder from JSON generated by [serialize].
static Descendant deserialize(Map<String, String> json) { static Descendant deserialize(Map<String, String> json) {
final Map<String, String> of = <String, String>{}; final Map<String, String> jsonOfMatcher =
final Map<String, String> matching = <String, String>{}; Map<String, String>.from(jsonDecode(json['of']));
final Map<String, String> other = <String, String>{}; final Map<String, String> jsonMatchingMatcher =
for (String key in json.keys) { Map<String, String>.from(jsonDecode(json['matching']));
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];
}
}
return Descendant( return Descendant(
of: SerializableFinder.deserialize(of), of: SerializableFinder.deserialize(jsonOfMatcher),
matching: SerializableFinder.deserialize(matching), matching: SerializableFinder.deserialize(jsonMatchingMatcher),
matchRoot: other['matchRoot'] == 'true', matchRoot: json['matchRoot'] == 'true',
firstMatchOnly: other['firstMatchOnly'] == 'true', firstMatchOnly: json['firstMatchOnly'] == 'true',
); );
} }
} }
...@@ -401,9 +395,9 @@ class Ancestor extends SerializableFinder { ...@@ -401,9 +395,9 @@ class Ancestor extends SerializableFinder {
@override @override
Map<String, String> serialize() { Map<String, String> serialize() {
return super.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>{ ..addAll(<String, String>{
'of': jsonEncode(of.serialize()),
'matching': jsonEncode(matching.serialize()),
'matchRoot': matchRoot ? 'true' : 'false', 'matchRoot': matchRoot ? 'true' : 'false',
'firstMatchOnly': firstMatchOnly ? 'true' : 'false', 'firstMatchOnly': firstMatchOnly ? 'true' : 'false',
}); });
...@@ -411,23 +405,15 @@ class Ancestor extends SerializableFinder { ...@@ -411,23 +405,15 @@ class Ancestor extends SerializableFinder {
/// Deserializes the finder from JSON generated by [serialize]. /// Deserializes the finder from JSON generated by [serialize].
static Ancestor deserialize(Map<String, String> json) { static Ancestor deserialize(Map<String, String> json) {
final Map<String, String> of = <String, String>{}; final Map<String, String> jsonOfMatcher =
final Map<String, String> matching = <String, String>{}; Map<String, String>.from(jsonDecode(json['of']));
final Map<String, String> other = <String, String>{}; final Map<String, String> jsonMatchingMatcher =
for (String key in json.keys) { Map<String, String>.from(jsonDecode(json['matching']));
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];
}
}
return Ancestor( return Ancestor(
of: SerializableFinder.deserialize(of), of: SerializableFinder.deserialize(jsonOfMatcher),
matching: SerializableFinder.deserialize(matching), matching: SerializableFinder.deserialize(jsonMatchingMatcher),
matchRoot: other['matchRoot'] == 'true', matchRoot: json['matchRoot'] == 'true',
firstMatchOnly: other['firstMatchOnly'] == 'true', firstMatchOnly: json['firstMatchOnly'] == 'true',
); );
} }
} }
......
...@@ -19,11 +19,8 @@ void main() { ...@@ -19,11 +19,8 @@ void main() {
); );
expect(a.serialize(), <String, String>{ expect(a.serialize(), <String, String>{
'finderType': 'Ancestor', 'finderType': 'Ancestor',
'of_finderType': 'ByType', 'of': '{"finderType":"ByType","type":"Text"}',
'of_type': 'Text', 'matching': '{"finderType":"ByValueKey","keyValueString":"hello","keyValueType":"String"}',
'matching_finderType': 'ByValueKey',
'matching_keyValueString': 'hello',
'matching_keyValueType': 'String',
'matchRoot': 'true', 'matchRoot': 'true',
'firstMatchOnly': 'true', 'firstMatchOnly': 'true',
}); });
...@@ -32,11 +29,8 @@ void main() { ...@@ -32,11 +29,8 @@ void main() {
test('Ancestor finder deserialize', () { test('Ancestor finder deserialize', () {
final Map<String, String> serialized = <String, String>{ final Map<String, String> serialized = <String, String>{
'finderType': 'Ancestor', 'finderType': 'Ancestor',
'of_finderType': 'ByType', 'of': '{"finderType":"ByType","type":"Text"}',
'of_type': 'Text', 'matching': '{"finderType":"ByValueKey","keyValueString":"hello","keyValueType":"String"}',
'matching_finderType': 'ByValueKey',
'matching_keyValueString': 'hello',
'matching_keyValueType': 'String',
'matchRoot': 'true', 'matchRoot': 'true',
'firstMatchOnly': 'true', 'firstMatchOnly': 'true',
}; };
...@@ -60,11 +54,8 @@ void main() { ...@@ -60,11 +54,8 @@ void main() {
); );
expect(a.serialize(), <String, String>{ expect(a.serialize(), <String, String>{
'finderType': 'Descendant', 'finderType': 'Descendant',
'of_finderType': 'ByType', 'of': '{"finderType":"ByType","type":"Text"}',
'of_type': 'Text', 'matching': '{"finderType":"ByValueKey","keyValueString":"hello","keyValueType":"String"}',
'matching_finderType': 'ByValueKey',
'matching_keyValueString': 'hello',
'matching_keyValueType': 'String',
'matchRoot': 'true', 'matchRoot': 'true',
'firstMatchOnly': 'true', 'firstMatchOnly': 'true',
}); });
...@@ -73,11 +64,8 @@ void main() { ...@@ -73,11 +64,8 @@ void main() {
test('Descendant finder deserialize', () { test('Descendant finder deserialize', () {
final Map<String, String> serialized = <String, String>{ final Map<String, String> serialized = <String, String>{
'finderType': 'Descendant', 'finderType': 'Descendant',
'of_finderType': 'ByType', 'of': '{"finderType":"ByType","type":"Text"}',
'of_type': 'Text', 'matching': '{"finderType":"ByValueKey","keyValueString":"hello","keyValueType":"String"}',
'matching_finderType': 'ByValueKey',
'matching_keyValueString': 'hello',
'matching_keyValueType': 'String',
'matchRoot': 'true', 'matchRoot': 'true',
'firstMatchOnly': '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