@ -84,6 +84,11 @@ class SwiftModelGenerator:
# Copy method
lines . extend ( self . _generate_copy_method ( model_name , properties ) )
lines . append ( " " )
# Add relationships function
lines . extend ( self . _generate_relationships ( model_name , properties ) )
lines . append ( " " )
lines . append ( " } " )
return " \n " . join ( lines )
@ -255,6 +260,7 @@ class SwiftModelGenerator:
type_name = prop [ ' type ' ]
is_optional = prop . get ( " optional " , False )
default_value = prop . get ( " defaultValue " , " nil " if is_optional else self . _get_default_value ( type_name ) )
option = prop . get ( " option " )
# Use the correct case reference based on observable flag
case_ref = f " _ { name } " if is_observable else name
@ -269,8 +275,19 @@ class SwiftModelGenerator:
elif enc_type in [ " tournament_payment " , " tournament_iscanceled " ] :
lines . append ( f " self. { name } = try Self._decode { name . capitalize ( ) } (container: container) " )
else :
lines . append ( f " self. { name } = try container.decodeIfPresent( { type_name } .self, forKey: . { case_ref } ) ?? { default_value } " )
# Handle Date with fractional option
if type_name == " Date " and option == " fractional " :
if is_optional :
lines . append ( f " if let dateString = try container.decodeIfPresent(String.self, forKey: . { case_ref } ) {{ " )
lines . append ( f " self. { name } = Date.iso8601FractionalFormatter.date(from: dateString) " )
lines . append ( " } else { " )
lines . append ( f " self. { name } = { default_value } " )
lines . append ( " } " )
else :
lines . append ( f " let dateString = try container.decode(String.self, forKey: . { case_ref } ) " )
lines . append ( f " self. { name } = Date.iso8601FractionalFormatter.date(from: dateString) ?? { default_value } " )
else :
lines . append ( f " self. { name } = try container.decodeIfPresent( { type_name } .self, forKey: . { case_ref } ) ?? { default_value } " )
lines . append ( " } " )
return lines
@ -280,7 +297,9 @@ class SwiftModelGenerator:
for prop in properties :
name = prop [ ' name ' ]
type_name = prop [ ' type ' ]
is_optional = prop . get ( ' optional ' , False )
option = prop . get ( " option " )
# Use the correct case reference based on observable flag
case_ref = f " _ { name } " if is_observable else name
@ -295,7 +314,17 @@ class SwiftModelGenerator:
elif enc_type in [ " tournament_payment " , " tournament_iscanceled " ] :
lines . append ( f " try _encode { name . capitalize ( ) } (container: &container) " )
else :
lines . append ( f " try container.encode(self. { name } , forKey: . { case_ref } ) " )
if type_name == " Date " and option == " fractional " :
if is_optional :
lines . append ( f " if let date = self. { name } {{ " )
lines . append ( f " try container.encode(Date.iso8601FractionalFormatter.string(from: date), forKey: . { case_ref } ) " )
lines . append ( " } else { " )
lines . append ( f " try container.encodeNil(forKey: . { case_ref } ) " )
lines . append ( " } " )
else :
lines . append ( f " try container.encode(Date.iso8601FractionalFormatter.string(from: self. { name } ), forKey: . { case_ref } ) " )
else :
lines . append ( f " try container.encode(self. { name } , forKey: . { case_ref } ) " )
lines . append ( " } " )
return lines
@ -325,6 +354,36 @@ class SwiftModelGenerator:
f " static func filterByStoreIdentifier() -> Bool {{ return { str ( filter_by_store ) . lower ( ) } }} "
]
def _generate_relationships ( self , model_name , properties : List [ Dict [ str , Any ] ] ) - > List [ str ] :
# Find all properties with foreign keys
foreign_key_props = [ p for p in properties if " foreignKey " in p ]
if not foreign_key_props :
# If no foreign keys, return empty array
return [
" static func relationships() -> [Relationship] { " ,
" return [] " ,
" } "
]
lines = [
" static func relationships() -> [Relationship] { " ,
" return [ "
]
# Generate relationship entries
for prop in foreign_key_props :
name = prop [ " name " ]
foreign_key = prop [ " foreignKey " ] . rstrip ( ' * ' ) # Remove asterisk if present
lines . append ( f " Relationship(type: { foreign_key } .self, keyPath: \\ Base { model_name } . { name } ), " )
# Close the array and function
lines . extend ( [
" ] " ,
" } "
] )
return lines
def _get_default_value ( self , type_name : str ) - > str :
""" Get default value for non-optional types """