From 1639f4200e2e380dfc5103782942c498b82094e4 Mon Sep 17 00:00:00 2001 From: Kevin Gray Date: Fri, 2 Feb 2018 15:38:46 -0500 Subject: [PATCH] updates parameters to be of type Map> --- lib/src/common.dart | 4 ++-- lib/src/router.dart | 6 +++--- lib/src/tree.dart | 25 +++++++++---------------- test/parser_test.dart | 24 ++++++++++++------------ 4 files changed, 26 insertions(+), 33 deletions(-) diff --git a/lib/src/common.dart b/lib/src/common.dart index 4fb2231..cd16e07 100644 --- a/lib/src/common.dart +++ b/lib/src/common.dart @@ -21,10 +21,10 @@ class Handler { } /// -typedef Route RouteCreator(RouteSettings route, Map parameters); +typedef Route RouteCreator(RouteSettings route, Map> parameters); /// -typedef Widget HandlerFunc(BuildContext context, Map parameters); +typedef Widget HandlerFunc(BuildContext context, Map> parameters); /// class AppRoute { diff --git a/lib/src/router.dart b/lib/src/router.dart index ad5585c..46dbc00 100644 --- a/lib/src/router.dart +++ b/lib/src/router.dart @@ -70,7 +70,7 @@ class Router { /// Route _notFoundRoute(BuildContext context, String path) { - RouteCreator creator = (RouteSettings routeSettings, Map parameters) { + RouteCreator creator = (RouteSettings routeSettings, Map> parameters) { return new MaterialPageRoute(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 parameters = match?.parameters ?? {}; + Map> parameters = match?.parameters ?? >{}; if (handler.type == HandlerType.function) { handler.handlerFunc(buildContext, parameters); return new RouteMatch(matchType: RouteMatchType.nonVisual); } - RouteCreator creator = (RouteSettings routeSettings, Map parameters) { + RouteCreator creator = (RouteSettings routeSettings, Map> parameters) { bool isNativeTransition = (transitionType == TransitionType.native || transitionType == TransitionType.nativeModal); if (isNativeTransition) { return new MaterialPageRoute(settings: routeSettings, fullscreenDialog: transitionType == TransitionType.nativeModal, diff --git a/lib/src/tree.dart b/lib/src/tree.dart index ea77340..e3a4c3f 100644 --- a/lib/src/tree.dart +++ b/lib/src/tree.dart @@ -18,7 +18,7 @@ class AppRouteMatch { // properties AppRoute route; - Map parameters = {}; + Map> parameters = >{}; } class RouteTreeNodeMatch { @@ -26,7 +26,7 @@ class RouteTreeNodeMatch { RouteTreeNodeMatch(this.node); RouteTreeNodeMatch.fromMatch(RouteTreeNodeMatch match, this.node) { - parameters = {}; + parameters = >{}; if (match != null) { parameters.addAll(match.parameters); } @@ -34,7 +34,7 @@ class RouteTreeNodeMatch { // properties RouteTreeNode node; - Map parameters = {}; + Map> parameters = >{}; } class RouteTreeNode { @@ -121,7 +121,7 @@ class RouteTree { List nextNodes = []; for (RouteTreeNode node in nodesToCheck) { String pathPart = checkComponent; - Map queryMap; + Map> 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 parseQueryString(String query) { + Map> parseQueryString(String query) { var search = new RegExp('([^&=]+)=?([^&]*)'); - var params = new Map(); + var params = new Map>(); 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) { - 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; diff --git a/test/parser_test.dart b/test/parser_test.dart index 94012a6..5ed2e5d 100644 --- a/test/parser_test.dart +++ b/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({ - "id" : "1234", + expect(match?.parameters, equals(>{ + "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({ - "id" : "1234", - "name" : "luke", + expect(match?.parameters, equals(>{ + "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({ - "name" : "luke", - "phrase" : "hello world", - "number" : "7", + expect(match?.parameters, equals(>{ + "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({ - "name" : "luke", - "phrase" : "hello world", + expect(match?.parameters, equals(>{ + "name" : ["luke"], + "phrase" : ["hello world"], "number" : ["7", "10", "13"], })); });