From 1c625a24359e2b47b78e42ed1e749954491e61fe Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Wed, 23 Mar 2016 14:57:42 -0400 Subject: [PATCH 1/4] Reintroduce subscripting ifNull/ifNotFound methods (with deprecation warnings) --- Sources/JSONSubscripting.swift | 102 ++++++++++++++++++++++++++++++ Tests/JSONDecodableTests.swift | 3 + Tests/JSONSubscriptingTests.swift | 27 +++++++- 3 files changed, 131 insertions(+), 1 deletion(-) diff --git a/Sources/JSONSubscripting.swift b/Sources/JSONSubscripting.swift index 309288e2..1339b703 100644 --- a/Sources/JSONSubscripting.swift +++ b/Sources/JSONSubscripting.swift @@ -539,3 +539,105 @@ extension JSON { } } + +// Deprecated methods. + +extension JSON { + + @available(*, deprecated, message="Use decode(_:alongPath:type:) with options [.MissingKeyBecomesNil]") + public func decode(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(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(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(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) + } + +} diff --git a/Tests/JSONDecodableTests.swift b/Tests/JSONDecodableTests.swift index 8997a6b6..158d4fbe 100644 --- a/Tests/JSONDecodableTests.swift +++ b/Tests/JSONDecodableTests.swift @@ -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`.") } diff --git a/Tests/JSONSubscriptingTests.swift b/Tests/JSONSubscriptingTests.swift index aa2828c7..5e5a4f99 100644 --- a/Tests/JSONSubscriptingTests.swift +++ b/Tests/JSONSubscriptingTests.swift @@ -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) } } @@ -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) -} \ No newline at end of file +} + +// 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) +} From 85f12a901633a2dd7de3467df58ab022e34aa8ac Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Thu, 24 Mar 2016 09:32:56 -0400 Subject: [PATCH 2/4] Use "// MARK" for deprecated methods --- Sources/JSONSubscripting.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/JSONSubscripting.swift b/Sources/JSONSubscripting.swift index 1339b703..4d403c2c 100644 --- a/Sources/JSONSubscripting.swift +++ b/Sources/JSONSubscripting.swift @@ -540,7 +540,7 @@ extension JSON { } -// Deprecated methods. +// MARK: - Deprecated methods extension JSON { From d9d77111f7a77ec4cdb78d7ddc606f66cc1139cd Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Thu, 24 Mar 2016 09:33:26 -0400 Subject: [PATCH 3/4] Add quotes around code snippets in deprecation messages --- Sources/JSONSubscripting.swift | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Sources/JSONSubscripting.swift b/Sources/JSONSubscripting.swift index 4d403c2c..785f8ce9 100644 --- a/Sources/JSONSubscripting.swift +++ b/Sources/JSONSubscripting.swift @@ -544,97 +544,97 @@ extension JSON { extension JSON { - @available(*, deprecated, message="Use decode(_:alongPath:type:) with options [.MissingKeyBecomesNil]") + @available(*, deprecated, message="Use 'decode(_:alongPath:type:)' with options '[.MissingKeyBecomesNil]'") public func decode(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]") + @available(*, deprecated, message="Use 'decode(_:alongPath:type:)' with options '[.NullBecomesNil]'") public func decode(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]") + @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]") + @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]") + @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]") + @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]") + @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]") + @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]") + @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]") + @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]") + @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]") + @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]") + @available(*, deprecated, message="Use 'arrayOf(_:alongPath:)' with options '[.MissingKeyBecomesNil]'") public func arrayOf(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]") + @available(*, deprecated, message="Use 'arrayOf(_:alongPath:)' with options '[.NullBecomesNil]'") public func arrayOf(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]") + @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]") + @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) From d14681c378cb86708dc2368f773c238deb4b9139 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Thu, 24 Mar 2016 09:34:11 -0400 Subject: [PATCH 4/4] Remove tests of deprecated methods --- Tests/JSONDecodableTests.swift | 3 --- Tests/JSONSubscriptingTests.swift | 25 ------------------------- 2 files changed, 28 deletions(-) diff --git a/Tests/JSONDecodableTests.swift b/Tests/JSONDecodableTests.swift index 158d4fbe..8997a6b6 100644 --- a/Tests/JSONDecodableTests.swift +++ b/Tests/JSONDecodableTests.swift @@ -194,9 +194,6 @@ 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`.") } diff --git a/Tests/JSONSubscriptingTests.swift b/Tests/JSONSubscriptingTests.swift index 5e5a4f99..a4fa0b13 100644 --- a/Tests/JSONSubscriptingTests.swift +++ b/Tests/JSONSubscriptingTests.swift @@ -343,18 +343,12 @@ 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) } } @@ -418,22 +412,3 @@ private func testUsage() { _ = 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) -}