From 73a842df360607ba9d2740a582959eb4cfabca1e Mon Sep 17 00:00:00 2001 From: Michael Law <1365977+lawmicha@users.noreply.github.com> Date: Wed, 30 Nov 2022 13:28:51 -0500 Subject: [PATCH 01/17] feat(amplify-codegen): iOS LazyReference --- .gitignore | 1 + .../appsync-swift-visitor.test.ts.snap | 27 +++++-- .../visitors/appsync-swift-visitor.test.ts | 9 ++- .../appsync-swift-visitor.test.ts.snap | 81 ++++++++++++++----- .../src/visitors/appsync-swift-visitor.ts | 70 ++++++++++++---- 5 files changed, 147 insertions(+), 41 deletions(-) diff --git a/.gitignore b/.gitignore index e4c43e0b0..0ea050207 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ packages/**/reports/junit/* test.out.log *.tsbuildinfo package-lock.json +.idea \ No newline at end of file diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap index 477b8419f..ffcec7e07 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -501,7 +501,12 @@ import Foundation public struct Project: Model { public let projectId: String public let name: String - public var team: Team? + internal var _team: LazyReference + public var team: Team? { + get async throws { + try await _team.get() + } + } public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? public var projectTeamTeamId: String? @@ -529,7 +534,7 @@ public struct Project: Model { projectTeamName: String? = nil) { self.projectId = projectId self.name = name - self.team = team + self._team = LazyReference(team) self.createdAt = createdAt self.updatedAt = updatedAt self.projectTeamTeamId = projectTeamTeamId @@ -601,7 +606,12 @@ import Foundation public struct Team: Model { public let teamId: String public let name: String - public var project: Project? + internal var _project: LazyReference + public var project: Project? { + get async throws { + try await _project.get() + } + } public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? @@ -621,7 +631,7 @@ public struct Team: Model { updatedAt: Temporal.DateTime? = nil) { self.teamId = teamId self.name = name - self.project = project + self._project = LazyReference(project) self.createdAt = createdAt self.updatedAt = updatedAt } @@ -902,7 +912,12 @@ import Foundation public struct task: Model { public let id: String public var title: String - public var todo: Todo? + internal var _todo: LazyReference + public var todo: Todo? { + get async throws { + try await _todo.get() + } + } public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? @@ -922,7 +937,7 @@ public struct task: Model { updatedAt: Temporal.DateTime? = nil) { self.id = id self.title = title - self.todo = todo + self._todo = LazyReference(todo) self.createdAt = createdAt self.updatedAt = updatedAt } diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts index 04c2d3e43..7a278b69c 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts @@ -668,7 +668,12 @@ describe('AppSyncSwiftVisitor', () => { public let id: String public var title: String public var done: Bool - public var todo: Todo? + internal var _todo: LazyReference + public var todo: Todo? { + get async throws { + try await _todo.get() + } + } public var time: Temporal.Time? public var createdOn: Temporal.Date? public var createdAt: Temporal.DateTime? @@ -700,7 +705,7 @@ describe('AppSyncSwiftVisitor', () => { self.id = id self.title = title self.done = done - self.todo = todo + self._todo = LazyReference(todo) self.time = time self.createdOn = createdOn self.createdAt = createdAt diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap index abe0b15c2..c51b8d7b9 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -77,7 +77,12 @@ import Foundation public struct Comment: Model { public let id: String public var content: String - public var post: Post? + internal var _post: LazyReference + public var post: Post? { + get async throws { + try await _post.get() + } + } public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? @@ -97,7 +102,7 @@ public struct Comment: Model { updatedAt: Temporal.DateTime? = nil) { self.id = id self.content = content - self.post = post + self._post = LazyReference(post) self.createdAt = createdAt self.updatedAt = updatedAt } @@ -150,7 +155,12 @@ import Foundation public struct Project2: Model { public let id: String public var name: String? - public var team: Team2? + internal var _team: LazyReference + public var team: Team2? { + get async throws { + try await _team.get() + } + } public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? public var project2TeamId: String? @@ -174,7 +184,7 @@ public struct Project2: Model { project2TeamId: String? = nil) { self.id = id self.name = name - self.team = team + self._team = LazyReference(team) self.createdAt = createdAt self.updatedAt = updatedAt self.project2TeamId = project2TeamId @@ -226,7 +236,12 @@ import Foundation public struct Team2: Model { public let id: String public var name: String - public var project: Project2? + internal var _project: LazyReference + public var project: Project2? { + get async throws { + try await _project.get() + } + } public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? @@ -246,7 +261,7 @@ public struct Team2: Model { updatedAt: Temporal.DateTime? = nil) { self.id = id self.name = name - self.project = project + self._project = LazyReference(project) self.createdAt = createdAt self.updatedAt = updatedAt } @@ -364,7 +379,12 @@ import Foundation public struct Post7V2: Model { public let id: String public var title: String - public var blog: Blog7V2? + internal var _blog: LazyReference + public var blog: Blog7V2? { + get async throws { + try await _blog.get() + } + } public var comments: List? public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? @@ -388,7 +408,7 @@ public struct Post7V2: Model { updatedAt: Temporal.DateTime? = nil) { self.id = id self.title = title - self.blog = blog + self._blog = LazyReference(blog) self.comments = comments self.createdAt = createdAt self.updatedAt = updatedAt @@ -440,7 +460,12 @@ import Foundation public struct Comment7V2: Model { public let id: String public var content: String? - public var post: Post7V2? + internal var _post: LazyReference + public var post: Post7V2? { + get async throws { + try await _post.get() + } + } public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? @@ -460,7 +485,7 @@ public struct Comment7V2: Model { updatedAt: Temporal.DateTime? = nil) { self.id = id self.content = content - self.post = post + self._post = LazyReference(post) self.createdAt = createdAt self.updatedAt = updatedAt } @@ -509,7 +534,12 @@ import Foundation public struct Project: Model { public let id: String public var name: String? - public var team: Team? + internal var _team: LazyReference + public var team: Team? { + get async throws { + try await _team.get() + } + } public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? public var projectTeamId: String? @@ -533,7 +563,7 @@ public struct Project: Model { projectTeamId: String? = nil) { self.id = id self.name = name - self.team = team + self._team = LazyReference(team) self.createdAt = createdAt self.updatedAt = updatedAt self.projectTeamId = projectTeamId @@ -585,7 +615,12 @@ import Foundation public struct Team: Model { public let id: String public var name: String - public var project: Project? + internal var _project: LazyReference + public var project: Project? { + get async throws { + try await _project.get() + } + } public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? @@ -605,7 +640,7 @@ public struct Team: Model { updatedAt: Temporal.DateTime? = nil) { self.id = id self.name = name - self.project = project + self._project = LazyReference(project) self.createdAt = createdAt self.updatedAt = updatedAt } @@ -1004,7 +1039,12 @@ public struct Project2: Model { public let id: String public var name: String? public var teamID: String? - public var team: Team2? + internal var _team: LazyReference + public var team: Team2? { + get async throws { + try await _team.get() + } + } public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? @@ -1028,7 +1068,7 @@ public struct Project2: Model { self.id = id self.name = name self.teamID = teamID - self.team = team + self._team = LazyReference(team) self.createdAt = createdAt self.updatedAt = updatedAt } @@ -1279,7 +1319,12 @@ import Foundation public struct Project: Model { public let id: String public var name: String? - public var team: Team? + internal var _team: LazyReference + public var team: Team? { + get async throws { + try await _team.get() + } + } public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? public var projectTeamId: String? @@ -1303,7 +1348,7 @@ public struct Project: Model { projectTeamId: String? = nil) { self.id = id self.name = name - self.team = team + self._team = LazyReference(team) self.createdAt = createdAt self.updatedAt = updatedAt self.projectTeamId = projectTeamId diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts index 11d2d4eca..523c4c2cc 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts @@ -62,10 +62,7 @@ export class AppSyncSwiftVisitor< const shouldUseModelNameFieldInHasManyAndBelongsTo = true; // This flag is going to be used to tight-trigger on JS implementations only. const shouldImputeKeyForUniDirectionalHasMany = false; - this.processDirectives( - shouldUseModelNameFieldInHasManyAndBelongsTo, - shouldImputeKeyForUniDirectionalHasMany - ); + this.processDirectives(shouldUseModelNameFieldInHasManyAndBelongsTo, shouldImputeKeyForUniDirectionalHasMany); const code = [`// swiftlint:disable all`]; if (this._parsedConfig.generate === CodeGenGenerateEnum.metadata) { @@ -98,15 +95,53 @@ export class AppSyncSwiftVisitor< const fieldType = this.getNativeType(field); const isVariable = !primaryKeyComponentFieldsName.includes(field.name); const listType: ListType = field.connectionInfo ? ListType.LIST : ListType.ARRAY; - structBlock.addProperty(this.getFieldName(field), fieldType, undefined, 'public', { - optional: !this.isFieldRequired(field), - isList: field.isList, - variable: isVariable, - isEnum: this.isEnumType(field), - listType: field.isList ? listType : undefined, - isListNullable: field.isListNullable, - handleListNullabilityTransparently: this.isHasManyConnectionField(field) ? false : this.config.handleListNullabilityTransparently, - }); + const connectionHasOneOrBelongsTo: boolean = field.connectionInfo + ? field.connectionInfo.kind === CodeGenConnectionType.HAS_ONE || field.connectionInfo.kind === CodeGenConnectionType.BELONGS_TO + : false; + if (connectionHasOneOrBelongsTo) { + structBlock.addProperty(`_${this.getFieldName(field)}`, `LazyReference<${fieldType}>`, undefined, `internal`, { + optional: false, + isList: field.isList, + variable: isVariable, + isEnum: this.isEnumType(field), + listType: field.isList ? listType : undefined, + isListNullable: field.isListNullable, + handleListNullabilityTransparently: this.isHasManyConnectionField(field) + ? false + : this.config.handleListNullabilityTransparently, + }); + structBlock.addProperty( + this.getFieldName(field), + fieldType, + undefined, + 'public', + { + optional: !this.isFieldRequired(field), + isList: field.isList, + variable: isVariable, + isEnum: this.isEnumType(field), + listType: field.isList ? listType : undefined, + isListNullable: field.isListNullable, + handleListNullabilityTransparently: this.isHasManyConnectionField(field) + ? false + : this.config.handleListNullabilityTransparently, + }, + undefined, + `get async throws { \n try await _${this.getFieldName(field)}.get()\n}`, + ); + } else { + structBlock.addProperty(this.getFieldName(field), fieldType, undefined, 'public', { + optional: !this.isFieldRequired(field), + isList: field.isList, + variable: isVariable, + isEnum: this.isEnumType(field), + listType: field.isList ? listType : undefined, + isListNullable: field.isListNullable, + handleListNullabilityTransparently: this.isHasManyConnectionField(field) + ? false + : this.config.handleListNullabilityTransparently, + }); + } }); const initParams: CodeGenField[] = this.getWritableFields(obj); const initImpl: string = `self.init(${indentMultiline( @@ -389,8 +424,13 @@ export class AppSyncSwiftVisitor< private getInitBody(fields: CodeGenField[]): string { let result = fields.map(field => { - const fieldName = escapeKeywords(this.getFieldName(field)); - return indent(`self.${fieldName} = ${fieldName}`); + const connectionHasOneOrBelongsTo: boolean = field.connectionInfo + ? field.connectionInfo.kind === CodeGenConnectionType.HAS_ONE || field.connectionInfo.kind === CodeGenConnectionType.BELONGS_TO + : false; + const propertyName = connectionHasOneOrBelongsTo ? `_${this.getFieldName(field)}` : escapeKeywords(this.getFieldName(field)); + const escapedFieldName = escapeKeywords(this.getFieldName(field)); + const fieldValue = connectionHasOneOrBelongsTo ? `LazyReference(${escapedFieldName})` : escapedFieldName; + return indent(`self.${propertyName} = ${fieldValue}`); }); return result.join('\n'); From 7cd64d173260eb485020b98dde46c7591020ffc3 Mon Sep 17 00:00:00 2001 From: Michael Law <1365977+lawmicha@users.noreply.github.com> Date: Thu, 1 Dec 2022 12:00:44 -0500 Subject: [PATCH 02/17] feat(amplify-codegen): optionality should be reflected in the computed property --- .../appsync-swift-visitor.test.ts.snap | 371 ++++++++++++++++++ .../appsync-swift-visitor.test.ts | 73 +++- .../src/visitors/appsync-swift-visitor.ts | 3 +- 3 files changed, 435 insertions(+), 12 deletions(-) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap index c51b8d7b9..b097ba879 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -1,5 +1,152 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`AppSyncSwiftVisitor - GQLv2 Regression Tests Works on explicit hasMany belongs to relationship @belongsTo (required associations) 1`] = ` +"// swiftlint:disable all +import Amplify +import Foundation + +public struct Post3: Model { + public let id: String + public var title: String + public var comments: List? + public var createdAt: Temporal.DateTime? + public var updatedAt: Temporal.DateTime? + + public init(id: String = UUID().uuidString, + title: String, + comments: List? = []) { + self.init(id: id, + title: title, + comments: comments, + createdAt: nil, + updatedAt: nil) + } + internal init(id: String = UUID().uuidString, + title: String, + comments: List? = [], + createdAt: Temporal.DateTime? = nil, + updatedAt: Temporal.DateTime? = nil) { + self.id = id + self.title = title + self.comments = comments + self.createdAt = createdAt + self.updatedAt = updatedAt + } +}" +`; + +exports[`AppSyncSwiftVisitor - GQLv2 Regression Tests Works on explicit hasMany belongs to relationship @belongsTo (required associations) 2`] = ` +"// swiftlint:disable all +import Amplify +import Foundation + +extension Post3 { + // MARK: - CodingKeys + public enum CodingKeys: String, ModelKey { + case id + case title + case comments + case createdAt + case updatedAt + } + + public static let keys = CodingKeys.self + // MARK: - ModelSchema + + public static let schema = defineSchema { model in + let post3 = Post3.keys + + model.pluralName = \\"Post3s\\" + + model.fields( + .id(), + .field(post3.title, is: .required, ofType: .string), + .hasMany(post3.comments, is: .optional, ofType: Comment3.self, associatedWith: Comment3.keys.post), + .field(post3.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime), + .field(post3.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) + ) + } +}" +`; + +exports[`AppSyncSwiftVisitor - GQLv2 Regression Tests Works on explicit hasMany belongs to relationship @belongsTo (required associations) 3`] = ` +"// swiftlint:disable all +import Amplify +import Foundation + +public struct Comment3: Model { + public let id: String + public var content: String + internal var _post: LazyReference + public var post: Post3 { + get async throws { + try await _post.require() + } + } + public var createdAt: Temporal.DateTime? + public var updatedAt: Temporal.DateTime? + + public init(id: String = UUID().uuidString, + content: String, + post: Post3) { + self.init(id: id, + content: content, + post: post, + createdAt: nil, + updatedAt: nil) + } + internal init(id: String = UUID().uuidString, + content: String, + post: Post3, + createdAt: Temporal.DateTime? = nil, + updatedAt: Temporal.DateTime? = nil) { + self.id = id + self.content = content + self._post = LazyReference(post) + self.createdAt = createdAt + self.updatedAt = updatedAt + } +}" +`; + +exports[`AppSyncSwiftVisitor - GQLv2 Regression Tests Works on explicit hasMany belongs to relationship @belongsTo (required associations) 4`] = ` +"// swiftlint:disable all +import Amplify +import Foundation + +extension Comment3 { + // MARK: - CodingKeys + public enum CodingKeys: String, ModelKey { + case id + case content + case post + case createdAt + case updatedAt + } + + public static let keys = CodingKeys.self + // MARK: - ModelSchema + + public static let schema = defineSchema { model in + let comment3 = Comment3.keys + + model.pluralName = \\"Comment3s\\" + + model.attributes( + .index(fields: [\\"postID\\", \\"content\\"], name: \\"byPost\\") + ) + + model.fields( + .id(), + .field(comment3.content, is: .required, ofType: .string), + .belongsTo(comment3.post, is: .required, ofType: Post3.self, targetName: \\"postID\\"), + .field(comment3.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime), + .field(comment3.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) + ) + } +}" +`; + exports[`AppSyncSwiftVisitor - GQLv2 Regression Tests Works on explicit hasMany belongs to relationship @belongsTo 1`] = ` "// swiftlint:disable all import Amplify @@ -526,6 +673,230 @@ extension Comment7V2 { }" `; +exports[`AppSyncSwiftVisitor - GQLv2 Regression Tests Works on implicit hasMany belongs to relationship @belongsTo (required associations) 1`] = ` +"// swiftlint:disable all +import Amplify +import Foundation + +public struct Blog8V2: Model { + public let id: String + public var name: String + public var posts: List? + public var createdAt: Temporal.DateTime? + public var updatedAt: Temporal.DateTime? + + public init(id: String = UUID().uuidString, + name: String, + posts: List? = []) { + self.init(id: id, + name: name, + posts: posts, + createdAt: nil, + updatedAt: nil) + } + internal init(id: String = UUID().uuidString, + name: String, + posts: List? = [], + createdAt: Temporal.DateTime? = nil, + updatedAt: Temporal.DateTime? = nil) { + self.id = id + self.name = name + self.posts = posts + self.createdAt = createdAt + self.updatedAt = updatedAt + } +}" +`; + +exports[`AppSyncSwiftVisitor - GQLv2 Regression Tests Works on implicit hasMany belongs to relationship @belongsTo (required associations) 2`] = ` +"// swiftlint:disable all +import Amplify +import Foundation + +extension Blog8V2 { + // MARK: - CodingKeys + public enum CodingKeys: String, ModelKey { + case id + case name + case posts + case createdAt + case updatedAt + } + + public static let keys = CodingKeys.self + // MARK: - ModelSchema + + public static let schema = defineSchema { model in + let blog8V2 = Blog8V2.keys + + model.pluralName = \\"Blog8V2s\\" + + model.fields( + .id(), + .field(blog8V2.name, is: .required, ofType: .string), + .hasMany(blog8V2.posts, is: .optional, ofType: Post8V2.self, associatedWith: Post8V2.keys.blog), + .field(blog8V2.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime), + .field(blog8V2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) + ) + } +}" +`; + +exports[`AppSyncSwiftVisitor - GQLv2 Regression Tests Works on implicit hasMany belongs to relationship @belongsTo (required associations) 3`] = ` +"// swiftlint:disable all +import Amplify +import Foundation + +public struct Post8V2: Model { + public let id: String + public var title: String + internal var _blog: LazyReference + public var blog: Blog8V2 { + get async throws { + try await _blog.require() + } + } + public var comments: List? + public var createdAt: Temporal.DateTime? + public var updatedAt: Temporal.DateTime? + + public init(id: String = UUID().uuidString, + title: String, + blog: Blog8V2, + comments: List? = []) { + self.init(id: id, + title: title, + blog: blog, + comments: comments, + createdAt: nil, + updatedAt: nil) + } + internal init(id: String = UUID().uuidString, + title: String, + blog: Blog8V2, + comments: List? = [], + createdAt: Temporal.DateTime? = nil, + updatedAt: Temporal.DateTime? = nil) { + self.id = id + self.title = title + self._blog = LazyReference(blog) + self.comments = comments + self.createdAt = createdAt + self.updatedAt = updatedAt + } +}" +`; + +exports[`AppSyncSwiftVisitor - GQLv2 Regression Tests Works on implicit hasMany belongs to relationship @belongsTo (required associations) 4`] = ` +"// swiftlint:disable all +import Amplify +import Foundation + +extension Post8V2 { + // MARK: - CodingKeys + public enum CodingKeys: String, ModelKey { + case id + case title + case blog + case comments + case createdAt + case updatedAt + } + + public static let keys = CodingKeys.self + // MARK: - ModelSchema + + public static let schema = defineSchema { model in + let post8V2 = Post8V2.keys + + model.pluralName = \\"Post8V2s\\" + + model.fields( + .id(), + .field(post8V2.title, is: .required, ofType: .string), + .belongsTo(post8V2.blog, is: .required, ofType: Blog8V2.self, targetName: \\"blog8V2PostsId\\"), + .hasMany(post8V2.comments, is: .optional, ofType: Comment8V2.self, associatedWith: Comment8V2.keys.post), + .field(post8V2.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime), + .field(post8V2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) + ) + } +}" +`; + +exports[`AppSyncSwiftVisitor - GQLv2 Regression Tests Works on implicit hasMany belongs to relationship @belongsTo (required associations) 5`] = ` +"// swiftlint:disable all +import Amplify +import Foundation + +public struct Comment8V2: Model { + public let id: String + public var content: String? + internal var _post: LazyReference + public var post: Post8V2 { + get async throws { + try await _post.require() + } + } + public var createdAt: Temporal.DateTime? + public var updatedAt: Temporal.DateTime? + + public init(id: String = UUID().uuidString, + content: String? = nil, + post: Post8V2) { + self.init(id: id, + content: content, + post: post, + createdAt: nil, + updatedAt: nil) + } + internal init(id: String = UUID().uuidString, + content: String? = nil, + post: Post8V2, + createdAt: Temporal.DateTime? = nil, + updatedAt: Temporal.DateTime? = nil) { + self.id = id + self.content = content + self._post = LazyReference(post) + self.createdAt = createdAt + self.updatedAt = updatedAt + } +}" +`; + +exports[`AppSyncSwiftVisitor - GQLv2 Regression Tests Works on implicit hasMany belongs to relationship @belongsTo (required associations) 6`] = ` +"// swiftlint:disable all +import Amplify +import Foundation + +extension Comment8V2 { + // MARK: - CodingKeys + public enum CodingKeys: String, ModelKey { + case id + case content + case post + case createdAt + case updatedAt + } + + public static let keys = CodingKeys.self + // MARK: - ModelSchema + + public static let schema = defineSchema { model in + let comment8V2 = Comment8V2.keys + + model.pluralName = \\"Comment8V2s\\" + + model.fields( + .id(), + .field(comment8V2.content, is: .optional, ofType: .string), + .belongsTo(comment8V2.post, is: .required, ofType: Post8V2.self, targetName: \\"post8V2CommentsId\\"), + .field(comment8V2.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime), + .field(comment8V2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) + ) + } +}" +`; + exports[`AppSyncSwiftVisitor - GQLv2 Regression Tests Works on implicit hasOne belongs to relationship @belongsTo 1`] = ` "// swiftlint:disable all import Amplify diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/appsync-swift-visitor.test.ts index a89160ea9..b20db95c4 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/appsync-swift-visitor.test.ts @@ -53,15 +53,17 @@ describe('AppSyncSwiftVisitor - GQLv2 Regression Tests', () => { }); it('Works on record creation and updating timestamp', () => { - const schema = /* GraphQL */ ` - type Todo @model(timestamps: { createdAt: "createdOn", updatedAt: "updatedOn" }) {content: String} + const schema = /* GraphQL */ ` + type Todo @model(timestamps: { createdAt: "createdOn", updatedAt: "updatedOn" }) { + content: String + } `; expect(getGQLv2Visitor(schema, 'Todo').generate()).toMatchSnapshot(); expect(getGQLv2Visitor(schema, 'Todo', CodeGenGenerateEnum.metadata).generate()).toMatchSnapshot(); }); it('Works on uni-directional implicit has one relationship @hasOne', () => { - const schema = /* GraphQL */ ` + const schema = /* GraphQL */ ` # Implicit field type Project @model { id: ID! @@ -81,7 +83,7 @@ describe('AppSyncSwiftVisitor - GQLv2 Regression Tests', () => { }); it('Works on uni-directional explicit has one relationship @hasOne', () => { - const schema = /* GraphQL */ ` + const schema = /* GraphQL */ ` # Explicit field type Project2 @model { id: ID! @@ -102,7 +104,7 @@ describe('AppSyncSwiftVisitor - GQLv2 Regression Tests', () => { }); it('Works on uni-directional implicit has many relationship @hasMany', () => { - const schema = /* GraphQL */ ` + const schema = /* GraphQL */ ` # Implicit type Post @model { id: ID! @@ -122,7 +124,7 @@ describe('AppSyncSwiftVisitor - GQLv2 Regression Tests', () => { }); it('Works on uni-directional explicit has many relationship @hasMany', () => { - const schema = /* GraphQL */ ` + const schema = /* GraphQL */ ` # Explicit type Post2 @model { id: ID! @@ -143,7 +145,7 @@ describe('AppSyncSwiftVisitor - GQLv2 Regression Tests', () => { }); it('Works on many to many relationship @manyToMany', () => { - const schema = /* GraphQL */ ` + const schema = /* GraphQL */ ` type Post @model { id: ID! title: String! @@ -164,7 +166,7 @@ describe('AppSyncSwiftVisitor - GQLv2 Regression Tests', () => { }); it('Works on implicit hasOne belongs to relationship @belongsTo', () => { - const schema = /* GraphQL */ ` + const schema = /* GraphQL */ ` # Implicit type Project @model { id: ID! @@ -185,7 +187,7 @@ describe('AppSyncSwiftVisitor - GQLv2 Regression Tests', () => { }); it('Works on explicit hasOne belongs to relationship @belongsTo', () => { - const schema = /* GraphQL */ ` + const schema = /* GraphQL */ ` # Explicit type Project2 @model { id: ID! @@ -207,7 +209,7 @@ describe('AppSyncSwiftVisitor - GQLv2 Regression Tests', () => { }); it('Works on explicit hasMany belongs to relationship @belongsTo', () => { - const schema = /* GraphQL */ ` + const schema = /* GraphQL */ ` # Explicit - Bi-directional Has Many type Post @model { id: ID! @@ -228,8 +230,30 @@ describe('AppSyncSwiftVisitor - GQLv2 Regression Tests', () => { expect(getGQLv2Visitor(schema, 'Comment', CodeGenGenerateEnum.metadata).generate()).toMatchSnapshot(); }); + it('Works on explicit hasMany belongs to relationship @belongsTo (required associations)', () => { + const schema = /* GraphQL */ ` + # Explicit - Bi-directional Has Many + type Post3 @model { + id: ID! + title: String! + comments: [Comment3] @hasMany(indexName: "byPost", fields: ["id"]) + } + + type Comment3 @model { + id: ID! + postID: ID! @index(name: "byPost", sortKeyFields: ["content"]) + content: String! + post: Post3! @belongsTo(fields: ["postID"]) + } + `; + expect(getGQLv2Visitor(schema, 'Post3').generate()).toMatchSnapshot(); + expect(getGQLv2Visitor(schema, 'Post3', CodeGenGenerateEnum.metadata).generate()).toMatchSnapshot(); + expect(getGQLv2Visitor(schema, 'Comment3').generate()).toMatchSnapshot(); + expect(getGQLv2Visitor(schema, 'Comment3', CodeGenGenerateEnum.metadata).generate()).toMatchSnapshot(); + }); + it('Works on implicit hasMany belongs to relationship @belongsTo (extended)', () => { - const schema = /* GraphQL */ ` + const schema = /* GraphQL */ ` # 7 - Blog Post Comment type Blog7V2 @model { id: ID! @@ -255,4 +279,31 @@ describe('AppSyncSwiftVisitor - GQLv2 Regression Tests', () => { expect(getGQLv2Visitor(schema, 'Comment7V2').generate()).toMatchSnapshot(); expect(getGQLv2Visitor(schema, 'Comment7V2', CodeGenGenerateEnum.metadata).generate()).toMatchSnapshot(); }); + it('Works on implicit hasMany belongs to relationship @belongsTo (required associations)', () => { + const schema = /* GraphQL */ ` + # 8 - Blog Post Comment (required associations) + type Blog8V2 @model { + id: ID! + name: String! + posts: [Post8V2] @hasMany + } + type Post8V2 @model { + id: ID! + title: String! + blog: Blog8V2! @belongsTo + comments: [Comment8V2] @hasMany + } + type Comment8V2 @model { + id: ID! + content: String + post: Post8V2! @belongsTo + } + `; + expect(getGQLv2Visitor(schema, 'Blog8V2').generate()).toMatchSnapshot(); + expect(getGQLv2Visitor(schema, 'Blog8V2', CodeGenGenerateEnum.metadata).generate()).toMatchSnapshot(); + expect(getGQLv2Visitor(schema, 'Post8V2').generate()).toMatchSnapshot(); + expect(getGQLv2Visitor(schema, 'Post8V2', CodeGenGenerateEnum.metadata).generate()).toMatchSnapshot(); + expect(getGQLv2Visitor(schema, 'Comment8V2').generate()).toMatchSnapshot(); + expect(getGQLv2Visitor(schema, 'Comment8V2', CodeGenGenerateEnum.metadata).generate()).toMatchSnapshot(); + }); }); diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts index 523c4c2cc..c7f6b1c62 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts @@ -110,6 +110,7 @@ export class AppSyncSwiftVisitor< ? false : this.config.handleListNullabilityTransparently, }); + const lazyLoadGetOrRequired = !this.isFieldRequired(field) ? 'get()' : 'require()'; structBlock.addProperty( this.getFieldName(field), fieldType, @@ -127,7 +128,7 @@ export class AppSyncSwiftVisitor< : this.config.handleListNullabilityTransparently, }, undefined, - `get async throws { \n try await _${this.getFieldName(field)}.get()\n}`, + `get async throws { \n try await _${this.getFieldName(field)}.${lazyLoadGetOrRequired}\n}`, ); } else { structBlock.addProperty(this.getFieldName(field), fieldType, undefined, 'public', { From 4c2b30b138e616927244691ce2afb034489b2290 Mon Sep 17 00:00:00 2001 From: Michael Law <1365977+lawmicha@users.noreply.github.com> Date: Fri, 2 Dec 2022 10:52:52 -0500 Subject: [PATCH 03/17] feat(amplify-codegen): add ModelPath extension --- .../appsync-swift-visitor.test.ts.snap | 241 ++++++++ .../visitors/appsync-swift-visitor.test.ts | 507 +++++++++++++++++ .../appsync-swift-visitor.test.ts.snap | 535 ++++++++++++++++++ .../src/visitors/appsync-swift-visitor.ts | 58 ++ 4 files changed, 1341 insertions(+) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap index ffcec7e07..88b75ffa0 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -78,6 +78,9 @@ extension Post { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } } extension Post: ModelIdentifiable { @@ -90,6 +93,23 @@ extension Post.IdentifierProtocol { title: String) -> Self { .make(fields:[(name: \\"id\\", value: id), (name: \\"title\\", value: title)]) } +} +extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var comments: ModelPath { + Comment.Path(name: \\"comments\\", isCollection: true, parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -171,6 +191,9 @@ extension Comment { .field(comment.postCommentsTitle, is: .optional, ofType: .string) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } } extension Comment: ModelIdentifiable { @@ -183,6 +206,26 @@ extension Comment.IdentifierProtocol { content: String) -> Self { .make(fields:[(name: \\"id\\", value: id), (name: \\"content\\", value: content)]) } +} +extension ModelPath where ModelType == Comment { + public var id: FieldPath { + string(\\"id\\") + } + public var content: FieldPath { + string(\\"content\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } + public var postCommentsId: FieldPath { + string(\\"postCommentsId\\") + } + public var postCommentsTitle: FieldPath { + string(\\"postCommentsTitle\\") + } }" `; @@ -257,6 +300,9 @@ extension ModelCompositePk { .field(modelCompositePk.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } } extension ModelCompositePk: ModelIdentifiable { @@ -269,6 +315,23 @@ extension ModelCompositePk.IdentifierProtocol { dob: Temporal.DateTime) -> Self { .make(fields:[(name: \\"id\\", value: id), (name: \\"dob\\", value: dob)]) } +} +extension ModelPath where ModelType == ModelCompositePk { + public var id: FieldPath { + string(\\"id\\") + } + public var dob: FieldPath { + datetime(\\"dob\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -336,6 +399,9 @@ extension ModelExplicitCustomPk { .field(modelExplicitCustomPk.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } } extension ModelExplicitCustomPk: ModelIdentifiable { @@ -347,6 +413,20 @@ extension ModelExplicitCustomPk.IdentifierProtocol { public static func identifier(userId: String) -> Self { .make(fields:[(name: \\"userId\\", value: userId)]) } +} +extension ModelPath where ModelType == ModelExplicitCustomPk { + public var userId: FieldPath { + string(\\"userId\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -414,11 +494,28 @@ extension ModelExplicitDefaultPk { .field(modelExplicitDefaultPk.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } } extension ModelExplicitDefaultPk: ModelIdentifiable { public typealias IdentifierFormat = ModelIdentifierFormat.Default public typealias IdentifierProtocol = DefaultModelIdentifier +} +extension ModelPath where ModelType == ModelExplicitDefaultPk { + public var id: FieldPath { + string(\\"id\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -485,11 +582,28 @@ extension ModelImplicitDefaultPk { .field(modelImplicitDefaultPk.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } } extension ModelImplicitDefaultPk: ModelIdentifiable { public typealias IdentifierFormat = ModelIdentifierFormat.Default public typealias IdentifierProtocol = DefaultModelIdentifier +} +extension ModelPath where ModelType == ModelImplicitDefaultPk { + public var id: FieldPath { + string(\\"id\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -583,6 +697,9 @@ extension Project { .field(project.projectTeamName, is: .optional, ofType: .string) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } } extension Project: ModelIdentifiable { @@ -595,6 +712,29 @@ extension Project.IdentifierProtocol { name: String) -> Self { .make(fields:[(name: \\"projectId\\", value: projectId), (name: \\"name\\", value: name)]) } +} +extension ModelPath where ModelType == Project { + public var projectId: FieldPath { + string(\\"projectId\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var team: ModelPath { + Team.Path(name: \\"team\\", parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } + public var projectTeamTeamId: FieldPath { + string(\\"projectTeamTeamId\\") + } + public var projectTeamName: FieldPath { + string(\\"projectTeamName\\") + } }" `; @@ -674,6 +814,9 @@ extension Team { .field(team.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } } extension Team: ModelIdentifiable { @@ -686,6 +829,23 @@ extension Team.IdentifierProtocol { name: String) -> Self { .make(fields:[(name: \\"teamId\\", value: teamId), (name: \\"name\\", value: name)]) } +} +extension ModelPath where ModelType == Team { + public var teamId: FieldPath { + string(\\"teamId\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var project: ModelPath { + Project.Path(name: \\"project\\", parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -797,6 +957,38 @@ extension ListContainer { .field(listContainer.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == ListContainer { + public var id: FieldPath { + string(\\"id\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var list: FieldPath { + int(\\"list\\") + } + public var requiredList: FieldPath { + string(\\"requiredList\\") + } + public var listOfRequired: FieldPath { + bool(\\"listOfRequired\\") + } + public var requiredListOfRequiredDates: FieldPath { + date(\\"requiredListOfRequiredDates\\") + } + public var listOfRequiredFloats: FieldPath { + double(\\"listOfRequiredFloats\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -901,6 +1093,35 @@ extension Todo { .field(todo.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Todo { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var requiredListOfTasks: ModelPath { + task.Path(name: \\"requiredListOfTasks\\", isCollection: true, parent: self) + } + public var listOfRequiredTasks: ModelPath { + task.Path(name: \\"listOfRequiredTasks\\", isCollection: true, parent: self) + } + public var listOfTasks: ModelPath { + task.Path(name: \\"listOfTasks\\", isCollection: true, parent: self) + } + public var requiredListOfRequiredTasks: ModelPath { + task.Path(name: \\"requiredListOfRequiredTasks\\", isCollection: true, parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -975,5 +1196,25 @@ extension task { .field(task.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == task { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var todo: ModelPath { + Todo.Path(name: \\"todo\\", parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts index 7a278b69c..f168a4928 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts @@ -127,6 +127,26 @@ describe('AppSyncSwiftVisitor', () => { .field(simpleModel.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == SimpleModel { + public var id: FieldPath { + string(\\"id\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var bar: FieldPath { + string(\\"bar\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -232,6 +252,23 @@ describe('AppSyncSwiftVisitor', () => { .field(snake_case.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == snake_case { + public var id: FieldPath { + string(\\"id\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -376,6 +413,32 @@ describe('AppSyncSwiftVisitor', () => { .field(authorBook.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == authorBook { + public var id: FieldPath { + string(\\"id\\") + } + public var author_id: FieldPath { + string(\\"author_id\\") + } + public var book_id: FieldPath { + string(\\"book_id\\") + } + public var author: FieldPath { + string(\\"author\\") + } + public var book: FieldPath { + string(\\"book\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -652,6 +715,41 @@ describe('AppSyncSwiftVisitor', () => { .field(todo.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == Todo { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var done: FieldPath { + bool(\\"done\\") + } + public var description: FieldPath { + string(\\"description\\") + } + public var due_date: FieldPath { + string(\\"due_date\\") + } + public var version: FieldPath { + int(\\"version\\") + } + public var value: FieldPath { + double(\\"value\\") + } + public var tasks: ModelPath { + task.Path(name: \\"tasks\\", isCollection: true, parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -753,6 +851,35 @@ describe('AppSyncSwiftVisitor', () => { .field(task.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == task { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var done: FieldPath { + bool(\\"done\\") + } + public var todo: ModelPath { + Todo.Path(name: \\"todo\\", parent: self) + } + public var time: FieldPath { + time(\\"time\\") + } + public var createdOn: FieldPath { + date(\\"createdOn\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -886,6 +1013,26 @@ describe('AppSyncSwiftVisitor', () => { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var editors: ModelPath { + PostEditor.Path(name: \\"editors\\", isCollection: true, parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); @@ -957,6 +1104,26 @@ describe('AppSyncSwiftVisitor', () => { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var editors: ModelPath { + PostEditor.Path(name: \\"editors\\", isCollection: true, parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -1077,6 +1244,35 @@ describe('AppSyncSwiftVisitor', () => { .field(objectWithNativeTypes.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == ObjectWithNativeTypes { + public var id: FieldPath { + string(\\"id\\") + } + public var intArr: FieldPath { + int(\\"intArr\\") + } + public var strArr: FieldPath { + string(\\"strArr\\") + } + public var floatArr: FieldPath { + double(\\"floatArr\\") + } + public var boolArr: FieldPath { + bool(\\"boolArr\\") + } + public var dateArr: FieldPath { + date(\\"dateArr\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -1199,6 +1395,26 @@ describe('AppSyncSwiftVisitor', () => { .field(attraction.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == Attraction { + public var id: FieldPath { + string(\\"id\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var tags: FieldPath { + string(\\"tags\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); @@ -1255,6 +1471,9 @@ describe('AppSyncSwiftVisitor', () => { .field(location.tags, is: .optional, ofType: .embeddedCollection(of: String.self)) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } }" `); @@ -1444,6 +1663,26 @@ describe('AppSyncSwiftVisitor', () => { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var owner: FieldPath { + string(\\"owner\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -1493,6 +1732,26 @@ describe('AppSyncSwiftVisitor', () => { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var author: FieldPath { + string(\\"author\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -1543,6 +1802,26 @@ describe('AppSyncSwiftVisitor', () => { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var author: FieldPath { + string(\\"author\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -1593,6 +1872,26 @@ describe('AppSyncSwiftVisitor', () => { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var author: FieldPath { + string(\\"author\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -1639,6 +1938,23 @@ describe('AppSyncSwiftVisitor', () => { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -1685,6 +2001,23 @@ describe('AppSyncSwiftVisitor', () => { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -1740,6 +2073,29 @@ describe('AppSyncSwiftVisitor', () => { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var author: FieldPath { + string(\\"author\\") + } + public var editors: FieldPath { + string(\\"editors\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -1793,6 +2149,29 @@ describe('AppSyncSwiftVisitor', () => { .field(employee.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == Employee { + public var id: FieldPath { + string(\\"id\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var address: FieldPath { + string(\\"address\\") + } + public var ssn: FieldPath { + string(\\"ssn\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -1842,6 +2221,23 @@ describe('AppSyncSwiftVisitor', () => { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -1891,6 +2287,26 @@ describe('AppSyncSwiftVisitor', () => { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var groups: FieldPath { + string(\\"groups\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -1938,6 +2354,23 @@ describe('AppSyncSwiftVisitor', () => { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -1985,6 +2418,23 @@ describe('AppSyncSwiftVisitor', () => { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -2031,6 +2481,23 @@ describe('AppSyncSwiftVisitor', () => { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -2091,6 +2558,26 @@ describe('AppSyncSwiftVisitor', () => { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var owner: FieldPath { + string(\\"owner\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); @@ -2151,6 +2638,26 @@ describe('AppSyncSwiftVisitor', () => { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } + } + extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var owner: FieldPath { + string(\\"owner\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `); }); diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap index b097ba879..04d28df1d 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -66,6 +66,26 @@ extension Post3 { .field(post3.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Post3 { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var comments: ModelPath { + Comment3.Path(name: \\"comments\\", isCollection: true, parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -144,6 +164,26 @@ extension Comment3 { .field(comment3.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Comment3 { + public var id: FieldPath { + string(\\"id\\") + } + public var content: FieldPath { + string(\\"content\\") + } + public var post: ModelPath { + Post3.Path(name: \\"post\\", parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -213,6 +253,26 @@ extension Post { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var comments: ModelPath { + Comment.Path(name: \\"comments\\", isCollection: true, parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -291,6 +351,26 @@ extension Comment { .field(comment.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Comment { + public var id: FieldPath { + string(\\"id\\") + } + public var content: FieldPath { + string(\\"content\\") + } + public var post: ModelPath { + Post.Path(name: \\"post\\", parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -372,6 +452,29 @@ extension Project2 { .field(project2.project2TeamId, is: .optional, ofType: .string) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Project2 { + public var id: FieldPath { + string(\\"id\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var team: ModelPath { + Team2.Path(name: \\"team\\", parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } + public var project2TeamId: FieldPath { + string(\\"project2TeamId\\") + } }" `; @@ -446,6 +549,26 @@ extension Team2 { .field(team2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Team2 { + public var id: FieldPath { + string(\\"id\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var project: ModelPath { + Project2.Path(name: \\"project\\", parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -515,6 +638,26 @@ extension Blog7V2 { .field(blog7V2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Blog7V2 { + public var id: FieldPath { + string(\\"id\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var posts: ModelPath { + Post7V2.Path(name: \\"posts\\", isCollection: true, parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -596,6 +739,29 @@ extension Post7V2 { .field(post7V2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Post7V2 { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var blog: ModelPath { + Blog7V2.Path(name: \\"blog\\", parent: self) + } + public var comments: ModelPath { + Comment7V2.Path(name: \\"comments\\", isCollection: true, parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -670,6 +836,26 @@ extension Comment7V2 { .field(comment7V2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Comment7V2 { + public var id: FieldPath { + string(\\"id\\") + } + public var content: FieldPath { + string(\\"content\\") + } + public var post: ModelPath { + Post7V2.Path(name: \\"post\\", parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -739,6 +925,26 @@ extension Blog8V2 { .field(blog8V2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Blog8V2 { + public var id: FieldPath { + string(\\"id\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var posts: ModelPath { + Post8V2.Path(name: \\"posts\\", isCollection: true, parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -820,6 +1026,29 @@ extension Post8V2 { .field(post8V2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Post8V2 { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var blog: ModelPath { + Blog8V2.Path(name: \\"blog\\", parent: self) + } + public var comments: ModelPath { + Comment8V2.Path(name: \\"comments\\", isCollection: true, parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -894,6 +1123,26 @@ extension Comment8V2 { .field(comment8V2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Comment8V2 { + public var id: FieldPath { + string(\\"id\\") + } + public var content: FieldPath { + string(\\"content\\") + } + public var post: ModelPath { + Post8V2.Path(name: \\"post\\", parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -975,6 +1224,29 @@ extension Project { .field(project.projectTeamId, is: .optional, ofType: .string) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Project { + public var id: FieldPath { + string(\\"id\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var team: ModelPath { + Team.Path(name: \\"team\\", parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } + public var projectTeamId: FieldPath { + string(\\"projectTeamId\\") + } }" `; @@ -1049,6 +1321,26 @@ extension Team { .field(team.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Team { + public var id: FieldPath { + string(\\"id\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var project: ModelPath { + Project.Path(name: \\"project\\", parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -1125,6 +1417,29 @@ extension Post { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var content: FieldPath { + string(\\"content\\") + } + public var tags: ModelPath { + PostTags.Path(name: \\"tags\\", isCollection: true, parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -1194,6 +1509,26 @@ extension Tag { .field(tag.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Tag { + public var id: FieldPath { + string(\\"id\\") + } + public var label: FieldPath { + string(\\"label\\") + } + public var posts: ModelPath { + PostTags.Path(name: \\"posts\\", isCollection: true, parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -1256,6 +1591,23 @@ extension Todo { .field(todo.updatedOn, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Todo { + public var id: FieldPath { + string(\\"id\\") + } + public var content: FieldPath { + string(\\"content\\") + } + public var createdOn: FieldPath { + datetime(\\"createdOn\\") + } + public var updatedOn: FieldPath { + datetime(\\"updatedOn\\") + } }" `; @@ -1325,6 +1677,26 @@ extension Post2 { .field(post2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Post2 { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var comments: ModelPath { + Comment2.Path(name: \\"comments\\", isCollection: true, parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -1398,6 +1770,26 @@ extension Comment2 { .field(comment2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Comment2 { + public var id: FieldPath { + string(\\"id\\") + } + public var postID: FieldPath { + string(\\"postID\\") + } + public var content: FieldPath { + string(\\"content\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -1479,6 +1871,29 @@ extension Project2 { .field(project2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Project2 { + public var id: FieldPath { + string(\\"id\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var teamID: FieldPath { + string(\\"teamID\\") + } + public var team: ModelPath { + Team2.Path(name: \\"team\\", parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -1541,6 +1956,23 @@ extension Team2 { .field(team2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Team2 { + public var id: FieldPath { + string(\\"id\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -1610,6 +2042,26 @@ extension Post { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Post { + public var id: FieldPath { + string(\\"id\\") + } + public var title: FieldPath { + string(\\"title\\") + } + public var comments: ModelPath { + Comment.Path(name: \\"comments\\", isCollection: true, parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -1679,6 +2131,26 @@ extension Comment { .field(comment.postCommentsId, is: .optional, ofType: .string) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Comment { + public var id: FieldPath { + string(\\"id\\") + } + public var content: FieldPath { + string(\\"content\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } + public var postCommentsId: FieldPath { + string(\\"postCommentsId\\") + } }" `; @@ -1760,6 +2232,29 @@ extension Project { .field(project.projectTeamId, is: .optional, ofType: .string) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Project { + public var id: FieldPath { + string(\\"id\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var team: ModelPath { + Team.Path(name: \\"team\\", parent: self) + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } + public var projectTeamId: FieldPath { + string(\\"projectTeamId\\") + } }" `; @@ -1822,6 +2317,23 @@ extension Team { .field(team.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Team { + public var id: FieldPath { + string(\\"id\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; @@ -1902,5 +2414,28 @@ extension Customer { .field(customer.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } + public class Path: ModelPath { } + + public static var rootPath: PropertyContainerPath? { Path() } +} +extension ModelPath where ModelType == Customer { + public var id: FieldPath { + string(\\"id\\") + } + public var name: FieldPath { + string(\\"name\\") + } + public var phoneNumber: FieldPath { + string(\\"phoneNumber\\") + } + public var accountRepresentativeID: FieldPath { + string(\\"accountRepresentativeID\\") + } + public var createdAt: FieldPath { + datetime(\\"createdAt\\") + } + public var updatedAt: FieldPath { + datetime(\\"updatedAt\\") + } }" `; diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts index c7f6b1c62..fce0749ea 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts @@ -295,6 +295,8 @@ export class AppSyncSwiftVisitor< result.push(''); result.push(this.generatePrimaryKeyExtensions(model)); } + // TODO: Most likely we will have a flag here as well, `if (this.isLazyLoadingEnabled())` + result.push(this.generateModelPathExtensions(model)); }); Object.values(this.getSelectedNonModels()).forEach(model => { @@ -346,6 +348,9 @@ export class AppSyncSwiftVisitor< indentMultiline(fields.join(',\n')), ')', '}', + `public class Path: ModelPath<${this.getModelName(model)}> { }`, + '', + 'public static var rootPath: PropertyContainerPath? { Path() }', ].join('\n'); extensionDeclaration.addProperty( 'schema', @@ -395,6 +400,40 @@ export class AppSyncSwiftVisitor< return result.join('\n\n'); } + protected generateModelPathExtensions(model: CodeGenModel): string { + const modelPathExtension = new SwiftDeclarationBlock() + .asKind('extension') + .withName(`ModelPath where ModelType == ${this.getModelName(model)}`); + + Object.values(model.fields).forEach(field => { + if (this.isEnumType(field) || this.isNonModelType(field)) { + return; + } + const fieldName = this.getFieldName(field); + const fieldType = this.getNativeType(field); + const pathType = field.connectionInfo ? 'ModelPath' : 'FieldPath'; + const pathValue = field.connectionInfo + ? field.connectionInfo.kind === CodeGenConnectionType.HAS_MANY + ? `${fieldType}.Path(name: \"${fieldName}\", isCollection: true, parent: self)` + : `${fieldType}.Path(name: \"${fieldName}\", parent: self)` + : `${this.getFieldTypePathValue(fieldType)}(\"${fieldName}\")`; + + modelPathExtension.addProperty( + `${fieldName}`, + `${pathType}<${fieldType}>`, + '', + undefined, + { + variable: true, + }, + undefined, + pathValue, + ); + }); + + return modelPathExtension.string; + } + protected generateClassLoader(): string { const structList = Object.values(this.modelMap).map(typeObj => `${this.getModelName(typeObj)}.self`); @@ -527,6 +566,25 @@ export class AppSyncSwiftVisitor< return '.string'; } + private getFieldTypePathValue(fieldType: String) { + if (fieldType === 'String') { + return 'string'; + } else if (fieldType === 'Int') { + return 'int'; + } else if (fieldType === 'Double') { + return 'double'; + } else if (fieldType === 'Bool') { + return 'bool'; + } else if (fieldType === 'Temporal.Date') { + return 'date'; + } else if (fieldType === 'Temporal.DateTime') { + return 'datetime'; + } else if (fieldType === 'Temporal.Time') { + return 'time'; + } + return fieldType; + } + protected getEnumValue(value: string): string { return camelCase(value); } From 3597912c87f072301168c59bb834a6d91e2f684e Mon Sep 17 00:00:00 2001 From: Michael Law <1365977+lawmicha@users.noreply.github.com> Date: Mon, 5 Dec 2022 10:19:27 -0500 Subject: [PATCH 04/17] feat(amplify-codegen): add mutating func to allow update/delete association --- .../appsync-swift-visitor.test.ts.snap | 9 +++++ .../visitors/appsync-swift-visitor.test.ts | 3 ++ .../appsync-swift-visitor.test.ts.snap | 36 +++++++++++++++++++ .../src/languages/swift-declaration-block.ts | 21 ++++++----- .../src/visitors/appsync-swift-visitor.ts | 33 +++++++++++++++++ 5 files changed, 94 insertions(+), 8 deletions(-) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap index 88b75ffa0..3661dbb34 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -654,6 +654,9 @@ public struct Project: Model { self.projectTeamTeamId = projectTeamTeamId self.projectTeamName = projectTeamName } + public mutating func setTeam(team: Team? = nil) { + self._team = LazyReference(team) + } }" `; @@ -775,6 +778,9 @@ public struct Team: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public mutating func setProject(project: Project? = nil) { + self._project = LazyReference(project) + } }" `; @@ -1162,6 +1168,9 @@ public struct task: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public mutating func setTodo(todo: Todo? = nil) { + self._todo = LazyReference(todo) + } }" `; diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts index f168a4928..e924664c6 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts @@ -809,6 +809,9 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } + public mutating func setTodo(todo: Todo? = nil) { + self._todo = LazyReference(todo) + } }" `); diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap index 04d28df1d..dcd9c7a7b 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -126,6 +126,9 @@ public struct Comment3: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public mutating func setPost(post: Post3) { + self._post = LazyReference(post) + } }" `; @@ -313,6 +316,9 @@ public struct Comment: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public mutating func setPost(post: Post? = nil) { + self._post = LazyReference(post) + } }" `; @@ -416,6 +422,9 @@ public struct Project2: Model { self.updatedAt = updatedAt self.project2TeamId = project2TeamId } + public mutating func setTeam(team: Team2? = nil) { + self._team = LazyReference(team) + } }" `; @@ -515,6 +524,9 @@ public struct Team2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public mutating func setProject(project: Project2? = nil) { + self._project = LazyReference(project) + } }" `; @@ -703,6 +715,9 @@ public struct Post7V2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public mutating func setBlog(blog: Blog7V2? = nil) { + self._blog = LazyReference(blog) + } }" `; @@ -802,6 +817,9 @@ public struct Comment7V2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public mutating func setPost(post: Post7V2? = nil) { + self._post = LazyReference(post) + } }" `; @@ -990,6 +1008,9 @@ public struct Post8V2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public mutating func setBlog(blog: Blog8V2) { + self._blog = LazyReference(blog) + } }" `; @@ -1089,6 +1110,9 @@ public struct Comment8V2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public mutating func setPost(post: Post8V2) { + self._post = LazyReference(post) + } }" `; @@ -1188,6 +1212,9 @@ public struct Project: Model { self.updatedAt = updatedAt self.projectTeamId = projectTeamId } + public mutating func setTeam(team: Team? = nil) { + self._team = LazyReference(team) + } }" `; @@ -1287,6 +1314,9 @@ public struct Team: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public mutating func setProject(project: Project? = nil) { + self._project = LazyReference(project) + } }" `; @@ -1835,6 +1865,9 @@ public struct Project2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public mutating func setTeam(team: Team2? = nil) { + self._team = LazyReference(team) + } }" `; @@ -2196,6 +2229,9 @@ public struct Project: Model { self.updatedAt = updatedAt self.projectTeamId = projectTeamId } + public mutating func setTeam(team: Team? = nil) { + self._team = LazyReference(team) + } }" `; diff --git a/packages/appsync-modelgen-plugin/src/languages/swift-declaration-block.ts b/packages/appsync-modelgen-plugin/src/languages/swift-declaration-block.ts index 2963cdff8..5fd65eaa0 100644 --- a/packages/appsync-modelgen-plugin/src/languages/swift-declaration-block.ts +++ b/packages/appsync-modelgen-plugin/src/languages/swift-declaration-block.ts @@ -123,9 +123,14 @@ export type VariableFlags = { isEnum?: boolean; isTypeAlias?: boolean; }; -export type StructFlags = VariableFlags & { optional?: boolean; static?: boolean; isListNullable?: boolean, handleListNullabilityTransparently?: boolean }; +export type StructFlags = VariableFlags & { + optional?: boolean; + static?: boolean; + isListNullable?: boolean; + handleListNullabilityTransparently?: boolean; +}; export type PropertyFlags = StructFlags; -export type MethodFlags = { static?: boolean }; +export type MethodFlags = { static?: boolean; mutating?: boolean }; export type DeclarationFlag = { final?: boolean }; export type Kind = 'class' | 'struct' | 'extension' | 'enum'; export type VariableDeclaration = { @@ -306,6 +311,7 @@ export class SwiftDeclarationBlock { [ method.access === 'DEFAULT' ? '' : method.access, method.flags.static ? 'static' : '', + method.flags.mutating ? 'mutating' : '', ['init', 'deinit'].includes(method.name) ? '' : 'func', `${escapeKeywords(method.name)}${argWithParenthesis}`, method.returnType ? `-> ${method.returnType}` : '', @@ -340,11 +346,10 @@ export class SwiftDeclarationBlock { const res: string[] = args.reduce((acc: string[], arg) => { const type = arg.flags.isList ? this.getListType(arg) : escapeKeywords(arg.type); if (arg.flags.handleListNullabilityTransparently) { - const isArgOptional = arg.flags.isList ? arg.flags.isListNullable : arg.flags.optional + const isArgOptional = arg.flags.isList ? arg.flags.isListNullable : arg.flags.optional; const val: string | null = arg.value ? arg.value : isArgOptional ? 'nil' : arg.flags.isList ? '[]' : null; acc.push([escapeKeywords(arg.name), ': ', type, isArgOptional ? '?' : '', val ? ` = ${val}` : ''].join('')); - } - else { + } else { const val: string | null = arg.value ? arg.value : arg.flags.isList ? '[]' : arg.flags.optional ? 'nil' : null; acc.push([escapeKeywords(arg.name), ': ', type, arg.flags.optional ? '?' : '', val ? ` = ${val}` : ''].join('')); } @@ -404,9 +409,9 @@ export class SwiftDeclarationBlock { private getListType(typeDeclaration: MethodArgument): string { let listMemberType = `${escapeKeywords(typeDeclaration.type)}`; if (typeDeclaration.flags.handleListNullabilityTransparently) { - listMemberType = typeDeclaration.flags.optional ? - `${escapeKeywords(typeDeclaration.type)}?`: - `${escapeKeywords(typeDeclaration.type)}` + listMemberType = typeDeclaration.flags.optional + ? `${escapeKeywords(typeDeclaration.type)}?` + : `${escapeKeywords(typeDeclaration.type)}`; } if (typeDeclaration.flags.listType === ListType.LIST) { return `List<${listMemberType}>`; diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts index fce0749ea..5191d59c7 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts @@ -99,6 +99,7 @@ export class AppSyncSwiftVisitor< ? field.connectionInfo.kind === CodeGenConnectionType.HAS_ONE || field.connectionInfo.kind === CodeGenConnectionType.BELONGS_TO : false; if (connectionHasOneOrBelongsTo) { + // lazy loading - create computed property of LazyReference structBlock.addProperty(`_${this.getFieldName(field)}`, `LazyReference<${fieldType}>`, undefined, `internal`, { optional: false, isList: field.isList, @@ -234,6 +235,38 @@ export class AppSyncSwiftVisitor< {}, ); } + + // mutating functions for updating/deleting + Object.entries(obj.fields).forEach(([fieldName, field]) => { + const connectionHasOneOrBelongsTo: boolean = field.connectionInfo + ? field.connectionInfo.kind === CodeGenConnectionType.HAS_ONE || field.connectionInfo.kind === CodeGenConnectionType.BELONGS_TO + : false; + if (connectionHasOneOrBelongsTo) { + // lazy loading - create setter functions for LazyReference + let fieldName = this.getFieldName(field); + let capitalizedFieldName = fieldName.charAt(0).toUpperCase() + fieldName.slice(1); + structBlock.addClassMethod( + `set${capitalizedFieldName}`, + null, + `self._${fieldName} = LazyReference(${fieldName})`, + [ + { + name: this.getFieldName(field), + type: this.getNativeType(field), + value: undefined, + flags: { optional: !this.isFieldRequired(field) }, + }, + ], + 'public', + { + mutating: true, + }, + ); + } + }); + + // custom decoder/encoder + result.push(structBlock.string); }); return result.join('\n'); From cd8027193a528dde03de965a393aef0d3ffa2da0 Mon Sep 17 00:00:00 2001 From: Michael Law <1365977+lawmicha@users.noreply.github.com> Date: Mon, 5 Dec 2022 14:18:33 -0500 Subject: [PATCH 05/17] feat(amplify-codegen): custom decoder encoder for LazyReference --- .../appsync-swift-visitor.test.ts.snap | 194 ++++++++ .../visitors/appsync-swift-visitor.test.ts | 252 +++++++++++ .../appsync-swift-visitor.test.ts.snap | 426 ++++++++++++++++++ .../src/languages/swift-declaration-block.ts | 3 +- .../src/visitors/appsync-swift-visitor.ts | 72 +++ 5 files changed, 946 insertions(+), 1 deletion(-) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap index 3661dbb34..5d90e8e50 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -39,6 +39,22 @@ public struct Post: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + title = try values.decode(String.self, .title) + comments = try values.decode(List?.self, .comments) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(title, forKey: .title) + try container.encode(comments, forKey: .comments) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -150,6 +166,24 @@ public struct Comment: Model { self.postCommentsId = postCommentsId self.postCommentsTitle = postCommentsTitle } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + content = try values.decode(String.self, .content) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + postCommentsId = try values.decode(String?.self, .postCommentsId) + postCommentsTitle = try values.decode(String?.self, .postCommentsTitle) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(content, forKey: .content) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + try container.encode(postCommentsId, forKey: .postCommentsId) + try container.encode(postCommentsTitle, forKey: .postCommentsTitle) + } }" `; @@ -261,6 +295,22 @@ public struct ModelCompositePk: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + dob = try values.decode(Temporal.DateTime.self, .dob) + name = try values.decode(String?.self, .name) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(dob, forKey: .dob) + try container.encode(name, forKey: .name) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -362,6 +412,20 @@ public struct ModelExplicitCustomPk: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + userId = try values.decode(String.self, .userId) + name = try values.decode(String?.self, .name) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(userId, forKey: .userId) + try container.encode(name, forKey: .name) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -457,6 +521,20 @@ public struct ModelExplicitDefaultPk: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + name = try values.decode(String?.self, .name) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -546,6 +624,20 @@ public struct ModelImplicitDefaultPk: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + name = try values.decode(String?.self, .name) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -657,6 +749,26 @@ public struct Project: Model { public mutating func setTeam(team: Team? = nil) { self._team = LazyReference(team) } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + projectId = try values.decode(String.self, .projectId) + name = try values.decode(String.self, .name) + _team = try values.decode(LazyReference).self, .team) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + projectTeamTeamId = try values.decode(String?.self, .projectTeamTeamId) + projectTeamName = try values.decode(String?.self, .projectTeamName) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(projectId, forKey: .projectId) + try container.encode(name, forKey: .name) + try container.encode(LazyReference(team), forKey: .LazyReference(team)) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + try container.encode(projectTeamTeamId, forKey: .projectTeamTeamId) + try container.encode(projectTeamName, forKey: .projectTeamName) + } }" `; @@ -781,6 +893,22 @@ public struct Team: Model { public mutating func setProject(project: Project? = nil) { self._project = LazyReference(project) } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + teamId = try values.decode(String.self, .teamId) + name = try values.decode(String.self, .name) + _project = try values.decode(LazyReference).self, .project) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(teamId, forKey: .teamId) + try container.encode(name, forKey: .name) + try container.encode(LazyReference(project), forKey: .LazyReference(project)) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -917,6 +1045,34 @@ public struct ListContainer: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + name = try values.decode(String?.self, .name) + list = try values.decode([Int].self, .list) + requiredList = try values.decode([String].self, .requiredList) + requiredListOfRequired = try values.decode([StatusEnum].self, .requiredListOfRequired) + listOfRequired = try values.decode([Bool].self, .listOfRequired) + requiredListOfRequiredDates = try values.decode([Temporal.Date].self, .requiredListOfRequiredDates) + listOfRequiredFloats = try values.decode([Double].self, .listOfRequiredFloats) + requiredListOfCustomTypes = try values.decode([CustomType].self, .requiredListOfCustomTypes) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(list, forKey: .list) + try container.encode(requiredList, forKey: .requiredList) + try container.encode(requiredListOfRequired, forKey: .requiredListOfRequired) + try container.encode(listOfRequired, forKey: .listOfRequired) + try container.encode(requiredListOfRequiredDates, forKey: .requiredListOfRequiredDates) + try container.encode(listOfRequiredFloats, forKey: .listOfRequiredFloats) + try container.encode(requiredListOfCustomTypes, forKey: .requiredListOfCustomTypes) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -1059,6 +1215,28 @@ public struct Todo: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + title = try values.decode(String.self, .title) + requiredListOfTasks = try values.decode(List?.self, .requiredListOfTasks) + listOfRequiredTasks = try values.decode(List?.self, .listOfRequiredTasks) + listOfTasks = try values.decode(List?.self, .listOfTasks) + requiredListOfRequiredTasks = try values.decode(List?.self, .requiredListOfRequiredTasks) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(title, forKey: .title) + try container.encode(requiredListOfTasks, forKey: .requiredListOfTasks) + try container.encode(listOfRequiredTasks, forKey: .listOfRequiredTasks) + try container.encode(listOfTasks, forKey: .listOfTasks) + try container.encode(requiredListOfRequiredTasks, forKey: .requiredListOfRequiredTasks) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -1171,6 +1349,22 @@ public struct task: Model { public mutating func setTodo(todo: Todo? = nil) { self._todo = LazyReference(todo) } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + title = try values.decode(String.self, .title) + _todo = try values.decode(LazyReference).self, .todo) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(title, forKey: .title) + try container.encode(LazyReference(todo), forKey: .LazyReference(todo)) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts index e924664c6..77cc4bd30 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts @@ -92,6 +92,22 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + name = try values.decode(String?.self, .name) + bar = try values.decode(String?.self, .bar) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(bar, forKey: .bar) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `); const metadataVisitor = getVisitor(schema, 'SimpleModel', CodeGenGenerateEnum.metadata); @@ -218,6 +234,20 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + name = try values.decode(String?.self, .name) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `); @@ -310,6 +340,20 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + first_name = try values.decode(String?.self, .first_name) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(first_name, forKey: .first_name) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `); }); @@ -368,6 +412,26 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + author_id = try values.decode(String.self, .author_id) + book_id = try values.decode(String.self, .book_id) + author = try values.decode(String?.self, .author) + book = try values.decode(String?.self, .book) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(author_id, forKey: .author_id) + try container.encode(book_id, forKey: .book_id) + try container.encode(author, forKey: .author) + try container.encode(book, forKey: .book) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `); @@ -669,6 +733,32 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + title = try values.decode(String.self, .title) + done = try values.decode(Bool.self, .done) + description = try values.decode(String?.self, .description) + due_date = try values.decode(String?.self, .due_date) + version = try values.decode(Int.self, .version) + value = try values.decode(Double?.self, .value) + tasks = try values.decode(List?.self, .tasks) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(title, forKey: .title) + try container.encode(done, forKey: .done) + try container.encode(description, forKey: .description) + try container.encode(due_date, forKey: .due_date) + try container.encode(version, forKey: .version) + try container.encode(value, forKey: .value) + try container.encode(tasks, forKey: .tasks) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `); @@ -812,6 +902,28 @@ describe('AppSyncSwiftVisitor', () => { public mutating func setTodo(todo: Todo? = nil) { self._todo = LazyReference(todo) } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + title = try values.decode(String.self, .title) + done = try values.decode(Bool.self, .done) + _todo = try values.decode(LazyReference).self, .todo) + time = try values.decode(Temporal.Time?.self, .time) + createdOn = try values.decode(Temporal.Date?.self, .createdOn) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(title, forKey: .title) + try container.encode(done, forKey: .done) + try container.encode(LazyReference(todo), forKey: .LazyReference(todo)) + try container.encode(time, forKey: .time) + try container.encode(createdOn, forKey: .createdOn) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `); @@ -981,6 +1093,22 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + title = try values.decode(String.self, .title) + editors = try values.decode(List?.self, .editors) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(title, forKey: .title) + try container.encode(editors, forKey: .editors) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `); @@ -1072,6 +1200,22 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + title = try values.decode(String.self, .title) + editors = try values.decode(List?.self, .editors) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(title, forKey: .title) + try container.encode(editors, forKey: .editors) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `); @@ -1203,6 +1347,30 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + intArr = try values.decode([Int].self, .intArr) + strArr = try values.decode([String].self, .strArr) + floatArr = try values.decode([Double].self, .floatArr) + boolArr = try values.decode([Bool].self, .boolArr) + dateArr = try values.decode([Temporal.Date].self, .dateArr) + enumArr = try values.decode([EnumType].self, .enumArr) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(intArr, forKey: .intArr) + try container.encode(strArr, forKey: .strArr) + try container.encode(floatArr, forKey: .floatArr) + try container.encode(boolArr, forKey: .boolArr) + try container.encode(dateArr, forKey: .dateArr) + try container.encode(enumArr, forKey: .enumArr) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `); @@ -1355,6 +1523,30 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + name = try values.decode(String.self, .name) + location = try values.decode(Location.self, .location) + nearByLocations = try values.decode([Location].self, .nearByLocations) + status = try values.decode(Status.self, .status) + statusHistory = try values.decode([Status].self, .statusHistory) + tags = try values.decode([String].self, .tags) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(location, forKey: .location) + try container.encode(nearByLocations, forKey: .nearByLocations) + try container.encode(status, forKey: .status) + try container.encode(statusHistory, forKey: .statusHistory) + try container.encode(tags, forKey: .tags) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `); @@ -1615,6 +1807,26 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + \`Class\` = try values.decode(Class?.self, .\`Class\`) + nonNullClass = try values.decode(Class.self, .nonNullClass) + classes = try values.decode([Class].self, .classes) + nonNullClasses = try values.decode([Class].self, .nonNullClasses) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(\`Class\`, forKey: .\`Class\`) + try container.encode(nonNullClass, forKey: .nonNullClass) + try container.encode(classes, forKey: .classes) + try container.encode(nonNullClasses, forKey: .nonNullClasses) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `); }); @@ -2708,6 +2920,22 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + name = try values.decode(String?.self, .name) + bar = try values.decode(String?.self, .bar) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(bar, forKey: .bar) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `); }); @@ -2738,6 +2966,18 @@ describe('AppSyncSwiftVisitor', () => { self.name = name self.bar = bar } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + name = try values.decode(String?.self, .name) + bar = try values.decode(String?.self, .bar) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(bar, forKey: .bar) + } }" `); }); @@ -2768,6 +3008,18 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `); }); diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap index dcd9c7a7b..d5dc4dc49 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -32,6 +32,22 @@ public struct Post3: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + title = try values.decode(String.self, .title) + comments = try values.decode(List?.self, .comments) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(title, forKey: .title) + try container.encode(comments, forKey: .comments) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -129,6 +145,22 @@ public struct Comment3: Model { public mutating func setPost(post: Post3) { self._post = LazyReference(post) } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + content = try values.decode(String.self, .content) + _post = try values.decode(LazyReference).self, .post) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(content, forKey: .content) + try container.encode(LazyReference(post), forKey: .LazyReference(post)) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -222,6 +254,22 @@ public struct Post: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + title = try values.decode(String.self, .title) + comments = try values.decode(List?.self, .comments) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(title, forKey: .title) + try container.encode(comments, forKey: .comments) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -319,6 +367,22 @@ public struct Comment: Model { public mutating func setPost(post: Post? = nil) { self._post = LazyReference(post) } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + content = try values.decode(String.self, .content) + _post = try values.decode(LazyReference).self, .post) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(content, forKey: .content) + try container.encode(LazyReference(post), forKey: .LazyReference(post)) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -425,6 +489,24 @@ public struct Project2: Model { public mutating func setTeam(team: Team2? = nil) { self._team = LazyReference(team) } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + name = try values.decode(String?.self, .name) + _team = try values.decode(LazyReference).self, .team) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + project2TeamId = try values.decode(String?.self, .project2TeamId) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(LazyReference(team), forKey: .LazyReference(team)) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + try container.encode(project2TeamId, forKey: .project2TeamId) + } }" `; @@ -527,6 +609,22 @@ public struct Team2: Model { public mutating func setProject(project: Project2? = nil) { self._project = LazyReference(project) } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + name = try values.decode(String.self, .name) + _project = try values.decode(LazyReference).self, .project) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(LazyReference(project), forKey: .LazyReference(project)) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -616,6 +714,22 @@ public struct Blog7V2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + name = try values.decode(String.self, .name) + posts = try values.decode(List?.self, .posts) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(posts, forKey: .posts) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -718,6 +832,24 @@ public struct Post7V2: Model { public mutating func setBlog(blog: Blog7V2? = nil) { self._blog = LazyReference(blog) } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + title = try values.decode(String.self, .title) + _blog = try values.decode(LazyReference).self, .blog) + comments = try values.decode(List?.self, .comments) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(title, forKey: .title) + try container.encode(LazyReference(blog), forKey: .LazyReference(blog)) + try container.encode(comments, forKey: .comments) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -820,6 +952,22 @@ public struct Comment7V2: Model { public mutating func setPost(post: Post7V2? = nil) { self._post = LazyReference(post) } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + content = try values.decode(String?.self, .content) + _post = try values.decode(LazyReference).self, .post) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(content, forKey: .content) + try container.encode(LazyReference(post), forKey: .LazyReference(post)) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -909,6 +1057,22 @@ public struct Blog8V2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + name = try values.decode(String.self, .name) + posts = try values.decode(List?.self, .posts) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(posts, forKey: .posts) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -1011,6 +1175,24 @@ public struct Post8V2: Model { public mutating func setBlog(blog: Blog8V2) { self._blog = LazyReference(blog) } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + title = try values.decode(String.self, .title) + _blog = try values.decode(LazyReference).self, .blog) + comments = try values.decode(List?.self, .comments) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(title, forKey: .title) + try container.encode(LazyReference(blog), forKey: .LazyReference(blog)) + try container.encode(comments, forKey: .comments) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -1113,6 +1295,22 @@ public struct Comment8V2: Model { public mutating func setPost(post: Post8V2) { self._post = LazyReference(post) } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + content = try values.decode(String?.self, .content) + _post = try values.decode(LazyReference).self, .post) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(content, forKey: .content) + try container.encode(LazyReference(post), forKey: .LazyReference(post)) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -1215,6 +1413,24 @@ public struct Project: Model { public mutating func setTeam(team: Team? = nil) { self._team = LazyReference(team) } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + name = try values.decode(String?.self, .name) + _team = try values.decode(LazyReference).self, .team) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + projectTeamId = try values.decode(String?.self, .projectTeamId) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(LazyReference(team), forKey: .LazyReference(team)) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + try container.encode(projectTeamId, forKey: .projectTeamId) + } }" `; @@ -1317,6 +1533,22 @@ public struct Team: Model { public mutating func setProject(project: Project? = nil) { self._project = LazyReference(project) } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + name = try values.decode(String.self, .name) + _project = try values.decode(LazyReference).self, .project) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(LazyReference(project), forKey: .LazyReference(project)) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -1411,6 +1643,24 @@ public struct Post: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + title = try values.decode(String.self, .title) + content = try values.decode(String?.self, .content) + tags = try values.decode(List?.self, .tags) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(title, forKey: .title) + try container.encode(content, forKey: .content) + try container.encode(tags, forKey: .tags) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -1505,6 +1755,22 @@ public struct Tag: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + label = try values.decode(String.self, .label) + posts = try values.decode(List?.self, .posts) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(label, forKey: .label) + try container.encode(posts, forKey: .posts) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -1589,6 +1855,20 @@ public struct Todo: Model { self.createdOn = createdOn self.updatedOn = updatedOn } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + content = try values.decode(String?.self, .content) + createdOn = try values.decode(Temporal.DateTime?.self, .createdOn) + updatedOn = try values.decode(Temporal.DateTime?.self, .updatedOn) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(content, forKey: .content) + try container.encode(createdOn, forKey: .createdOn) + try container.encode(updatedOn, forKey: .updatedOn) + } }" `; @@ -1673,6 +1953,22 @@ public struct Post2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + title = try values.decode(String.self, .title) + comments = try values.decode(List?.self, .comments) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(title, forKey: .title) + try container.encode(comments, forKey: .comments) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -1762,6 +2058,22 @@ public struct Comment2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + postID = try values.decode(String.self, .postID) + content = try values.decode(String.self, .content) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(postID, forKey: .postID) + try container.encode(content, forKey: .content) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -1868,6 +2180,24 @@ public struct Project2: Model { public mutating func setTeam(team: Team2? = nil) { self._team = LazyReference(team) } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + name = try values.decode(String?.self, .name) + teamID = try values.decode(String?.self, .teamID) + _team = try values.decode(LazyReference).self, .team) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(teamID, forKey: .teamID) + try container.encode(LazyReference(team), forKey: .LazyReference(team)) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -1957,6 +2287,20 @@ public struct Team2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + name = try values.decode(String.self, .name) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -2041,6 +2385,22 @@ public struct Post: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + title = try values.decode(String.self, .title) + comments = try values.decode(List?.self, .comments) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(title, forKey: .title) + try container.encode(comments, forKey: .comments) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -2130,6 +2490,22 @@ public struct Comment: Model { self.updatedAt = updatedAt self.postCommentsId = postCommentsId } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + content = try values.decode(String.self, .content) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + postCommentsId = try values.decode(String?.self, .postCommentsId) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(content, forKey: .content) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + try container.encode(postCommentsId, forKey: .postCommentsId) + } }" `; @@ -2232,6 +2608,24 @@ public struct Project: Model { public mutating func setTeam(team: Team? = nil) { self._team = LazyReference(team) } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + name = try values.decode(String?.self, .name) + _team = try values.decode(LazyReference).self, .team) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + projectTeamId = try values.decode(String?.self, .projectTeamId) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(LazyReference(team), forKey: .LazyReference(team)) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + try container.encode(projectTeamId, forKey: .projectTeamId) + } }" `; @@ -2321,6 +2715,20 @@ public struct Team: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + name = try values.decode(String.self, .name) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; @@ -2410,6 +2818,24 @@ public struct Customer: Model { self.createdAt = createdAt self.updatedAt = updatedAt } + public init(from: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, .id) + name = try values.decode(String.self, .name) + phoneNumber = try values.decode(String?.self, .phoneNumber) + accountRepresentativeID = try values.decode(String.self, .accountRepresentativeID) + createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(name, forKey: .name) + try container.encode(phoneNumber, forKey: .phoneNumber) + try container.encode(accountRepresentativeID, forKey: .accountRepresentativeID) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } }" `; diff --git a/packages/appsync-modelgen-plugin/src/languages/swift-declaration-block.ts b/packages/appsync-modelgen-plugin/src/languages/swift-declaration-block.ts index 5fd65eaa0..032fe3194 100644 --- a/packages/appsync-modelgen-plugin/src/languages/swift-declaration-block.ts +++ b/packages/appsync-modelgen-plugin/src/languages/swift-declaration-block.ts @@ -130,7 +130,7 @@ export type StructFlags = VariableFlags & { handleListNullabilityTransparently?: boolean; }; export type PropertyFlags = StructFlags; -export type MethodFlags = { static?: boolean; mutating?: boolean }; +export type MethodFlags = { static?: boolean; mutating?: boolean; throws?: boolean }; export type DeclarationFlag = { final?: boolean }; export type Kind = 'class' | 'struct' | 'extension' | 'enum'; export type VariableDeclaration = { @@ -314,6 +314,7 @@ export class SwiftDeclarationBlock { method.flags.mutating ? 'mutating' : '', ['init', 'deinit'].includes(method.name) ? '' : 'func', `${escapeKeywords(method.name)}${argWithParenthesis}`, + method.flags.throws ? 'throws' : '', method.returnType ? `-> ${method.returnType}` : '', '{', ], diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts index 5191d59c7..0bc237fd9 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts @@ -266,6 +266,36 @@ export class AppSyncSwiftVisitor< }); // custom decoder/encoder + structBlock.addClassMethod( + 'init', + null, + this.getDecoderBody(obj.fields), + [ + { + value: undefined, + name: 'from', + type: 'Decoder', + flags: {}, + }, + ], + 'public', + { throws: true }, + ); + structBlock.addClassMethod( + 'encode', + null, + this.getEncoderBody(obj.fields), + [ + { + value: undefined, + name: 'to encoder', + type: 'Encoder', + flags: {}, + }, + ], + 'public', + { throws: true }, + ); result.push(structBlock.string); }); @@ -508,6 +538,48 @@ export class AppSyncSwiftVisitor< return result.join('\n'); } + + private getDecoderBody(fields: CodeGenField[]): string { + let result: string[] = []; + result.push(indent('let values = try decoder.container(keyedBy: CodingKeys.self)')); + fields.forEach(field => { + const connectionHasOneOrBelongsTo: boolean = field.connectionInfo + ? field.connectionInfo.kind === CodeGenConnectionType.HAS_ONE || field.connectionInfo.kind === CodeGenConnectionType.BELONGS_TO + : false; + const escapedFieldName = escapeKeywords(this.getFieldName(field)); + const assignedFieldName = connectionHasOneOrBelongsTo ? `_${this.getFieldName(field)}` : escapedFieldName; + + const nativeType = this.getNativeType(field); + const optionality = !this.isFieldRequired(field) ? '?' : ''; + const fieldType = connectionHasOneOrBelongsTo + ? `LazyReference<${nativeType}>)` + : field.isList + ? field.connectionInfo + ? `List<${nativeType}>${optionality}` + : `[${nativeType}]` + : `${nativeType}${optionality}`; + result.push(indent(`${assignedFieldName} = try values.decode(${fieldType}.self, .${escapedFieldName})`)); + }); + + return result.join('\n'); + } + + private getEncoderBody(fields: CodeGenField[]): string { + let result: string[] = []; + result.push(indent('var container = encoder.container(keyedBy: CodingKeys.self)')); + fields.forEach(field => { + const connectionHasOneOrBelongsTo: boolean = field.connectionInfo + ? field.connectionInfo.kind === CodeGenConnectionType.HAS_ONE || field.connectionInfo.kind === CodeGenConnectionType.BELONGS_TO + : false; + + const escapedFieldName = escapeKeywords(this.getFieldName(field)); + const fieldValue = connectionHasOneOrBelongsTo ? `LazyReference(${escapedFieldName})` : escapedFieldName; + result.push(indent(`try container.encode(${fieldValue}, forKey: .${fieldValue})`)); + }); + + return result.join('\n'); + } + protected getListType(typeStr: string, field: CodeGenField): string { return `${typeStr}`; } From eb47245083af3b84f2d5a398d61359ed08b47130 Mon Sep 17 00:00:00 2001 From: Michael Law <1365977+lawmicha@users.noreply.github.com> Date: Tue, 6 Dec 2022 00:56:21 -0500 Subject: [PATCH 06/17] feat(amplify-codegen): custom decoder encoder for LazyReference 2 --- .../appsync-swift-visitor.test.ts.snap | 6 ++--- .../visitors/appsync-swift-visitor.test.ts | 2 +- .../appsync-swift-visitor.test.ts.snap | 24 +++++++++---------- .../src/visitors/appsync-swift-visitor.ts | 6 ++++- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap index 5d90e8e50..80ea61781 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -753,7 +753,7 @@ public struct Project: Model { let values = try decoder.container(keyedBy: CodingKeys.self) projectId = try values.decode(String.self, .projectId) name = try values.decode(String.self, .name) - _team = try values.decode(LazyReference).self, .team) + _team = try values.decodeIfPresent(LazyReference).self, .team) ?? LazyReference(identifiers: nil) createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) projectTeamTeamId = try values.decode(String?.self, .projectTeamTeamId) @@ -897,7 +897,7 @@ public struct Team: Model { let values = try decoder.container(keyedBy: CodingKeys.self) teamId = try values.decode(String.self, .teamId) name = try values.decode(String.self, .name) - _project = try values.decode(LazyReference).self, .project) + _project = try values.decodeIfPresent(LazyReference).self, .project) ?? LazyReference(identifiers: nil) createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) } @@ -1353,7 +1353,7 @@ public struct task: Model { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) title = try values.decode(String.self, .title) - _todo = try values.decode(LazyReference).self, .todo) + _todo = try values.decodeIfPresent(LazyReference).self, .todo) ?? LazyReference(identifiers: nil) createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) } diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts index 77cc4bd30..cf0f0016f 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts @@ -907,7 +907,7 @@ describe('AppSyncSwiftVisitor', () => { id = try values.decode(String.self, .id) title = try values.decode(String.self, .title) done = try values.decode(Bool.self, .done) - _todo = try values.decode(LazyReference).self, .todo) + _todo = try values.decodeIfPresent(LazyReference).self, .todo) ?? LazyReference(identifiers: nil) time = try values.decode(Temporal.Time?.self, .time) createdOn = try values.decode(Temporal.Date?.self, .createdOn) createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap index d5dc4dc49..0b6435781 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -149,7 +149,7 @@ public struct Comment3: Model { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) content = try values.decode(String.self, .content) - _post = try values.decode(LazyReference).self, .post) + _post = try values.decodeIfPresent(LazyReference).self, .post) ?? LazyReference(identifiers: nil) createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) } @@ -371,7 +371,7 @@ public struct Comment: Model { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) content = try values.decode(String.self, .content) - _post = try values.decode(LazyReference).self, .post) + _post = try values.decodeIfPresent(LazyReference).self, .post) ?? LazyReference(identifiers: nil) createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) } @@ -493,7 +493,7 @@ public struct Project2: Model { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String?.self, .name) - _team = try values.decode(LazyReference).self, .team) + _team = try values.decodeIfPresent(LazyReference).self, .team) ?? LazyReference(identifiers: nil) createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) project2TeamId = try values.decode(String?.self, .project2TeamId) @@ -613,7 +613,7 @@ public struct Team2: Model { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String.self, .name) - _project = try values.decode(LazyReference).self, .project) + _project = try values.decodeIfPresent(LazyReference).self, .project) ?? LazyReference(identifiers: nil) createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) } @@ -836,7 +836,7 @@ public struct Post7V2: Model { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) title = try values.decode(String.self, .title) - _blog = try values.decode(LazyReference).self, .blog) + _blog = try values.decodeIfPresent(LazyReference).self, .blog) ?? LazyReference(identifiers: nil) comments = try values.decode(List?.self, .comments) createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) @@ -956,7 +956,7 @@ public struct Comment7V2: Model { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) content = try values.decode(String?.self, .content) - _post = try values.decode(LazyReference).self, .post) + _post = try values.decodeIfPresent(LazyReference).self, .post) ?? LazyReference(identifiers: nil) createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) } @@ -1179,7 +1179,7 @@ public struct Post8V2: Model { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) title = try values.decode(String.self, .title) - _blog = try values.decode(LazyReference).self, .blog) + _blog = try values.decodeIfPresent(LazyReference).self, .blog) ?? LazyReference(identifiers: nil) comments = try values.decode(List?.self, .comments) createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) @@ -1299,7 +1299,7 @@ public struct Comment8V2: Model { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) content = try values.decode(String?.self, .content) - _post = try values.decode(LazyReference).self, .post) + _post = try values.decodeIfPresent(LazyReference).self, .post) ?? LazyReference(identifiers: nil) createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) } @@ -1417,7 +1417,7 @@ public struct Project: Model { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String?.self, .name) - _team = try values.decode(LazyReference).self, .team) + _team = try values.decodeIfPresent(LazyReference).self, .team) ?? LazyReference(identifiers: nil) createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) projectTeamId = try values.decode(String?.self, .projectTeamId) @@ -1537,7 +1537,7 @@ public struct Team: Model { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String.self, .name) - _project = try values.decode(LazyReference).self, .project) + _project = try values.decodeIfPresent(LazyReference).self, .project) ?? LazyReference(identifiers: nil) createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) } @@ -2185,7 +2185,7 @@ public struct Project2: Model { id = try values.decode(String.self, .id) name = try values.decode(String?.self, .name) teamID = try values.decode(String?.self, .teamID) - _team = try values.decode(LazyReference).self, .team) + _team = try values.decodeIfPresent(LazyReference).self, .team) ?? LazyReference(identifiers: nil) createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) } @@ -2612,7 +2612,7 @@ public struct Project: Model { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String?.self, .name) - _team = try values.decode(LazyReference).self, .team) + _team = try values.decodeIfPresent(LazyReference).self, .team) ?? LazyReference(identifiers: nil) createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) projectTeamId = try values.decode(String?.self, .projectTeamId) diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts index 0bc237fd9..7c70d143c 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts @@ -558,7 +558,11 @@ export class AppSyncSwiftVisitor< ? `List<${nativeType}>${optionality}` : `[${nativeType}]` : `${nativeType}${optionality}`; - result.push(indent(`${assignedFieldName} = try values.decode(${fieldType}.self, .${escapedFieldName})`)); + const decodeMethod = connectionHasOneOrBelongsTo ? 'decodeIfPresent' : 'decode'; + const defaultLazyReference = connectionHasOneOrBelongsTo ? ' ?? LazyReference(identifiers: nil)' : ''; + result.push( + indent(`${assignedFieldName} = try values.${decodeMethod}(${fieldType}.self, .${escapedFieldName})${defaultLazyReference}`), + ); }); return result.join('\n'); From 41239b49075e6f939ca1310a77b25d81f0100ba8 Mon Sep 17 00:00:00 2001 From: Michael Law <1365977+lawmicha@users.noreply.github.com> Date: Tue, 6 Dec 2022 10:09:16 -0500 Subject: [PATCH 07/17] feat(amplify-codegen): custom decoder encoder for LazyReference 3 --- .../appsync-swift-visitor.test.ts.snap | 22 ++++---- .../visitors/appsync-swift-visitor.test.ts | 28 +++++----- .../appsync-swift-visitor.test.ts.snap | 52 +++++++++---------- .../src/visitors/appsync-swift-visitor.ts | 2 +- 4 files changed, 52 insertions(+), 52 deletions(-) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap index 80ea61781..07eb6a13d 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -39,7 +39,7 @@ public struct Post: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) title = try values.decode(String.self, .title) @@ -166,7 +166,7 @@ public struct Comment: Model { self.postCommentsId = postCommentsId self.postCommentsTitle = postCommentsTitle } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) content = try values.decode(String.self, .content) @@ -295,7 +295,7 @@ public struct ModelCompositePk: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) dob = try values.decode(Temporal.DateTime.self, .dob) @@ -412,7 +412,7 @@ public struct ModelExplicitCustomPk: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) userId = try values.decode(String.self, .userId) name = try values.decode(String?.self, .name) @@ -521,7 +521,7 @@ public struct ModelExplicitDefaultPk: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String?.self, .name) @@ -624,7 +624,7 @@ public struct ModelImplicitDefaultPk: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String?.self, .name) @@ -749,7 +749,7 @@ public struct Project: Model { public mutating func setTeam(team: Team? = nil) { self._team = LazyReference(team) } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) projectId = try values.decode(String.self, .projectId) name = try values.decode(String.self, .name) @@ -893,7 +893,7 @@ public struct Team: Model { public mutating func setProject(project: Project? = nil) { self._project = LazyReference(project) } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) teamId = try values.decode(String.self, .teamId) name = try values.decode(String.self, .name) @@ -1045,7 +1045,7 @@ public struct ListContainer: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String?.self, .name) @@ -1215,7 +1215,7 @@ public struct Todo: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) title = try values.decode(String.self, .title) @@ -1349,7 +1349,7 @@ public struct task: Model { public mutating func setTodo(todo: Todo? = nil) { self._todo = LazyReference(todo) } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) title = try values.decode(String.self, .title) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts index cf0f0016f..db3499425 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts @@ -92,7 +92,7 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String?.self, .name) @@ -234,7 +234,7 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String?.self, .name) @@ -340,7 +340,7 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) first_name = try values.decode(String?.self, .first_name) @@ -412,7 +412,7 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) author_id = try values.decode(String.self, .author_id) @@ -733,7 +733,7 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) title = try values.decode(String.self, .title) @@ -902,7 +902,7 @@ describe('AppSyncSwiftVisitor', () => { public mutating func setTodo(todo: Todo? = nil) { self._todo = LazyReference(todo) } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) title = try values.decode(String.self, .title) @@ -1093,7 +1093,7 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) title = try values.decode(String.self, .title) @@ -1200,7 +1200,7 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) title = try values.decode(String.self, .title) @@ -1347,7 +1347,7 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) intArr = try values.decode([Int].self, .intArr) @@ -1523,7 +1523,7 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String.self, .name) @@ -1807,7 +1807,7 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) \`Class\` = try values.decode(Class?.self, .\`Class\`) @@ -2920,7 +2920,7 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String?.self, .name) @@ -2966,7 +2966,7 @@ describe('AppSyncSwiftVisitor', () => { self.name = name self.bar = bar } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String?.self, .name) @@ -3008,7 +3008,7 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap index 0b6435781..ca53ac9a3 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -32,7 +32,7 @@ public struct Post3: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) title = try values.decode(String.self, .title) @@ -145,7 +145,7 @@ public struct Comment3: Model { public mutating func setPost(post: Post3) { self._post = LazyReference(post) } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) content = try values.decode(String.self, .content) @@ -254,7 +254,7 @@ public struct Post: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) title = try values.decode(String.self, .title) @@ -367,7 +367,7 @@ public struct Comment: Model { public mutating func setPost(post: Post? = nil) { self._post = LazyReference(post) } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) content = try values.decode(String.self, .content) @@ -489,7 +489,7 @@ public struct Project2: Model { public mutating func setTeam(team: Team2? = nil) { self._team = LazyReference(team) } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String?.self, .name) @@ -609,7 +609,7 @@ public struct Team2: Model { public mutating func setProject(project: Project2? = nil) { self._project = LazyReference(project) } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String.self, .name) @@ -714,7 +714,7 @@ public struct Blog7V2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String.self, .name) @@ -832,7 +832,7 @@ public struct Post7V2: Model { public mutating func setBlog(blog: Blog7V2? = nil) { self._blog = LazyReference(blog) } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) title = try values.decode(String.self, .title) @@ -952,7 +952,7 @@ public struct Comment7V2: Model { public mutating func setPost(post: Post7V2? = nil) { self._post = LazyReference(post) } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) content = try values.decode(String?.self, .content) @@ -1057,7 +1057,7 @@ public struct Blog8V2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String.self, .name) @@ -1175,7 +1175,7 @@ public struct Post8V2: Model { public mutating func setBlog(blog: Blog8V2) { self._blog = LazyReference(blog) } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) title = try values.decode(String.self, .title) @@ -1295,7 +1295,7 @@ public struct Comment8V2: Model { public mutating func setPost(post: Post8V2) { self._post = LazyReference(post) } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) content = try values.decode(String?.self, .content) @@ -1413,7 +1413,7 @@ public struct Project: Model { public mutating func setTeam(team: Team? = nil) { self._team = LazyReference(team) } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String?.self, .name) @@ -1533,7 +1533,7 @@ public struct Team: Model { public mutating func setProject(project: Project? = nil) { self._project = LazyReference(project) } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String.self, .name) @@ -1643,7 +1643,7 @@ public struct Post: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) title = try values.decode(String.self, .title) @@ -1755,7 +1755,7 @@ public struct Tag: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) label = try values.decode(String.self, .label) @@ -1855,7 +1855,7 @@ public struct Todo: Model { self.createdOn = createdOn self.updatedOn = updatedOn } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) content = try values.decode(String?.self, .content) @@ -1953,7 +1953,7 @@ public struct Post2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) title = try values.decode(String.self, .title) @@ -2058,7 +2058,7 @@ public struct Comment2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) postID = try values.decode(String.self, .postID) @@ -2180,7 +2180,7 @@ public struct Project2: Model { public mutating func setTeam(team: Team2? = nil) { self._team = LazyReference(team) } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String?.self, .name) @@ -2287,7 +2287,7 @@ public struct Team2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String.self, .name) @@ -2385,7 +2385,7 @@ public struct Post: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) title = try values.decode(String.self, .title) @@ -2490,7 +2490,7 @@ public struct Comment: Model { self.updatedAt = updatedAt self.postCommentsId = postCommentsId } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) content = try values.decode(String.self, .content) @@ -2608,7 +2608,7 @@ public struct Project: Model { public mutating func setTeam(team: Team? = nil) { self._team = LazyReference(team) } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String?.self, .name) @@ -2715,7 +2715,7 @@ public struct Team: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String.self, .name) @@ -2818,7 +2818,7 @@ public struct Customer: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from: Decoder) throws { + public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, .id) name = try values.decode(String.self, .name) diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts index 7c70d143c..563ca559e 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts @@ -273,7 +273,7 @@ export class AppSyncSwiftVisitor< [ { value: undefined, - name: 'from', + name: 'from decoder', type: 'Decoder', flags: {}, }, From df7ee60b4d33458f5b1e3cb1da24d3af042bcdc4 Mon Sep 17 00:00:00 2001 From: Michael Law <1365977+lawmicha@users.noreply.github.com> Date: Tue, 6 Dec 2022 11:40:33 -0500 Subject: [PATCH 08/17] feat(amplify-codegen): custom decoder encoder for LazyReference 4 --- .../appsync-swift-visitor.test.ts.snap | 134 ++++---- .../visitors/appsync-swift-visitor.test.ts | 170 +++++----- .../appsync-swift-visitor.test.ts.snap | 294 +++++++++--------- .../src/visitors/appsync-swift-visitor.ts | 8 +- 4 files changed, 303 insertions(+), 303 deletions(-) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap index 07eb6a13d..f26962b60 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -41,11 +41,11 @@ public struct Post: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - title = try values.decode(String.self, .title) - comments = try values.decode(List?.self, .comments) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + title = try values.decode(String.self, forKey: .title) + comments = try values.decode(List?.self, forKey: .comments) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -168,12 +168,12 @@ public struct Comment: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - content = try values.decode(String.self, .content) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) - postCommentsId = try values.decode(String?.self, .postCommentsId) - postCommentsTitle = try values.decode(String?.self, .postCommentsTitle) + id = try values.decode(String.self, forKey: .id) + content = try values.decode(String.self, forKey: .content) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + postCommentsId = try values.decode(String?.self, forKey: .postCommentsId) + postCommentsTitle = try values.decode(String?.self, forKey: .postCommentsTitle) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -297,11 +297,11 @@ public struct ModelCompositePk: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - dob = try values.decode(Temporal.DateTime.self, .dob) - name = try values.decode(String?.self, .name) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + dob = try values.decode(Temporal.DateTime.self, forKey: .dob) + name = try values.decode(String?.self, forKey: .name) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -414,10 +414,10 @@ public struct ModelExplicitCustomPk: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - userId = try values.decode(String.self, .userId) - name = try values.decode(String?.self, .name) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + userId = try values.decode(String.self, forKey: .userId) + name = try values.decode(String?.self, forKey: .name) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -523,10 +523,10 @@ public struct ModelExplicitDefaultPk: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - name = try values.decode(String?.self, .name) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + name = try values.decode(String?.self, forKey: .name) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -626,10 +626,10 @@ public struct ModelImplicitDefaultPk: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - name = try values.decode(String?.self, .name) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + name = try values.decode(String?.self, forKey: .name) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -751,19 +751,19 @@ public struct Project: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - projectId = try values.decode(String.self, .projectId) - name = try values.decode(String.self, .name) - _team = try values.decodeIfPresent(LazyReference).self, .team) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) - projectTeamTeamId = try values.decode(String?.self, .projectTeamTeamId) - projectTeamName = try values.decode(String?.self, .projectTeamName) + projectId = try values.decode(String.self, forKey: .projectId) + name = try values.decode(String.self, forKey: .name) + _team = try values.decodeIfPresent(LazyReference.self, forKey: .team) ?? LazyReference(identifiers: nil) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + projectTeamTeamId = try values.decode(String?.self, forKey: .projectTeamTeamId) + projectTeamName = try values.decode(String?.self, forKey: .projectTeamName) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(projectId, forKey: .projectId) try container.encode(name, forKey: .name) - try container.encode(LazyReference(team), forKey: .LazyReference(team)) + try container.encode(_team, forKey: .team) try container.encode(createdAt, forKey: .createdAt) try container.encode(updatedAt, forKey: .updatedAt) try container.encode(projectTeamTeamId, forKey: .projectTeamTeamId) @@ -895,17 +895,17 @@ public struct Team: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - teamId = try values.decode(String.self, .teamId) - name = try values.decode(String.self, .name) - _project = try values.decodeIfPresent(LazyReference).self, .project) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + teamId = try values.decode(String.self, forKey: .teamId) + name = try values.decode(String.self, forKey: .name) + _project = try values.decodeIfPresent(LazyReference.self, forKey: .project) ?? LazyReference(identifiers: nil) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(teamId, forKey: .teamId) try container.encode(name, forKey: .name) - try container.encode(LazyReference(project), forKey: .LazyReference(project)) + try container.encode(_project, forKey: .project) try container.encode(createdAt, forKey: .createdAt) try container.encode(updatedAt, forKey: .updatedAt) } @@ -1047,17 +1047,17 @@ public struct ListContainer: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - name = try values.decode(String?.self, .name) - list = try values.decode([Int].self, .list) - requiredList = try values.decode([String].self, .requiredList) - requiredListOfRequired = try values.decode([StatusEnum].self, .requiredListOfRequired) - listOfRequired = try values.decode([Bool].self, .listOfRequired) - requiredListOfRequiredDates = try values.decode([Temporal.Date].self, .requiredListOfRequiredDates) - listOfRequiredFloats = try values.decode([Double].self, .listOfRequiredFloats) - requiredListOfCustomTypes = try values.decode([CustomType].self, .requiredListOfCustomTypes) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + name = try values.decode(String?.self, forKey: .name) + list = try values.decode([Int].self, forKey: .list) + requiredList = try values.decode([String].self, forKey: .requiredList) + requiredListOfRequired = try values.decode([StatusEnum].self, forKey: .requiredListOfRequired) + listOfRequired = try values.decode([Bool].self, forKey: .listOfRequired) + requiredListOfRequiredDates = try values.decode([Temporal.Date].self, forKey: .requiredListOfRequiredDates) + listOfRequiredFloats = try values.decode([Double].self, forKey: .listOfRequiredFloats) + requiredListOfCustomTypes = try values.decode([CustomType].self, forKey: .requiredListOfCustomTypes) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -1217,14 +1217,14 @@ public struct Todo: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - title = try values.decode(String.self, .title) - requiredListOfTasks = try values.decode(List?.self, .requiredListOfTasks) - listOfRequiredTasks = try values.decode(List?.self, .listOfRequiredTasks) - listOfTasks = try values.decode(List?.self, .listOfTasks) - requiredListOfRequiredTasks = try values.decode(List?.self, .requiredListOfRequiredTasks) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + title = try values.decode(String.self, forKey: .title) + requiredListOfTasks = try values.decode(List?.self, forKey: .requiredListOfTasks) + listOfRequiredTasks = try values.decode(List?.self, forKey: .listOfRequiredTasks) + listOfTasks = try values.decode(List?.self, forKey: .listOfTasks) + requiredListOfRequiredTasks = try values.decode(List?.self, forKey: .requiredListOfRequiredTasks) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -1351,17 +1351,17 @@ public struct task: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - title = try values.decode(String.self, .title) - _todo = try values.decodeIfPresent(LazyReference).self, .todo) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + title = try values.decode(String.self, forKey: .title) + _todo = try values.decodeIfPresent(LazyReference.self, forKey: .todo) ?? LazyReference(identifiers: nil) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(id, forKey: .id) try container.encode(title, forKey: .title) - try container.encode(LazyReference(todo), forKey: .LazyReference(todo)) + try container.encode(_todo, forKey: .todo) try container.encode(createdAt, forKey: .createdAt) try container.encode(updatedAt, forKey: .updatedAt) } diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts index db3499425..15d96f86b 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts @@ -94,11 +94,11 @@ describe('AppSyncSwiftVisitor', () => { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - name = try values.decode(String?.self, .name) - bar = try values.decode(String?.self, .bar) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + name = try values.decode(String?.self, forKey: .name) + bar = try values.decode(String?.self, forKey: .bar) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -236,10 +236,10 @@ describe('AppSyncSwiftVisitor', () => { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - name = try values.decode(String?.self, .name) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + name = try values.decode(String?.self, forKey: .name) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -342,10 +342,10 @@ describe('AppSyncSwiftVisitor', () => { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - first_name = try values.decode(String?.self, .first_name) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + first_name = try values.decode(String?.self, forKey: .first_name) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -414,13 +414,13 @@ describe('AppSyncSwiftVisitor', () => { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - author_id = try values.decode(String.self, .author_id) - book_id = try values.decode(String.self, .book_id) - author = try values.decode(String?.self, .author) - book = try values.decode(String?.self, .book) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + author_id = try values.decode(String.self, forKey: .author_id) + book_id = try values.decode(String.self, forKey: .book_id) + author = try values.decode(String?.self, forKey: .author) + book = try values.decode(String?.self, forKey: .book) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -735,16 +735,16 @@ describe('AppSyncSwiftVisitor', () => { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - title = try values.decode(String.self, .title) - done = try values.decode(Bool.self, .done) - description = try values.decode(String?.self, .description) - due_date = try values.decode(String?.self, .due_date) - version = try values.decode(Int.self, .version) - value = try values.decode(Double?.self, .value) - tasks = try values.decode(List?.self, .tasks) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + title = try values.decode(String.self, forKey: .title) + done = try values.decode(Bool.self, forKey: .done) + description = try values.decode(String?.self, forKey: .description) + due_date = try values.decode(String?.self, forKey: .due_date) + version = try values.decode(Int.self, forKey: .version) + value = try values.decode(Double?.self, forKey: .value) + tasks = try values.decode(List?.self, forKey: .tasks) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -904,21 +904,21 @@ describe('AppSyncSwiftVisitor', () => { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - title = try values.decode(String.self, .title) - done = try values.decode(Bool.self, .done) - _todo = try values.decodeIfPresent(LazyReference).self, .todo) ?? LazyReference(identifiers: nil) - time = try values.decode(Temporal.Time?.self, .time) - createdOn = try values.decode(Temporal.Date?.self, .createdOn) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + title = try values.decode(String.self, forKey: .title) + done = try values.decode(Bool.self, forKey: .done) + _todo = try values.decodeIfPresent(LazyReference.self, forKey: .todo) ?? LazyReference(identifiers: nil) + time = try values.decode(Temporal.Time?.self, forKey: .time) + createdOn = try values.decode(Temporal.Date?.self, forKey: .createdOn) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(id, forKey: .id) try container.encode(title, forKey: .title) try container.encode(done, forKey: .done) - try container.encode(LazyReference(todo), forKey: .LazyReference(todo)) + try container.encode(_todo, forKey: .todo) try container.encode(time, forKey: .time) try container.encode(createdOn, forKey: .createdOn) try container.encode(createdAt, forKey: .createdAt) @@ -1095,11 +1095,11 @@ describe('AppSyncSwiftVisitor', () => { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - title = try values.decode(String.self, .title) - editors = try values.decode(List?.self, .editors) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + title = try values.decode(String.self, forKey: .title) + editors = try values.decode(List?.self, forKey: .editors) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -1202,11 +1202,11 @@ describe('AppSyncSwiftVisitor', () => { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - title = try values.decode(String.self, .title) - editors = try values.decode(List?.self, .editors) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + title = try values.decode(String.self, forKey: .title) + editors = try values.decode(List?.self, forKey: .editors) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -1349,15 +1349,15 @@ describe('AppSyncSwiftVisitor', () => { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - intArr = try values.decode([Int].self, .intArr) - strArr = try values.decode([String].self, .strArr) - floatArr = try values.decode([Double].self, .floatArr) - boolArr = try values.decode([Bool].self, .boolArr) - dateArr = try values.decode([Temporal.Date].self, .dateArr) - enumArr = try values.decode([EnumType].self, .enumArr) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + intArr = try values.decode([Int].self, forKey: .intArr) + strArr = try values.decode([String].self, forKey: .strArr) + floatArr = try values.decode([Double].self, forKey: .floatArr) + boolArr = try values.decode([Bool].self, forKey: .boolArr) + dateArr = try values.decode([Temporal.Date].self, forKey: .dateArr) + enumArr = try values.decode([EnumType].self, forKey: .enumArr) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -1525,15 +1525,15 @@ describe('AppSyncSwiftVisitor', () => { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - name = try values.decode(String.self, .name) - location = try values.decode(Location.self, .location) - nearByLocations = try values.decode([Location].self, .nearByLocations) - status = try values.decode(Status.self, .status) - statusHistory = try values.decode([Status].self, .statusHistory) - tags = try values.decode([String].self, .tags) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + name = try values.decode(String.self, forKey: .name) + location = try values.decode(Location.self, forKey: .location) + nearByLocations = try values.decode([Location].self, forKey: .nearByLocations) + status = try values.decode(Status.self, forKey: .status) + statusHistory = try values.decode([Status].self, forKey: .statusHistory) + tags = try values.decode([String].self, forKey: .tags) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -1809,13 +1809,13 @@ describe('AppSyncSwiftVisitor', () => { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - \`Class\` = try values.decode(Class?.self, .\`Class\`) - nonNullClass = try values.decode(Class.self, .nonNullClass) - classes = try values.decode([Class].self, .classes) - nonNullClasses = try values.decode([Class].self, .nonNullClasses) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + \`Class\` = try values.decode(Class?.self, forKey: .\`Class\`) + nonNullClass = try values.decode(Class.self, forKey: .nonNullClass) + classes = try values.decode([Class].self, forKey: .classes) + nonNullClasses = try values.decode([Class].self, forKey: .nonNullClasses) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -2922,11 +2922,11 @@ describe('AppSyncSwiftVisitor', () => { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - name = try values.decode(String?.self, .name) - bar = try values.decode(String?.self, .bar) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + name = try values.decode(String?.self, forKey: .name) + bar = try values.decode(String?.self, forKey: .bar) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -2968,9 +2968,9 @@ describe('AppSyncSwiftVisitor', () => { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - name = try values.decode(String?.self, .name) - bar = try values.decode(String?.self, .bar) + id = try values.decode(String.self, forKey: .id) + name = try values.decode(String?.self, forKey: .name) + bar = try values.decode(String?.self, forKey: .bar) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -3010,9 +3010,9 @@ describe('AppSyncSwiftVisitor', () => { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap index ca53ac9a3..73036f966 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -34,11 +34,11 @@ public struct Post3: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - title = try values.decode(String.self, .title) - comments = try values.decode(List?.self, .comments) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + title = try values.decode(String.self, forKey: .title) + comments = try values.decode(List?.self, forKey: .comments) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -147,17 +147,17 @@ public struct Comment3: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - content = try values.decode(String.self, .content) - _post = try values.decodeIfPresent(LazyReference).self, .post) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + content = try values.decode(String.self, forKey: .content) + _post = try values.decodeIfPresent(LazyReference.self, forKey: .post) ?? LazyReference(identifiers: nil) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(id, forKey: .id) try container.encode(content, forKey: .content) - try container.encode(LazyReference(post), forKey: .LazyReference(post)) + try container.encode(_post, forKey: .post) try container.encode(createdAt, forKey: .createdAt) try container.encode(updatedAt, forKey: .updatedAt) } @@ -256,11 +256,11 @@ public struct Post: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - title = try values.decode(String.self, .title) - comments = try values.decode(List?.self, .comments) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + title = try values.decode(String.self, forKey: .title) + comments = try values.decode(List?.self, forKey: .comments) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -369,17 +369,17 @@ public struct Comment: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - content = try values.decode(String.self, .content) - _post = try values.decodeIfPresent(LazyReference).self, .post) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + content = try values.decode(String.self, forKey: .content) + _post = try values.decodeIfPresent(LazyReference.self, forKey: .post) ?? LazyReference(identifiers: nil) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(id, forKey: .id) try container.encode(content, forKey: .content) - try container.encode(LazyReference(post), forKey: .LazyReference(post)) + try container.encode(_post, forKey: .post) try container.encode(createdAt, forKey: .createdAt) try container.encode(updatedAt, forKey: .updatedAt) } @@ -491,18 +491,18 @@ public struct Project2: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - name = try values.decode(String?.self, .name) - _team = try values.decodeIfPresent(LazyReference).self, .team) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) - project2TeamId = try values.decode(String?.self, .project2TeamId) + id = try values.decode(String.self, forKey: .id) + name = try values.decode(String?.self, forKey: .name) + _team = try values.decodeIfPresent(LazyReference.self, forKey: .team) ?? LazyReference(identifiers: nil) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + project2TeamId = try values.decode(String?.self, forKey: .project2TeamId) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(id, forKey: .id) try container.encode(name, forKey: .name) - try container.encode(LazyReference(team), forKey: .LazyReference(team)) + try container.encode(_team, forKey: .team) try container.encode(createdAt, forKey: .createdAt) try container.encode(updatedAt, forKey: .updatedAt) try container.encode(project2TeamId, forKey: .project2TeamId) @@ -611,17 +611,17 @@ public struct Team2: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - name = try values.decode(String.self, .name) - _project = try values.decodeIfPresent(LazyReference).self, .project) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + name = try values.decode(String.self, forKey: .name) + _project = try values.decodeIfPresent(LazyReference.self, forKey: .project) ?? LazyReference(identifiers: nil) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(id, forKey: .id) try container.encode(name, forKey: .name) - try container.encode(LazyReference(project), forKey: .LazyReference(project)) + try container.encode(_project, forKey: .project) try container.encode(createdAt, forKey: .createdAt) try container.encode(updatedAt, forKey: .updatedAt) } @@ -716,11 +716,11 @@ public struct Blog7V2: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - name = try values.decode(String.self, .name) - posts = try values.decode(List?.self, .posts) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + name = try values.decode(String.self, forKey: .name) + posts = try values.decode(List?.self, forKey: .posts) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -834,18 +834,18 @@ public struct Post7V2: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - title = try values.decode(String.self, .title) - _blog = try values.decodeIfPresent(LazyReference).self, .blog) ?? LazyReference(identifiers: nil) - comments = try values.decode(List?.self, .comments) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + title = try values.decode(String.self, forKey: .title) + _blog = try values.decodeIfPresent(LazyReference.self, forKey: .blog) ?? LazyReference(identifiers: nil) + comments = try values.decode(List?.self, forKey: .comments) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(id, forKey: .id) try container.encode(title, forKey: .title) - try container.encode(LazyReference(blog), forKey: .LazyReference(blog)) + try container.encode(_blog, forKey: .blog) try container.encode(comments, forKey: .comments) try container.encode(createdAt, forKey: .createdAt) try container.encode(updatedAt, forKey: .updatedAt) @@ -954,17 +954,17 @@ public struct Comment7V2: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - content = try values.decode(String?.self, .content) - _post = try values.decodeIfPresent(LazyReference).self, .post) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + content = try values.decode(String?.self, forKey: .content) + _post = try values.decodeIfPresent(LazyReference.self, forKey: .post) ?? LazyReference(identifiers: nil) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(id, forKey: .id) try container.encode(content, forKey: .content) - try container.encode(LazyReference(post), forKey: .LazyReference(post)) + try container.encode(_post, forKey: .post) try container.encode(createdAt, forKey: .createdAt) try container.encode(updatedAt, forKey: .updatedAt) } @@ -1059,11 +1059,11 @@ public struct Blog8V2: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - name = try values.decode(String.self, .name) - posts = try values.decode(List?.self, .posts) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + name = try values.decode(String.self, forKey: .name) + posts = try values.decode(List?.self, forKey: .posts) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -1177,18 +1177,18 @@ public struct Post8V2: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - title = try values.decode(String.self, .title) - _blog = try values.decodeIfPresent(LazyReference).self, .blog) ?? LazyReference(identifiers: nil) - comments = try values.decode(List?.self, .comments) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + title = try values.decode(String.self, forKey: .title) + _blog = try values.decodeIfPresent(LazyReference.self, forKey: .blog) ?? LazyReference(identifiers: nil) + comments = try values.decode(List?.self, forKey: .comments) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(id, forKey: .id) try container.encode(title, forKey: .title) - try container.encode(LazyReference(blog), forKey: .LazyReference(blog)) + try container.encode(_blog, forKey: .blog) try container.encode(comments, forKey: .comments) try container.encode(createdAt, forKey: .createdAt) try container.encode(updatedAt, forKey: .updatedAt) @@ -1297,17 +1297,17 @@ public struct Comment8V2: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - content = try values.decode(String?.self, .content) - _post = try values.decodeIfPresent(LazyReference).self, .post) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + content = try values.decode(String?.self, forKey: .content) + _post = try values.decodeIfPresent(LazyReference.self, forKey: .post) ?? LazyReference(identifiers: nil) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(id, forKey: .id) try container.encode(content, forKey: .content) - try container.encode(LazyReference(post), forKey: .LazyReference(post)) + try container.encode(_post, forKey: .post) try container.encode(createdAt, forKey: .createdAt) try container.encode(updatedAt, forKey: .updatedAt) } @@ -1415,18 +1415,18 @@ public struct Project: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - name = try values.decode(String?.self, .name) - _team = try values.decodeIfPresent(LazyReference).self, .team) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) - projectTeamId = try values.decode(String?.self, .projectTeamId) + id = try values.decode(String.self, forKey: .id) + name = try values.decode(String?.self, forKey: .name) + _team = try values.decodeIfPresent(LazyReference.self, forKey: .team) ?? LazyReference(identifiers: nil) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + projectTeamId = try values.decode(String?.self, forKey: .projectTeamId) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(id, forKey: .id) try container.encode(name, forKey: .name) - try container.encode(LazyReference(team), forKey: .LazyReference(team)) + try container.encode(_team, forKey: .team) try container.encode(createdAt, forKey: .createdAt) try container.encode(updatedAt, forKey: .updatedAt) try container.encode(projectTeamId, forKey: .projectTeamId) @@ -1535,17 +1535,17 @@ public struct Team: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - name = try values.decode(String.self, .name) - _project = try values.decodeIfPresent(LazyReference).self, .project) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + name = try values.decode(String.self, forKey: .name) + _project = try values.decodeIfPresent(LazyReference.self, forKey: .project) ?? LazyReference(identifiers: nil) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(id, forKey: .id) try container.encode(name, forKey: .name) - try container.encode(LazyReference(project), forKey: .LazyReference(project)) + try container.encode(_project, forKey: .project) try container.encode(createdAt, forKey: .createdAt) try container.encode(updatedAt, forKey: .updatedAt) } @@ -1645,12 +1645,12 @@ public struct Post: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - title = try values.decode(String.self, .title) - content = try values.decode(String?.self, .content) - tags = try values.decode(List?.self, .tags) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + title = try values.decode(String.self, forKey: .title) + content = try values.decode(String?.self, forKey: .content) + tags = try values.decode(List?.self, forKey: .tags) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -1757,11 +1757,11 @@ public struct Tag: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - label = try values.decode(String.self, .label) - posts = try values.decode(List?.self, .posts) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + label = try values.decode(String.self, forKey: .label) + posts = try values.decode(List?.self, forKey: .posts) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -1857,10 +1857,10 @@ public struct Todo: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - content = try values.decode(String?.self, .content) - createdOn = try values.decode(Temporal.DateTime?.self, .createdOn) - updatedOn = try values.decode(Temporal.DateTime?.self, .updatedOn) + id = try values.decode(String.self, forKey: .id) + content = try values.decode(String?.self, forKey: .content) + createdOn = try values.decode(Temporal.DateTime?.self, forKey: .createdOn) + updatedOn = try values.decode(Temporal.DateTime?.self, forKey: .updatedOn) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -1955,11 +1955,11 @@ public struct Post2: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - title = try values.decode(String.self, .title) - comments = try values.decode(List?.self, .comments) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + title = try values.decode(String.self, forKey: .title) + comments = try values.decode(List?.self, forKey: .comments) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -2060,11 +2060,11 @@ public struct Comment2: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - postID = try values.decode(String.self, .postID) - content = try values.decode(String.self, .content) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + postID = try values.decode(String.self, forKey: .postID) + content = try values.decode(String.self, forKey: .content) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -2182,19 +2182,19 @@ public struct Project2: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - name = try values.decode(String?.self, .name) - teamID = try values.decode(String?.self, .teamID) - _team = try values.decodeIfPresent(LazyReference).self, .team) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + name = try values.decode(String?.self, forKey: .name) + teamID = try values.decode(String?.self, forKey: .teamID) + _team = try values.decodeIfPresent(LazyReference.self, forKey: .team) ?? LazyReference(identifiers: nil) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(id, forKey: .id) try container.encode(name, forKey: .name) try container.encode(teamID, forKey: .teamID) - try container.encode(LazyReference(team), forKey: .LazyReference(team)) + try container.encode(_team, forKey: .team) try container.encode(createdAt, forKey: .createdAt) try container.encode(updatedAt, forKey: .updatedAt) } @@ -2289,10 +2289,10 @@ public struct Team2: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - name = try values.decode(String.self, .name) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + name = try values.decode(String.self, forKey: .name) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -2387,11 +2387,11 @@ public struct Post: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - title = try values.decode(String.self, .title) - comments = try values.decode(List?.self, .comments) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + title = try values.decode(String.self, forKey: .title) + comments = try values.decode(List?.self, forKey: .comments) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -2492,11 +2492,11 @@ public struct Comment: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - content = try values.decode(String.self, .content) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) - postCommentsId = try values.decode(String?.self, .postCommentsId) + id = try values.decode(String.self, forKey: .id) + content = try values.decode(String.self, forKey: .content) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + postCommentsId = try values.decode(String?.self, forKey: .postCommentsId) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -2610,18 +2610,18 @@ public struct Project: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - name = try values.decode(String?.self, .name) - _team = try values.decodeIfPresent(LazyReference).self, .team) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) - projectTeamId = try values.decode(String?.self, .projectTeamId) + id = try values.decode(String.self, forKey: .id) + name = try values.decode(String?.self, forKey: .name) + _team = try values.decodeIfPresent(LazyReference.self, forKey: .team) ?? LazyReference(identifiers: nil) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + projectTeamId = try values.decode(String?.self, forKey: .projectTeamId) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(id, forKey: .id) try container.encode(name, forKey: .name) - try container.encode(LazyReference(team), forKey: .LazyReference(team)) + try container.encode(_team, forKey: .team) try container.encode(createdAt, forKey: .createdAt) try container.encode(updatedAt, forKey: .updatedAt) try container.encode(projectTeamId, forKey: .projectTeamId) @@ -2717,10 +2717,10 @@ public struct Team: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - name = try values.decode(String.self, .name) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + name = try values.decode(String.self, forKey: .name) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -2820,12 +2820,12 @@ public struct Customer: Model { } public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, .id) - name = try values.decode(String.self, .name) - phoneNumber = try values.decode(String?.self, .phoneNumber) - accountRepresentativeID = try values.decode(String.self, .accountRepresentativeID) - createdAt = try values.decode(Temporal.DateTime?.self, .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, .updatedAt) + id = try values.decode(String.self, forKey: .id) + name = try values.decode(String.self, forKey: .name) + phoneNumber = try values.decode(String?.self, forKey: .phoneNumber) + accountRepresentativeID = try values.decode(String.self, forKey: .accountRepresentativeID) + createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts index 563ca559e..8d326b084 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts @@ -552,7 +552,7 @@ export class AppSyncSwiftVisitor< const nativeType = this.getNativeType(field); const optionality = !this.isFieldRequired(field) ? '?' : ''; const fieldType = connectionHasOneOrBelongsTo - ? `LazyReference<${nativeType}>)` + ? `LazyReference<${nativeType}>` : field.isList ? field.connectionInfo ? `List<${nativeType}>${optionality}` @@ -561,7 +561,7 @@ export class AppSyncSwiftVisitor< const decodeMethod = connectionHasOneOrBelongsTo ? 'decodeIfPresent' : 'decode'; const defaultLazyReference = connectionHasOneOrBelongsTo ? ' ?? LazyReference(identifiers: nil)' : ''; result.push( - indent(`${assignedFieldName} = try values.${decodeMethod}(${fieldType}.self, .${escapedFieldName})${defaultLazyReference}`), + indent(`${assignedFieldName} = try values.${decodeMethod}(${fieldType}.self, forKey: .${escapedFieldName})${defaultLazyReference}`), ); }); @@ -577,8 +577,8 @@ export class AppSyncSwiftVisitor< : false; const escapedFieldName = escapeKeywords(this.getFieldName(field)); - const fieldValue = connectionHasOneOrBelongsTo ? `LazyReference(${escapedFieldName})` : escapedFieldName; - result.push(indent(`try container.encode(${fieldValue}, forKey: .${fieldValue})`)); + const fieldValue = connectionHasOneOrBelongsTo ? `_${this.getFieldName(field)}` : escapeKeywords(this.getFieldName(field)); + result.push(indent(`try container.encode(${fieldValue}, forKey: .${escapedFieldName})`)); }); return result.join('\n'); From c1e088a2313d727f0393040b272780ab3497f8f1 Mon Sep 17 00:00:00 2001 From: Michael Law <1365977+lawmicha@users.noreply.github.com> Date: Tue, 6 Dec 2022 13:13:01 -0500 Subject: [PATCH 09/17] feat(amplify-codegen): custom decoder encoder for LazyReference 5 --- .../appsync-swift-visitor.test.ts.snap | 6 ++--- .../visitors/appsync-swift-visitor.test.ts | 2 +- .../appsync-swift-visitor.test.ts.snap | 24 +++++++++---------- .../src/visitors/appsync-swift-visitor.ts | 2 +- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap index f26962b60..7cfe85534 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -746,7 +746,7 @@ public struct Project: Model { self.projectTeamTeamId = projectTeamTeamId self.projectTeamName = projectTeamName } - public mutating func setTeam(team: Team? = nil) { + public mutating func setTeam(_ team: Team? = nil) { self._team = LazyReference(team) } public init(from decoder: Decoder) throws { @@ -890,7 +890,7 @@ public struct Team: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setProject(project: Project? = nil) { + public mutating func setProject(_ project: Project? = nil) { self._project = LazyReference(project) } public init(from decoder: Decoder) throws { @@ -1346,7 +1346,7 @@ public struct task: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setTodo(todo: Todo? = nil) { + public mutating func setTodo(_ todo: Todo? = nil) { self._todo = LazyReference(todo) } public init(from decoder: Decoder) throws { diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts index 15d96f86b..ec0c5857e 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts @@ -899,7 +899,7 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setTodo(todo: Todo? = nil) { + public mutating func setTodo(_ todo: Todo? = nil) { self._todo = LazyReference(todo) } public init(from decoder: Decoder) throws { diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap index 73036f966..e2c250d59 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -142,7 +142,7 @@ public struct Comment3: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setPost(post: Post3) { + public mutating func setPost(_ post: Post3) { self._post = LazyReference(post) } public init(from decoder: Decoder) throws { @@ -364,7 +364,7 @@ public struct Comment: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setPost(post: Post? = nil) { + public mutating func setPost(_ post: Post? = nil) { self._post = LazyReference(post) } public init(from decoder: Decoder) throws { @@ -486,7 +486,7 @@ public struct Project2: Model { self.updatedAt = updatedAt self.project2TeamId = project2TeamId } - public mutating func setTeam(team: Team2? = nil) { + public mutating func setTeam(_ team: Team2? = nil) { self._team = LazyReference(team) } public init(from decoder: Decoder) throws { @@ -606,7 +606,7 @@ public struct Team2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setProject(project: Project2? = nil) { + public mutating func setProject(_ project: Project2? = nil) { self._project = LazyReference(project) } public init(from decoder: Decoder) throws { @@ -829,7 +829,7 @@ public struct Post7V2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setBlog(blog: Blog7V2? = nil) { + public mutating func setBlog(_ blog: Blog7V2? = nil) { self._blog = LazyReference(blog) } public init(from decoder: Decoder) throws { @@ -949,7 +949,7 @@ public struct Comment7V2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setPost(post: Post7V2? = nil) { + public mutating func setPost(_ post: Post7V2? = nil) { self._post = LazyReference(post) } public init(from decoder: Decoder) throws { @@ -1172,7 +1172,7 @@ public struct Post8V2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setBlog(blog: Blog8V2) { + public mutating func setBlog(_ blog: Blog8V2) { self._blog = LazyReference(blog) } public init(from decoder: Decoder) throws { @@ -1292,7 +1292,7 @@ public struct Comment8V2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setPost(post: Post8V2) { + public mutating func setPost(_ post: Post8V2) { self._post = LazyReference(post) } public init(from decoder: Decoder) throws { @@ -1410,7 +1410,7 @@ public struct Project: Model { self.updatedAt = updatedAt self.projectTeamId = projectTeamId } - public mutating func setTeam(team: Team? = nil) { + public mutating func setTeam(_ team: Team? = nil) { self._team = LazyReference(team) } public init(from decoder: Decoder) throws { @@ -1530,7 +1530,7 @@ public struct Team: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setProject(project: Project? = nil) { + public mutating func setProject(_ project: Project? = nil) { self._project = LazyReference(project) } public init(from decoder: Decoder) throws { @@ -2177,7 +2177,7 @@ public struct Project2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setTeam(team: Team2? = nil) { + public mutating func setTeam(_ team: Team2? = nil) { self._team = LazyReference(team) } public init(from decoder: Decoder) throws { @@ -2605,7 +2605,7 @@ public struct Project: Model { self.updatedAt = updatedAt self.projectTeamId = projectTeamId } - public mutating func setTeam(team: Team? = nil) { + public mutating func setTeam(_ team: Team? = nil) { self._team = LazyReference(team) } public init(from decoder: Decoder) throws { diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts index 8d326b084..625995a03 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts @@ -251,7 +251,7 @@ export class AppSyncSwiftVisitor< `self._${fieldName} = LazyReference(${fieldName})`, [ { - name: this.getFieldName(field), + name: `_ ${this.getFieldName(field)}`, type: this.getNativeType(field), value: undefined, flags: { optional: !this.isFieldRequired(field) }, From 395b1a789bac033708e11fa1ad26c067e4598c80 Mon Sep 17 00:00:00 2001 From: Michael Law <1365977+lawmicha@users.noreply.github.com> Date: Fri, 9 Dec 2022 09:17:32 -0500 Subject: [PATCH 10/17] feat(amplify-codegen): address PR feedback --- .../src/languages/swift-declaration-block.ts | 10 ++- .../src/visitors/appsync-swift-visitor.ts | 82 +++++++++---------- 2 files changed, 50 insertions(+), 42 deletions(-) diff --git a/packages/appsync-modelgen-plugin/src/languages/swift-declaration-block.ts b/packages/appsync-modelgen-plugin/src/languages/swift-declaration-block.ts index 032fe3194..777bacd36 100644 --- a/packages/appsync-modelgen-plugin/src/languages/swift-declaration-block.ts +++ b/packages/appsync-modelgen-plugin/src/languages/swift-declaration-block.ts @@ -160,6 +160,7 @@ export type StructMethod = { }; export class SwiftDeclarationBlock { _name: string = ''; + _condition: string = ''; _kind: Kind = 'struct'; _protocols: string[] = []; _access: Access = 'DEFAULT'; @@ -194,6 +195,11 @@ export class SwiftDeclarationBlock { return this; } + withCondition(condition: string): SwiftDeclarationBlock { + this._condition = condition; + return this; + } + withBlock(block: string): SwiftDeclarationBlock { this._block = [block]; @@ -332,7 +338,9 @@ export class SwiftDeclarationBlock { this._flags.final ? 'final' : '', this.getAccessStr(), this._kind, - `${escapeKeywords(this._name)}${this._protocols.length ? `: ${this._protocols.join(', ')}` : ''}`, + `${escapeKeywords(this._name)}${this._protocols.length ? `: ${this._protocols.join(', ')}` : ''}${ + this._condition ? ` where ${this._condition}` : '' + }`, '{', ], false, diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts index 625995a03..8a88018f7 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts @@ -3,16 +3,16 @@ import { camelCase } from 'change-case'; import { GraphQLSchema } from 'graphql'; import { lowerCaseFirst } from 'lower-case-first'; import { schemaTypeMap } from '../configs/swift-config'; -import { SwiftDeclarationBlock, escapeKeywords, ListType } from '../languages/swift-declaration-block'; +import { escapeKeywords, ListType, SwiftDeclarationBlock } from '../languages/swift-declaration-block'; import { CodeGenConnectionType } from '../utils/process-connections'; import { AppSyncModelVisitor, CodeGenField, CodeGenGenerateEnum, CodeGenModel, - RawAppSyncModelConfig, - ParsedAppSyncModelConfig, CodeGenPrimaryKeyType, + ParsedAppSyncModelConfig, + RawAppSyncModelConfig, } from './appsync-visitor'; import { AuthDirective, AuthStrategy } from '../utils/process-auth'; import { printWarning } from '../utils/warn'; @@ -95,10 +95,7 @@ export class AppSyncSwiftVisitor< const fieldType = this.getNativeType(field); const isVariable = !primaryKeyComponentFieldsName.includes(field.name); const listType: ListType = field.connectionInfo ? ListType.LIST : ListType.ARRAY; - const connectionHasOneOrBelongsTo: boolean = field.connectionInfo - ? field.connectionInfo.kind === CodeGenConnectionType.HAS_ONE || field.connectionInfo.kind === CodeGenConnectionType.BELONGS_TO - : false; - if (connectionHasOneOrBelongsTo) { + if (this.isHasOneOrBelongsToConnectionField(field)) { // lazy loading - create computed property of LazyReference structBlock.addProperty(`_${this.getFieldName(field)}`, `LazyReference<${fieldType}>`, undefined, `internal`, { optional: false, @@ -107,9 +104,7 @@ export class AppSyncSwiftVisitor< isEnum: this.isEnumType(field), listType: field.isList ? listType : undefined, isListNullable: field.isListNullable, - handleListNullabilityTransparently: this.isHasManyConnectionField(field) - ? false - : this.config.handleListNullabilityTransparently, + handleListNullabilityTransparently: this.config.handleListNullabilityTransparently, }); const lazyLoadGetOrRequired = !this.isFieldRequired(field) ? 'get()' : 'require()'; structBlock.addProperty( @@ -124,9 +119,7 @@ export class AppSyncSwiftVisitor< isEnum: this.isEnumType(field), listType: field.isList ? listType : undefined, isListNullable: field.isListNullable, - handleListNullabilityTransparently: this.isHasManyConnectionField(field) - ? false - : this.config.handleListNullabilityTransparently, + handleListNullabilityTransparently: this.config.handleListNullabilityTransparently, }, undefined, `get async throws { \n try await _${this.getFieldName(field)}.${lazyLoadGetOrRequired}\n}`, @@ -238,10 +231,7 @@ export class AppSyncSwiftVisitor< // mutating functions for updating/deleting Object.entries(obj.fields).forEach(([fieldName, field]) => { - const connectionHasOneOrBelongsTo: boolean = field.connectionInfo - ? field.connectionInfo.kind === CodeGenConnectionType.HAS_ONE || field.connectionInfo.kind === CodeGenConnectionType.BELONGS_TO - : false; - if (connectionHasOneOrBelongsTo) { + if (this.isHasOneOrBelongsToConnectionField(field)) { // lazy loading - create setter functions for LazyReference let fieldName = this.getFieldName(field); let capitalizedFieldName = fieldName.charAt(0).toUpperCase() + fieldName.slice(1); @@ -466,7 +456,8 @@ export class AppSyncSwiftVisitor< protected generateModelPathExtensions(model: CodeGenModel): string { const modelPathExtension = new SwiftDeclarationBlock() .asKind('extension') - .withName(`ModelPath where ModelType == ${this.getModelName(model)}`); + .withName(`ModelPath`) + .withCondition(`ModelType == ${this.getModelName(model)}`); Object.values(model.fields).forEach(field => { if (this.isEnumType(field) || this.isNonModelType(field)) { @@ -482,7 +473,7 @@ export class AppSyncSwiftVisitor< : `${this.getFieldTypePathValue(fieldType)}(\"${fieldName}\")`; modelPathExtension.addProperty( - `${fieldName}`, + fieldName, `${pathType}<${fieldType}>`, '', undefined, @@ -527,11 +518,9 @@ export class AppSyncSwiftVisitor< private getInitBody(fields: CodeGenField[]): string { let result = fields.map(field => { - const connectionHasOneOrBelongsTo: boolean = field.connectionInfo - ? field.connectionInfo.kind === CodeGenConnectionType.HAS_ONE || field.connectionInfo.kind === CodeGenConnectionType.BELONGS_TO - : false; - const propertyName = connectionHasOneOrBelongsTo ? `_${this.getFieldName(field)}` : escapeKeywords(this.getFieldName(field)); + const connectionHasOneOrBelongsTo = this.isHasOneOrBelongsToConnectionField(field); const escapedFieldName = escapeKeywords(this.getFieldName(field)); + const propertyName = connectionHasOneOrBelongsTo ? `_${this.getFieldName(field)}` : escapedFieldName; const fieldValue = connectionHasOneOrBelongsTo ? `LazyReference(${escapedFieldName})` : escapedFieldName; return indent(`self.${propertyName} = ${fieldValue}`); }); @@ -543,21 +532,10 @@ export class AppSyncSwiftVisitor< let result: string[] = []; result.push(indent('let values = try decoder.container(keyedBy: CodingKeys.self)')); fields.forEach(field => { - const connectionHasOneOrBelongsTo: boolean = field.connectionInfo - ? field.connectionInfo.kind === CodeGenConnectionType.HAS_ONE || field.connectionInfo.kind === CodeGenConnectionType.BELONGS_TO - : false; + const connectionHasOneOrBelongsTo = this.isHasOneOrBelongsToConnectionField(field); const escapedFieldName = escapeKeywords(this.getFieldName(field)); const assignedFieldName = connectionHasOneOrBelongsTo ? `_${this.getFieldName(field)}` : escapedFieldName; - - const nativeType = this.getNativeType(field); - const optionality = !this.isFieldRequired(field) ? '?' : ''; - const fieldType = connectionHasOneOrBelongsTo - ? `LazyReference<${nativeType}>` - : field.isList - ? field.connectionInfo - ? `List<${nativeType}>${optionality}` - : `[${nativeType}]` - : `${nativeType}${optionality}`; + const fieldType = this.getDecoderBodyFieldType(field); const decodeMethod = connectionHasOneOrBelongsTo ? 'decodeIfPresent' : 'decode'; const defaultLazyReference = connectionHasOneOrBelongsTo ? ' ?? LazyReference(identifiers: nil)' : ''; result.push( @@ -568,16 +546,28 @@ export class AppSyncSwiftVisitor< return result.join('\n'); } + private getDecoderBodyFieldType(field: CodeGenField): string { + const nativeType = this.getNativeType(field); + const optionality = !this.isFieldRequired(field) ? '?' : ''; + + if (this.isHasOneOrBelongsToConnectionField(field)) { + return `LazyReference<${nativeType}>`; + } + if (field.isList) { + if (field.connectionInfo) { + return `List<${nativeType}>${optionality}`; + } + return `[${nativeType}]`; + } + return `${nativeType}${optionality}`; + } + private getEncoderBody(fields: CodeGenField[]): string { let result: string[] = []; result.push(indent('var container = encoder.container(keyedBy: CodingKeys.self)')); fields.forEach(field => { - const connectionHasOneOrBelongsTo: boolean = field.connectionInfo - ? field.connectionInfo.kind === CodeGenConnectionType.HAS_ONE || field.connectionInfo.kind === CodeGenConnectionType.BELONGS_TO - : false; - const escapedFieldName = escapeKeywords(this.getFieldName(field)); - const fieldValue = connectionHasOneOrBelongsTo ? `_${this.getFieldName(field)}` : escapeKeywords(this.getFieldName(field)); + const fieldValue = this.isHasOneOrBelongsToConnectionField(field) ? `_${this.getFieldName(field)}` : escapedFieldName; result.push(indent(`try container.encode(${fieldValue}, forKey: .${escapedFieldName})`)); }); @@ -742,6 +732,16 @@ export class AppSyncSwiftVisitor< return false; } + protected isHasOneOrBelongsToConnectionField(field: CodeGenField): boolean { + if ( + field.connectionInfo && + (field.connectionInfo.kind === CodeGenConnectionType.HAS_ONE || field.connectionInfo.kind === CodeGenConnectionType.BELONGS_TO) + ) { + return true; + } + return false; + } + protected generateAuthRules(model: CodeGenModel): string { const authDirectives: AuthDirective[] = (model.directives.filter(d => d.name === 'auth') as any) as AuthDirective[]; const rules: string[] = []; From d760bca3aa44cb99ca9c892988a3bfd9de69375f Mon Sep 17 00:00:00 2001 From: Michael Law <1365977+lawmicha@users.noreply.github.com> Date: Fri, 9 Dec 2022 09:49:45 -0500 Subject: [PATCH 11/17] feat(amplify-codegen): add feature flag generateLazyReferenceAndModelPath check --- .../amplify-codegen/src/commands/models.js | 2 + .../visitors/appsync-swift-visitor.test.ts | 1 + .../appsync-swift-visitor.test.ts | 2 + .../src/visitors/appsync-swift-visitor.ts | 135 ++++++++++-------- .../src/visitors/appsync-visitor.ts | 12 ++ 5 files changed, 89 insertions(+), 63 deletions(-) diff --git a/packages/amplify-codegen/src/commands/models.js b/packages/amplify-codegen/src/commands/models.js index 25d5e7a47..66c581884 100644 --- a/packages/amplify-codegen/src/commands/models.js +++ b/packages/amplify-codegen/src/commands/models.js @@ -103,6 +103,7 @@ async function generateModels(context, generateOptions = null) { const usePipelinedTransformer = readFeatureFlag('graphQLTransformer.useExperimentalPipelinedTransformer'); const transformerVersion = readNumericFeatureFlag('graphQLTransformer.transformerVersion'); const respectPrimaryKeyAttributesOnConnectionField = readFeatureFlag('graphQLTransformer.respectPrimaryKeyAttributesOnConnectionField'); + const generateLazyReferenceAndModelPath = readFeatureFlag('codegen.generateLazyReferenceAndModelPath'); let isTimestampFieldsAdded = readFeatureFlag('codegen.addTimestampFields'); let enableDartNullSafety = readFeatureFlag('codegen.enableDartNullSafety'); @@ -146,6 +147,7 @@ async function generateModels(context, generateOptions = null) { transformerVersion, dartUpdateAmplifyCoreDependency, respectPrimaryKeyAttributesOnConnectionField, + generateLazyReferenceAndModelPath, codegenVersion: packageVersion, overrideOutputDir, // This needs to live under `config` in order for the GraphQL types to work out. }, diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts index ec0c5857e..dd085f51f 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts @@ -11,6 +11,7 @@ const defaultIosVisitorSetings = { handleListNullabilityTransparently: true, transformerVersion: 1, respectPrimaryKeyAttributesOnConnectionField: false, + generateLazyReferenceAndModelPath: true, }; const buildSchemaWithDirectives = (schema: String): GraphQLSchema => { return buildSchema([schema, directives, scalars].join('\n')); diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/appsync-swift-visitor.test.ts index b20db95c4..c690f04c2 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/appsync-swift-visitor.test.ts @@ -17,6 +17,7 @@ const getGQLv2Visitor = ( generateIndexRules: boolean = true, handleListNullabilityTransparently: boolean = true, transformerVersion: number = 2, + generateLazyReferenceAndModelPath: boolean = true, ) => { const ast = parse(schema); const builtSchema = buildSchemaWithDirectives(schema); @@ -31,6 +32,7 @@ const getGQLv2Visitor = ( generateIndexRules, handleListNullabilityTransparently, transformerVersion: transformerVersion, + generateLazyReferenceAndModelPath: generateLazyReferenceAndModelPath, }, { selectedType, generate }, ); diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts index 8a88018f7..8af157dc6 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts @@ -95,7 +95,7 @@ export class AppSyncSwiftVisitor< const fieldType = this.getNativeType(field); const isVariable = !primaryKeyComponentFieldsName.includes(field.name); const listType: ListType = field.connectionInfo ? ListType.LIST : ListType.ARRAY; - if (this.isHasOneOrBelongsToConnectionField(field)) { + if (this.isGenerateLazyReferenceModelPathEnabled() && this.isHasOneOrBelongsToConnectionField(field)) { // lazy loading - create computed property of LazyReference structBlock.addProperty(`_${this.getFieldName(field)}`, `LazyReference<${fieldType}>`, undefined, `internal`, { optional: false, @@ -229,63 +229,65 @@ export class AppSyncSwiftVisitor< ); } - // mutating functions for updating/deleting - Object.entries(obj.fields).forEach(([fieldName, field]) => { - if (this.isHasOneOrBelongsToConnectionField(field)) { - // lazy loading - create setter functions for LazyReference - let fieldName = this.getFieldName(field); - let capitalizedFieldName = fieldName.charAt(0).toUpperCase() + fieldName.slice(1); - structBlock.addClassMethod( - `set${capitalizedFieldName}`, - null, - `self._${fieldName} = LazyReference(${fieldName})`, - [ + if (this.isGenerateLazyReferenceModelPathEnabled()) { + // mutating functions for updating/deleting + Object.entries(obj.fields).forEach(([fieldName, field]) => { + if (this.isHasOneOrBelongsToConnectionField(field)) { + // lazy loading - create setter functions for LazyReference + let fieldName = this.getFieldName(field); + let capitalizedFieldName = fieldName.charAt(0).toUpperCase() + fieldName.slice(1); + structBlock.addClassMethod( + `set${capitalizedFieldName}`, + null, + `self._${fieldName} = LazyReference(${fieldName})`, + [ + { + name: `_ ${this.getFieldName(field)}`, + type: this.getNativeType(field), + value: undefined, + flags: { optional: !this.isFieldRequired(field) }, + }, + ], + 'public', { - name: `_ ${this.getFieldName(field)}`, - type: this.getNativeType(field), - value: undefined, - flags: { optional: !this.isFieldRequired(field) }, + mutating: true, }, - ], - 'public', + ); + } + }); + + // custom decoder/encoder + structBlock.addClassMethod( + 'init', + null, + this.getDecoderBody(obj.fields), + [ { - mutating: true, + value: undefined, + name: 'from decoder', + type: 'Decoder', + flags: {}, }, - ); - } - }); - - // custom decoder/encoder - structBlock.addClassMethod( - 'init', - null, - this.getDecoderBody(obj.fields), - [ - { - value: undefined, - name: 'from decoder', - type: 'Decoder', - flags: {}, - }, - ], - 'public', - { throws: true }, - ); - structBlock.addClassMethod( - 'encode', - null, - this.getEncoderBody(obj.fields), - [ - { - value: undefined, - name: 'to encoder', - type: 'Encoder', - flags: {}, - }, - ], - 'public', - { throws: true }, - ); + ], + 'public', + { throws: true }, + ); + structBlock.addClassMethod( + 'encode', + null, + this.getEncoderBody(obj.fields), + [ + { + value: undefined, + name: 'to encoder', + type: 'Encoder', + flags: {}, + }, + ], + 'public', + { throws: true }, + ); + } result.push(structBlock.string); }); @@ -348,8 +350,9 @@ export class AppSyncSwiftVisitor< result.push(''); result.push(this.generatePrimaryKeyExtensions(model)); } - // TODO: Most likely we will have a flag here as well, `if (this.isLazyLoadingEnabled())` - result.push(this.generateModelPathExtensions(model)); + if (this.isGenerateLazyReferenceModelPathEnabled()) { + result.push(this.generateModelPathExtensions(model)); + } }); Object.values(this.getSelectedNonModels()).forEach(model => { @@ -389,6 +392,7 @@ export class AppSyncSwiftVisitor< const keyDirectives = this.config.generateIndexRules ? this.generateKeyRules(model) : []; const priamryKeyRules = this.generatePrimaryKeyRules(model); const attributes = [...keyDirectives, priamryKeyRules].filter(f => f); + const isGenerateModelPathEnabled = this.isGenerateLazyReferenceModelPathEnabled(); const closure = [ '{ model in', `let ${keysName} = ${this.getModelName(model)}.keys`, @@ -401,9 +405,9 @@ export class AppSyncSwiftVisitor< indentMultiline(fields.join(',\n')), ')', '}', - `public class Path: ModelPath<${this.getModelName(model)}> { }`, + isGenerateModelPathEnabled ? `public class Path: ModelPath<${this.getModelName(model)}> { }` : '', '', - 'public static var rootPath: PropertyContainerPath? { Path() }', + isGenerateModelPathEnabled ? 'public static var rootPath: PropertyContainerPath? { Path() }' : '', ].join('\n'); extensionDeclaration.addProperty( 'schema', @@ -518,11 +522,16 @@ export class AppSyncSwiftVisitor< private getInitBody(fields: CodeGenField[]): string { let result = fields.map(field => { - const connectionHasOneOrBelongsTo = this.isHasOneOrBelongsToConnectionField(field); - const escapedFieldName = escapeKeywords(this.getFieldName(field)); - const propertyName = connectionHasOneOrBelongsTo ? `_${this.getFieldName(field)}` : escapedFieldName; - const fieldValue = connectionHasOneOrBelongsTo ? `LazyReference(${escapedFieldName})` : escapedFieldName; - return indent(`self.${propertyName} = ${fieldValue}`); + if (this.isGenerateLazyReferenceModelPathEnabled()) { + const connectionHasOneOrBelongsTo = this.isHasOneOrBelongsToConnectionField(field); + const escapedFieldName = escapeKeywords(this.getFieldName(field)); + const propertyName = connectionHasOneOrBelongsTo ? `_${this.getFieldName(field)}` : escapedFieldName; + const fieldValue = connectionHasOneOrBelongsTo ? `LazyReference(${escapedFieldName})` : escapedFieldName; + return indent(`self.${propertyName} = ${fieldValue}`); + } else { + const fieldName = escapeKeywords(this.getFieldName(field)); + return indent(`self.${fieldName} = ${fieldName}`); + } }); return result.join('\n'); diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts index 6c005086f..4cac5473e 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts @@ -132,6 +132,12 @@ export interface RawAppSyncModelConfig extends RawConfig { * @descriptions optional boolean which determines whether to use custom primary key support */ respectPrimaryKeyAttributesOnConnectionField?: boolean; + /** + * @name generateLazyReferenceAndModelPath + * @type boolean + * @descriptions optional boolean which determines whether to generate LazyReference and ModelPath for iOS + */ + generateLazyReferenceAndModelPath?: boolean; /** * @name codegenVersion * @type string @@ -150,6 +156,7 @@ export interface ParsedAppSyncModelConfig extends ParsedConfig { usePipelinedTransformer?: boolean; transformerVersion?: number; respectPrimaryKeyAttributesOnConnectionField?: boolean; + generateLazyReferenceAndModelPath?: boolean; codegenVersion?: string; } export type CodeGenArgumentsMap = Record; @@ -239,6 +246,7 @@ export class AppSyncModelVisitor< usePipelinedTransformer: rawConfig.usePipelinedTransformer, transformerVersion: rawConfig.transformerVersion, respectPrimaryKeyAttributesOnConnectionField: rawConfig.respectPrimaryKeyAttributesOnConnectionField, + generateLazyReferenceAndModelPath: rawConfig.generateLazyReferenceAndModelPath, codegenVersion: rawConfig.codegenVersion, }); @@ -1112,6 +1120,10 @@ export class AppSyncModelVisitor< ); } + protected isGenerateLazyReferenceModelPathEnabled(): boolean { + return this.config.generateLazyReferenceAndModelPath ?? false; + } + get models() { return this.modelMap; } From 0d0e3e2707d6d10eded43ddfb4dc6e511d4a43da Mon Sep 17 00:00:00 2001 From: Michael Law <1365977+lawmicha@users.noreply.github.com> Date: Fri, 9 Dec 2022 11:29:45 -0500 Subject: [PATCH 12/17] feat(amplify-codegen): do not generate ModelPath for embedded types --- .../src/__tests__/visitors/appsync-swift-visitor.test.ts | 3 --- .../src/visitors/appsync-swift-visitor.ts | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts index dd085f51f..ee18d1ef5 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts @@ -1667,9 +1667,6 @@ describe('AppSyncSwiftVisitor', () => { .field(location.tags, is: .optional, ofType: .embeddedCollection(of: String.self)) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } }" `); diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts index 8af157dc6..b4efbda82 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts @@ -392,7 +392,7 @@ export class AppSyncSwiftVisitor< const keyDirectives = this.config.generateIndexRules ? this.generateKeyRules(model) : []; const priamryKeyRules = this.generatePrimaryKeyRules(model); const attributes = [...keyDirectives, priamryKeyRules].filter(f => f); - const isGenerateModelPathEnabled = this.isGenerateLazyReferenceModelPathEnabled(); + const isGenerateModelPathEnabled = this.isGenerateLazyReferenceModelPathEnabled() && !this.selectedTypeIsNonModel(); const closure = [ '{ model in', `let ${keysName} = ${this.getModelName(model)}.keys`, From 3ec946d2484083b076ad8ba7367cb28af3705a20 Mon Sep 17 00:00:00 2001 From: Michael Law <1365977+lawmicha@users.noreply.github.com> Date: Fri, 9 Dec 2022 12:15:43 -0500 Subject: [PATCH 13/17] feat(amplify-codegen): do not generate custom encoder/decoder if not necessary --- .../appsync-swift-visitor.test.ts.snap | 142 ----------- .../visitors/appsync-swift-visitor.test.ts | 230 ------------------ .../appsync-swift-visitor.test.ts.snap | 222 ----------------- .../src/visitors/appsync-swift-visitor.ts | 66 ++--- 4 files changed, 35 insertions(+), 625 deletions(-) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap index 7cfe85534..974c35fa2 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -39,22 +39,6 @@ public struct Post: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - title = try values.decode(String.self, forKey: .title) - comments = try values.decode(List?.self, forKey: .comments) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(title, forKey: .title) - try container.encode(comments, forKey: .comments) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -166,24 +150,6 @@ public struct Comment: Model { self.postCommentsId = postCommentsId self.postCommentsTitle = postCommentsTitle } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - content = try values.decode(String.self, forKey: .content) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - postCommentsId = try values.decode(String?.self, forKey: .postCommentsId) - postCommentsTitle = try values.decode(String?.self, forKey: .postCommentsTitle) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(content, forKey: .content) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - try container.encode(postCommentsId, forKey: .postCommentsId) - try container.encode(postCommentsTitle, forKey: .postCommentsTitle) - } }" `; @@ -295,22 +261,6 @@ public struct ModelCompositePk: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - dob = try values.decode(Temporal.DateTime.self, forKey: .dob) - name = try values.decode(String?.self, forKey: .name) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(dob, forKey: .dob) - try container.encode(name, forKey: .name) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -412,20 +362,6 @@ public struct ModelExplicitCustomPk: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - userId = try values.decode(String.self, forKey: .userId) - name = try values.decode(String?.self, forKey: .name) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(userId, forKey: .userId) - try container.encode(name, forKey: .name) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -521,20 +457,6 @@ public struct ModelExplicitDefaultPk: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - name = try values.decode(String?.self, forKey: .name) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(name, forKey: .name) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -624,20 +546,6 @@ public struct ModelImplicitDefaultPk: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - name = try values.decode(String?.self, forKey: .name) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(name, forKey: .name) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -1045,34 +953,6 @@ public struct ListContainer: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - name = try values.decode(String?.self, forKey: .name) - list = try values.decode([Int].self, forKey: .list) - requiredList = try values.decode([String].self, forKey: .requiredList) - requiredListOfRequired = try values.decode([StatusEnum].self, forKey: .requiredListOfRequired) - listOfRequired = try values.decode([Bool].self, forKey: .listOfRequired) - requiredListOfRequiredDates = try values.decode([Temporal.Date].self, forKey: .requiredListOfRequiredDates) - listOfRequiredFloats = try values.decode([Double].self, forKey: .listOfRequiredFloats) - requiredListOfCustomTypes = try values.decode([CustomType].self, forKey: .requiredListOfCustomTypes) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(name, forKey: .name) - try container.encode(list, forKey: .list) - try container.encode(requiredList, forKey: .requiredList) - try container.encode(requiredListOfRequired, forKey: .requiredListOfRequired) - try container.encode(listOfRequired, forKey: .listOfRequired) - try container.encode(requiredListOfRequiredDates, forKey: .requiredListOfRequiredDates) - try container.encode(listOfRequiredFloats, forKey: .listOfRequiredFloats) - try container.encode(requiredListOfCustomTypes, forKey: .requiredListOfCustomTypes) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -1215,28 +1095,6 @@ public struct Todo: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - title = try values.decode(String.self, forKey: .title) - requiredListOfTasks = try values.decode(List?.self, forKey: .requiredListOfTasks) - listOfRequiredTasks = try values.decode(List?.self, forKey: .listOfRequiredTasks) - listOfTasks = try values.decode(List?.self, forKey: .listOfTasks) - requiredListOfRequiredTasks = try values.decode(List?.self, forKey: .requiredListOfRequiredTasks) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(title, forKey: .title) - try container.encode(requiredListOfTasks, forKey: .requiredListOfTasks) - try container.encode(listOfRequiredTasks, forKey: .listOfRequiredTasks) - try container.encode(listOfTasks, forKey: .listOfTasks) - try container.encode(requiredListOfRequiredTasks, forKey: .requiredListOfRequiredTasks) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts index ee18d1ef5..2ce3087f4 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts @@ -93,22 +93,6 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - name = try values.decode(String?.self, forKey: .name) - bar = try values.decode(String?.self, forKey: .bar) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(name, forKey: .name) - try container.encode(bar, forKey: .bar) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `); const metadataVisitor = getVisitor(schema, 'SimpleModel', CodeGenGenerateEnum.metadata); @@ -235,20 +219,6 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - name = try values.decode(String?.self, forKey: .name) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(name, forKey: .name) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `); @@ -341,20 +311,6 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - first_name = try values.decode(String?.self, forKey: .first_name) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(first_name, forKey: .first_name) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `); }); @@ -413,26 +369,6 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - author_id = try values.decode(String.self, forKey: .author_id) - book_id = try values.decode(String.self, forKey: .book_id) - author = try values.decode(String?.self, forKey: .author) - book = try values.decode(String?.self, forKey: .book) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(author_id, forKey: .author_id) - try container.encode(book_id, forKey: .book_id) - try container.encode(author, forKey: .author) - try container.encode(book, forKey: .book) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `); @@ -734,32 +670,6 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - title = try values.decode(String.self, forKey: .title) - done = try values.decode(Bool.self, forKey: .done) - description = try values.decode(String?.self, forKey: .description) - due_date = try values.decode(String?.self, forKey: .due_date) - version = try values.decode(Int.self, forKey: .version) - value = try values.decode(Double?.self, forKey: .value) - tasks = try values.decode(List?.self, forKey: .tasks) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(title, forKey: .title) - try container.encode(done, forKey: .done) - try container.encode(description, forKey: .description) - try container.encode(due_date, forKey: .due_date) - try container.encode(version, forKey: .version) - try container.encode(value, forKey: .value) - try container.encode(tasks, forKey: .tasks) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `); @@ -1094,22 +1004,6 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - title = try values.decode(String.self, forKey: .title) - editors = try values.decode(List?.self, forKey: .editors) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(title, forKey: .title) - try container.encode(editors, forKey: .editors) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `); @@ -1201,22 +1095,6 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - title = try values.decode(String.self, forKey: .title) - editors = try values.decode(List?.self, forKey: .editors) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(title, forKey: .title) - try container.encode(editors, forKey: .editors) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `); @@ -1348,30 +1226,6 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - intArr = try values.decode([Int].self, forKey: .intArr) - strArr = try values.decode([String].self, forKey: .strArr) - floatArr = try values.decode([Double].self, forKey: .floatArr) - boolArr = try values.decode([Bool].self, forKey: .boolArr) - dateArr = try values.decode([Temporal.Date].self, forKey: .dateArr) - enumArr = try values.decode([EnumType].self, forKey: .enumArr) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(intArr, forKey: .intArr) - try container.encode(strArr, forKey: .strArr) - try container.encode(floatArr, forKey: .floatArr) - try container.encode(boolArr, forKey: .boolArr) - try container.encode(dateArr, forKey: .dateArr) - try container.encode(enumArr, forKey: .enumArr) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `); @@ -1524,30 +1378,6 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - name = try values.decode(String.self, forKey: .name) - location = try values.decode(Location.self, forKey: .location) - nearByLocations = try values.decode([Location].self, forKey: .nearByLocations) - status = try values.decode(Status.self, forKey: .status) - statusHistory = try values.decode([Status].self, forKey: .statusHistory) - tags = try values.decode([String].self, forKey: .tags) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(name, forKey: .name) - try container.encode(location, forKey: .location) - try container.encode(nearByLocations, forKey: .nearByLocations) - try container.encode(status, forKey: .status) - try container.encode(statusHistory, forKey: .statusHistory) - try container.encode(tags, forKey: .tags) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `); @@ -1805,26 +1635,6 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - \`Class\` = try values.decode(Class?.self, forKey: .\`Class\`) - nonNullClass = try values.decode(Class.self, forKey: .nonNullClass) - classes = try values.decode([Class].self, forKey: .classes) - nonNullClasses = try values.decode([Class].self, forKey: .nonNullClasses) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(\`Class\`, forKey: .\`Class\`) - try container.encode(nonNullClass, forKey: .nonNullClass) - try container.encode(classes, forKey: .classes) - try container.encode(nonNullClasses, forKey: .nonNullClasses) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `); }); @@ -2918,22 +2728,6 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - name = try values.decode(String?.self, forKey: .name) - bar = try values.decode(String?.self, forKey: .bar) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(name, forKey: .name) - try container.encode(bar, forKey: .bar) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `); }); @@ -2964,18 +2758,6 @@ describe('AppSyncSwiftVisitor', () => { self.name = name self.bar = bar } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - name = try values.decode(String?.self, forKey: .name) - bar = try values.decode(String?.self, forKey: .bar) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(name, forKey: .name) - try container.encode(bar, forKey: .bar) - } }" `); }); @@ -3006,18 +2788,6 @@ describe('AppSyncSwiftVisitor', () => { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `); }); diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap index e2c250d59..d6c29f8dc 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -32,22 +32,6 @@ public struct Post3: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - title = try values.decode(String.self, forKey: .title) - comments = try values.decode(List?.self, forKey: .comments) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(title, forKey: .title) - try container.encode(comments, forKey: .comments) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -254,22 +238,6 @@ public struct Post: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - title = try values.decode(String.self, forKey: .title) - comments = try values.decode(List?.self, forKey: .comments) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(title, forKey: .title) - try container.encode(comments, forKey: .comments) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -714,22 +682,6 @@ public struct Blog7V2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - name = try values.decode(String.self, forKey: .name) - posts = try values.decode(List?.self, forKey: .posts) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(name, forKey: .name) - try container.encode(posts, forKey: .posts) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -1057,22 +1009,6 @@ public struct Blog8V2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - name = try values.decode(String.self, forKey: .name) - posts = try values.decode(List?.self, forKey: .posts) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(name, forKey: .name) - try container.encode(posts, forKey: .posts) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -1643,24 +1579,6 @@ public struct Post: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - title = try values.decode(String.self, forKey: .title) - content = try values.decode(String?.self, forKey: .content) - tags = try values.decode(List?.self, forKey: .tags) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(title, forKey: .title) - try container.encode(content, forKey: .content) - try container.encode(tags, forKey: .tags) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -1755,22 +1673,6 @@ public struct Tag: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - label = try values.decode(String.self, forKey: .label) - posts = try values.decode(List?.self, forKey: .posts) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(label, forKey: .label) - try container.encode(posts, forKey: .posts) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -1855,20 +1757,6 @@ public struct Todo: Model { self.createdOn = createdOn self.updatedOn = updatedOn } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - content = try values.decode(String?.self, forKey: .content) - createdOn = try values.decode(Temporal.DateTime?.self, forKey: .createdOn) - updatedOn = try values.decode(Temporal.DateTime?.self, forKey: .updatedOn) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(content, forKey: .content) - try container.encode(createdOn, forKey: .createdOn) - try container.encode(updatedOn, forKey: .updatedOn) - } }" `; @@ -1953,22 +1841,6 @@ public struct Post2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - title = try values.decode(String.self, forKey: .title) - comments = try values.decode(List?.self, forKey: .comments) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(title, forKey: .title) - try container.encode(comments, forKey: .comments) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -2058,22 +1930,6 @@ public struct Comment2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - postID = try values.decode(String.self, forKey: .postID) - content = try values.decode(String.self, forKey: .content) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(postID, forKey: .postID) - try container.encode(content, forKey: .content) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -2287,20 +2143,6 @@ public struct Team2: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - name = try values.decode(String.self, forKey: .name) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(name, forKey: .name) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -2385,22 +2227,6 @@ public struct Post: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - title = try values.decode(String.self, forKey: .title) - comments = try values.decode(List?.self, forKey: .comments) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(title, forKey: .title) - try container.encode(comments, forKey: .comments) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -2490,22 +2316,6 @@ public struct Comment: Model { self.updatedAt = updatedAt self.postCommentsId = postCommentsId } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - content = try values.decode(String.self, forKey: .content) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - postCommentsId = try values.decode(String?.self, forKey: .postCommentsId) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(content, forKey: .content) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - try container.encode(postCommentsId, forKey: .postCommentsId) - } }" `; @@ -2715,20 +2525,6 @@ public struct Team: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - name = try values.decode(String.self, forKey: .name) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(name, forKey: .name) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -2818,24 +2614,6 @@ public struct Customer: Model { self.createdAt = createdAt self.updatedAt = updatedAt } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - name = try values.decode(String.self, forKey: .name) - phoneNumber = try values.decode(String?.self, forKey: .phoneNumber) - accountRepresentativeID = try values.decode(String.self, forKey: .accountRepresentativeID) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(name, forKey: .name) - try container.encode(phoneNumber, forKey: .phoneNumber) - try container.encode(accountRepresentativeID, forKey: .accountRepresentativeID) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts index b4efbda82..11796404c 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts @@ -231,9 +231,11 @@ export class AppSyncSwiftVisitor< if (this.isGenerateLazyReferenceModelPathEnabled()) { // mutating functions for updating/deleting + var customEncodingAndDecodingRequired = false; Object.entries(obj.fields).forEach(([fieldName, field]) => { if (this.isHasOneOrBelongsToConnectionField(field)) { // lazy loading - create setter functions for LazyReference + customEncodingAndDecodingRequired = true; let fieldName = this.getFieldName(field); let capitalizedFieldName = fieldName.charAt(0).toUpperCase() + fieldName.slice(1); structBlock.addClassMethod( @@ -256,37 +258,39 @@ export class AppSyncSwiftVisitor< } }); - // custom decoder/encoder - structBlock.addClassMethod( - 'init', - null, - this.getDecoderBody(obj.fields), - [ - { - value: undefined, - name: 'from decoder', - type: 'Decoder', - flags: {}, - }, - ], - 'public', - { throws: true }, - ); - structBlock.addClassMethod( - 'encode', - null, - this.getEncoderBody(obj.fields), - [ - { - value: undefined, - name: 'to encoder', - type: 'Encoder', - flags: {}, - }, - ], - 'public', - { throws: true }, - ); + if (customEncodingAndDecodingRequired) { + // custom decoder/encoder + structBlock.addClassMethod( + 'init', + null, + this.getDecoderBody(obj.fields), + [ + { + value: undefined, + name: 'from decoder', + type: 'Decoder', + flags: {}, + }, + ], + 'public', + { throws: true }, + ); + structBlock.addClassMethod( + 'encode', + null, + this.getEncoderBody(obj.fields), + [ + { + value: undefined, + name: 'to encoder', + type: 'Encoder', + flags: {}, + }, + ], + 'public', + { throws: true }, + ); + } } result.push(structBlock.string); From 1f753606a6eb441be858a4a6a71a5a7cc100e1a6 Mon Sep 17 00:00:00 2001 From: Michael Law <1365977+lawmicha@users.noreply.github.com> Date: Mon, 12 Dec 2022 19:57:45 -0500 Subject: [PATCH 14/17] feat(amplify-codegen): generate decodeIfPresent for List --- .../__snapshots__/appsync-swift-visitor.test.ts.snap | 4 ++-- .../src/visitors/appsync-swift-visitor.ts | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap index d6c29f8dc..24eb1b79c 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -789,7 +789,7 @@ public struct Post7V2: Model { id = try values.decode(String.self, forKey: .id) title = try values.decode(String.self, forKey: .title) _blog = try values.decodeIfPresent(LazyReference.self, forKey: .blog) ?? LazyReference(identifiers: nil) - comments = try values.decode(List?.self, forKey: .comments) + comments = try values.decodeIfPresent(List?.self, forKey: .comments) ?? .init() createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } @@ -1116,7 +1116,7 @@ public struct Post8V2: Model { id = try values.decode(String.self, forKey: .id) title = try values.decode(String.self, forKey: .title) _blog = try values.decodeIfPresent(LazyReference.self, forKey: .blog) ?? LazyReference(identifiers: nil) - comments = try values.decode(List?.self, forKey: .comments) + comments = try values.decodeIfPresent(List?.self, forKey: .comments) ?? .init() createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts index 11796404c..a3bd4f117 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts @@ -549,10 +549,13 @@ export class AppSyncSwiftVisitor< const escapedFieldName = escapeKeywords(this.getFieldName(field)); const assignedFieldName = connectionHasOneOrBelongsTo ? `_${this.getFieldName(field)}` : escapedFieldName; const fieldType = this.getDecoderBodyFieldType(field); - const decodeMethod = connectionHasOneOrBelongsTo ? 'decodeIfPresent' : 'decode'; + const decodeMethod = field.connectionInfo ? 'decodeIfPresent' : 'decode'; const defaultLazyReference = connectionHasOneOrBelongsTo ? ' ?? LazyReference(identifiers: nil)' : ''; + const defaultListReference = this.isHasManyConnectionField(field) ? ' ?? .init()' : ''; result.push( - indent(`${assignedFieldName} = try values.${decodeMethod}(${fieldType}.self, forKey: .${escapedFieldName})${defaultLazyReference}`), + indent( + `${assignedFieldName} = try values.${decodeMethod}(${fieldType}.self, forKey: .${escapedFieldName})${defaultLazyReference}${defaultListReference}`, + ), ); }); From 110f06b2e9696b3fb6da4f462ef177ea1ec2f382 Mon Sep 17 00:00:00 2001 From: Michael Law <1365977+lawmicha@users.noreply.github.com> Date: Tue, 13 Dec 2022 11:50:54 -0500 Subject: [PATCH 15/17] feat(amplify-codegen): feature flag rename --- packages/amplify-codegen/src/commands/models.js | 4 ++-- .../__tests__/visitors/appsync-swift-visitor.test.ts | 2 +- .../appsync-swift-visitor.test.ts | 4 ++-- .../src/visitors/appsync-swift-visitor.ts | 10 +++++----- .../src/visitors/appsync-visitor.ts | 12 ++++++------ 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/packages/amplify-codegen/src/commands/models.js b/packages/amplify-codegen/src/commands/models.js index 66c581884..e1bab4521 100644 --- a/packages/amplify-codegen/src/commands/models.js +++ b/packages/amplify-codegen/src/commands/models.js @@ -103,7 +103,7 @@ async function generateModels(context, generateOptions = null) { const usePipelinedTransformer = readFeatureFlag('graphQLTransformer.useExperimentalPipelinedTransformer'); const transformerVersion = readNumericFeatureFlag('graphQLTransformer.transformerVersion'); const respectPrimaryKeyAttributesOnConnectionField = readFeatureFlag('graphQLTransformer.respectPrimaryKeyAttributesOnConnectionField'); - const generateLazyReferenceAndModelPath = readFeatureFlag('codegen.generateLazyReferenceAndModelPath'); + const generateModelsForLazyLoadAndCustomSelectionSet = readFeatureFlag('codegen.generateModelsForLazyLoadAndCustomSelectionSet'); let isTimestampFieldsAdded = readFeatureFlag('codegen.addTimestampFields'); let enableDartNullSafety = readFeatureFlag('codegen.enableDartNullSafety'); @@ -147,7 +147,7 @@ async function generateModels(context, generateOptions = null) { transformerVersion, dartUpdateAmplifyCoreDependency, respectPrimaryKeyAttributesOnConnectionField, - generateLazyReferenceAndModelPath, + generateModelsForLazyLoadAndCustomSelectionSet, codegenVersion: packageVersion, overrideOutputDir, // This needs to live under `config` in order for the GraphQL types to work out. }, diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts index 2ce3087f4..2554ee62e 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts @@ -11,7 +11,7 @@ const defaultIosVisitorSetings = { handleListNullabilityTransparently: true, transformerVersion: 1, respectPrimaryKeyAttributesOnConnectionField: false, - generateLazyReferenceAndModelPath: true, + generateModelsForLazyLoadAndCustomSelectionSet: true, }; const buildSchemaWithDirectives = (schema: String): GraphQLSchema => { return buildSchema([schema, directives, scalars].join('\n')); diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/appsync-swift-visitor.test.ts index c690f04c2..dba4e482b 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/appsync-swift-visitor.test.ts @@ -17,7 +17,7 @@ const getGQLv2Visitor = ( generateIndexRules: boolean = true, handleListNullabilityTransparently: boolean = true, transformerVersion: number = 2, - generateLazyReferenceAndModelPath: boolean = true, + generateModelsForLazyLoadAndCustomSelectionSet: boolean = true, ) => { const ast = parse(schema); const builtSchema = buildSchemaWithDirectives(schema); @@ -32,7 +32,7 @@ const getGQLv2Visitor = ( generateIndexRules, handleListNullabilityTransparently, transformerVersion: transformerVersion, - generateLazyReferenceAndModelPath: generateLazyReferenceAndModelPath, + generateModelsForLazyLoadAndCustomSelectionSet: generateModelsForLazyLoadAndCustomSelectionSet, }, { selectedType, generate }, ); diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts index a3bd4f117..01604253a 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts @@ -95,7 +95,7 @@ export class AppSyncSwiftVisitor< const fieldType = this.getNativeType(field); const isVariable = !primaryKeyComponentFieldsName.includes(field.name); const listType: ListType = field.connectionInfo ? ListType.LIST : ListType.ARRAY; - if (this.isGenerateLazyReferenceModelPathEnabled() && this.isHasOneOrBelongsToConnectionField(field)) { + if (this.isGenerateModelsForLazyLoadAndCustomSelectionSet() && this.isHasOneOrBelongsToConnectionField(field)) { // lazy loading - create computed property of LazyReference structBlock.addProperty(`_${this.getFieldName(field)}`, `LazyReference<${fieldType}>`, undefined, `internal`, { optional: false, @@ -229,7 +229,7 @@ export class AppSyncSwiftVisitor< ); } - if (this.isGenerateLazyReferenceModelPathEnabled()) { + if (this.isGenerateModelsForLazyLoadAndCustomSelectionSet()) { // mutating functions for updating/deleting var customEncodingAndDecodingRequired = false; Object.entries(obj.fields).forEach(([fieldName, field]) => { @@ -354,7 +354,7 @@ export class AppSyncSwiftVisitor< result.push(''); result.push(this.generatePrimaryKeyExtensions(model)); } - if (this.isGenerateLazyReferenceModelPathEnabled()) { + if (this.isGenerateModelsForLazyLoadAndCustomSelectionSet()) { result.push(this.generateModelPathExtensions(model)); } }); @@ -396,7 +396,7 @@ export class AppSyncSwiftVisitor< const keyDirectives = this.config.generateIndexRules ? this.generateKeyRules(model) : []; const priamryKeyRules = this.generatePrimaryKeyRules(model); const attributes = [...keyDirectives, priamryKeyRules].filter(f => f); - const isGenerateModelPathEnabled = this.isGenerateLazyReferenceModelPathEnabled() && !this.selectedTypeIsNonModel(); + const isGenerateModelPathEnabled = this.isGenerateModelsForLazyLoadAndCustomSelectionSet() && !this.selectedTypeIsNonModel(); const closure = [ '{ model in', `let ${keysName} = ${this.getModelName(model)}.keys`, @@ -526,7 +526,7 @@ export class AppSyncSwiftVisitor< private getInitBody(fields: CodeGenField[]): string { let result = fields.map(field => { - if (this.isGenerateLazyReferenceModelPathEnabled()) { + if (this.isGenerateModelsForLazyLoadAndCustomSelectionSet()) { const connectionHasOneOrBelongsTo = this.isHasOneOrBelongsToConnectionField(field); const escapedFieldName = escapeKeywords(this.getFieldName(field)); const propertyName = connectionHasOneOrBelongsTo ? `_${this.getFieldName(field)}` : escapedFieldName; diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts index 4cac5473e..6ec29dc33 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts @@ -133,11 +133,11 @@ export interface RawAppSyncModelConfig extends RawConfig { */ respectPrimaryKeyAttributesOnConnectionField?: boolean; /** - * @name generateLazyReferenceAndModelPath + * @name generateModelsForLazyLoadAndCustomSelectionSet * @type boolean * @descriptions optional boolean which determines whether to generate LazyReference and ModelPath for iOS */ - generateLazyReferenceAndModelPath?: boolean; + generateModelsForLazyLoadAndCustomSelectionSet?: boolean; /** * @name codegenVersion * @type string @@ -156,7 +156,7 @@ export interface ParsedAppSyncModelConfig extends ParsedConfig { usePipelinedTransformer?: boolean; transformerVersion?: number; respectPrimaryKeyAttributesOnConnectionField?: boolean; - generateLazyReferenceAndModelPath?: boolean; + generateModelsForLazyLoadAndCustomSelectionSet?: boolean; codegenVersion?: string; } export type CodeGenArgumentsMap = Record; @@ -246,7 +246,7 @@ export class AppSyncModelVisitor< usePipelinedTransformer: rawConfig.usePipelinedTransformer, transformerVersion: rawConfig.transformerVersion, respectPrimaryKeyAttributesOnConnectionField: rawConfig.respectPrimaryKeyAttributesOnConnectionField, - generateLazyReferenceAndModelPath: rawConfig.generateLazyReferenceAndModelPath, + generateModelsForLazyLoadAndCustomSelectionSet: rawConfig.generateModelsForLazyLoadAndCustomSelectionSet, codegenVersion: rawConfig.codegenVersion, }); @@ -1120,8 +1120,8 @@ export class AppSyncModelVisitor< ); } - protected isGenerateLazyReferenceModelPathEnabled(): boolean { - return this.config.generateLazyReferenceAndModelPath ?? false; + protected isGenerateModelsForLazyLoadAndCustomSelectionSet(): boolean { + return this.config.generateModelsForLazyLoadAndCustomSelectionSet ?? false; } get models() { From 1c6f4ebac9eecf061b021bb418d7c670e0681c44 Mon Sep 17 00:00:00 2001 From: Michael Law <1365977+lawmicha@users.noreply.github.com> Date: Wed, 21 Dec 2022 17:22:33 -0500 Subject: [PATCH 16/17] feat(amplify-codegen): fix decoder for optional fields that are not connections --- .../appsync-swift-visitor.test.ts.snap | 16 ++--- .../visitors/appsync-swift-visitor.test.ts | 8 +-- .../appsync-swift-visitor.test.ts.snap | 68 +++++++++---------- .../src/visitors/appsync-swift-visitor.ts | 3 +- 4 files changed, 48 insertions(+), 47 deletions(-) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap index 974c35fa2..503951086 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -662,10 +662,10 @@ public struct Project: Model { projectId = try values.decode(String.self, forKey: .projectId) name = try values.decode(String.self, forKey: .name) _team = try values.decodeIfPresent(LazyReference.self, forKey: .team) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - projectTeamTeamId = try values.decode(String?.self, forKey: .projectTeamTeamId) - projectTeamName = try values.decode(String?.self, forKey: .projectTeamName) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + projectTeamTeamId = try? values.decode(String?.self, forKey: .projectTeamTeamId) + projectTeamName = try? values.decode(String?.self, forKey: .projectTeamName) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -806,8 +806,8 @@ public struct Team: Model { teamId = try values.decode(String.self, forKey: .teamId) name = try values.decode(String.self, forKey: .name) _project = try values.decodeIfPresent(LazyReference.self, forKey: .project) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -1212,8 +1212,8 @@ public struct task: Model { id = try values.decode(String.self, forKey: .id) title = try values.decode(String.self, forKey: .title) _todo = try values.decodeIfPresent(LazyReference.self, forKey: .todo) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts index 2554ee62e..91cca4382 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts @@ -819,10 +819,10 @@ describe('AppSyncSwiftVisitor', () => { title = try values.decode(String.self, forKey: .title) done = try values.decode(Bool.self, forKey: .done) _todo = try values.decodeIfPresent(LazyReference.self, forKey: .todo) ?? LazyReference(identifiers: nil) - time = try values.decode(Temporal.Time?.self, forKey: .time) - createdOn = try values.decode(Temporal.Date?.self, forKey: .createdOn) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + time = try? values.decode(Temporal.Time?.self, forKey: .time) + createdOn = try? values.decode(Temporal.Date?.self, forKey: .createdOn) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap index 24eb1b79c..1405eec58 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -134,8 +134,8 @@ public struct Comment3: Model { id = try values.decode(String.self, forKey: .id) content = try values.decode(String.self, forKey: .content) _post = try values.decodeIfPresent(LazyReference.self, forKey: .post) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -340,8 +340,8 @@ public struct Comment: Model { id = try values.decode(String.self, forKey: .id) content = try values.decode(String.self, forKey: .content) _post = try values.decodeIfPresent(LazyReference.self, forKey: .post) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -460,11 +460,11 @@ public struct Project2: Model { public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, forKey: .id) - name = try values.decode(String?.self, forKey: .name) + name = try? values.decode(String?.self, forKey: .name) _team = try values.decodeIfPresent(LazyReference.self, forKey: .team) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - project2TeamId = try values.decode(String?.self, forKey: .project2TeamId) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + project2TeamId = try? values.decode(String?.self, forKey: .project2TeamId) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -582,8 +582,8 @@ public struct Team2: Model { id = try values.decode(String.self, forKey: .id) name = try values.decode(String.self, forKey: .name) _project = try values.decodeIfPresent(LazyReference.self, forKey: .project) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -790,8 +790,8 @@ public struct Post7V2: Model { title = try values.decode(String.self, forKey: .title) _blog = try values.decodeIfPresent(LazyReference.self, forKey: .blog) ?? LazyReference(identifiers: nil) comments = try values.decodeIfPresent(List?.self, forKey: .comments) ?? .init() - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -907,10 +907,10 @@ public struct Comment7V2: Model { public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, forKey: .id) - content = try values.decode(String?.self, forKey: .content) + content = try? values.decode(String?.self, forKey: .content) _post = try values.decodeIfPresent(LazyReference.self, forKey: .post) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -1117,8 +1117,8 @@ public struct Post8V2: Model { title = try values.decode(String.self, forKey: .title) _blog = try values.decodeIfPresent(LazyReference.self, forKey: .blog) ?? LazyReference(identifiers: nil) comments = try values.decodeIfPresent(List?.self, forKey: .comments) ?? .init() - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -1234,10 +1234,10 @@ public struct Comment8V2: Model { public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, forKey: .id) - content = try values.decode(String?.self, forKey: .content) + content = try? values.decode(String?.self, forKey: .content) _post = try values.decodeIfPresent(LazyReference.self, forKey: .post) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -1352,11 +1352,11 @@ public struct Project: Model { public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, forKey: .id) - name = try values.decode(String?.self, forKey: .name) + name = try? values.decode(String?.self, forKey: .name) _team = try values.decodeIfPresent(LazyReference.self, forKey: .team) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - projectTeamId = try values.decode(String?.self, forKey: .projectTeamId) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + projectTeamId = try? values.decode(String?.self, forKey: .projectTeamId) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -1474,8 +1474,8 @@ public struct Team: Model { id = try values.decode(String.self, forKey: .id) name = try values.decode(String.self, forKey: .name) _project = try values.decodeIfPresent(LazyReference.self, forKey: .project) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -2039,11 +2039,11 @@ public struct Project2: Model { public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, forKey: .id) - name = try values.decode(String?.self, forKey: .name) - teamID = try values.decode(String?.self, forKey: .teamID) + name = try? values.decode(String?.self, forKey: .name) + teamID = try? values.decode(String?.self, forKey: .teamID) _team = try values.decodeIfPresent(LazyReference.self, forKey: .team) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) @@ -2421,11 +2421,11 @@ public struct Project: Model { public init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, forKey: .id) - name = try values.decode(String?.self, forKey: .name) + name = try? values.decode(String?.self, forKey: .name) _team = try values.decodeIfPresent(LazyReference.self, forKey: .team) ?? LazyReference(identifiers: nil) - createdAt = try values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - projectTeamId = try values.decode(String?.self, forKey: .projectTeamId) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + projectTeamId = try? values.decode(String?.self, forKey: .projectTeamId) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts index 01604253a..bbe1cc3e4 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts @@ -552,9 +552,10 @@ export class AppSyncSwiftVisitor< const decodeMethod = field.connectionInfo ? 'decodeIfPresent' : 'decode'; const defaultLazyReference = connectionHasOneOrBelongsTo ? ' ?? LazyReference(identifiers: nil)' : ''; const defaultListReference = this.isHasManyConnectionField(field) ? ' ?? .init()' : ''; + const optionalTry = !this.isFieldRequired(field) && !field.connectionInfo ? '?' : ''; result.push( indent( - `${assignedFieldName} = try values.${decodeMethod}(${fieldType}.self, forKey: .${escapedFieldName})${defaultLazyReference}${defaultListReference}`, + `${assignedFieldName} = try${optionalTry} values.${decodeMethod}(${fieldType}.self, forKey: .${escapedFieldName})${defaultLazyReference}${defaultListReference}`, ), ); }); From 6145eceb56eb6e73b3f9eca68e3a0c6dbc6c82a4 Mon Sep 17 00:00:00 2001 From: Michael Law <1365977+lawmicha@users.noreply.github.com> Date: Wed, 25 Jan 2023 17:49:24 -0500 Subject: [PATCH 17/17] feat(amplify-codegen): update regression test flag --- .../appsync-swift-visitor.test.ts.snap | 883 +----------------- .../appsync-swift-visitor.test.ts | 2 +- 2 files changed, 25 insertions(+), 860 deletions(-) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap index 1405eec58..23aa33357 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -66,26 +66,6 @@ extension Post3 { .field(post3.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Post3 { - public var id: FieldPath { - string(\\"id\\") - } - public var title: FieldPath { - string(\\"title\\") - } - public var comments: ModelPath { - Comment3.Path(name: \\"comments\\", isCollection: true, parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -97,12 +77,7 @@ import Foundation public struct Comment3: Model { public let id: String public var content: String - internal var _post: LazyReference - public var post: Post3 { - get async throws { - try await _post.require() - } - } + public var post: Post3 public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? @@ -122,29 +97,10 @@ public struct Comment3: Model { updatedAt: Temporal.DateTime? = nil) { self.id = id self.content = content - self._post = LazyReference(post) + self.post = post self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setPost(_ post: Post3) { - self._post = LazyReference(post) - } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - content = try values.decode(String.self, forKey: .content) - _post = try values.decodeIfPresent(LazyReference.self, forKey: .post) ?? LazyReference(identifiers: nil) - createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(content, forKey: .content) - try container.encode(_post, forKey: .post) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -183,26 +139,6 @@ extension Comment3 { .field(comment3.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Comment3 { - public var id: FieldPath { - string(\\"id\\") - } - public var content: FieldPath { - string(\\"content\\") - } - public var post: ModelPath { - Post3.Path(name: \\"post\\", parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -272,26 +208,6 @@ extension Post { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Post { - public var id: FieldPath { - string(\\"id\\") - } - public var title: FieldPath { - string(\\"title\\") - } - public var comments: ModelPath { - Comment.Path(name: \\"comments\\", isCollection: true, parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -303,12 +219,7 @@ import Foundation public struct Comment: Model { public let id: String public var content: String - internal var _post: LazyReference - public var post: Post? { - get async throws { - try await _post.get() - } - } + public var post: Post? public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? @@ -328,29 +239,10 @@ public struct Comment: Model { updatedAt: Temporal.DateTime? = nil) { self.id = id self.content = content - self._post = LazyReference(post) + self.post = post self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setPost(_ post: Post? = nil) { - self._post = LazyReference(post) - } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - content = try values.decode(String.self, forKey: .content) - _post = try values.decodeIfPresent(LazyReference.self, forKey: .post) ?? LazyReference(identifiers: nil) - createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(content, forKey: .content) - try container.encode(_post, forKey: .post) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -389,26 +281,6 @@ extension Comment { .field(comment.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Comment { - public var id: FieldPath { - string(\\"id\\") - } - public var content: FieldPath { - string(\\"content\\") - } - public var post: ModelPath { - Post.Path(name: \\"post\\", parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -420,12 +292,7 @@ import Foundation public struct Project2: Model { public let id: String public var name: String? - internal var _team: LazyReference - public var team: Team2? { - get async throws { - try await _team.get() - } - } + public var team: Team2? public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? public var project2TeamId: String? @@ -449,32 +316,11 @@ public struct Project2: Model { project2TeamId: String? = nil) { self.id = id self.name = name - self._team = LazyReference(team) + self.team = team self.createdAt = createdAt self.updatedAt = updatedAt self.project2TeamId = project2TeamId } - public mutating func setTeam(_ team: Team2? = nil) { - self._team = LazyReference(team) - } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - name = try? values.decode(String?.self, forKey: .name) - _team = try values.decodeIfPresent(LazyReference.self, forKey: .team) ?? LazyReference(identifiers: nil) - createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - project2TeamId = try? values.decode(String?.self, forKey: .project2TeamId) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(name, forKey: .name) - try container.encode(_team, forKey: .team) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - try container.encode(project2TeamId, forKey: .project2TeamId) - } }" `; @@ -511,29 +357,6 @@ extension Project2 { .field(project2.project2TeamId, is: .optional, ofType: .string) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Project2 { - public var id: FieldPath { - string(\\"id\\") - } - public var name: FieldPath { - string(\\"name\\") - } - public var team: ModelPath { - Team2.Path(name: \\"team\\", parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } - public var project2TeamId: FieldPath { - string(\\"project2TeamId\\") - } }" `; @@ -545,12 +368,7 @@ import Foundation public struct Team2: Model { public let id: String public var name: String - internal var _project: LazyReference - public var project: Project2? { - get async throws { - try await _project.get() - } - } + public var project: Project2? public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? @@ -570,29 +388,10 @@ public struct Team2: Model { updatedAt: Temporal.DateTime? = nil) { self.id = id self.name = name - self._project = LazyReference(project) + self.project = project self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setProject(_ project: Project2? = nil) { - self._project = LazyReference(project) - } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - name = try values.decode(String.self, forKey: .name) - _project = try values.decodeIfPresent(LazyReference.self, forKey: .project) ?? LazyReference(identifiers: nil) - createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(name, forKey: .name) - try container.encode(_project, forKey: .project) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -627,26 +426,6 @@ extension Team2 { .field(team2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Team2 { - public var id: FieldPath { - string(\\"id\\") - } - public var name: FieldPath { - string(\\"name\\") - } - public var project: ModelPath { - Project2.Path(name: \\"project\\", parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -716,26 +495,6 @@ extension Blog7V2 { .field(blog7V2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Blog7V2 { - public var id: FieldPath { - string(\\"id\\") - } - public var name: FieldPath { - string(\\"name\\") - } - public var posts: ModelPath { - Post7V2.Path(name: \\"posts\\", isCollection: true, parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -747,12 +506,7 @@ import Foundation public struct Post7V2: Model { public let id: String public var title: String - internal var _blog: LazyReference - public var blog: Blog7V2? { - get async throws { - try await _blog.get() - } - } + public var blog: Blog7V2? public var comments: List? public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? @@ -776,32 +530,11 @@ public struct Post7V2: Model { updatedAt: Temporal.DateTime? = nil) { self.id = id self.title = title - self._blog = LazyReference(blog) + self.blog = blog self.comments = comments self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setBlog(_ blog: Blog7V2? = nil) { - self._blog = LazyReference(blog) - } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - title = try values.decode(String.self, forKey: .title) - _blog = try values.decodeIfPresent(LazyReference.self, forKey: .blog) ?? LazyReference(identifiers: nil) - comments = try values.decodeIfPresent(List?.self, forKey: .comments) ?? .init() - createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(title, forKey: .title) - try container.encode(_blog, forKey: .blog) - try container.encode(comments, forKey: .comments) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -838,29 +571,6 @@ extension Post7V2 { .field(post7V2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Post7V2 { - public var id: FieldPath { - string(\\"id\\") - } - public var title: FieldPath { - string(\\"title\\") - } - public var blog: ModelPath { - Blog7V2.Path(name: \\"blog\\", parent: self) - } - public var comments: ModelPath { - Comment7V2.Path(name: \\"comments\\", isCollection: true, parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -872,12 +582,7 @@ import Foundation public struct Comment7V2: Model { public let id: String public var content: String? - internal var _post: LazyReference - public var post: Post7V2? { - get async throws { - try await _post.get() - } - } + public var post: Post7V2? public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? @@ -897,29 +602,10 @@ public struct Comment7V2: Model { updatedAt: Temporal.DateTime? = nil) { self.id = id self.content = content - self._post = LazyReference(post) + self.post = post self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setPost(_ post: Post7V2? = nil) { - self._post = LazyReference(post) - } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - content = try? values.decode(String?.self, forKey: .content) - _post = try values.decodeIfPresent(LazyReference.self, forKey: .post) ?? LazyReference(identifiers: nil) - createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(content, forKey: .content) - try container.encode(_post, forKey: .post) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -954,26 +640,6 @@ extension Comment7V2 { .field(comment7V2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Comment7V2 { - public var id: FieldPath { - string(\\"id\\") - } - public var content: FieldPath { - string(\\"content\\") - } - public var post: ModelPath { - Post7V2.Path(name: \\"post\\", parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -1043,26 +709,6 @@ extension Blog8V2 { .field(blog8V2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Blog8V2 { - public var id: FieldPath { - string(\\"id\\") - } - public var name: FieldPath { - string(\\"name\\") - } - public var posts: ModelPath { - Post8V2.Path(name: \\"posts\\", isCollection: true, parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -1074,12 +720,7 @@ import Foundation public struct Post8V2: Model { public let id: String public var title: String - internal var _blog: LazyReference - public var blog: Blog8V2 { - get async throws { - try await _blog.require() - } - } + public var blog: Blog8V2 public var comments: List? public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? @@ -1103,32 +744,11 @@ public struct Post8V2: Model { updatedAt: Temporal.DateTime? = nil) { self.id = id self.title = title - self._blog = LazyReference(blog) + self.blog = blog self.comments = comments self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setBlog(_ blog: Blog8V2) { - self._blog = LazyReference(blog) - } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - title = try values.decode(String.self, forKey: .title) - _blog = try values.decodeIfPresent(LazyReference.self, forKey: .blog) ?? LazyReference(identifiers: nil) - comments = try values.decodeIfPresent(List?.self, forKey: .comments) ?? .init() - createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(title, forKey: .title) - try container.encode(_blog, forKey: .blog) - try container.encode(comments, forKey: .comments) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -1165,29 +785,6 @@ extension Post8V2 { .field(post8V2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Post8V2 { - public var id: FieldPath { - string(\\"id\\") - } - public var title: FieldPath { - string(\\"title\\") - } - public var blog: ModelPath { - Blog8V2.Path(name: \\"blog\\", parent: self) - } - public var comments: ModelPath { - Comment8V2.Path(name: \\"comments\\", isCollection: true, parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -1199,12 +796,7 @@ import Foundation public struct Comment8V2: Model { public let id: String public var content: String? - internal var _post: LazyReference - public var post: Post8V2 { - get async throws { - try await _post.require() - } - } + public var post: Post8V2 public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? @@ -1224,29 +816,10 @@ public struct Comment8V2: Model { updatedAt: Temporal.DateTime? = nil) { self.id = id self.content = content - self._post = LazyReference(post) + self.post = post self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setPost(_ post: Post8V2) { - self._post = LazyReference(post) - } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - content = try? values.decode(String?.self, forKey: .content) - _post = try values.decodeIfPresent(LazyReference.self, forKey: .post) ?? LazyReference(identifiers: nil) - createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(content, forKey: .content) - try container.encode(_post, forKey: .post) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -1281,26 +854,6 @@ extension Comment8V2 { .field(comment8V2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Comment8V2 { - public var id: FieldPath { - string(\\"id\\") - } - public var content: FieldPath { - string(\\"content\\") - } - public var post: ModelPath { - Post8V2.Path(name: \\"post\\", parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -1312,12 +865,7 @@ import Foundation public struct Project: Model { public let id: String public var name: String? - internal var _team: LazyReference - public var team: Team? { - get async throws { - try await _team.get() - } - } + public var team: Team? public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? public var projectTeamId: String? @@ -1341,32 +889,11 @@ public struct Project: Model { projectTeamId: String? = nil) { self.id = id self.name = name - self._team = LazyReference(team) + self.team = team self.createdAt = createdAt self.updatedAt = updatedAt self.projectTeamId = projectTeamId } - public mutating func setTeam(_ team: Team? = nil) { - self._team = LazyReference(team) - } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - name = try? values.decode(String?.self, forKey: .name) - _team = try values.decodeIfPresent(LazyReference.self, forKey: .team) ?? LazyReference(identifiers: nil) - createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - projectTeamId = try? values.decode(String?.self, forKey: .projectTeamId) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(name, forKey: .name) - try container.encode(_team, forKey: .team) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - try container.encode(projectTeamId, forKey: .projectTeamId) - } }" `; @@ -1403,29 +930,6 @@ extension Project { .field(project.projectTeamId, is: .optional, ofType: .string) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Project { - public var id: FieldPath { - string(\\"id\\") - } - public var name: FieldPath { - string(\\"name\\") - } - public var team: ModelPath { - Team.Path(name: \\"team\\", parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } - public var projectTeamId: FieldPath { - string(\\"projectTeamId\\") - } }" `; @@ -1437,12 +941,7 @@ import Foundation public struct Team: Model { public let id: String public var name: String - internal var _project: LazyReference - public var project: Project? { - get async throws { - try await _project.get() - } - } + public var project: Project? public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? @@ -1462,29 +961,10 @@ public struct Team: Model { updatedAt: Temporal.DateTime? = nil) { self.id = id self.name = name - self._project = LazyReference(project) + self.project = project self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setProject(_ project: Project? = nil) { - self._project = LazyReference(project) - } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - name = try values.decode(String.self, forKey: .name) - _project = try values.decodeIfPresent(LazyReference.self, forKey: .project) ?? LazyReference(identifiers: nil) - createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(name, forKey: .name) - try container.encode(_project, forKey: .project) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -1519,26 +999,6 @@ extension Team { .field(team.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Team { - public var id: FieldPath { - string(\\"id\\") - } - public var name: FieldPath { - string(\\"name\\") - } - public var project: ModelPath { - Project.Path(name: \\"project\\", parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -1615,29 +1075,6 @@ extension Post { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Post { - public var id: FieldPath { - string(\\"id\\") - } - public var title: FieldPath { - string(\\"title\\") - } - public var content: FieldPath { - string(\\"content\\") - } - public var tags: ModelPath { - PostTags.Path(name: \\"tags\\", isCollection: true, parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -1707,26 +1144,6 @@ extension Tag { .field(tag.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Tag { - public var id: FieldPath { - string(\\"id\\") - } - public var label: FieldPath { - string(\\"label\\") - } - public var posts: ModelPath { - PostTags.Path(name: \\"posts\\", isCollection: true, parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -1789,23 +1206,6 @@ extension Todo { .field(todo.updatedOn, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Todo { - public var id: FieldPath { - string(\\"id\\") - } - public var content: FieldPath { - string(\\"content\\") - } - public var createdOn: FieldPath { - datetime(\\"createdOn\\") - } - public var updatedOn: FieldPath { - datetime(\\"updatedOn\\") - } }" `; @@ -1875,26 +1275,6 @@ extension Post2 { .field(post2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Post2 { - public var id: FieldPath { - string(\\"id\\") - } - public var title: FieldPath { - string(\\"title\\") - } - public var comments: ModelPath { - Comment2.Path(name: \\"comments\\", isCollection: true, parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -1968,26 +1348,6 @@ extension Comment2 { .field(comment2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Comment2 { - public var id: FieldPath { - string(\\"id\\") - } - public var postID: FieldPath { - string(\\"postID\\") - } - public var content: FieldPath { - string(\\"content\\") - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -2000,12 +1360,7 @@ public struct Project2: Model { public let id: String public var name: String? public var teamID: String? - internal var _team: LazyReference - public var team: Team2? { - get async throws { - try await _team.get() - } - } + public var team: Team2? public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? @@ -2029,31 +1384,10 @@ public struct Project2: Model { self.id = id self.name = name self.teamID = teamID - self._team = LazyReference(team) + self.team = team self.createdAt = createdAt self.updatedAt = updatedAt } - public mutating func setTeam(_ team: Team2? = nil) { - self._team = LazyReference(team) - } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - name = try? values.decode(String?.self, forKey: .name) - teamID = try? values.decode(String?.self, forKey: .teamID) - _team = try values.decodeIfPresent(LazyReference.self, forKey: .team) ?? LazyReference(identifiers: nil) - createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(name, forKey: .name) - try container.encode(teamID, forKey: .teamID) - try container.encode(_team, forKey: .team) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - } }" `; @@ -2090,29 +1424,6 @@ extension Project2 { .field(project2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Project2 { - public var id: FieldPath { - string(\\"id\\") - } - public var name: FieldPath { - string(\\"name\\") - } - public var teamID: FieldPath { - string(\\"teamID\\") - } - public var team: ModelPath { - Team2.Path(name: \\"team\\", parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -2175,23 +1486,6 @@ extension Team2 { .field(team2.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Team2 { - public var id: FieldPath { - string(\\"id\\") - } - public var name: FieldPath { - string(\\"name\\") - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -2261,26 +1555,6 @@ extension Post { .field(post.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Post { - public var id: FieldPath { - string(\\"id\\") - } - public var title: FieldPath { - string(\\"title\\") - } - public var comments: ModelPath { - Comment.Path(name: \\"comments\\", isCollection: true, parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -2350,26 +1624,6 @@ extension Comment { .field(comment.postCommentsId, is: .optional, ofType: .string) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Comment { - public var id: FieldPath { - string(\\"id\\") - } - public var content: FieldPath { - string(\\"content\\") - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } - public var postCommentsId: FieldPath { - string(\\"postCommentsId\\") - } }" `; @@ -2381,12 +1635,7 @@ import Foundation public struct Project: Model { public let id: String public var name: String? - internal var _team: LazyReference - public var team: Team? { - get async throws { - try await _team.get() - } - } + public var team: Team? public var createdAt: Temporal.DateTime? public var updatedAt: Temporal.DateTime? public var projectTeamId: String? @@ -2410,32 +1659,11 @@ public struct Project: Model { projectTeamId: String? = nil) { self.id = id self.name = name - self._team = LazyReference(team) + self.team = team self.createdAt = createdAt self.updatedAt = updatedAt self.projectTeamId = projectTeamId } - public mutating func setTeam(_ team: Team? = nil) { - self._team = LazyReference(team) - } - public init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - id = try values.decode(String.self, forKey: .id) - name = try? values.decode(String?.self, forKey: .name) - _team = try values.decodeIfPresent(LazyReference.self, forKey: .team) ?? LazyReference(identifiers: nil) - createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) - updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) - projectTeamId = try? values.decode(String?.self, forKey: .projectTeamId) - } - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - try container.encode(name, forKey: .name) - try container.encode(_team, forKey: .team) - try container.encode(createdAt, forKey: .createdAt) - try container.encode(updatedAt, forKey: .updatedAt) - try container.encode(projectTeamId, forKey: .projectTeamId) - } }" `; @@ -2472,29 +1700,6 @@ extension Project { .field(project.projectTeamId, is: .optional, ofType: .string) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Project { - public var id: FieldPath { - string(\\"id\\") - } - public var name: FieldPath { - string(\\"name\\") - } - public var team: ModelPath { - Team.Path(name: \\"team\\", parent: self) - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } - public var projectTeamId: FieldPath { - string(\\"projectTeamId\\") - } }" `; @@ -2557,23 +1762,6 @@ extension Team { .field(team.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Team { - public var id: FieldPath { - string(\\"id\\") - } - public var name: FieldPath { - string(\\"name\\") - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; @@ -2654,28 +1842,5 @@ extension Customer { .field(customer.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) ) } - public class Path: ModelPath { } - - public static var rootPath: PropertyContainerPath? { Path() } -} -extension ModelPath where ModelType == Customer { - public var id: FieldPath { - string(\\"id\\") - } - public var name: FieldPath { - string(\\"name\\") - } - public var phoneNumber: FieldPath { - string(\\"phoneNumber\\") - } - public var accountRepresentativeID: FieldPath { - string(\\"accountRepresentativeID\\") - } - public var createdAt: FieldPath { - datetime(\\"createdAt\\") - } - public var updatedAt: FieldPath { - datetime(\\"updatedAt\\") - } }" `; diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/appsync-swift-visitor.test.ts index dba4e482b..77973cb8a 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/appsync-swift-visitor.test.ts @@ -17,7 +17,7 @@ const getGQLv2Visitor = ( generateIndexRules: boolean = true, handleListNullabilityTransparently: boolean = true, transformerVersion: number = 2, - generateModelsForLazyLoadAndCustomSelectionSet: boolean = true, + generateModelsForLazyLoadAndCustomSelectionSet: boolean = false, ) => { const ast = parse(schema); const builtSchema = buildSchemaWithDirectives(schema);