Skip to content
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

Type scheme once only #5

Merged
merged 6 commits into from Jun 6, 2018
Merged

Type scheme once only #5

merged 6 commits into from Jun 6, 2018

Conversation

ghost
Copy link

@ghost ghost commented Jun 4, 2018

I would typically have a component assembler in my codebase somewhere that would interact with Crossroads.

The assembler would build paths.

Consuming the scheme once makes it a bit easier to manage an assembler and I want to avoid typing strings often.

See this pull request in action here

@giginet giginet self-requested a review June 4, 2018 16:27
@giginet
Copy link
Owner

giginet commented Jun 4, 2018

Cool! I'm waiting WWDC keynote now. So I'll check this PR tomorrow 😅

@ghost
Copy link
Author

ghost commented Jun 4, 2018

Me too! Enjoy.

@ghost ghost changed the title Develop Type scheme once only Jun 5, 2018
Copy link
Owner

@giginet giginet left a comment

Choose a reason for hiding this comment

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

@rob-nash Hi!
Thank you contribution. Your proposal looks good. however, some points should be fixed.

@@ -3,21 +3,13 @@ import Foundation
public typealias SimpleRouter = Router<Void>

public final class Router<UserInfo> {
public let scheme: String
private let scheme: String
Copy link
Owner

Choose a reason for hiding this comment

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

Should we conceal this property?

Copy link
Owner

Choose a reason for hiding this comment

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

Maybe, I guess that nobody use Router.scheme from external. 🤔

Copy link
Author

Choose a reason for hiding this comment

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

I think so. I think it might be good to release API, once it becomes useful.

@@ -22,6 +22,23 @@ final class RouterTest: XCTestCase {
XCTAssertFalse(router.responds(to: URL(string: "notfoobar://aaa/bbb")!))
XCTAssertTrue(router.responds(to: URL(string: "foobar://spam/ham")!))
}

func testCanRespond2() {
Copy link
Owner

Choose a reason for hiding this comment

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

testCanRespondWithoutScheme and others


internal func register(_ route: Route<UserInfo>) {
if scheme != route.patternURL.scheme {
assertionFailure("Router and pattern must have the same schemes. expect: \(scheme), actual: \(route.patternURL.scheme)")
Copy link
Owner

Choose a reason for hiding this comment

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

Please do not remove this error.

@@ -22,6 +22,23 @@ final class RouterTest: XCTestCase {
XCTAssertFalse(router.responds(to: URL(string: "notfoobar://aaa/bbb")!))
XCTAssertTrue(router.responds(to: URL(string: "foobar://spam/ham")!))
}

func testCanRespond2() {
let router = SimpleRouter(scheme: schema)
Copy link
Owner

Choose a reason for hiding this comment

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

Oh, I found typo. Could you fix them?

/schema/scheme/

@@ -35,12 +27,17 @@ public final class Router<UserInfo> {

public func register(_ routes: [(String, Route<UserInfo>.Handler)]) {
for (pattern, handler) in routes {
var pattern = pattern
Copy link
Owner

Choose a reason for hiding this comment

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

You can write more clearly code.

    public func register(_ routes: [(String, Route<UserInfo>.Handler)]) {
        for (pattern, handler) in routes {
            let patternURLString: String
            if pattern.hasPrefix("\(scheme)://") {
                patternURLString = pattern
            } else {
                patternURLString = "\(scheme)://\(pattern)"
            }
            guard let patternURL = PatternURL(string: patternURLString) else {
                assertionFailure("\(pattern) is invalid")
                continue
            }
            let route = Route(pattern: patternURL, handler: handler)
            register(route)
        }
    }

Copy link
Owner

Choose a reason for hiding this comment

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

I think you needn't remove internal func register(_ route: Route<UserInfo>).

XCTAssertTrue(router.responds(to: URL(string: "foobar://foo")!))
XCTAssertTrue(router.responds(to: URL(string: "foobar://foo/bar")!))
XCTAssertTrue(router.responds(to: URL(string: "foobar://foo/10000")!))
XCTAssertFalse(router.responds(to: URL(string: "notfoobar://aaa/bbb")!))
Copy link
Owner

Choose a reason for hiding this comment

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

Could you add assertions to check without scheme patterns?

XCTAssertFalse(router.responds(to: URL(string: "static")!))
XCTAssertFalse(router.responds(to: URL(string: "foo/bar")!))
XCTAssertFalse(router.responds(to: URL(string: "spam/ham")!))

@ghost
Copy link
Author

ghost commented Jun 5, 2018

Have made the changes @giginet suggested 👍

Copy link
Owner

@giginet giginet left a comment

Choose a reason for hiding this comment

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

Great job! 👍

I'll merge this after correcting lint warnings.

https://travis-ci.org/giginet/Crossroad/jobs/388382200#L604

Thank you! 🎉

private var routes: [Route<UserInfo>] = []

public init(scheme: String) {
self.scheme = scheme
}

Copy link
Owner

Choose a reason for hiding this comment

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

Could you execute swiftlint autocorrect?

@giginet
Copy link
Owner

giginet commented Jun 5, 2018

You can ignore type_body_length violation.
But, please fix trailing_whitespace violations.
You will fix them by only executing swiftlint autocorrect

@giginet giginet merged commit ecf4887 into giginet:master Jun 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant