Repository: segmentio/analytics-swift (swift)

Issue:
Support Swift's `Set` as a JSON type
**Is your feature request related to a problem? Please describe.**
`track(name: String, properties: [String: Any]? = nil)` will crash if one of the `Any` is a Swift `Set`. It will _not_ crash if that same property is an `Array`.

**Describe the solution you'd like**
Because to JSON, `Array` and `Set` are identical, it would be nice if Segment's `JSON` type could encode `Set` without crashing

**Describe alternatives you've considered**
At this point, we just ensure we're always sending an `Array`

**Additional context**
N/A

Pull request description:
- Fixes #198 .
- Adds support for Set as a JSON type.
- Added test.

Patch:
diff --git a/Sources/Segment/Utilities/JSON.swift b/Sources/Segment/Utilities/JSON.swift
index a435c097..308040f4 100644
--- a/Sources/Segment/Utilities/JSON.swift
+++ b/Sources/Segment/Utilities/JSON.swift
@@ -59,7 +59,9 @@ public enum JSON: Equatable {
             self = .string(string)
         case let bool as Bool:
             self = .bool(bool)
-        case let array as [Any]:
+        case let aSet as Set<AnyHashable>:
+            self = .array(try aSet.map(JSON.init))
+        case let array as Array<Any>:
             self = .array(try array.map(JSON.init))
         case let object as [String: Any]:
             self = .object(try object.mapValues(JSON.init))


Test patch:
diff --git a/Tests/Segment-Tests/JSON_Tests.swift b/Tests/Segment-Tests/JSON_Tests.swift
index 525d4c2f..1fa42166 100644
--- a/Tests/Segment-Tests/JSON_Tests.swift
+++ b/Tests/Segment-Tests/JSON_Tests.swift
@@ -52,6 +52,16 @@ class JSONTests: XCTestCase {
         }
     }
     
+    func testJSONCollectionTypes() throws {
+        let testSet: Set = ["1", "2", "3"]
+        let traits = try! JSON(["type": NSNull(), "preferences": ["bwack"], "key": testSet])
+        let jsonSet = traits["key"]
+        XCTAssertNotNil(jsonSet)
+        let array = jsonSet!.arrayValue!
+        XCTAssertNotNil(array)
+        XCTAssertEqual(array.count, 3)
+    }
+    
     func testJSONNil() throws {
         let traits = try JSON(["type": NSNull(), "preferences": ["bwack"], "key": nil])
         let encoder = JSONEncoder()