Skip to content
This repository has been archived by the owner on Sep 19, 2018. It is now read-only.

Reintroduce subscripting ifNull/ifNotFound methods (with deprecation warnings) #149

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
102 changes: 102 additions & 0 deletions Sources/JSONSubscripting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,105 @@ extension JSON {
}

}

// Deprecated methods.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have feels about // MARK: - Deprecated methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I feel it should be that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha. 👍


extension JSON {

@available(*, deprecated, message="Use decode(_:alongPath:type:) with options [.MissingKeyBecomesNil]")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it doesn't get formatted nicely, but do you think it's worth using backticks around the code in these messages? I suggest it because it may help readers of these messages to quickly identify the thing they need to do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like built-in warnings use single quotes around code bits. Will change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

public func decode<Decoded: JSONDecodable>(path: JSONPathType..., ifNotFound: Swift.Bool, type: Decoded.Type = Decoded.self) throws -> Decoded? {
let options: SubscriptingOptions = ifNotFound ? [.MissingKeyBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Decoded.init)
}

@available(*, deprecated, message="Use decode(_:alongPath:type:) with options [.NullBecomesNil]")
public func decode<Decoded: JSONDecodable>(path: JSONPathType..., ifNull: Swift.Bool, type: Decoded.Type = Decoded.self) throws -> Decoded? {
let options: SubscriptingOptions = ifNull ? [.NullBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Decoded.init)
}

@available(*, deprecated, message="Use double(_:alongPath:) with options [.MissingKeyBecomesNil]")
public func double(path: JSONPathType..., ifNotFound: Swift.Bool) throws -> Swift.Double? {
let options: SubscriptingOptions = ifNotFound ? [.MissingKeyBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Swift.Double.init)
}

@available(*, deprecated, message="Use double(_:alongPath:) with options [.NullBecomesNil]")
public func double(path: JSONPathType..., ifNull: Swift.Bool) throws -> Swift.Double? {
let options: SubscriptingOptions = ifNull ? [.NullBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Swift.Double.init)
}

@available(*, deprecated, message="Use int(_:alongPath:) with options [.MissingKeyBecomesNil]")
public func int(path: JSONPathType..., ifNotFound: Swift.Bool) throws -> Swift.Int? {
let options: SubscriptingOptions = ifNotFound ? [.MissingKeyBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Swift.Int.init)
}

@available(*, deprecated, message="Use int(_:alongPath:) with options [.NullBecomesNil]")
public func int(path: JSONPathType..., ifNull: Swift.Bool) throws -> Swift.Int? {
let options: SubscriptingOptions = ifNull ? [.NullBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Swift.Int.init)
}

@available(*, deprecated, message="Use string(_:alongPath:) with options [.MissingKeyBecomesNil]")
public func string(path: JSONPathType..., ifNotFound: Swift.Bool) throws -> Swift.String? {
let options: SubscriptingOptions = ifNotFound ? [.MissingKeyBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Swift.String.init)
}

@available(*, deprecated, message="Use string(_:alongPath:) with options [.NullBecomesNil]")
public func string(path: JSONPathType..., ifNull: Swift.Bool) throws -> Swift.String? {
let options: SubscriptingOptions = ifNull ? [.NullBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Swift.String.init)
}

@available(*, deprecated, message="Use bool(_:alongPath:) with options [.MissingKeyBecomesNil]")
public func bool(path: JSONPathType..., ifNotFound: Swift.Bool) throws -> Swift.Bool? {
let options: SubscriptingOptions = ifNotFound ? [.MissingKeyBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Swift.Bool.init)
}

@available(*, deprecated, message="Use bool(_:alongPath:) with options [.NullBecomesNil]")
public func bool(path: JSONPathType..., ifNull: Swift.Bool) throws -> Swift.Bool? {
let options: SubscriptingOptions = ifNull ? [.NullBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: Swift.Bool.init)
}

@available(*, deprecated, message="Use array(_:alongPath:) with options [.MissingKeyBecomesNil]")
public func array(path: JSONPathType..., ifNotFound: Swift.Bool) throws -> [JSON]? {
let options: SubscriptingOptions = ifNotFound ? [.MissingKeyBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: JSON.getArray)
}

@available(*, deprecated, message="Use array(_:alongPath:) with options [.NullBecomesNil]")
public func array(path: JSONPathType..., ifNull: Swift.Bool) throws -> [JSON]? {
let options: SubscriptingOptions = ifNull ? [.NullBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: JSON.getArray)
}

@available(*, deprecated, message="Use arrayOf(_:alongPath:) with options [.MissingKeyBecomesNil]")
public func arrayOf<Decoded: JSONDecodable>(path: JSONPathType..., ifNotFound: Swift.Bool) throws -> [Decoded]? {
let options: SubscriptingOptions = ifNotFound ? [.MissingKeyBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: JSON.getArrayOf)
}

@available(*, deprecated, message="Use arrayOf(_:alongPath:) with options [.NullBecomesNil]")
public func arrayOf<Decoded: JSONDecodable>(path: JSONPathType..., ifNull: Swift.Bool) throws -> [Decoded]? {
let options: SubscriptingOptions = ifNull ? [.NullBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: JSON.getArrayOf)
}

@available(*, deprecated, message="Use dictionary(_:alongPath:) with options [.MissingKeyBecomesNil]")
public func dictionary(path: JSONPathType..., ifNotFound: Swift.Bool) throws -> [Swift.String: JSON]? {
let options: SubscriptingOptions = ifNotFound ? [.MissingKeyBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: JSON.getDictionary)
}

@available(*, deprecated, message="Use dictionary(_:alongPath:) with options [.NullBecomesNil]")
public func dictionary(path: JSONPathType..., ifNull: Swift.Bool) throws -> [Swift.String: JSON]? {
let options: SubscriptingOptions = ifNull ? [.NullBecomesNil] : []
return try mapOptionalAtPath(path, alongPath: options, transform: JSON.getDictionary)
}

}
3 changes: 3 additions & 0 deletions Tests/JSONDecodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ class JSONDecodableTests: XCTestCase {
do {
let value: Int? = try JSONDictionary.int("key", alongPath: .NullBecomesNil)
XCTAssertEqual(value, nil)

let deprecatedValue: Int? = try JSONDictionary.int("key", ifNull: true)
XCTAssertEqual(deprecatedValue, nil)
} catch {
XCTFail("Should have retrieved nil for key `key` in `JSONDictionary` when specifying `ifNull` to be `true`.")
}
Expand Down
27 changes: 26 additions & 1 deletion Tests/JSONSubscriptingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,18 @@ class JSONSubscriptingTests: XCTestCase {
let earlyNull = [ "foo": nil ] as JSON
let string = try! earlyNull.string("foo", "bar", "baz", alongPath: .NullBecomesNil)
XCTAssertNil(string)

let deprecatedString = try! earlyNull.string("foo", "bar", "baz", ifNull: true)
XCTAssertNil(deprecatedString)
}

func testThatOptionalSubscriptingKeyNotFoundSucceeds() {
let keyNotFound = [ "foo": 2 ] as JSON
let string = try! keyNotFound.string("bar", alongPath: .MissingKeyBecomesNil)
XCTAssertNil(string)

let deprecatedString = try! keyNotFound.string("bar", ifNotFound: true)
XCTAssertNil(deprecatedString)
}

}
Expand Down Expand Up @@ -411,4 +417,23 @@ private func testUsage() {
_ = try? j.int(stringConst, 2, alongPath: .MissingKeyBecomesNil)
_ = try? j.int(stringConst, 3, alongPath: .NullBecomesNil)
_ = try? j.int(stringConst, 4, or: 42)
}
}

// Just for deprecated syntax validation, not for execution or being counted for coverage.
private func testDeprecatedUsage() {
let j = JSON.Null

_ = try? j.int(ifNotFound: true)
_ = try? j.int(ifNull: true)

_ = try? j.int("key", ifNotFound: true)
_ = try? j.int("key", ifNull: true)

_ = try? j.int(2, ifNotFound: true)
_ = try? j.int(3, ifNull: true)

let stringConst = "key"

_ = try? j.int(stringConst, 2, ifNotFound: true)
_ = try? j.int(stringConst, 3, ifNull: true)
}