devxoul / Umbrella

☂️ Analytics abstraction layer for Swift
MIT License
615 stars 48 forks source link

Use PrimitiveType as values of parameter dictionary #26

Open devxoul opened 5 years ago

devxoul commented 5 years ago

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.

codecov-io commented 5 years ago

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.