Skip to content

Commit 5aeda85

Browse files
authored
Merge pull request #471 from aws-amplify/main
Release Amplify Codegen v3.0.5
2 parents 5661ac6 + 750ba15 commit 5aeda85

File tree

2 files changed

+40
-32
lines changed

2 files changed

+40
-32
lines changed

packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ extension Post {
8282
8383
extension Post: ModelIdentifiable {
8484
public typealias IdentifierFormat = ModelIdentifierFormat.Custom
85-
public typealias Identifier = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
85+
public typealias IdentifierProtocol = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
8686
}
8787
88-
extension Post.Identifier {
88+
extension Post.IdentifierProtocol {
8989
public static func identifier(id: String,
9090
title: String) -> Self {
9191
.make(fields:[(name: \\"id\\", value: id), (name: \\"title\\", value: title)])
@@ -175,10 +175,10 @@ extension Comment {
175175
176176
extension Comment: ModelIdentifiable {
177177
public typealias IdentifierFormat = ModelIdentifierFormat.Custom
178-
public typealias Identifier = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
178+
public typealias IdentifierProtocol = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
179179
}
180180
181-
extension Comment.Identifier {
181+
extension Comment.IdentifierProtocol {
182182
public static func identifier(id: String,
183183
content: String) -> Self {
184184
.make(fields:[(name: \\"id\\", value: id), (name: \\"content\\", value: content)])
@@ -261,10 +261,10 @@ extension ModelCompositePk {
261261
262262
extension ModelCompositePk: ModelIdentifiable {
263263
public typealias IdentifierFormat = ModelIdentifierFormat.Custom
264-
public typealias Identifier = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
264+
public typealias IdentifierProtocol = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
265265
}
266266
267-
extension ModelCompositePk.Identifier {
267+
extension ModelCompositePk.IdentifierProtocol {
268268
public static func identifier(id: String,
269269
dob: Temporal.DateTime) -> Self {
270270
.make(fields:[(name: \\"id\\", value: id), (name: \\"dob\\", value: dob)])
@@ -340,10 +340,10 @@ extension ModelExplicitCustomPk {
340340
341341
extension ModelExplicitCustomPk: ModelIdentifiable {
342342
public typealias IdentifierFormat = ModelIdentifierFormat.Custom
343-
public typealias Identifier = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
343+
public typealias IdentifierProtocol = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
344344
}
345345
346-
extension ModelExplicitCustomPk.Identifier {
346+
extension ModelExplicitCustomPk.IdentifierProtocol {
347347
public static func identifier(userId: String) -> Self {
348348
.make(fields:[(name: \\"userId\\", value: userId)])
349349
}
@@ -417,14 +417,8 @@ extension ModelExplicitDefaultPk {
417417
}
418418
419419
extension ModelExplicitDefaultPk: ModelIdentifiable {
420-
public typealias IdentifierFormat = ModelIdentifierFormat.Custom
421-
public typealias Identifier = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
422-
}
423-
424-
extension ModelExplicitDefaultPk.Identifier {
425-
public static func identifier(id: String) -> Self {
426-
.make(fields:[(name: \\"id\\", value: id)])
427-
}
420+
public typealias IdentifierFormat = ModelIdentifierFormat.Default
421+
public typealias IdentifierProtocol = DefaultModelIdentifier<Self>
428422
}"
429423
`;
430424
@@ -495,7 +489,7 @@ extension ModelImplicitDefaultPk {
495489
496490
extension ModelImplicitDefaultPk: ModelIdentifiable {
497491
public typealias IdentifierFormat = ModelIdentifierFormat.Default
498-
public typealias Identifier = DefaultModelIdentifier<Self>
492+
public typealias IdentifierProtocol = DefaultModelIdentifier<Self>
499493
}"
500494
`;
501495
@@ -588,10 +582,10 @@ extension Project {
588582
589583
extension Project: ModelIdentifiable {
590584
public typealias IdentifierFormat = ModelIdentifierFormat.Custom
591-
public typealias Identifier = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
585+
public typealias IdentifierProtocol = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
592586
}
593587
594-
extension Project.Identifier {
588+
extension Project.IdentifierProtocol {
595589
public static func identifier(projectId: String,
596590
name: String) -> Self {
597591
.make(fields:[(name: \\"projectId\\", value: projectId), (name: \\"name\\", value: name)])
@@ -674,10 +668,10 @@ extension Team {
674668
675669
extension Team: ModelIdentifiable {
676670
public typealias IdentifierFormat = ModelIdentifierFormat.Custom
677-
public typealias Identifier = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
671+
public typealias IdentifierProtocol = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
678672
}
679673
680-
extension Team.Identifier {
674+
extension Team.IdentifierProtocol {
681675
public static func identifier(teamId: String,
682676
name: String) -> Self {
683677
.make(fields:[(name: \\"teamId\\", value: teamId), (name: \\"name\\", value: name)])

packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ export class AppSyncSwiftVisitor<
292292
const authRules = this.generateAuthRules(model);
293293
const keyDirectives = this.config.generateIndexRules ? this.generateKeyRules(model) : [];
294294
const priamryKeyRules = this.generatePrimaryKeyRules(model);
295-
const attributes = [ ...keyDirectives, priamryKeyRules ].filter(f => f);
295+
const attributes = [...keyDirectives, priamryKeyRules].filter(f => f);
296296
const closure = [
297297
'{ model in',
298298
`let ${keysName} = ${this.getModelName(model)}.keys`,
@@ -320,22 +320,34 @@ export class AppSyncSwiftVisitor<
320320
let result: string[] = [];
321321
const primaryKeyField = model.fields.find(field => field.primaryKeyInfo)!;
322322
const { primaryKeyType, sortKeyFields } = primaryKeyField.primaryKeyInfo!;
323-
const useDefaultExplicitID = primaryKeyType === CodeGenPrimaryKeyType.ManagedId;
323+
const useDefaultExplicitID =
324+
primaryKeyType === CodeGenPrimaryKeyType.ManagedId || primaryKeyType === CodeGenPrimaryKeyType.OptionallyManagedId;
324325

325-
const identifiableExtension = new SwiftDeclarationBlock().asKind('extension').withName(`${this.getModelName(model)}: ModelIdentifiable`);
326+
const identifiableExtension = new SwiftDeclarationBlock()
327+
.asKind('extension')
328+
.withName(`${this.getModelName(model)}: ModelIdentifiable`);
326329
// identifier format
327330
const identifierFormatValue = `ModelIdentifierFormat.${useDefaultExplicitID ? 'Default' : 'Custom'}`;
328331
identifiableExtension.addProperty('IdentifierFormat', '', identifierFormatValue, 'public', { isTypeAlias: true });
329332
// identifier
330333
const identifierValue = useDefaultExplicitID ? 'DefaultModelIdentifier<Self>' : 'ModelIdentifier<Self, ModelIdentifierFormat.Custom>';
331-
identifiableExtension.addProperty('Identifier', '', identifierValue, 'public', { isTypeAlias: true });
334+
identifiableExtension.addProperty('IdentifierProtocol', '', identifierValue, 'public', { isTypeAlias: true });
332335
result.push(identifiableExtension.string);
333336

334337
if (!useDefaultExplicitID) {
335-
const identifierExtension = new SwiftDeclarationBlock().asKind('extension').withName(`${this.getModelName(model)}.Identifier`);
336-
const primaryKeyComponentFields = [ primaryKeyField, ...sortKeyFields ];
337-
const identifierArgs = primaryKeyComponentFields.map(field => ({name: this.getFieldName(field), type: this.getNativeType(field), flags: {}, value: undefined}));
338-
const identifierBody = `.make(fields:[${primaryKeyComponentFields.map(field => `(name: "${this.getFieldName(field)}", value: ${this.getFieldName(field)})`).join(', ')}])`;
338+
const identifierExtension = new SwiftDeclarationBlock()
339+
.asKind('extension')
340+
.withName(`${this.getModelName(model)}.IdentifierProtocol`);
341+
const primaryKeyComponentFields = [primaryKeyField, ...sortKeyFields];
342+
const identifierArgs = primaryKeyComponentFields.map(field => ({
343+
name: this.getFieldName(field),
344+
type: this.getNativeType(field),
345+
flags: {},
346+
value: undefined,
347+
}));
348+
const identifierBody = `.make(fields:[${primaryKeyComponentFields
349+
.map(field => `(name: "${this.getFieldName(field)}", value: ${this.getFieldName(field)})`)
350+
.join(', ')}])`;
339351
identifierExtension.addClassMethod('identifier', 'Self', identifierBody, identifierArgs, 'public', { static: true });
340352
result.push(identifierExtension.string);
341353
}
@@ -404,8 +416,8 @@ export class AppSyncSwiftVisitor<
404416
if (connectionInfo.kind === CodeGenConnectionType.HAS_MANY) {
405417
return `.hasMany(${name}, is: ${isRequired}, ofType: ${typeName}, associatedWith: ${this.getModelName(
406418
connectionInfo.connectedModel,
407-
)}.keys.${this.getFieldName(connectionInfo.associatedWith)})`;
408-
}
419+
)}.keys.${this.getFieldName(connectionInfo.associatedWith)})`;
420+
}
409421
if (connectionInfo.kind === CodeGenConnectionType.HAS_ONE) {
410422
const targetNameAttrStr = this.isCustomPKEnabled()
411423
? `targetNames: [${connectionInfo.targetNames.map(target => `"${target}"`).join(', ')}]`
@@ -505,7 +517,9 @@ export class AppSyncSwiftVisitor<
505517
const primaryKeyField = model.fields.find(f => f.primaryKeyInfo)!;
506518
const { sortKeyFields } = primaryKeyField.primaryKeyInfo!;
507519
const modelName = lowerCaseFirst(this.getModelName(model));
508-
return `.primaryKey(fields: [${[primaryKeyField, ...sortKeyFields].map(field => `${modelName}.${this.getFieldName(field)}`).join(', ')}])`
520+
return `.primaryKey(fields: [${[primaryKeyField, ...sortKeyFields]
521+
.map(field => `${modelName}.${this.getFieldName(field)}`)
522+
.join(', ')}])`;
509523
}
510524

511525
protected isHasManyConnectionField(field: CodeGenField): boolean {

0 commit comments

Comments
 (0)