@@ -23,9 +23,9 @@ export interface RawAppSyncModelSwiftConfig extends RawAppSyncModelConfig {
2323 * @type boolean
2424 * @descriptions optional boolean, if true emits the provider value of @auth directives
2525 */
26- emitAuthProvider ?: boolean ;
26+ emitAuthProvider ?: boolean ;
2727
28- /**
28+ /**
2929 * @name directives
3030 * @type boolean
3131 * @description optional, defines if custom indexes defined by @key directive should be generated.
@@ -90,7 +90,7 @@ export class AppSyncSwiftVisitor<
9090 isEnum : this . isEnumType ( field ) ,
9191 listType : field . isList ? listType : undefined ,
9292 isListNullable : field . isListNullable ,
93- handleListNullabilityTransparently : this . isHasManyConnectionField ( field ) ? false : this . config . handleListNullabilityTransparently
93+ handleListNullabilityTransparently : this . isHasManyConnectionField ( field ) ? false : this . config . handleListNullabilityTransparently ,
9494 } ) ;
9595 } ) ;
9696 const initParams : CodeGenField [ ] = this . getWritableFields ( obj ) ;
@@ -102,7 +102,7 @@ export class AppSyncSwiftVisitor<
102102 } )
103103 . join ( ',\n' ) ,
104104 ) . trim ( ) } )`;
105- if ( this . config . isTimestampFieldsAdded ) {
105+ if ( this . config . isTimestampFieldsAdded && this . hasReadOnlyFields ( obj ) ) {
106106 //public constructor
107107 structBlock . addClassMethod (
108108 'init' ,
@@ -120,7 +120,9 @@ export class AppSyncSwiftVisitor<
120120 isEnum : this . isEnumType ( field ) ,
121121 listType : field . isList ? listType : undefined ,
122122 isListNullable : field . isListNullable ,
123- handleListNullabilityTransparently : this . isHasManyConnectionField ( field ) ? false : this . config . handleListNullabilityTransparently
123+ handleListNullabilityTransparently : this . isHasManyConnectionField ( field )
124+ ? false
125+ : this . config . handleListNullabilityTransparently ,
124126 } ,
125127 } ;
126128 } ) ,
@@ -144,7 +146,9 @@ export class AppSyncSwiftVisitor<
144146 isEnum : this . isEnumType ( field ) ,
145147 listType : field . isList ? listType : undefined ,
146148 isListNullable : field . isListNullable ,
147- handleListNullabilityTransparently : this . isHasManyConnectionField ( field ) ? false : this . config . handleListNullabilityTransparently
149+ handleListNullabilityTransparently : this . isHasManyConnectionField ( field )
150+ ? false
151+ : this . config . handleListNullabilityTransparently ,
148152 } ,
149153 } ;
150154 } ) ,
@@ -169,7 +173,9 @@ export class AppSyncSwiftVisitor<
169173 isEnum : this . isEnumType ( field ) ,
170174 listType : field . isList ? listType : undefined ,
171175 isListNullable : field . isListNullable ,
172- handleListNullabilityTransparently : this . isHasManyConnectionField ( field ) ? false : this . config . handleListNullabilityTransparently
176+ handleListNullabilityTransparently : this . isHasManyConnectionField ( field )
177+ ? false
178+ : this . config . handleListNullabilityTransparently ,
173179 } ,
174180 } ;
175181 } ) ,
@@ -214,7 +220,7 @@ export class AppSyncSwiftVisitor<
214220 isEnum : this . isEnumType ( field ) ,
215221 listType : field . isList ? ListType . ARRAY : undefined ,
216222 isListNullable : field . isListNullable ,
217- handleListNullabilityTransparently : this . isHasManyConnectionField ( field ) ? false : this . config . handleListNullabilityTransparently
223+ handleListNullabilityTransparently : this . isHasManyConnectionField ( field ) ? false : this . config . handleListNullabilityTransparently ,
218224 } ) ;
219225 } ) ;
220226 result . push ( structBlock . string ) ;
@@ -228,9 +234,7 @@ export class AppSyncSwiftVisitor<
228234 Object . values ( this . getSelectedModels ( ) )
229235 . filter ( m => m . type === 'model' )
230236 . 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 ) ) ;
234238
235239 this . generateCodingKeys ( this . getModelName ( model ) , model , schemaDeclarations ) ,
236240 this . generateModelSchema ( this . getModelName ( model ) , model , schemaDeclarations ) ;
@@ -239,9 +243,7 @@ export class AppSyncSwiftVisitor<
239243 } ) ;
240244
241245 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 ) ) ;
245247
246248 this . generateCodingKeys ( this . getNonModelName ( model ) , model , schemaDeclarations ) ,
247249 this . generateModelSchema ( this . getNonModelName ( model ) , model , schemaDeclarations ) ;
@@ -350,8 +352,10 @@ export class AppSyncSwiftVisitor<
350352 const name = `${ modelKeysName } .${ this . getFieldName ( field ) } ` ;
351353 const typeName = this . getSwiftModelTypeName ( field ) ;
352354 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 ) ;
355359 const isRequired = isRequiredField ? '.required' : '.optional' ;
356360 // connected field
357361 if ( connectionInfo ) {
@@ -438,14 +442,14 @@ export class AppSyncSwiftVisitor<
438442
439443 protected generateKeyRules ( model : CodeGenModel ) : string [ ] {
440444 const keyDirectives = model . directives
441- . filter ( ( directive ) => directive . name === 'key' )
442- . map ( ( directive ) => {
445+ . filter ( directive => directive . name === 'key' )
446+ . map ( directive => {
443447 const name = directive . arguments . name ? `"${ directive . arguments . name } "` : 'nil' ;
444448 const fields : string = directive . arguments . fields . map ( ( field : string ) => `"${ field } "` ) . join ( ', ' ) ;
445449 return `.index(fields: [${ fields } ], name: ${ name } )` ;
446450 } ) ;
447451
448- return keyDirectives
452+ return keyDirectives ;
449453 }
450454
451455 protected isHasManyConnectionField ( field : CodeGenField ) : boolean {
@@ -471,8 +475,8 @@ export class AppSyncSwiftVisitor<
471475 authRule . push ( 'allow: .private' ) ;
472476 break ;
473477 case AuthStrategy . public :
474- authRule . push ( 'allow: .public' ) ;
475- break ;
478+ authRule . push ( 'allow: .public' ) ;
479+ break ;
476480 case AuthStrategy . groups :
477481 authRule . push ( 'allow: .groups' ) ;
478482 authRule . push ( `groupClaim: "${ rule . groupClaim } "` ) ;
@@ -502,4 +506,8 @@ export class AppSyncSwiftVisitor<
502506 protected getWritableFields ( model : CodeGenModel ) : CodeGenField [ ] {
503507 return model . fields . filter ( f => ! f . isReadOnly ) ;
504508 }
509+
510+ protected hasReadOnlyFields ( model : CodeGenModel ) : boolean {
511+ return model . fields . filter ( f => f . isReadOnly ) . length !== 0 ;
512+ }
505513}
0 commit comments