parse_route.dart
6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import 'package:flutter/widgets.dart';
import 'package:get/src/navigation/routes/get_route.dart';
class ParseRouteTree {
final List<_ParseRouteTreeNode> _nodes = <_ParseRouteTreeNode>[];
// bool _hasDefaultRoute = false;
void addRoute(GetPage route) {
String path = route.name;
if (path == Navigator.defaultRouteName) {
// if (_hasDefaultRoute) {
// throw ("Default route was already defined");
// }
var node = _ParseRouteTreeNode(path, _ParseRouteTreeNodeType.component);
node.routes = [route];
_nodes.add(node);
// _hasDefaultRoute = true;
return;
}
if (path.startsWith("/")) {
path = path.substring(1);
}
List<String> pathComponents = path.split('/');
_ParseRouteTreeNode parent;
for (int i = 0; i < pathComponents.length; i++) {
String component = pathComponents[i];
_ParseRouteTreeNode node = _nodeForComponent(component, parent);
if (node == null) {
_ParseRouteTreeNodeType type = _typeForComponent(component);
node = _ParseRouteTreeNode(component, type);
node.parent = parent;
if (parent == null) {
_nodes.add(node);
} else {
parent.nodes.add(node);
}
}
if (i == pathComponents.length - 1) {
if (node.routes == null) {
node.routes = [route];
} else {
node.routes.add(route);
}
}
parent = node;
}
}
_GetPageMatch matchRoute(String path) {
String usePath = path;
if (usePath.startsWith("/")) {
usePath = path.substring(1);
}
// should take off url parameters first..
final uri = Uri.tryParse(usePath);
// List<String> components = usePath.split("/");
List<String> components = uri.pathSegments;
if (path == Navigator.defaultRouteName) {
components = ["/"];
}
Map<_ParseRouteTreeNode, _ParseRouteTreeNodeMatch> nodeMatches =
<_ParseRouteTreeNode, _ParseRouteTreeNodeMatch>{};
List<_ParseRouteTreeNode> nodesToCheck = _nodes;
for (String checkComponent in components) {
Map<_ParseRouteTreeNode, _ParseRouteTreeNodeMatch> currentMatches =
<_ParseRouteTreeNode, _ParseRouteTreeNodeMatch>{};
List<_ParseRouteTreeNode> nextNodes = <_ParseRouteTreeNode>[];
for (_ParseRouteTreeNode node in nodesToCheck) {
String pathPart = checkComponent;
Map<String, String> queryMap = {};
if (checkComponent.contains("?") && !checkComponent.contains("=")) {
var splitParam = checkComponent.split("?");
pathPart = splitParam[0];
queryMap = {pathPart: splitParam[1]};
} else if (checkComponent.contains("?")) {
var splitParam = checkComponent.split("?");
var splitParam2 = splitParam[1].split("=");
if (!splitParam2[1].contains("&")) {
pathPart = splitParam[0];
queryMap = {splitParam2[0]: splitParam2[1]};
} else {
pathPart = splitParam[0];
final segunda = splitParam[1];
var other = segunda.split(RegExp(r"[&,=]"));
for (var i = 0; i < (other.length - 1); i++) {
bool impar = (i % 2 == 0);
if (impar) {
queryMap.addAll({other[0 + i]: other[1 + i]});
}
}
}
}
bool isMatch = (node.part == pathPart || node.isParameter());
if (isMatch) {
_ParseRouteTreeNodeMatch parentMatch = nodeMatches[node.parent];
_ParseRouteTreeNodeMatch match =
_ParseRouteTreeNodeMatch.fromMatch(parentMatch, node);
// TODO: find a way to clean this implementation.
match.parameters.addAll(uri.queryParameters);
if (node.isParameter()) {
String paramKey = node.part.substring(1);
match.parameters[paramKey] = pathPart;
}
if (queryMap != null) {
match.parameters.addAll(queryMap);
}
currentMatches[node] = match;
if (node.nodes != null) {
nextNodes.addAll(node.nodes);
}
}
}
nodeMatches = currentMatches;
nodesToCheck = nextNodes;
if (currentMatches.values.length == 0) {
return null;
}
}
List<_ParseRouteTreeNodeMatch> matches = nodeMatches.values.toList();
if (matches.length > 0) {
_ParseRouteTreeNodeMatch match = matches.first;
_ParseRouteTreeNode nodeToUse = match.node;
if (nodeToUse != null &&
nodeToUse.routes != null &&
nodeToUse.routes.length > 0) {
List<GetPage> routes = nodeToUse.routes;
_GetPageMatch routeMatch = _GetPageMatch(routes[0]);
routeMatch.parameters = match.parameters;
return routeMatch;
}
}
return null;
}
_ParseRouteTreeNode _nodeForComponent(
String component, _ParseRouteTreeNode parent) {
List<_ParseRouteTreeNode> nodes = _nodes;
if (parent != null) {
nodes = parent.nodes;
}
for (_ParseRouteTreeNode node in nodes) {
if (node.part == component) {
return node;
}
}
return null;
}
_ParseRouteTreeNodeType _typeForComponent(String component) {
_ParseRouteTreeNodeType type = _ParseRouteTreeNodeType.component;
if (_isParameterComponent(component)) {
type = _ParseRouteTreeNodeType.parameter;
}
return type;
}
bool _isParameterComponent(String component) {
return component.startsWith(":");
}
Map<String, String> parseQueryString(String query) {
var search = RegExp('([^&=]+)=?([^&]*)');
var params = Map<String, 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));
params[key] = value;
}
return params;
}
}
class _ParseRouteTreeNodeMatch {
_ParseRouteTreeNodeMatch(this.node);
_ParseRouteTreeNodeMatch.fromMatch(
_ParseRouteTreeNodeMatch match, this.node) {
parameters = <String, String>{};
if (match != null) {
parameters.addAll(match.parameters);
}
}
_ParseRouteTreeNode node;
Map<String, String> parameters = <String, String>{};
}
class _ParseRouteTreeNode {
_ParseRouteTreeNode(this.part, this.type);
String part;
_ParseRouteTreeNodeType type;
List<GetPage> routes = <GetPage>[];
List<_ParseRouteTreeNode> nodes = <_ParseRouteTreeNode>[];
_ParseRouteTreeNode parent;
bool isParameter() {
return type == _ParseRouteTreeNodeType.parameter;
}
}
class _GetPageMatch {
_GetPageMatch(this.route);
GetPage route;
Map<String, String> parameters = <String, String>{};
}
enum _ParseRouteTreeNodeType {
component,
parameter,
}