Skip to content

Commit 34da4a4

Browse files
authored
Merge pull request #9871 from itaiferber/swift-4.0-branch
[4.0] Enhancements to Codable API
2 parents c2894ed + e114f8e commit 34da4a4

File tree

10 files changed

+1463
-344
lines changed

10 files changed

+1463
-344
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2032,7 +2032,7 @@ ERROR(broken_decodable_requirement,none,
20322032
NOTE(codable_extraneous_codingkey_case_here,none,
20332033
"CodingKey case %0 does match any stored properties", (Identifier))
20342034
NOTE(codable_non_conforming_property_here,none,
2035-
"cannot automatically synthesize %0 because %1 does not conform to %0", (Type, Identifier))
2035+
"cannot automatically synthesize %0 because %1 does not conform to %0", (Type, Type))
20362036
NOTE(codable_non_decoded_property_here,none,
20372037
"cannot automatically synthesize %0 because %1 does not have a matching CodingKey and does not have a default value", (Type, Identifier))
20382038
NOTE(codable_codingkeys_type_is_not_an_enum_here,none,

include/swift/AST/KnownIdentifiers.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ IDENTIFIER(Darwin)
4040
IDENTIFIER(dealloc)
4141
IDENTIFIER(Decodable)
4242
IDENTIFIER(decode)
43+
IDENTIFIER(decodeIfPresent)
4344
IDENTIFIER(Decoder)
4445
IDENTIFIER(decoder)
4546
IDENTIFIER(deinit)
4647
IDENTIFIER(Element)
4748
IDENTIFIER(Encodable)
4849
IDENTIFIER(encode)
50+
IDENTIFIER(encodeIfPresent)
4951
IDENTIFIER(Encoder)
5052
IDENTIFIER(encoder)
5153
IDENTIFIER(error)

lib/Sema/DerivedConformanceCodable.cpp

Lines changed: 192 additions & 80 deletions
Large diffs are not rendered by default.

stdlib/public/SDK/Foundation/JSONEncoder.swift

Lines changed: 213 additions & 91 deletions
Large diffs are not rendered by default.

stdlib/public/SDK/Foundation/PlistEncoder.swift

Lines changed: 216 additions & 74 deletions
Large diffs are not rendered by default.

stdlib/public/core/Codable.swift

Lines changed: 528 additions & 84 deletions
Large diffs are not rendered by default.

test/IDE/complete_generic_optional.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ struct Foo<T> {
1111
// SR-642 Code completion does not instantiate generic arguments of a type wrapped in an optional
1212
let x: Foo<Bar>? = Foo<Bar>()
1313
x.#^FOO_OPTIONAL_1^#
14-
// FOO_OPTIONAL_1: Begin completions, 6 items
14+
// FOO_OPTIONAL_1: Begin completions, 7 items
1515
// FOO_OPTIONAL_1-DAG: Decl[InstanceMethod]/CurrNominal/Erase[1]: ?.myFunction({#(foobar): Bar#})[#Void#]; name=myFunction(foobar: Bar)
1616
// FOO_OPTIONAL_1: End completions

test/decl/protocol/special/coding/class_codable_nonconforming_property.swift

Lines changed: 149 additions & 5 deletions
Large diffs are not rendered by default.

test/decl/protocol/special/coding/struct_codable_nonconforming_property.swift

Lines changed: 149 additions & 5 deletions
Large diffs are not rendered by default.

test/stdlib/TestJSONEncoder.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ class TestJSONEncoder : TestJSONEncoderSuper {
5858

5959
func testEncodingTopLevelStructuredClass() {
6060
// Person is a class with multiple fields.
61+
let expectedJSON = "{\"name\":\"Johnny Appleseed\",\"email\":\"[email protected]\"}".data(using: .utf8)!
6162
let person = Person.testValue
62-
_testRoundTrip(of: person)
63+
_testRoundTrip(of: person, expectedJSON: expectedJSON)
6364
}
6465

6566
func testEncodingTopLevelDeepStructuredType() {
@@ -471,13 +472,21 @@ fileprivate class Person : Codable, Equatable {
471472
let name: String
472473
let email: String
473474

474-
init(name: String, email: String) {
475+
// FIXME: This property is present only in order to test the expected result of Codable synthesis in the compiler.
476+
// We want to test against expected encoded output (to ensure this generates an encodeIfPresent call), but we need an output format for that.
477+
// Once we have a VerifyingEncoder for compiler unit tests, we should move this test there.
478+
let website: URL?
479+
480+
init(name: String, email: String, website: URL? = nil) {
475481
self.name = name
476482
self.email = email
483+
self.website = website
477484
}
478485

479486
static func ==(_ lhs: Person, _ rhs: Person) -> Bool {
480-
return lhs.name == rhs.name && lhs.email == rhs.email
487+
return lhs.name == rhs.name &&
488+
lhs.email == rhs.email &&
489+
lhs.website == rhs.website
481490
}
482491

483492
static var testValue: Person {

0 commit comments

Comments
 (0)