Skip to content

Commit 463fef4

Browse files
authored
fix(amplify-codegen): rename ModelIdentifiable.Identifier to IdentifierProtocol (#467)
1 parent 5661ac6 commit 463fef4

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

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

Lines changed: 15 additions & 15 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
}
@@ -418,10 +418,10 @@ extension ModelExplicitDefaultPk {
418418
419419
extension ModelExplicitDefaultPk: ModelIdentifiable {
420420
public typealias IdentifierFormat = ModelIdentifierFormat.Custom
421-
public typealias Identifier = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
421+
public typealias IdentifierProtocol = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
422422
}
423423
424-
extension ModelExplicitDefaultPk.Identifier {
424+
extension ModelExplicitDefaultPk.IdentifierProtocol {
425425
public static func identifier(id: String) -> Self {
426426
.make(fields:[(name: \\"id\\", value: id)])
427427
}
@@ -495,7 +495,7 @@ extension ModelImplicitDefaultPk {
495495
496496
extension ModelImplicitDefaultPk: ModelIdentifiable {
497497
public typealias IdentifierFormat = ModelIdentifierFormat.Default
498-
public typealias Identifier = DefaultModelIdentifier<Self>
498+
public typealias IdentifierProtocol = DefaultModelIdentifier<Self>
499499
}"
500500
`;
501501
@@ -588,10 +588,10 @@ extension Project {
588588
589589
extension Project: ModelIdentifiable {
590590
public typealias IdentifierFormat = ModelIdentifierFormat.Custom
591-
public typealias Identifier = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
591+
public typealias IdentifierProtocol = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
592592
}
593593
594-
extension Project.Identifier {
594+
extension Project.IdentifierProtocol {
595595
public static func identifier(projectId: String,
596596
name: String) -> Self {
597597
.make(fields:[(name: \\"projectId\\", value: projectId), (name: \\"name\\", value: name)])
@@ -674,10 +674,10 @@ extension Team {
674674
675675
extension Team: ModelIdentifiable {
676676
public typealias IdentifierFormat = ModelIdentifierFormat.Custom
677-
public typealias Identifier = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
677+
public typealias IdentifierProtocol = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
678678
}
679679
680-
extension Team.Identifier {
680+
extension Team.IdentifierProtocol {
681681
public static func identifier(teamId: String,
682682
name: String) -> Self {
683683
.make(fields:[(name: \\"teamId\\", value: teamId), (name: \\"name\\", value: name)])

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

Lines changed: 23 additions & 10 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`,
@@ -322,20 +322,31 @@ export class AppSyncSwiftVisitor<
322322
const { primaryKeyType, sortKeyFields } = primaryKeyField.primaryKeyInfo!;
323323
const useDefaultExplicitID = primaryKeyType === CodeGenPrimaryKeyType.ManagedId;
324324

325-
const identifiableExtension = new SwiftDeclarationBlock().asKind('extension').withName(`${this.getModelName(model)}: ModelIdentifiable`);
325+
const identifiableExtension = new SwiftDeclarationBlock()
326+
.asKind('extension')
327+
.withName(`${this.getModelName(model)}: ModelIdentifiable`);
326328
// identifier format
327329
const identifierFormatValue = `ModelIdentifierFormat.${useDefaultExplicitID ? 'Default' : 'Custom'}`;
328330
identifiableExtension.addProperty('IdentifierFormat', '', identifierFormatValue, 'public', { isTypeAlias: true });
329331
// identifier
330332
const identifierValue = useDefaultExplicitID ? 'DefaultModelIdentifier<Self>' : 'ModelIdentifier<Self, ModelIdentifierFormat.Custom>';
331-
identifiableExtension.addProperty('Identifier', '', identifierValue, 'public', { isTypeAlias: true });
333+
identifiableExtension.addProperty('IdentifierProtocol', '', identifierValue, 'public', { isTypeAlias: true });
332334
result.push(identifiableExtension.string);
333335

334336
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(', ')}])`;
337+
const identifierExtension = new SwiftDeclarationBlock()
338+
.asKind('extension')
339+
.withName(`${this.getModelName(model)}.IdentifierProtocol`);
340+
const primaryKeyComponentFields = [primaryKeyField, ...sortKeyFields];
341+
const identifierArgs = primaryKeyComponentFields.map(field => ({
342+
name: this.getFieldName(field),
343+
type: this.getNativeType(field),
344+
flags: {},
345+
value: undefined,
346+
}));
347+
const identifierBody = `.make(fields:[${primaryKeyComponentFields
348+
.map(field => `(name: "${this.getFieldName(field)}", value: ${this.getFieldName(field)})`)
349+
.join(', ')}])`;
339350
identifierExtension.addClassMethod('identifier', 'Self', identifierBody, identifierArgs, 'public', { static: true });
340351
result.push(identifierExtension.string);
341352
}
@@ -404,8 +415,8 @@ export class AppSyncSwiftVisitor<
404415
if (connectionInfo.kind === CodeGenConnectionType.HAS_MANY) {
405416
return `.hasMany(${name}, is: ${isRequired}, ofType: ${typeName}, associatedWith: ${this.getModelName(
406417
connectionInfo.connectedModel,
407-
)}.keys.${this.getFieldName(connectionInfo.associatedWith)})`;
408-
}
418+
)}.keys.${this.getFieldName(connectionInfo.associatedWith)})`;
419+
}
409420
if (connectionInfo.kind === CodeGenConnectionType.HAS_ONE) {
410421
const targetNameAttrStr = this.isCustomPKEnabled()
411422
? `targetNames: [${connectionInfo.targetNames.map(target => `"${target}"`).join(', ')}]`
@@ -505,7 +516,9 @@ export class AppSyncSwiftVisitor<
505516
const primaryKeyField = model.fields.find(f => f.primaryKeyInfo)!;
506517
const { sortKeyFields } = primaryKeyField.primaryKeyInfo!;
507518
const modelName = lowerCaseFirst(this.getModelName(model));
508-
return `.primaryKey(fields: [${[primaryKeyField, ...sortKeyFields].map(field => `${modelName}.${this.getFieldName(field)}`).join(', ')}])`
519+
return `.primaryKey(fields: [${[primaryKeyField, ...sortKeyFields]
520+
.map(field => `${modelName}.${this.getFieldName(field)}`)
521+
.join(', ')}])`;
509522
}
510523

511524
protected isHasManyConnectionField(field: CodeGenField): boolean {

0 commit comments

Comments
 (0)