@@ -2,10 +2,10 @@ public struct FileContents {
22
33 var originalTableName : String
44 var columns : [ Column ]
5- var primaryKey : Column ?
5+ var primaryKey : PrimaryKey ?
66 var foreignKeys : [ ForeignKey ]
77
8- init ( originalTableName: String , columns: [ Column ] , primaryKey: Column ? , foreignKeys: [ ForeignKey ] ) {
8+ init ( originalTableName: String , columns: [ Column ] , primaryKey: PrimaryKey ? , foreignKeys: [ ForeignKey ] ) {
99 self . originalTableName = originalTableName
1010 self . columns = columns
1111 self . primaryKey = primaryKey
@@ -23,22 +23,27 @@ public struct FileContents {
2323 }
2424 return " "
2525 }
26+
2627 /// gets swift naming convention ClassName
2728 public var className : String {
2829 return self . originalTableName. format ( )
2930 }
31+
3032 /// gets class name declaration
3133 var classDeclaration : String {
3234 return " final class \( self . className) : Model, Content { "
3335 }
36+
3437 /// gets the schema declaration
3538 var schema : String {
3639 return " static let schema = \" \( self . originalTableName) \" "
3740 }
41+
3842 /// gets the fromatted schema
3943 var schemaFormatted : String {
4044 return " \n \t \( self . schema) \n "
4145 }
46+
4247 /// gets closing declaration
4348 var endDeclaration : String {
4449 return " \n } "
@@ -47,7 +52,7 @@ public struct FileContents {
4752 /// gets property wrapper for primary key
4853 var primaryKeyWrapper : String ? {
4954 if let pk = self . primaryKey {
50- return self . getPropertyWrapper ( column: pk, isPrimary : true , isForeign : false )
55+ return self . getPropertyWrapper ( column: pk)
5156 }
5257 return nil
5358 }
@@ -77,8 +82,8 @@ public struct FileContents {
7782 var foreignKeyDeclarations : String ? {
7883 if self . foreignKeys. isEmpty { return nil }
7984 return self . foreignKeys. map { fk in
80- let propertyWrapper = self . getPropertyWrapper ( column: fk, isPrimary : false , isForeign : true )
81- let property = self . getForeignKeyPropertyDeclaration ( column: fk)
85+ let propertyWrapper = self . getPropertyWrapper ( column: fk)
86+ let property = self . getPropertyDeclaration ( column: fk)
8287 return " \n \n \t " + propertyWrapper + " \n \t " + property
8388 } . joined ( )
8489 }
@@ -95,11 +100,11 @@ public struct FileContents {
95100 var trimmedColumns : [ Column ] {
96101 var processedColumns : [ Column ] = [ ]
97102 if let pk = self . primaryKey {
98- processedColumns. append ( pk)
103+ processedColumns. append ( pk. toColumn )
99104 }
100105 if !self . foreignKeys. isEmpty {
101106 processedColumns += self . foreignKeys. compactMap { fk in
102- return fk. convertToColumn ( )
107+ return fk. toColumn
103108 }
104109 }
105110 return self . columns. filter { !processedColumns. contains ( $0) }
@@ -108,9 +113,9 @@ public struct FileContents {
108113 /// gets declarations for remaining columns
109114 var columnProperties : String ? {
110115 if self . trimmedColumns. isEmpty { return nil }
111- return self . trimmedColumns. map { col in
112- let propertyWrapper = self . getPropertyWrapper ( column: col , isPrimary : false , isForeign : false )
113- let property = self . getPropertyDeclaration ( column: col )
116+ return self . trimmedColumns. map { column in
117+ let propertyWrapper = self . getPropertyWrapper ( column: column )
118+ let property = self . getPropertyDeclaration ( column: column )
114119 return " \n \t " + propertyWrapper + " \n \t " + property + " \n "
115120 } . joined ( )
116121 }
@@ -123,35 +128,17 @@ public struct FileContents {
123128 return " "
124129 }
125130
126- /// returns propertyWrapper for column
127- func getPropertyWrapper< T> ( column: T , isPrimary: Bool , isForeign: Bool ) -> String where T: ViiColumn {
128- if isPrimary {
129- return " @ID(key: \" \( column. columnName) \" ) "
130- }
131- if isForeign {
132- if column. isNullable {
133- return " @OptionalParent(key: \" \( column. columnName) \" ) "
134- }
135- return " @Parent(key: \" \( column. columnName) \" ) "
136- }
137- if SQLType . timestampable. contains ( SQLType ( column. dataType) ) {
138- return " @Timestamp(key: \" \( column. columnName) \" ) "
139- }
140- return " @Field(key: \" \( column. columnName) \" ) "
131+
132+ /// Takes a `ViiColumn` returns the property wrapper declaration
133+ /// - Parameter column: `ViiColumn`
134+ /// - returns: `String` representation of `@propertyWrapper`
135+ func getPropertyWrapper< T> ( column: T ) -> String where T: ViiColumn {
136+ return column. getPropertyWrapper ( )
141137 }
142138
143139 /// returns property declartion and optionality
144- func getPropertyDeclaration( column: Column ) -> String {
145- let dataType = SQLType ( column. dataType) . swiftType
146- let isNullable = column. isNullable ? " ? " : " "
147- return " var \( column. columnName. format ( ) . lowerCasedFirstLetter ( ) ) : \( dataType) \( isNullable) "
148- }
149-
150- func getForeignKeyPropertyDeclaration( column: ForeignKey ) -> String {
151- let isNullable = column. isNullable ? " ? " : " "
152- let formattedVar = column. columnName. format ( ) . lowerCasedFirstLetter ( )
153- let tableReference = column. constrainedTable. format ( )
154- return " var \( formattedVar) : \( tableReference) \( isNullable) "
140+ func getPropertyDeclaration< T> ( column: T ) -> String where T: ViiColumn {
141+ return column. getPropertyDeclaration ( )
155142 }
156143
157144 func getInitializer( ) -> String {
@@ -161,29 +148,22 @@ public struct FileContents {
161148 /// gets the full initializer
162149 func getFullInitializer( ) -> String {
163150 let initial = " \n \n \t init( "
164- let args = self . columns. map { col in
165- let dataType = SQLType ( col. dataType) . swiftType
166- let optionality = col. isNullable ? " ?, " : " , "
167- return " \( col. columnName. format ( ) . lowerCasedFirstLetter ( ) ) : \( dataType) \( optionality) "
151+ let args = self . columns. map { column in
152+ var propertyType = column. swiftDataType
153+ if column. isNullable {
154+ propertyType += " ? = nil "
155+ }
156+ return " \( column. swiftVariableName) : \( propertyType) , "
168157 } . joined ( )
169- let assignment = self . columns. map { col in
170- return " \n \t \t self. " + col . columnName . format ( ) . lowerCasedFirstLetter ( ) + " = " + col . columnName . format ( ) . lowerCasedFirstLetter ( )
158+ let assignment = self . columns. map { column in
159+ return " \n \t \t self. " + column . swiftVariableName + " = " + column . swiftVariableName
171160 } . joined ( )
172161 return initial + args. dropLast ( ) + " ){ " + assignment + " \n \t } "
173162 }
174163
175164 /// returns the file contents
176165 public func getFileContents( ) -> String {
177- return " import Vapor \n "
178- + imports
179- + classDeclaration
180- + schemaFormatted
181- + primaryKeyDeclarationFormatted
182- + foreignKeyDeclarationsFormatted
183- + columnDeclarationsFormatted
184- + getInitializer( )
185- + getFullInitializer( )
186- + endDeclaration
166+ return " import Fluent \n import Vapor \n \( imports) \( classDeclaration) \( schemaFormatted) \( primaryKeyDeclarationFormatted) \( foreignKeyDeclarationsFormatted) \( columnDeclarationsFormatted) \( getInitializer ( ) ) \( getFullInitializer ( ) ) \( endDeclaration) "
187167 }
188168}
189169
0 commit comments