Skip to content

Add phrase lookup method #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion OSRMTextInstructions.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
DA5F58FE1E85C19E00BA4D0A /* TokenType.swift in Sources */ = {isa = PBXBuildFile; fileRef = C51B63E81E65FA04002F4634 /* TokenType.swift */; };
DA5F59011E85C1B200BA4D0A /* MapboxDirections.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA5F58FF1E85C1B200BA4D0A /* MapboxDirections.framework */; };
DA5F59021E85C1B200BA4D0A /* Polyline.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA5F59001E85C1B200BA4D0A /* Polyline.framework */; };
DA9CB7741F6C72D800AD2E21 /* PhraseName.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA9CB7731F6C72D800AD2E21 /* PhraseName.swift */; };
DA9CB7751F6C72D800AD2E21 /* PhraseName.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA9CB7731F6C72D800AD2E21 /* PhraseName.swift */; };
DA9CB7761F6C72D800AD2E21 /* PhraseName.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA9CB7731F6C72D800AD2E21 /* PhraseName.swift */; };
DA9CB7771F6C72D800AD2E21 /* PhraseName.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA9CB7731F6C72D800AD2E21 /* PhraseName.swift */; };
/* End PBXBuildFile section */

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

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -210,6 +215,7 @@
358D14561E5E355600ADE590 /* Info.plist */,
358D14571E5E355600ADE590 /* OSRMTextInstructions.h */,
358D14581E5E355600ADE590 /* OSRMTextInstructions.swift */,
DA9CB7731F6C72D800AD2E21 /* PhraseName.swift */,
C51B63E81E65FA04002F4634 /* TokenType.swift */,
);
path = OSRMTextInstructions;
Expand Down Expand Up @@ -411,12 +417,13 @@
35EBDB5C1E5E1572006EB3CD = {
CreatedOnToolsVersion = 8.2.1;
DevelopmentTeam = GJZR2MEM28;
LastSwiftMigration = 0820;
LastSwiftMigration = 0830;
ProvisioningStyle = Automatic;
};
DA5F58921E85A32C00BA4D0A = {
CreatedOnToolsVersion = 8.2.1;
DevelopmentTeam = GJZR2MEM28;
LastSwiftMigration = 0830;
ProvisioningStyle = Automatic;
};
DA5F589A1E85A32C00BA4D0A = {
Expand All @@ -427,6 +434,7 @@
DA5F58C71E85BA5D00BA4D0A = {
CreatedOnToolsVersion = 8.2.1;
DevelopmentTeam = GJZR2MEM28;
LastSwiftMigration = 0830;
ProvisioningStyle = Automatic;
};
DA5F58CF1E85BA5D00BA4D0A = {
Expand All @@ -437,6 +445,7 @@
DA5F58F31E85C13700BA4D0A = {
CreatedOnToolsVersion = 8.2.1;
DevelopmentTeam = GJZR2MEM28;
LastSwiftMigration = 0830;
ProvisioningStyle = Automatic;
};
};
Expand Down Expand Up @@ -636,6 +645,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
DA9CB7741F6C72D800AD2E21 /* PhraseName.swift in Sources */,
358D145B1E5E355600ADE590 /* OSRMTextInstructions.swift in Sources */,
C51B63E91E65FA04002F4634 /* TokenType.swift in Sources */,
);
Expand All @@ -645,6 +655,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
DA9CB7751F6C72D800AD2E21 /* PhraseName.swift in Sources */,
DA5F58AC1E85B0A500BA4D0A /* OSRMTextInstructions.swift in Sources */,
DA5F58AD1E85B0A500BA4D0A /* TokenType.swift in Sources */,
);
Expand All @@ -662,6 +673,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
DA9CB7761F6C72D800AD2E21 /* PhraseName.swift in Sources */,
DA5F58E41E85BE1500BA4D0A /* OSRMTextInstructions.swift in Sources */,
DA5F58E51E85BE1500BA4D0A /* TokenType.swift in Sources */,
);
Expand All @@ -679,6 +691,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
DA9CB7771F6C72D800AD2E21 /* PhraseName.swift in Sources */,
DA5F58FD1E85C19E00BA4D0A /* OSRMTextInstructions.swift in Sources */,
DA5F58FE1E85C19E00BA4D0A /* TokenType.swift in Sources */,
);
Expand Down Expand Up @@ -896,6 +909,7 @@
DA5F58A41E85A32C00BA4D0A /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
Expand All @@ -915,13 +929,15 @@
PRODUCT_NAME = OSRMTextInstructions;
SDKROOT = macosx;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
DA5F58A51E85A32C00BA4D0A /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
Expand Down Expand Up @@ -988,6 +1004,7 @@
DA5F58DA1E85BA5D00BA4D0A /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = GJZR2MEM28;
Expand All @@ -1005,6 +1022,7 @@
PRODUCT_NAME = OSRMTextInstructions;
SDKROOT = appletvos;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = 3;
};
Expand All @@ -1013,6 +1031,7 @@
DA5F58DB1E85BA5D00BA4D0A /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = GJZR2MEM28;
Expand Down Expand Up @@ -1075,6 +1094,7 @@
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = GJZR2MEM28;
Expand All @@ -1092,6 +1112,7 @@
PRODUCT_NAME = OSRMTextInstructions;
SDKROOT = watchos;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = 4;
WATCHOS_DEPLOYMENT_TARGET = 3.1;
Expand All @@ -1102,6 +1123,7 @@
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = GJZR2MEM28;
Expand Down Expand Up @@ -1198,6 +1220,7 @@
DA5F58FB1E85C13700BA4D0A /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
14 changes: 11 additions & 3 deletions OSRMTextInstructions/OSRMTextInstructions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ public class OSRMInstructionFormatter: Formatter {
return instructions["constants"] as! [String: Any]
}

