Skip to content

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.

3 participants