Skip to content

Commit

Permalink
fix(v1): CPK has-many associatedFields
Browse files Browse the repository at this point in the history
  • Loading branch information
lawmicha committed Feb 2, 2023
1 parent 9bf836c commit 764842d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 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?, associatedFieldNames: [String])
case hasOne(associatedFieldName: String?, targetNames: [String])
case belongsTo(associatedFieldName: String?, targetNames: [String])

Expand All @@ -97,9 +97,9 @@ public enum ModelAssociation {
let targetNames = targetName.map { [$0] } ?? []
return .belongsTo(associatedFieldName: nil, targetNames: targetNames)
}

public static func hasMany(associatedWith: CodingKey?) -> ModelAssociation {
return .hasMany(associatedFieldName: associatedWith?.stringValue)
public static func hasMany(associatedWith: CodingKey? = nil, associatedWithFields: [CodingKey] = []) -> ModelAssociation {
return .hasMany(associatedFieldName: associatedWith?.stringValue, associatedFieldNames: associatedWithFields.map { $0.stringValue })
}

@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 @@ -241,6 +241,18 @@ public enum ModelFieldDefinition {
association: .hasMany(associatedWith: associatedKey))
}

public static func hasMany(_ key: CodingKey,
is nullability: ModelFieldNullability = .required,
isReadOnly: Bool = false,
ofType type: Model.Type,
associatedFields associatedKeys: [CodingKey]) -> ModelFieldDefinition {
return .field(key,
is: nullability,
isReadOnly: isReadOnly,
ofType: .collection(of: type),
association: .hasMany(associatedWith: associatedKeys.first ?? nil, associatedFields: associatedKeys))
}

public static func hasOne(_ key: CodingKey,
is nullability: ModelFieldNullability = .required,
isReadOnly: Bool = false,
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", associatedFieldNames: ["post"]))
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 764842d

Please sign in to comment.