Skip to content

Commit f575243

Browse files
authored
fix: revert CPK has-one associatedFields (#2750)
1 parent 9ce0c58 commit f575243

File tree

6 files changed

+11
-31
lines changed

6 files changed

+11
-31
lines changed

Amplify/Categories/DataStore/Model/Internal/Schema/ModelField+Association.swift

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ import Foundation
8888
/// directly by host applications. The behavior of this may change without warning.
8989
public enum ModelAssociation {
9090
case hasMany(associatedFieldName: String?, associatedFieldNames: [String] = [])
91-
case hasOne(associatedFieldName: String?, associatedFieldNames: [String] = [], targetNames: [String])
91+
case hasOne(associatedFieldName: String?, targetNames: [String])
9292
case belongsTo(associatedFieldName: String?, targetNames: [String])
9393

9494
public static let belongsTo: ModelAssociation = .belongsTo(associatedFieldName: nil, targetNames: [])
@@ -104,19 +104,14 @@ public enum ModelAssociation {
104104
associatedFieldNames: associatedFields.map { $0.stringValue })
105105
}
106106

107-
@available(*, deprecated, message: "Use hasOne(associatedWith:associatedFields:targetNames:)")
108-
public static func hasOne(associatedWith: CodingKey?,
109-
targetName: String? = nil) -> ModelAssociation {
107+
@available(*, deprecated, message: "Use hasOne(associatedWith:targetNames:)")
108+
public static func hasOne(associatedWith: CodingKey?, targetName: String? = nil) -> ModelAssociation {
110109
let targetNames = targetName.map { [$0] } ?? []
111110
return .hasOne(associatedWith: associatedWith, targetNames: targetNames)
112111
}
113112

114-
public static func hasOne(associatedWith: CodingKey? = nil,
115-
associatedFields: [CodingKey] = [],
116-
targetNames: [String] = []) -> ModelAssociation {
117-
return .hasOne(associatedFieldName: associatedWith?.stringValue,
118-
associatedFieldNames: associatedFields.map { $0.stringValue },
119-
targetNames: targetNames)
113+
public static func hasOne(associatedWith: CodingKey?, targetNames: [String] = []) -> ModelAssociation {
114+
return .hasOne(associatedFieldName: associatedWith?.stringValue, targetNames: targetNames)
120115
}
121116

122117
@available(*, deprecated, message: "Use belongsTo(associatedWith:targetNames:)")
@@ -242,7 +237,7 @@ extension ModelField {
242237
let associatedModel = requiredAssociatedModelName
243238
switch association {
244239
case .belongsTo(let associatedKey, _),
245-
.hasOne(let associatedKey, _, _),
240+
.hasOne(let associatedKey, _),
246241
.hasMany(let associatedKey, _):
247242
// TODO handle modelName casing (convert to camelCase)
248243
let key = associatedKey ?? associatedModel

Amplify/Categories/DataStore/Model/Internal/Schema/ModelSchema+Definition.swift

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -280,21 +280,6 @@ public enum ModelFieldDefinition {
280280
association: .hasOne(associatedWith: associatedKey, targetNames: targetNames))
281281
}
282282

283-
public static func hasOne(_ key: CodingKey,
284-
is nullability: ModelFieldNullability = .required,
285-
isReadOnly: Bool = false,
286-
ofType type: Model.Type,
287-
associatedFields associatedKeys: [CodingKey],
288-
targetNames: [String]) -> ModelFieldDefinition {
289-
return .field(key,
290-
is: nullability,
291-
isReadOnly: isReadOnly,
292-
ofType: .model(type: type),
293-
association: .hasOne(associatedWith: associatedKeys.first,
294-
associatedFields: associatedKeys,
295-
targetNames: targetNames))
296-
}
297-
298283
public static func belongsTo(_ key: CodingKey,
299284
is nullability: ModelFieldNullability = .required,
300285
isReadOnly: Bool = false,

AmplifyPlugins/Core/AWSPluginsCore/Model/Support/Model+GraphQL.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ extension Model {
207207
let defaultFieldName = modelName.camelCased() + modelField.name.pascalCased() + "Id"
208208
if case let .belongsTo(_, targetNames) = modelField.association, !targetNames.isEmpty {
209209
return targetNames
210-
} else if case let .hasOne(_, _, targetNames) = modelField.association,
210+
} else if case let .hasOne(_, targetNames) = modelField.association,
211211
!targetNames.isEmpty {
212212
return targetNames
213213
}

AmplifyPlugins/Core/AWSPluginsCore/Model/Support/QueryPredicate+GraphQL.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ extension QueryPredicateOperation: GraphQLFilterConvertible {
131131
}
132132
let targetName = targetNames.first ?? defaultFieldName
133133
return targetName
134-
case .hasOne(_, _, let targetNames):
134+
case .hasOne(_, let targetNames):
135135
guard targetNames.count == 1 else {
136136
preconditionFailure("QueryPredicate not supported on associated field with composite key: \(field)")
137137
}

AmplifyPlugins/DataStore/AWSDataStoreCategoryPlugin/Storage/SQLite/ModelSchema+SQLite.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ extension ModelField: SQLColumn {
8282
var sqlName: String {
8383
if case let .belongsTo(_, targetNames) = association {
8484
return foreignKeySqlName(withAssociationTargets: targetNames)
85-
} else if case let .hasOne(_, _, targetNames) = association {
85+
} else if case let .hasOne(_, targetNames) = association {
8686
return foreignKeySqlName(withAssociationTargets: targetNames)
8787
}
8888
return name

AmplifyTests/CategoryTests/DataStore/ModelFieldAssociationTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ModelFieldAssociationTests: XCTestCase {
3737

3838
func testHasOneWithCodingKeys() {
3939
let hasOne = ModelAssociation.hasOne(associatedWith: Comment.keys.post, targetName: nil)
40-
guard case .hasOne(let fieldName, _, let target) = hasOne else {
40+
guard case .hasOne(let fieldName, let target) = hasOne else {
4141
XCTFail("Should create hasOne association")
4242
return
4343
}
@@ -47,7 +47,7 @@ class ModelFieldAssociationTests: XCTestCase {
4747

4848
func testHasOneWithCodingKeysWithTargetName() {
4949
let hasOne = ModelAssociation.hasOne(associatedWith: Comment.keys.post, targetName: "postID")
50-
guard case .hasOne(let fieldName, _, let target) = hasOne else {
50+
guard case .hasOne(let fieldName, let target) = hasOne else {
5151
XCTFail("Should create hasOne association")
5252
return
5353
}

0 commit comments

Comments
 (0)