Skip to content

Commit 0f32529

Browse files
authored
Merge pull request swiftlang#15954 from nkcsgexi/optional-dictionary-migration
2 parents 80defe5 + a138744 commit 0f32529

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,9 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
719719
case NodeAnnotation::OptionalDictionaryKeyUpdate:
720720
Segs = {"Optional", "Dictionary", "[String: Any]?"};
721721
Segs.push_back((Twine("[") + NewType +": Any]?").str());
722-
Segs.push_back("// Not implemented");
722+
Segs.push_back((Twine("\tguard let input = input else { return nil }\n"
723+
"\treturn Dictionary(uniqueKeysWithValues: input.map"
724+
" { key, value in (") + NewType + "(rawValue: key), value)})").str());
723725
break;
724726
case NodeAnnotation::ArrayMemberUpdate:
725727
Segs = {"", "Array", "[String]"};

test/Migrator/Inputs/Cities.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ public class Container {
3838
public var Value: String = ""
3939
public func addingAttributes(_ input: [String: Any]) {}
4040
public func adding(attributes: [String: Any]) {}
41-
}
41+
public func adding(optionalAttributes: [String: Any]?) {}
42+
public init(optionalAttributes: [String: Any]?) {}
43+
}

test/Migrator/Inputs/string-representable.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,26 @@
3232
"RightComment": "SimpleAttribute",
3333
"ModuleName": "Cities"
3434
},
35+
{
36+
"DiffItemKind": "CommonDiffItem",
37+
"NodeKind": "Function",
38+
"NodeAnnotation": "OptionalDictionaryKeyUpdate",
39+
"ChildIndex": "1",
40+
"LeftUsr": "s:6Cities9ContainerC6adding18optionalAttributesys10DictionaryVySSypGSg_tF",
41+
"LeftComment": "",
42+
"RightUsr": "",
43+
"RightComment": "SimpleAttribute",
44+
"ModuleName": "Cities"
45+
},
46+
{
47+
"DiffItemKind": "CommonDiffItem",
48+
"NodeKind": "Constructor",
49+
"NodeAnnotation": "OptionalDictionaryKeyUpdate",
50+
"ChildIndex": "1",
51+
"LeftUsr": "s:6Cities9ContainerC18optionalAttributesACs10DictionaryVySSypGSg_tcfc",
52+
"LeftComment": "",
53+
"RightUsr": "",
54+
"RightComment": "SimpleAttribute",
55+
"ModuleName": "Cities"
56+
},
3557
]

test/Migrator/string-representable.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ func foo(_ c: Container) -> String {
1111
c.addingAttributes(["a": "b", "a": "b", "a": "b"])
1212
c.addingAttributes(["a": "b", "a": "b", "a": "b"])
1313
c.adding(attributes: ["a": 1, "a": 2, "a": 3])
14+
c.adding(optionalAttributes: ["a": 1, "a": 2, "a": 3])
15+
_ = Container(optionalAttributes: nil)
1416
return c.Value
1517
}

test/Migrator/string-representable.swift.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ func foo(_ c: Container) -> String {
1111
c.addingAttributes(converToCitiesContainerAttributeDictionary(["a": "b", "a": "b", "a": "b"]))
1212
c.addingAttributes(converToCitiesContainerAttributeDictionary(["a": "b", "a": "b", "a": "b"]))
1313
c.adding(attributes: converToSimpleAttributeDictionary(["a": 1, "a": 2, "a": 3]))
14+
c.adding(optionalAttributes: converToOptionalSimpleAttributeDictionary(["a": 1, "a": 2, "a": 3]))
15+
_ = Container(optionalAttributes: converToOptionalSimpleAttributeDictionary(nil))
1416
return c.Value.rawValue
1517
}
1618

@@ -23,3 +25,9 @@ fileprivate func converToCitiesContainerAttributeDictionary(_ input: [String: An
2325
fileprivate func converToSimpleAttributeDictionary(_ input: [String: Any]) -> [SimpleAttribute: Any] {
2426
return Dictionary(uniqueKeysWithValues: input.map { key, value in (SimpleAttribute(rawValue: key), value)})
2527
}
28+
29+
// Helper function inserted by Swift 4.2 migrator.
30+
fileprivate func converToOptionalSimpleAttributeDictionary(_ input: [String: Any]?) -> [SimpleAttribute: Any]? {
31+
guard let input = input else { return nil }
32+
return Dictionary(uniqueKeysWithValues: input.map { key, value in (SimpleAttribute(rawValue: key), value)})
33+
}

0 commit comments

Comments
 (0)