Skip to content

Commit f923e14

Browse files
authored
fix(appsync-modelgen-plugin): restore the old init if timestamp disabled (#177)
* fix(appsync-modelgen-plugin): restore the old init if timestamp disabled * fix: ff value
1 parent a99d67c commit f923e14

File tree

3 files changed

+151
-52
lines changed

3 files changed

+151
-52
lines changed

packages/amplify-codegen/src/commands/models.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,13 @@ async function generateModels(context) {
4848
const outputPath = path.join(projectRoot, getModelOutputPath(context));
4949
const schema = parse(schemaContent);
5050
const projectConfig = context.amplify.getProjectConfig();
51-
51+
//get modelgen package
5252
const modelgenPackageMigrationflag = 'codegen.useAppSyncModelgenPlugin';
53-
5453
const appSyncDataStoreCodeGen = getModelgenPackage(FeatureFlags.getBoolean(modelgenPackageMigrationflag));
55-
54+
//get timestamp config value
5655
let isTimestampFieldsAdded = false;
5756
try {
58-
isTimestampFieldsAdded = FeatureFlags.getBoolean('addTimestampFields');
57+
isTimestampFieldsAdded = FeatureFlags.getBoolean('codegen.addTimestampFields');
5958
} catch (err) {
6059
isTimestampFieldsAdded = false;
6160
}

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

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ const buildSchemaWithDirectives = (schema: String): GraphQLSchema => {
88
return buildSchema([schema, directives, scalars].join('\n'));
99
};
1010

11-
const getVisitor = (schema: string, selectedType?: string, generate: CodeGenGenerateEnum = CodeGenGenerateEnum.code) => {
11+
const getVisitor = (
12+
schema: string,
13+
selectedType?: string,
14+
generate: CodeGenGenerateEnum = CodeGenGenerateEnum.code,
15+
isTimestampFieldsAdded: boolean = true,
16+
) => {
1217
const ast = parse(schema);
1318
const builtSchema = buildSchemaWithDirectives(schema);
1419
const visitor = new AppSyncSwiftVisitor(
1520
builtSchema,
16-
{ directives, target: 'swift', scalars: SWIFT_SCALAR_MAP, isTimestampFieldsAdded: true },
21+
{ directives, target: 'swift', scalars: SWIFT_SCALAR_MAP, isTimestampFieldsAdded },
1722
{ selectedType, generate },
1823
);
1924
visit(ast, { leave: visitor });
@@ -789,7 +794,7 @@ describe('AppSyncSwiftVisitor', () => {
789794
val2
790795
}
791796
type ObjectWithNativeTypes @model {
792-
id: ID!,
797+
id: ID!
793798
intArr: [Int]
794799
strArr: [String]
795800
floatArr: [Float]
@@ -1148,7 +1153,7 @@ describe('AppSyncSwiftVisitor', () => {
11481153
}
11491154
11501155
type Foo @model {
1151-
id: ID!,
1156+
id: ID!
11521157
Class: Class
11531158
nonNullClass: Class!
11541159
classes: [Class]
@@ -1866,4 +1871,74 @@ describe('AppSyncSwiftVisitor', () => {
18661871
}"
18671872
`);
18681873
});
1874+
describe('timestamp fields', () => {
1875+
const schema = /* GraphQL */ `
1876+
type SimpleModel @model {
1877+
id: ID!
1878+
name: String
1879+
bar: String
1880+
}
1881+
`;
1882+
it('should generate timestamp fields if timestamp is enabled', () => {
1883+
const visitor = getVisitor(schema, 'SimpleModel', CodeGenGenerateEnum.code, true);
1884+
const generatedCode = visitor.generate();
1885+
expect(generatedCode).toMatchInlineSnapshot(`
1886+
"// swiftlint:disable all
1887+
import Amplify
1888+
import Foundation
1889+
1890+
public struct SimpleModel: Model {
1891+
public let id: String
1892+
public var name: String?
1893+
public var bar: String?
1894+
public var createdAt: Temporal.DateTime?
1895+
public var updatedAt: Temporal.DateTime?
1896+
1897+
public init(id: String = UUID().uuidString,
1898+
name: String? = nil,
1899+
bar: String? = nil) {
1900+
self.init(id: id,
1901+
name: name,
1902+
bar: bar,
1903+
createdAt: nil,
1904+
updatedAt: nil)
1905+
}
1906+
internal init(id: String = UUID().uuidString,
1907+
name: String? = nil,
1908+
bar: String? = nil,
1909+
createdAt: Temporal.DateTime? = nil,
1910+
updatedAt: Temporal.DateTime? = nil) {
1911+
self.id = id
1912+
self.name = name
1913+
self.bar = bar
1914+
self.createdAt = createdAt
1915+
self.updatedAt = updatedAt
1916+
}
1917+
}"
1918+
`);
1919+
});
1920+
it('should keep the original init method if timestamp is disabled', () => {
1921+
const visitor = getVisitor(schema, 'SimpleModel', CodeGenGenerateEnum.code, false);
1922+
const generatedCode = visitor.generate();
1923+
expect(generatedCode).toMatchInlineSnapshot(`
1924+
"// swiftlint:disable all
1925+
import Amplify
1926+
import Foundation
1927+
1928+
public struct SimpleModel: Model {
1929+
public let id: String
1930+
public var name: String?
1931+
public var bar: String?
1932+
1933+
public init(id: String = UUID().uuidString,
1934+
name: String? = nil,
1935+
bar: String? = nil) {
1936+
self.id = id
1937+
self.name = name
1938+
self.bar = bar
1939+
}
1940+
}"
1941+
`);
1942+
});
1943+
});
18691944
});

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

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -65,50 +65,75 @@ export class AppSyncSwiftVisitor<
6565
})
6666
.join(',\n'),
6767
).trim()})`;
68-
//public constructor
69-
structBlock.addClassMethod(
70-
'init',
71-
null,
72-
initImpl,
73-
initParams.map(field => {
74-
const listType: ListType = field.connectionInfo ? ListType.LIST : ListType.ARRAY;
75-
return {
76-
name: this.getFieldName(field),
77-
type: this.getNativeType(field),
78-
value: field.name === 'id' ? 'UUID().uuidString' : undefined,
79-
flags: {
80-
optional: field.isNullable,
81-
isList: field.isList,
82-
isEnum: this.isEnumType(field),
83-
listType: field.isList ? listType : undefined,
84-
},
85-
};
86-
}),
87-
'public',
88-
{},
89-
);
90-
//internal constructor
91-
structBlock.addClassMethod(
92-
'init',
93-
null,
94-
this.getInitBody(obj.fields),
95-
obj.fields.map(field => {
96-
const listType: ListType = field.connectionInfo ? ListType.LIST : ListType.ARRAY;
97-
return {
98-
name: this.getFieldName(field),
99-
type: this.getNativeType(field),
100-
value: field.name === 'id' ? 'UUID().uuidString' : undefined,
101-
flags: {
102-
optional: field.isNullable,
103-
isList: field.isList,
104-
isEnum: this.isEnumType(field),
105-
listType: field.isList ? listType : undefined,
106-
},
107-
};
108-
}),
109-
'internal',
110-
{},
111-
);
68+
if (this.config.isTimestampFieldsAdded) {
69+
//public constructor
70+
structBlock.addClassMethod(
71+
'init',
72+
null,
73+
initImpl,
74+
initParams.map(field => {
75+
const listType: ListType = field.connectionInfo ? ListType.LIST : ListType.ARRAY;
76+
return {
77+
name: this.getFieldName(field),
78+
type: this.getNativeType(field),
79+
value: field.name === 'id' ? 'UUID().uuidString' : undefined,
80+
flags: {
81+
optional: field.isNullable,
82+
isList: field.isList,
83+
isEnum: this.isEnumType(field),
84+
listType: field.isList ? listType : undefined,
85+
},
86+
};
87+
}),
88+
'public',
89+
{},
90+
);
91+
//internal constructor
92+
structBlock.addClassMethod(
93+
'init',
94+
null,
95+
this.getInitBody(obj.fields),
96+
obj.fields.map(field => {
97+
const listType: ListType = field.connectionInfo ? ListType.LIST : ListType.ARRAY;
98+
return {
99+
name: this.getFieldName(field),
100+
type: this.getNativeType(field),
101+
value: field.name === 'id' ? 'UUID().uuidString' : undefined,
102+
flags: {
103+
optional: field.isNullable,
104+
isList: field.isList,
105+
isEnum: this.isEnumType(field),
106+
listType: field.isList ? listType : undefined,
107+
},
108+
};
109+
}),
110+
'internal',
111+
{},
112+
);
113+
} else {
114+
//old constructor
115+
structBlock.addClassMethod(
116+
'init',
117+
null,
118+
this.getInitBody(obj.fields),
119+
obj.fields.map(field => {
120+
const listType: ListType = field.connectionInfo ? ListType.LIST : ListType.ARRAY;
121+
return {
122+
name: this.getFieldName(field),
123+
type: this.getNativeType(field),
124+
value: field.name === 'id' ? 'UUID().uuidString' : undefined,
125+
flags: {
126+
optional: field.isNullable,
127+
isList: field.isList,
128+
isEnum: this.isEnumType(field),
129+
listType: field.isList ? listType : undefined,
130+
},
131+
};
132+
}),
133+
'public',
134+
{},
135+
);
136+
}
112137
result.push(structBlock.string);
113138
});
114139
return result.join('\n');

0 commit comments

Comments
 (0)