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

Rework Route type #41

Open
emma-k-alexandra opened this issue Feb 7, 2022 · 0 comments
Open

Rework Route type #41

emma-k-alexandra opened this issue Feb 7, 2022 · 0 comments
Assignees
Labels
breaking change This proposal contains breaking changes to the public API of the package enhancement New feature or request

Comments

@emma-k-alexandra
Copy link
Owner

Since v11 of WMATA.swift, Route has been a typealias for String. Prior to v11, Route was a struct, and before that Route was an enum similar to Station, and way, way back Route was actually a class.

There's been a lot of shifting to figure out what the right type for this rather simple piece of data is. I think today's String value is the closest to correct so far, but it still isn't quite satisfactory. Some sharp edges remain with Routes that I think can be alleviated.

A Route has two fundamental parts: The actual route and the variant. Examples:

Route values: A4, Z8 W2

Variants values: /*2, *4, /

Both values can change often as new routes and variants are introduced. Currently, the String value of Route handles these frequent changes. However, several of WMATA's APIs like Bus.Positions and Bus.Incidents do not allow for variants when calling them. This is documented, but it's still possible to call the APIs using routes with variants since Route is just a String.

Here, the package can improve developer experience. If Route was its own type with knowledge of the route value and the variant provided by the developer, the package could choose to use the correct Route information in each endpoint. This would move guidance from documentation into typed checks in code, which is almost always preferable.

Here's the type I think could work well for Route:

struct Route: Codable, RawRepresentable {
    var route: String
    var variant: String?

    init(_ route: String, variant: String? = nil) {
       ...
    }
    
    init?(rawValue: String) {
        ...
    }
}

Route(rawValue: "Z8*4")
Route("10A")
Route("Z8", variant: "*4")

I think it could be a pain to parse the routes coming out go WMATA's API into their two parts because the WMATA is super inconsistent about naming route variants.

Overall though, I think this struct would allow the package to help the developer more and more accurately represent what a Route actually is.

Any input welcome.

@emma-k-alexandra emma-k-alexandra added enhancement New feature or request breaking change This proposal contains breaking changes to the public API of the package labels Feb 7, 2022
@emma-k-alexandra emma-k-alexandra self-assigned this Feb 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change This proposal contains breaking changes to the public API of the package enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant