Skip to content

Commit a55c12a

Browse files
authored
Merge pull request #40 from Project-OSRM/1ec5-phrase-name
Add phrase lookup method
2 parents b48a5a0 + e0b53fa commit a55c12a

File tree

4 files changed

+89
-9
lines changed

4 files changed

+89
-9
lines changed

OSRMTextInstructions.xcodeproj/project.pbxproj

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
DA5F58FE1E85C19E00BA4D0A /* TokenType.swift in Sources */ = {isa = PBXBuildFile; fileRef = C51B63E81E65FA04002F4634 /* TokenType.swift */; };
3838
DA5F59011E85C1B200BA4D0A /* MapboxDirections.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA5F58FF1E85C1B200BA4D0A /* MapboxDirections.framework */; };
3939
DA5F59021E85C1B200BA4D0A /* Polyline.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA5F59001E85C1B200BA4D0A /* Polyline.framework */; };
40+
DA9CB7741F6C72D800AD2E21 /* PhraseName.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA9CB7731F6C72D800AD2E21 /* PhraseName.swift */; };
41+
DA9CB7751F6C72D800AD2E21 /* PhraseName.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA9CB7731F6C72D800AD2E21 /* PhraseName.swift */; };
42+
DA9CB7761F6C72D800AD2E21 /* PhraseName.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA9CB7731F6C72D800AD2E21 /* PhraseName.swift */; };
43+
DA9CB7771F6C72D800AD2E21 /* PhraseName.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA9CB7731F6C72D800AD2E21 /* PhraseName.swift */; };
4044
/* End PBXBuildFile section */
4145

4246
/* Begin PBXContainerItemProxy section */
@@ -87,6 +91,7 @@
8791
DA5F58F41E85C13700BA4D0A /* OSRMTextInstructions.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = OSRMTextInstructions.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8892
DA5F58FF1E85C1B200BA4D0A /* MapboxDirections.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MapboxDirections.framework; path = Carthage/Build/watchOS/MapboxDirections.framework; sourceTree = "<group>"; };
8993
DA5F59001E85C1B200BA4D0A /* Polyline.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Polyline.framework; path = Carthage/Build/watchOS/Polyline.framework; sourceTree = "<group>"; };
94+
DA9CB7731F6C72D800AD2E21 /* PhraseName.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PhraseName.swift; sourceTree = "<group>"; };
9095
/* End PBXFileReference section */
9196

9297
/* Begin PBXFrameworksBuildPhase section */
@@ -210,6 +215,7 @@
210215
358D14561E5E355600ADE590 /* Info.plist */,
211216
358D14571E5E355600ADE590 /* OSRMTextInstructions.h */,
212217
358D14581E5E355600ADE590 /* OSRMTextInstructions.swift */,
218+
DA9CB7731F6C72D800AD2E21 /* PhraseName.swift */,
213219
C51B63E81E65FA04002F4634 /* TokenType.swift */,
214220
);
215221
path = OSRMTextInstructions;
@@ -411,12 +417,13 @@
411417
35EBDB5C1E5E1572006EB3CD = {
412418
CreatedOnToolsVersion = 8.2.1;
413419
DevelopmentTeam = GJZR2MEM28;
414-
LastSwiftMigration = 0820;
420+
LastSwiftMigration = 0830;
415421
ProvisioningStyle = Automatic;
416422
};
417423
DA5F58921E85A32C00BA4D0A = {
418424
CreatedOnToolsVersion = 8.2.1;
419425
DevelopmentTeam = GJZR2MEM28;
426+
LastSwiftMigration = 0830;
420427
ProvisioningStyle = Automatic;
421428
};
422429
DA5F589A1E85A32C00BA4D0A = {
@@ -427,6 +434,7 @@
427434
DA5F58C71E85BA5D00BA4D0A = {
428435
CreatedOnToolsVersion = 8.2.1;
429436
DevelopmentTeam = GJZR2MEM28;
437+
LastSwiftMigration = 0830;
430438
ProvisioningStyle = Automatic;
431439
};
432440
DA5F58CF1E85BA5D00BA4D0A = {
@@ -437,6 +445,7 @@
437445
DA5F58F31E85C13700BA4D0A = {
438446
CreatedOnToolsVersion = 8.2.1;
439447
DevelopmentTeam = GJZR2MEM28;
448+
LastSwiftMigration = 0830;
440449
ProvisioningStyle = Automatic;
441450
};
442451
};
@@ -636,6 +645,7 @@
636645
isa = PBXSourcesBuildPhase;
637646
buildActionMask = 2147483647;
638647
files = (
648+
DA9CB7741F6C72D800AD2E21 /* PhraseName.swift in Sources */,
639649
358D145B1E5E355600ADE590 /* OSRMTextInstructions.swift in Sources */,
640650
C51B63E91E65FA04002F4634 /* TokenType.swift in Sources */,
641651
);
@@ -645,6 +655,7 @@
645655
isa = PBXSourcesBuildPhase;
646656
buildActionMask = 2147483647;
647657
files = (
658+
DA9CB7751F6C72D800AD2E21 /* PhraseName.swift in Sources */,
648659
DA5F58AC1E85B0A500BA4D0A /* OSRMTextInstructions.swift in Sources */,
649660
DA5F58AD1E85B0A500BA4D0A /* TokenType.swift in Sources */,
650661
);
@@ -662,6 +673,7 @@
662673
isa = PBXSourcesBuildPhase;
663674
buildActionMask = 2147483647;
664675
files = (
676+
DA9CB7761F6C72D800AD2E21 /* PhraseName.swift in Sources */,
665677
DA5F58E41E85BE1500BA4D0A /* OSRMTextInstructions.swift in Sources */,
666678
DA5F58E51E85BE1500BA4D0A /* TokenType.swift in Sources */,
667679
);
@@ -679,6 +691,7 @@
679691
isa = PBXSourcesBuildPhase;
680692
buildActionMask = 2147483647;
681693
files = (
694+
DA9CB7771F6C72D800AD2E21 /* PhraseName.swift in Sources */,
682695
DA5F58FD1E85C19E00BA4D0A /* OSRMTextInstructions.swift in Sources */,
683696
DA5F58FE1E85C19E00BA4D0A /* TokenType.swift in Sources */,
684697
);
@@ -896,6 +909,7 @@
896909
DA5F58A41E85A32C00BA4D0A /* Debug */ = {
897910
isa = XCBuildConfiguration;
898911
buildSettings = {
912+
CLANG_ENABLE_MODULES = YES;
899913
CODE_SIGN_IDENTITY = "";
900914
COMBINE_HIDPI_IMAGES = YES;
901915
DEFINES_MODULE = YES;
@@ -915,13 +929,15 @@
915929
PRODUCT_NAME = OSRMTextInstructions;
916930
SDKROOT = macosx;
917931
SKIP_INSTALL = YES;
932+
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
918933
SWIFT_VERSION = 3.0;
919934
};
920935
name = Debug;
921936
};
922937
DA5F58A51E85A32C00BA4D0A /* Release */ = {
923938
isa = XCBuildConfiguration;
924939
buildSettings = {
940+
CLANG_ENABLE_MODULES = YES;
925941
CODE_SIGN_IDENTITY = "";
926942
COMBINE_HIDPI_IMAGES = YES;
927943
DEFINES_MODULE = YES;
@@ -988,6 +1004,7 @@
9881004
DA5F58DA1E85BA5D00BA4D0A /* Debug */ = {
9891005
isa = XCBuildConfiguration;
9901006
buildSettings = {
1007+
CLANG_ENABLE_MODULES = YES;
9911008
CODE_SIGN_IDENTITY = "";
9921009
DEFINES_MODULE = YES;
9931010
DEVELOPMENT_TEAM = GJZR2MEM28;
@@ -1005,6 +1022,7 @@
10051022
PRODUCT_NAME = OSRMTextInstructions;
10061023
SDKROOT = appletvos;
10071024
SKIP_INSTALL = YES;
1025+
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
10081026
SWIFT_VERSION = 3.0;
10091027
TARGETED_DEVICE_FAMILY = 3;
10101028
};
@@ -1013,6 +1031,7 @@
10131031
DA5F58DB1E85BA5D00BA4D0A /* Release */ = {
10141032
isa = XCBuildConfiguration;
10151033
buildSettings = {
1034+
CLANG_ENABLE_MODULES = YES;
10161035
CODE_SIGN_IDENTITY = "";
10171036
DEFINES_MODULE = YES;
10181037
DEVELOPMENT_TEAM = GJZR2MEM28;
@@ -1075,6 +1094,7 @@
10751094
isa = XCBuildConfiguration;
10761095
buildSettings = {
10771096
APPLICATION_EXTENSION_API_ONLY = YES;
1097+
CLANG_ENABLE_MODULES = YES;
10781098
CODE_SIGN_IDENTITY = "";
10791099
DEFINES_MODULE = YES;
10801100
DEVELOPMENT_TEAM = GJZR2MEM28;
@@ -1092,6 +1112,7 @@
10921112
PRODUCT_NAME = OSRMTextInstructions;
10931113
SDKROOT = watchos;
10941114
SKIP_INSTALL = YES;
1115+
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
10951116
SWIFT_VERSION = 3.0;
10961117
TARGETED_DEVICE_FAMILY = 4;
10971118
WATCHOS_DEPLOYMENT_TARGET = 3.1;
@@ -1102,6 +1123,7 @@
11021123
isa = XCBuildConfiguration;
11031124
buildSettings = {
11041125
APPLICATION_EXTENSION_API_ONLY = YES;
1126+
CLANG_ENABLE_MODULES = YES;
11051127
CODE_SIGN_IDENTITY = "";
11061128
DEFINES_MODULE = YES;
11071129
DEVELOPMENT_TEAM = GJZR2MEM28;
@@ -1198,6 +1220,7 @@
11981220
DA5F58FB1E85C13700BA4D0A /* Release */,
11991221
);
12001222
defaultConfigurationIsVisible = 0;
1223+
defaultConfigurationName = Release;
12011224
};
12021225
/* End XCConfigurationList section */
12031226
};

OSRMTextInstructions/OSRMTextInstructions.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ public class OSRMInstructionFormatter: Formatter {
9999
return instructions["constants"] as! [String: Any]
100100
}
101101

102+
/**
103+
Returns a format string with the given name.
104+
105+
- returns: A format string suitable for `String.replacingTokens(using:)`.
106+
*/
107+
public func phrase(named name: PhraseName) -> String {
108+
let phrases = instructions["phrase"] as! [String: String]
109+
return phrases["\(name)"]!
110+
}
111+
102112
func laneConfig(intersection: Intersection) -> String? {
103113
guard let approachLanes = intersection.approachLanes else {
104114
return ""
@@ -234,9 +244,7 @@ public class OSRMInstructionFormatter: Formatter {
234244
let isMotorway = roadClasses?.contains(.motorway) ?? false
235245

236246
if let name = name, let ref = ref, name != ref, !isMotorway {
237-
let phrases = instructions["phrase"] as! [String: String]
238-
let phrase = phrases["name and ref"]!
239-
wayName = phrase.replacingTokens(using: { (tokenType) -> String in
247+
wayName = phrase(named: .nameWithCode).replacingTokens(using: { (tokenType) -> String in
240248
switch tokenType {
241249
case .wayName:
242250
return modifyValueByKey?(.wayName, name) ?? name
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import Foundation
2+
3+
/**
4+
Name of a phrase.
5+
6+
Use this type with `OSRMInstructionFormatter.phrase(named:)`.
7+
*/
8+
@objc(OSRMPhraseName)
9+
public enum PhraseName: Int, CustomStringConvertible {
10+
case instructionWithDistance
11+
case twoInstructions
12+
case twoInstructionsWithDistance
13+
case nameWithCode
14+
15+
public init?(description: String) {
16+
let name: PhraseName
17+
switch description {
18+
case "one in distance":
19+
name = .instructionWithDistance
20+
case "two linked":
21+
name = .twoInstructions
22+
case "two linked by distance":
23+
name = .twoInstructionsWithDistance
24+
case "name and ref":
25+
name = .nameWithCode
26+
default:
27+
return nil
28+
}
29+
self.init(rawValue: name.rawValue)
30+
}
31+
32+
public var description: String {
33+
switch self {
34+
case .instructionWithDistance:
35+
return "one in distance"
36+
case .twoInstructions:
37+
return "two linked"
38+
case .twoInstructionsWithDistance:
39+
return "two linked by distance"
40+
case .nameWithCode:
41+
return "name and ref"
42+
}
43+
}
44+
}

OSRMTextInstructionsTests/OSRMTextInstructionsTests.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ class OSRMTextInstructionsTests: XCTestCase {
2424
let bundle = Bundle(for: OSRMTextInstructionsTests.self)
2525
let url = bundle.url(forResource: "v5", withExtension: nil, subdirectory: "osrm-text-instructions/test/fixtures/")!
2626

27-
let phrases = instructions.instructions["phrase"] as! [String: String]
28-
2927
var directoryContents: [URL] = []
3028
XCTAssertNoThrow(directoryContents = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: []))
3129
for type in directoryContents {
@@ -39,11 +37,18 @@ class OSRMTextInstructionsTests: XCTestCase {
3937
var json: [String: Any] = [:]
4038
XCTAssertNoThrow(json = try JSONSerialization.jsonObject(with: rawJSON, options: []) as! [String: Any])
4139

42-
let phrase = fixture.deletingPathExtension().lastPathComponent.replacingOccurrences(of: "_", with: " ")
40+
let phraseInFileName = fixture.deletingPathExtension().lastPathComponent.replacingOccurrences(of: "_", with: " ")
41+
let phraseName = PhraseName(description: phraseInFileName)
42+
XCTAssertNotNil(phraseName)
43+
var phrase: String?
44+
if let phraseName = phraseName {
45+
phrase = instructions.phrase(named: phraseName)
46+
}
47+
XCTAssertNotNil(phrase)
4348
let fixtureOptions = json["options"] as! [String: String]
4449

4550
let expectedValue = (json["phrases"] as! [String: String])["en"]
46-
let actualValue = phrases[phrase]?.replacingTokens(using: { (tokenType) -> String in
51+
let actualValue = phrase?.replacingTokens(using: { (tokenType) -> String in
4752
var replacement: String?
4853
switch tokenType {
4954
case .firstInstruction:
@@ -53,7 +58,7 @@ class OSRMTextInstructionsTests: XCTestCase {
5358
case .distance:
5459
replacement = fixtureOptions["distance"]
5560
default:
56-
XCTFail("Unexpected token type \(tokenType) in phrase \(phrase)")
61+
XCTFail("Unexpected token type \(tokenType) in phrase \(phraseInFileName)")
5762
}
5863
XCTAssertNotNil(replacement, "Missing fixture option for \(tokenType)")
5964
return replacement ?? ""

0 commit comments

Comments
 (0)