@@ -23,9 +23,9 @@ export interface RawAppSyncModelSwiftConfig extends RawAppSyncModelConfig {
23
23
* @type boolean
24
24
* @descriptions optional boolean, if true emits the provider value of @auth directives
25
25
*/
26
- emitAuthProvider ?: boolean ;
26
+ emitAuthProvider ?: boolean ;
27
27
28
- /**
28
+ /**
29
29
* @name directives
30
30
* @type boolean
31
31
* @description optional, defines if custom indexes defined by @key directive should be generated.
@@ -90,7 +90,7 @@ export class AppSyncSwiftVisitor<
90
90
isEnum : this . isEnumType ( field ) ,
91
91
listType : field . isList ? listType : undefined ,
92
92
isListNullable : field . isListNullable ,
93
- handleListNullabilityTransparently : this . isHasManyConnectionField ( field ) ? false : this . config . handleListNullabilityTransparently
93
+ handleListNullabilityTransparently : this . isHasManyConnectionField ( field ) ? false : this . config . handleListNullabilityTransparently ,
94
94
} ) ;
95
95
} ) ;
96
96
const initParams : CodeGenField [ ] = this . getWritableFields ( obj ) ;
@@ -102,7 +102,7 @@ export class AppSyncSwiftVisitor<
102
102
} )
103
103
. join ( ',\n' ) ,
104
104
) . trim ( ) } )`;
105
- if ( this . config . isTimestampFieldsAdded ) {
105
+ if ( this . config . isTimestampFieldsAdded && this . hasReadOnlyFields ( obj ) ) {
106
106
//public constructor
107
107
structBlock . addClassMethod (
108
108
'init' ,
@@ -120,7 +120,9 @@ export class AppSyncSwiftVisitor<
120
120
isEnum : this . isEnumType ( field ) ,
121
121
listType : field . isList ? listType : undefined ,
122
122
isListNullable : field . isListNullable ,
123
- handleListNullabilityTransparently : this . isHasManyConnectionField ( field ) ? false : this . config . handleListNullabilityTransparently
123
+ handleListNullabilityTransparently : this . isHasManyConnectionField ( field )
124
+ ? false
125
+ : this . config . handleListNullabilityTransparently ,
124
126
} ,
125
127
} ;
126
128
} ) ,
@@ -144,7 +146,9 @@ export class AppSyncSwiftVisitor<
144
146
isEnum : this . isEnumType ( field ) ,
145
147
listType : field . isList ? listType : undefined ,
146
148
isListNullable : field . isListNullable ,
147
- handleListNullabilityTransparently : this . isHasManyConnectionField ( field ) ? false : this . config . handleListNullabilityTransparently
149
+ handleListNullabilityTransparently : this . isHasManyConnectionField ( field )
150
+ ? false
151
+ : this . config . handleListNullabilityTransparently ,
148
152
} ,
149
153
} ;
150
154
} ) ,
@@ -169,7 +173,9 @@ export class AppSyncSwiftVisitor<
169
173
isEnum : this . isEnumType ( field ) ,
170
174
listType : field . isList ? listType : undefined ,
171
175
isListNullable : field . isListNullable ,
172
- handleListNullabilityTransparently : this . isHasManyConnectionField ( field ) ? false : this . config . handleListNullabilityTransparently
176
+ handleListNullabilityTransparently : this . isHasManyConnectionField ( field )
177
+ ? false
178
+ : this . config . handleListNullabilityTransparently ,
173
179
} ,
174
180
} ;
175
181
} ) ,
@@ -214,7 +220,7 @@ export class AppSyncSwiftVisitor<
214
220
isEnum : this . isEnumType ( field ) ,
215
221
listType : field . isList ? ListType . ARRAY : undefined ,
216
222
isListNullable : field . isListNullable ,
217
- handleListNullabilityTransparently : this . isHasManyConnectionField ( field ) ? false : this . config . handleListNullabilityTransparently
223
+ handleListNullabilityTransparently : this . isHasManyConnectionField ( field ) ? false : this . config . handleListNullabilityTransparently ,
218
224
} ) ;
219
225
} ) ;
220
226
result . push ( structBlock . string ) ;
@@ -228,9 +234,7 @@ export class AppSyncSwiftVisitor<
228
234
Object . values ( this . getSelectedModels ( ) )
229
235
. filter ( m => m . type === 'model' )
230
236
. forEach ( model => {
231
- const schemaDeclarations = new SwiftDeclarationBlock ( )
232
- . asKind ( 'extension' )
233
- . withName ( this . getModelName ( model ) ) ;
237
+ const schemaDeclarations = new SwiftDeclarationBlock ( ) . asKind ( 'extension' ) . withName ( this . getModelName ( model ) ) ;
234
238
235
239
this . generateCodingKeys ( this . getModelName ( model ) , model , schemaDeclarations ) ,
236
240
this . generateModelSchema ( this . getModelName ( model ) , model , schemaDeclarations ) ;
@@ -239,9 +243,7 @@ export class AppSyncSwiftVisitor<
239
243
} ) ;
240
244
241
245
Object . values ( this . getSelectedNonModels ( ) ) . forEach ( model => {
242
- const schemaDeclarations = new SwiftDeclarationBlock ( )
243
- . asKind ( 'extension' )
244
- . withName ( this . getNonModelName ( model ) ) ;
246
+ const schemaDeclarations = new SwiftDeclarationBlock ( ) . asKind ( 'extension' ) . withName ( this . getNonModelName ( model ) ) ;
245
247
246
248
this . generateCodingKeys ( this . getNonModelName ( model ) , model , schemaDeclarations ) ,
247
249
this . generateModelSchema ( this . getNonModelName ( model ) , model , schemaDeclarations ) ;
@@ -350,8 +352,10 @@ export class AppSyncSwiftVisitor<
350
352
const name = `${ modelKeysName } .${ this . getFieldName ( field ) } ` ;
351
353
const typeName = this . getSwiftModelTypeName ( field ) ;
352
354
const { connectionInfo } = field ;
353
- const isRequiredField = ( ( ! this . isHasManyConnectionField ( field ) ) && this . config . handleListNullabilityTransparently ) ?
354
- this . isRequiredField ( field ) : this . isFieldRequired ( field ) ;
355
+ const isRequiredField =
356
+ ! this . isHasManyConnectionField ( field ) && this . config . handleListNullabilityTransparently
357
+ ? this . isRequiredField ( field )
358
+ : this . isFieldRequired ( field ) ;
355
359
const isRequired = isRequiredField ? '.required' : '.optional' ;
356
360
// connected field
357
361
if ( connectionInfo ) {
@@ -438,14 +442,14 @@ export class AppSyncSwiftVisitor<
438
442
439
443
protected generateKeyRules ( model : CodeGenModel ) : string [ ] {
440
444
const keyDirectives = model . directives
441
- . filter ( ( directive ) => directive . name === 'key' )
442
- . map ( ( directive ) => {
445
+ . filter ( directive => directive . name === 'key' )
446
+ . map ( directive => {
443
447
const name = directive . arguments . name ? `"${ directive . arguments . name } "` : 'nil' ;
444
448
const fields : string = directive . arguments . fields . map ( ( field : string ) => `"${ field } "` ) . join ( ', ' ) ;
445
449
return `.index(fields: [${ fields } ], name: ${ name } )` ;
446
450
} ) ;
447
451
448
- return keyDirectives
452
+ return keyDirectives ;
449
453
}
450
454
451
455
protected isHasManyConnectionField ( field : CodeGenField ) : boolean {
@@ -471,8 +475,8 @@ export class AppSyncSwiftVisitor<
471
475
authRule . push ( 'allow: .private' ) ;
472
476
break ;
473
477
case AuthStrategy . public :
474
- authRule . push ( 'allow: .public' ) ;
475
- break ;
478
+ authRule . push ( 'allow: .public' ) ;
479
+ break ;
476
480
case AuthStrategy . groups :
477
481
authRule . push ( 'allow: .groups' ) ;
478
482
authRule . push ( `groupClaim: "${ rule . groupClaim } "` ) ;
@@ -502,4 +506,8 @@ export class AppSyncSwiftVisitor<
502
506
protected getWritableFields ( model : CodeGenModel ) : CodeGenField [ ] {
503
507
return model . fields . filter ( f => ! f . isReadOnly ) ;
504
508
}
509
+
510
+ protected hasReadOnlyFields ( model : CodeGenModel ) : boolean {
511
+ return model . fields . filter ( f => f . isReadOnly ) . length !== 0 ;
512
+ }
505
513
}
0 commit comments