浏览代码

updates parameters to be of type Map<String, List<String>>

Kevin Gray 7 年之前
父节点
当前提交
1639f4200e
共有 4 个文件被更改,包括 26 次插入33 次删除
  1. 2 2
      lib/src/common.dart
  2. 3 3
      lib/src/router.dart
  3. 9 16
      lib/src/tree.dart
  4. 12 12
      test/parser_test.dart

+ 2 - 2
lib/src/common.dart

@@ -21,10 +21,10 @@ class Handler {
 }
 
 ///
-typedef Route<T> RouteCreator<T>(RouteSettings route, Map<String, dynamic> parameters);
+typedef Route<T> RouteCreator<T>(RouteSettings route, Map<String, List<String>> parameters);
 
 ///
-typedef Widget HandlerFunc(BuildContext context, Map<String, dynamic> parameters);
+typedef Widget HandlerFunc(BuildContext context, Map<String, List<String>> parameters);
 
 ///
 class AppRoute {

+ 3 - 3
lib/src/router.dart

@@ -70,7 +70,7 @@ class Router {
 
   ///
   Route<Null> _notFoundRoute(BuildContext context, String path) {
-    RouteCreator<Null> creator = (RouteSettings routeSettings, Map<String, dynamic> parameters) {
+    RouteCreator<Null> creator = (RouteSettings routeSettings, Map<String, List<String>> parameters) {
       return new MaterialPageRoute<Null>(settings: routeSettings, builder: (BuildContext context) {
         return notFoundHandler.handlerFunc(context, parameters);
       });
@@ -93,13 +93,13 @@ class Router {
     if (route == null && notFoundHandler == null) {
       return new RouteMatch(matchType: RouteMatchType.noMatch, errorMessage: "No matching route was found");
     }
-    Map<String, String> parameters = match?.parameters ?? <String, String>{};
+    Map<String, List<String>> parameters = match?.parameters ?? <String, List<String>>{};
     if (handler.type == HandlerType.function) {
       handler.handlerFunc(buildContext, parameters);
       return new RouteMatch(matchType: RouteMatchType.nonVisual);
     }
 
-    RouteCreator creator = (RouteSettings routeSettings, Map<String, dynamic> parameters) {
+    RouteCreator creator = (RouteSettings routeSettings, Map<String, List<String>> parameters) {
       bool isNativeTransition = (transitionType == TransitionType.native || transitionType == TransitionType.nativeModal);
       if (isNativeTransition) {
         return new MaterialPageRoute<dynamic>(settings: routeSettings, fullscreenDialog: transitionType == TransitionType.nativeModal,

+ 9 - 16
lib/src/tree.dart

@@ -18,7 +18,7 @@ class AppRouteMatch {
 
   // properties
   AppRoute route;
-  Map<String, dynamic> parameters = <String, dynamic>{};
+  Map<String, List<String>> parameters = <String, List<String>>{};
 }
 
 class RouteTreeNodeMatch {
@@ -26,7 +26,7 @@ class RouteTreeNodeMatch {
   RouteTreeNodeMatch(this.node);
 
   RouteTreeNodeMatch.fromMatch(RouteTreeNodeMatch match, this.node) {
-    parameters = <String, dynamic>{};
+    parameters = <String, List<String>>{};
     if (match != null) {
       parameters.addAll(match.parameters);
     }
@@ -34,7 +34,7 @@ class RouteTreeNodeMatch {
 
   // properties
   RouteTreeNode node;
-  Map<String, dynamic> parameters = <String, dynamic>{};
+  Map<String, List<String>> parameters = <String, List<String>>{};
 }
 
 class RouteTreeNode {
@@ -121,7 +121,7 @@ class RouteTree {
       List<RouteTreeNode> nextNodes = <RouteTreeNode>[];
       for (RouteTreeNode node in nodesToCheck) {
         String pathPart = checkComponent;
-        Map<String, dynamic> queryMap;
+        Map<String, List<String>> queryMap;
         if (checkComponent.contains("?")) {
           var splitParam = checkComponent.split("?");
           pathPart = splitParam[0];
@@ -133,7 +133,7 @@ class RouteTree {
           RouteTreeNodeMatch match = new RouteTreeNodeMatch.fromMatch(parentMatch, node);
           if (node.isParameter()) {
             String paramKey = node.part.substring(1);
-            match.parameters[paramKey] = pathPart;
+            match.parameters[paramKey] = [pathPart];
           }
           if (queryMap != null) {
             match.parameters.addAll(queryMap);
@@ -211,25 +211,18 @@ class RouteTree {
     return component.startsWith(":");
   }
 
-  Map<String, dynamic> parseQueryString(String query) {
+  Map<String, List<String>> parseQueryString(String query) {
     var search = new RegExp('([^&=]+)=?([^&]*)');
-    var params = new Map();
+    var params = new Map<String, List<String>>();
     if (query.startsWith('?')) query = query.substring(1);
     decode(String s) => Uri.decodeComponent(s.replaceAll('+', ' '));
     for (Match match in search.allMatches(query)) {
       String key = decode(match.group(1));
       String value = decode(match.group(2));
       if (params.containsKey(key)) {
-        dynamic paramValue = params[key];
-        if (paramValue is List<String>) {
-          paramValue.add(value);
-        } else if (paramValue is String) {
-          params[key] = [paramValue, value];
-        } else {
-          params[key] = value;
-        }
+        params[key].add(value);
       } else {
-        params[key] = value;
+        params[key] = [value];
       }
     }
     return params;

+ 12 - 12
test/parser_test.dart

@@ -16,8 +16,8 @@ void main() {
     Router router = new Router();
     router.define(route, handler: null);
     AppRouteMatch match = router.match(path);
-    expect(match?.parameters, equals(<String, String>{
-      "id" : "1234",
+    expect(match?.parameters, equals(<String, List<String>>{
+      "id" : ["1234"],
     }));
   });
 
@@ -27,9 +27,9 @@ void main() {
     Router router = new Router();
     router.define(route, handler: null);
     AppRouteMatch match = router.match(path);
-    expect(match?.parameters, equals(<String, String>{
-      "id" : "1234",
-      "name" : "luke",
+    expect(match?.parameters, equals(<String, List<String>>{
+      "id" : ["1234"],
+      "name" : ["luke"],
     }));
   });
 
@@ -39,10 +39,10 @@ void main() {
     Router router = new Router();
     router.define(route, handler: null);
     AppRouteMatch match = router.match(path);
-    expect(match?.parameters, equals(<String, String>{
-      "name" : "luke",
-      "phrase" : "hello world",
-      "number" : "7",
+    expect(match?.parameters, equals(<String, List<String>>{
+      "name" : ["luke"],
+      "phrase" : ["hello world"],
+      "number" : ["7"],
     }));
   });
 
@@ -52,9 +52,9 @@ void main() {
     Router router = new Router();
     router.define(route, handler: null);
     AppRouteMatch match = router.match(path);
-    expect(match?.parameters, equals(<String, dynamic>{
-      "name" : "luke",
-      "phrase" : "hello world",
+    expect(match?.parameters, equals(<String, List<String>>{
+      "name" : ["luke"],
+      "phrase" : ["hello world"],
       "number" : ["7", "10", "13"],
     }));
   });