Skip to content

Stop accepting null as a basic type for userdefaults #188

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

Merged
merged 1 commit into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Sources/Segment/Utilities/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ internal class Storage: Subscriber {
result = true
} else {
switch value {
case is NSNull:
fallthrough
// NSNull is not valid for UserDefaults
//case is NSNull:
// fallthrough
case is Decimal:
fallthrough
case is NSNumber:
Expand Down
50 changes: 50 additions & 0 deletions Tests/Segment-Tests/Storage_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,56 @@ class StorageTests: XCTestCase {
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testSettingsWrite() throws {
let dummySettings = """
{
"integrations": {
"Segment.io": {
"apiKey": "1234",
"unbundledIntegrations": [],
"addBundledMetadata": true
}
},
"middlewareSettings": {
"routingRules": [
{
"transformers": [
[
{
"type": "allow_properties",
"config": {
"allow": {
"traits": null,
"context": null,
"_metadata": null,
"integrations": null,
}
}
}
]
]
}
]
},
}
"""
let jsonData = dummySettings.data(using: .utf8)!
let jsonSettings = try! JSONSerialization.jsonObject(with: jsonData)

let analytics = Analytics(configuration: Configuration(writeKey: "test"))
analytics.storage.hardReset(doYouKnowHowToUseThis: true)

// this will crash if it fails.
let j = try! JSON(jsonSettings)
analytics.storage.write(.settings, value: j)

RunLoop.main.run(until: Date.distantPast)

let result: JSON? = analytics.storage.read(.settings)

XCTAssertNotNil(result)
}

func testBasicWriting() throws {
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
Expand Down