-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathRouting.swift
123 lines (106 loc) · 3.29 KB
/
Routing.swift
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
import Benchmark
import Foundation
import URLRouting
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
/// This benchmark demonstrates how you can build a URL request router that can transform an input
/// request into a more well-structured data type, such as an enum. We build a router that can
/// recognize one of 5 routes for a website.
let routingSuite = BenchmarkSuite(name: "Routing") { suite in
if #available(macOS 10.13, *) {
#if compiler(>=5.5)
enum AppRoute: Equatable {
case home
case contactUs
case episodes(EpisodesRoute)
}
enum EpisodesRoute: Equatable {
case index
case episode(id: Int, route: EpisodeRoute)
}
enum EpisodeRoute: Equatable {
case show
case comments(CommentsRoute)
}
enum CommentsRoute: Equatable {
case post(Comment)
case show(count: Int)
}
struct Comment: Codable, Equatable {
let commenter: String
let message: String
}
let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys
let commentsRouter = OneOf {
Route(.case(CommentsRoute.post)) {
Method.post
Body(.json(Comment.self, encoder: encoder))
}
Route(.case(CommentsRoute.show)) {
Query {
Field("count", default: 10) { Digits() }
}
}
}
let episodeRouter = OneOf {
Route(EpisodeRoute.show)
Route(.case(EpisodeRoute.comments)) {
Path { From(.utf8) { "comments".utf8 } }
commentsRouter
}
}
let episodesRouter = OneOf {
Route(EpisodesRoute.index)
Route(.case(EpisodesRoute.episode)) {
Path { Digits() }
episodeRouter
}
}
let router = OneOf {
Route(AppRoute.home)
Route(AppRoute.contactUs) {
Path { From(.utf8) { "contact-us".utf8 } }
}
Route(.case(AppRoute.episodes)) {
Path { From(.utf8) { "episodes".utf8 } }
episodesRouter
}
}
let requests = [
URLRequestData(),
URLRequestData(path: "/contact-us"),
URLRequestData(path: "/episodes"),
URLRequestData(path: "/episodes/1"),
URLRequestData(path: "/episodes/1/comments"),
URLRequestData(path: "/episodes/1/comments", query: ["count": ["20"]]),
URLRequestData(
method: "POST",
path: "/episodes/1/comments",
body: .init(#"{"commenter":"Blob","message":"Hi!"}"#.utf8)
),
]
var output: [AppRoute]!
var expectedOutput: [AppRoute] = [
.home,
.contactUs,
.episodes(.index),
.episodes(.episode(id: 1, route: .show)),
.episodes(.episode(id: 1, route: .comments(.show(count: 10)))),
.episodes(.episode(id: 1, route: .comments(.show(count: 20)))),
.episodes(
.episode(id: 1, route: .comments(.post(.init(commenter: "Blob", message: "Hi!"))))),
]
suite.benchmark("Parser") {
output = try requests.map {
var input = $0
return try router.parse(&input)
}
} tearDown: {
precondition(output == expectedOutput)
precondition(requests == output.map { try! router.print($0) })
}
#endif
}
}