/**
Returns a format string with the given name.

- returns: A format string suitable for `String.replacingTokens(using:)`.
*/
public func phrase(named name: PhraseName) -> String {
let phrases = instructions["phrase"] as! [String: String]
return phrases["\(name)"]!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to make this return optional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PhraseName is a limited set of values, so if a valid PhraseName is passed in, I think the return value should be required.

}

func laneConfig(intersection: Intersection) -> String? {
guard let approachLanes = intersection.approachLanes else {
return ""
Expand Down Expand Up @@ -234,9 +244,7 @@ public class OSRMInstructionFormatter: Formatter {
let isMotorway = roadClasses?.contains(.motorway) ?? false

if let name = name, let ref = ref, name != ref, !isMotorway {
let phrases = instructions["phrase"] as! [String: String]
let phrase = phrases["name and ref"]!
wayName = phrase.replacingTokens(using: { (tokenType) -> String in
wayName = phrase(named: .nameWithCode).replacingTokens(using: { (tokenType) -> String in
switch tokenType {
case .wayName:
return modifyValueByKey?(.wayName, name) ?? name
Expand Down
44 changes: 44 additions & 0 deletions OSRMTextInstructions/PhraseName.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Foundation

/**
Name of a phrase.

Use this type with `OSRMInstructionFormatter.phrase(named:)`.
*/
@objc(OSRMPhraseName)
public enum PhraseName: Int, CustomStringConvertible {
case instructionWithDistance
case twoInstructions
case twoInstructionsWithDistance
case nameWithCode

public init?(description: String) {
let name: PhraseName
switch description {
case "one in distance":
name = .instructionWithDistance
case "two linked":
name = .twoInstructions
case "two linked by distance":
name = .twoInstructionsWithDistance
case "name and ref":
name = .nameWithCode
default:
return nil
}
self.init(rawValue: name.rawValue)
}

public var description: String {
switch self {
case .instructionWithDistance:
return "one in distance"
case .twoInstructions:
return "two linked"
case .twoInstructionsWithDistance:
return "two linked by distance"
case .nameWithCode:
return "name and ref"
}
}
}
15 changes: 10 additions & 5 deletions OSRMTextInstructionsTests/OSRMTextInstructionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class OSRMTextInstructionsTests: XCTestCase {
let bundle = Bundle(for: OSRMTextInstructionsTests.self)
let url = bundle.url(forResource: "v5", withExtension: nil, subdirectory: "osrm-text-instructions/test/fixtures/")!

let phrases = instructions.instructions["phrase"] as! [String: String]

var directoryContents: [URL] = []
XCTAssertNoThrow(directoryContents = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: []))
for type in directoryContents {
Expand All @@ -39,11 +37,18 @@ class OSRMTextInstructionsTests: XCTestCase {
var json: [String: Any] = [:]
XCTAssertNoThrow(json = try JSONSerialization.jsonObject(with: rawJSON, options: []) as! [String: Any])

let phrase = fixture.deletingPathExtension().lastPathComponent.replacingOccurrences(of: "_", with: " ")
let phraseInFileName = fixture.deletingPathExtension().lastPathComponent.replacingOccurrences(of: "_", with: " ")
let phraseName = PhraseName(description: phraseInFileName)
XCTAssertNotNil(phraseName)
var phrase: String?
if let phraseName = phraseName {
phrase = instructions.phrase(named: phraseName)
}
XCTAssertNotNil(phrase)
let fixtureOptions = json["options"] as! [String: String]

let expectedValue = (json["phrases"] as! [String: String])["en"]
let actualValue = phrases[phrase]?.replacingTokens(using: { (tokenType) -> String in
let actualValue = phrase?.replacingTokens(using: { (tokenType) -> String in
var replacement: String?
switch tokenType {
case .firstInstruction:
Expand All @@ -53,7 +58,7 @@ class OSRMTextInstructionsTests: XCTestCase {
case .distance:
replacement = fixtureOptions["distance"]
default:
XCTFail("Unexpected token type \(tokenType) in phrase \(phrase)")
XCTFail("Unexpected token type \(tokenType) in phrase \(phraseInFileName)")
}
XCTAssertNotNil(replacement, "Missing fixture option for \(tokenType)")
return replacement ?? ""
Expand Down