Skip to content

Use PrimitiveType as values of parameter dictionary #26

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

devxoul
Copy link
Owner

@devxoul devxoul commented Sep 8, 2019

Background

Property parameters is defined as [String: Any] which means that you can return any type of value. It can cause a human error when an event has an arbitrary type as an associated value. For example:

enum Month: Int {
  case jan = 1, feb, mar
}

enum MyEvent {
  case selectMonth(Month)
}

extension MyEvent: EventType {
  var parameters: [String: Any]? {
    switch self {
    case let .selectMonth(month):
      // expected: ["month": 3]
      // actual  : ["month": "mar"]
      return ["month": month]
    }
  }
}

It would be great if the compiler can warn you.

Solution

Define a protocol PrimitiveType which primitive types such as Int or String conform to. Use this protocol as a value of parameters dictionary so that the compiler can warn you if you return some wrong type.

  extension MyEvent: EventType {
-   var parameters: [String: Any]? {
+   var parameters: [String: PrimitiveType]? {
      switch self {
      case let .selectMonth(month):
        return ["month": month] // ❌ 'Month' does not conform to protocol 'PrimitiveType'
      }
    }
  }

Discussion

It contains a breaking API change. It should be released with the new major version.

@devxoul devxoul added this to the 1.0.0 milestone Sep 8, 2019
@codecov-io
Copy link

codecov-io commented Sep 8, 2019

Codecov Report

Merging #26 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #26   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            2         2           
  Lines           33        33           
=========================================
  Hits            33        33           
Impacted Files Coverage Δ
Sources/Umbrella/Umbrella.swift 100.00% <ø> (ø)
Sources/Umbrella/RuntimeProviderType.swift 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 38c6eb3...a5403bc. Read the comment docs.

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.

2 participants