Skip to content

Commit

Permalink
fix(v1): CPK has-many targetNames
Browse files Browse the repository at this point in the history
  • Loading branch information
lawmicha committed Feb 2, 2023
1 parent 9bf836c commit 88c4d67
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ import Foundation
/// - Warning: Although this has `public` access, it is intended for internal & codegen use and should not be used
/// directly by host applications. The behavior of this may change without warning.
public enum ModelAssociation {
case hasMany(associatedFieldName: String?)
case hasMany(associatedFieldName: String?, targetNames: [String])
case hasOne(associatedFieldName: String?, targetNames: [String])
case belongsTo(associatedFieldName: String?, targetNames: [String])

Expand All @@ -98,8 +98,8 @@ public enum ModelAssociation {
return .belongsTo(associatedFieldName: nil, targetNames: targetNames)
}

public static func hasMany(associatedWith: CodingKey?) -> ModelAssociation {
return .hasMany(associatedFieldName: associatedWith?.stringValue)
public static func hasMany(associatedWith: CodingKey?, targetNames: [String] = []) -> ModelAssociation {
return .hasMany(associatedFieldName: associatedWith?.stringValue, targetNames: targetNames)
}

@available(*, deprecated, message: "Use hasOne(associatedWith:targetNames:)")
Expand Down Expand Up @@ -234,13 +234,9 @@ extension ModelField {
if hasAssociation {
let associatedModel = requiredAssociatedModelName
switch association {
case .belongsTo(let associatedKey, _):
// TODO handle modelName casing (convert to camelCase)
let key = associatedKey ?? associatedModel
let schema = ModelRegistry.modelSchema(from: associatedModel)
return schema?.field(withName: key)
case .hasOne(let associatedKey, _),
.hasMany(let associatedKey):
case .belongsTo(let associatedKey, _),
.hasOne(let associatedKey, _),
.hasMany(let associatedKey, _):
// TODO handle modelName casing (convert to camelCase)
let key = associatedKey ?? associatedModel
let schema = ModelRegistry.modelSchema(from: associatedModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,13 @@ public enum ModelFieldDefinition {
is nullability: ModelFieldNullability = .required,
isReadOnly: Bool = false,
ofType type: Model.Type,
associatedWith associatedKey: CodingKey) -> ModelFieldDefinition {
associatedWith associatedKey: CodingKey,
targetNames: [String] = []) -> ModelFieldDefinition {
return .field(key,
is: nullability,
isReadOnly: isReadOnly,
ofType: .collection(of: type),
association: .hasMany(associatedWith: associatedKey))
association: .hasMany(associatedWith: associatedKey, targetNames: targetNames))
}

public static func hasOne(_ key: CodingKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class DataStoreObserveQueryOperationTests: XCTestCase {
let secondSnapshot = expectation(description: "second query snapshots")
let thirdSnapshot = expectation(description: "third query snapshot")
thirdSnapshot.isInverted = true

var querySnapshots = [DataStoreQuerySnapshot<Post>]()
let dispatchedModelSyncedEvent = AtomicValue(initialValue: false)
let operation = AWSDataStoreObserveQueryOperation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct TestJsonModelRegistration: AmplifyModelRegistration {
let comments = ModelField(name: "comments",
type: .collection(of: "Comment"),
isRequired: false,
association: .hasMany(associatedFieldName: "post"))
association: .hasMany(associatedFieldName: "post", targetNames: []))
let postSchema = ModelSchema(name: "Post",
listPluralName: "Posts",
syncPluralName: "Posts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ModelFieldAssociationTests: XCTestCase {

func testHasManyWithCodingKeys() {
let hasMany = ModelAssociation.hasMany(associatedWith: Comment.keys.post)
guard case .hasMany(let fieldName) = hasMany else {
guard case .hasMany(let fieldName, _) = hasMany else {
XCTFail("Should create hasMany association")
return
}
Expand Down

0 comments on commit 88c4d67

Please sign in to comment.