forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary_pattern_matching.swift
157 lines (144 loc) · 4.42 KB
/
dictionary_pattern_matching.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
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
// RUN: %target-run-simple-swift | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: objc_interop
import Foundation
struct State {
let name: String
let population: Int
let abbrev: String
}
func stateFromPlistVerbose(_ plist: Dictionary<String, Any>) -> State? {
if let name = plist["name"] as? NSString {
if let population = plist["population"] as? NSNumber {
if let abbrev = plist["abbrev"] as? NSString {
if abbrev.length == 2 {
return State(name: name as String,
population: population.intValue,
abbrev: abbrev as String)
}
}
}
}
return nil
}
func stateFromPlistCool(_ plist: Dictionary<String, Any>) -> State? {
switch (plist["name"], plist["population"], plist["abbrev"]) {
case let (name as String, pop as Int, abbr as String)
where abbr.count == 2:
return State(name: name,
population: pop,
abbrev: abbr)
default:
return nil
}
}
let goodStatePlist: Dictionary<String, Any> = [
"name" as String: "California",
"population" as String: 38_040_000,
"abbrev" as String: "CA",
]
let invalidStatePlist1: Dictionary<String, Any> = [
"name": "California",
"population": "hella",
"abbrev": "CA",
]
let invalidStatePlist2: Dictionary<String, Any> = [
"name": "California",
"population": 38_040_000,
"abbrev": "Cali",
]
let invalidStatePlist3: Dictionary<String, Any> = [
"name": "California",
"population": 38_040_000,
]
// CHECK-LABEL: some:
// CHECK: name: "California"
// CHECK: population: 38040000
// CHECK: abbrev: "CA"
dump(stateFromPlistVerbose(goodStatePlist))
// CHECK-LABEL: some:
// CHECK: name: "California"
// CHECK: population: 38040000
// CHECK: abbrev: "CA"
dump(stateFromPlistCool(goodStatePlist))
// CHECK-LABEL: nil
dump(stateFromPlistVerbose(invalidStatePlist1))
// CHECK-LABEL: nil
dump(stateFromPlistCool(invalidStatePlist1))
// CHECK-LABEL: nil
dump(stateFromPlistVerbose(invalidStatePlist2))
// CHECK-LABEL: nil
dump(stateFromPlistCool(invalidStatePlist2))
// CHECK-LABEL: nil
dump(stateFromPlistVerbose(invalidStatePlist3))
// CHECK-LABEL: nil
dump(stateFromPlistCool(invalidStatePlist3))
struct Country {
let name: String
let population: Int
}
enum Statistic : CustomReflectable {
case ForState(State)
case ForCountry(Country)
var customMirror: Mirror {
switch self {
case .ForState(let state):
return Mirror(
self, children: ["State": state], displayStyle: .`enum`)
case .ForCountry(let country):
return Mirror(
self, children: ["Country": country], displayStyle: .`enum`)
}
}
}
func statisticFromPlist(_ plist: Dictionary<String, Any>) -> Statistic? {
switch (plist["kind"], plist["name"], plist["population"], plist["abbrev"]) {
case let ("state" as String, name as String, population as Int, abbrev as String)
where abbrev.count == 2:
return Statistic.ForState(State(name: name,
population: population,
abbrev: abbrev))
case let ("country" as String, name as String, population as Int, .none):
return Statistic.ForCountry(Country(name: name,
population: population))
default:
return nil
}
}
let goodStatePlist2: Dictionary<String, Any> = [
"kind": "state",
"name": "California",
"population": 38_040_000,
"abbrev": "CA"
]
let goodCountryPlist: Dictionary<String, Any> = [
"kind": "country",
"name": "India",
"population": 1_23_70_00_000,
]
let invalidCountryPlist1: Dictionary<String, Any> = [
"kind": "country",
"name": "India",
"population": 1_23_70_00_000,
"abbrev": "IN"
]
let invalidCountryPlist2: Dictionary<String, Any> = [
"kind": "country",
"name": "India",
"population": "123 crore",
]
let invalidKindPlist: Dictionary<String, Any> = [
"kind": "planet",
"name": "Mercury",
"population": 0
]
// CHECK-LABEL: ▿ Optional(main.Statistic.ForState(main.State(name: "California", population: 38040000, abbrev: "CA")))
dump(statisticFromPlist(goodStatePlist2))
// CHECK-LABEL: ▿ Optional(main.Statistic.ForCountry(main.Country(name: "India", population: 1237000000)))
dump(statisticFromPlist(goodCountryPlist))
// CHECK-LABEL: nil
dump(statisticFromPlist(invalidCountryPlist1))
// CHECK-LABEL: nil
dump(statisticFromPlist(invalidCountryPlist2))
// CHECK-LABEL: nil
dump(statisticFromPlist(invalidKindPlist))