From c30539390bff3f209c60f560cee2718aee2d0bdd Mon Sep 17 00:00:00 2001 From: Laurent Date: Wed, 14 May 2025 15:31:11 +0200 Subject: [PATCH] upgrade generator to match LeStorage changes --- PadelClubData/Data/Club.swift | 4 ++ PadelClubData/Data/Event.swift | 5 ++ PadelClubData/Data/Gen/BaseClub.swift | 8 +-- PadelClubData/Data/Gen/BaseCourt.swift | 8 +-- PadelClubData/Data/Gen/BaseCustomUser.swift | 2 +- PadelClubData/Data/Gen/BaseDateInterval.swift | 2 +- PadelClubData/Data/Gen/BaseDrawLog.swift | 6 +-- PadelClubData/Data/Gen/BaseEvent.swift | 10 ++-- PadelClubData/Data/Gen/BaseGroupStage.swift | 6 +-- PadelClubData/Data/Gen/BaseMatch.swift | 6 +-- .../Data/Gen/BaseMatchScheduler.swift | 6 +-- PadelClubData/Data/Gen/BaseMonthData.swift | 2 +- .../Data/Gen/BasePlayerRegistration.swift | 6 +-- PadelClubData/Data/Gen/BasePurchase.swift | 6 +-- PadelClubData/Data/Gen/BaseRound.swift | 8 +-- .../Data/Gen/BaseTeamRegistration.swift | 4 +- PadelClubData/Data/Gen/BaseTeamScore.swift | 6 +-- PadelClubData/Data/Gen/BaseTournament.swift | 6 +-- PadelClubData/Data/Gen/Drawlog.json | 2 +- PadelClubData/Data/Gen/GroupStage.json | 2 +- PadelClubData/Data/Gen/Match.json | 4 +- PadelClubData/Data/Gen/MatchScheduler.json | 2 +- PadelClubData/Data/Gen/Round.json | 2 +- PadelClubData/Data/Gen/TeamRegistration.json | 2 +- PadelClubData/Data/Gen/TeamScore.json | 4 +- PadelClubData/Data/Gen/generator.py | 51 +++++++++++++++---- PadelClubDataTests/SyncDataAccessTests.swift | 15 +++--- 27 files changed, 114 insertions(+), 71 deletions(-) diff --git a/PadelClubData/Data/Club.swift b/PadelClubData/Data/Club.swift index 3c6a901..c26daf1 100644 --- a/PadelClubData/Data/Club.swift +++ b/PadelClubData/Data/Club.swift @@ -40,6 +40,10 @@ final public class Club: BaseClub { // DataStore.shared.courts.deleteDependencies(customizedCourts, shouldBeSynchronized: shouldBeSynchronized) } + public override func deleteUnusedSharedDependencies(store: Store) { + store.deleteUnusedSharedDependencies(type: Court.self, shouldBeSynchronized: false) { $0.club == self.id } + } + } extension Club { diff --git a/PadelClubData/Data/Event.swift b/PadelClubData/Data/Event.swift index 855710a..b86bbd9 100644 --- a/PadelClubData/Data/Event.swift +++ b/PadelClubData/Data/Event.swift @@ -25,6 +25,11 @@ final public class Event: BaseEvent { super.init() } + public override func deleteUnusedSharedDependencies(store: Store) { + store.deleteUnusedSharedDependencies(type: Tournament.self, shouldBeSynchronized: false) { $0.event == self.id && $0.isDeleted == false } + store.deleteUnusedSharedDependencies(type: DateInterval.self, shouldBeSynchronized: false) { $0.event == self.id } + } + public override func deleteDependencies(store: Store, shouldBeSynchronized: Bool) { store.deleteDependencies(type: Tournament.self, shouldBeSynchronized: shouldBeSynchronized) { $0.event == self.id && $0.isDeleted == false } diff --git a/PadelClubData/Data/Gen/BaseClub.swift b/PadelClubData/Data/Gen/BaseClub.swift index 5262305..b087195 100644 --- a/PadelClubData/Data/Gen/BaseClub.swift +++ b/PadelClubData/Data/Gen/BaseClub.swift @@ -1,4 +1,4 @@ -// Generated by SwiftModelGenerator +// Generated by LeStorageGenerator // Do not modify this file manually import Foundation @@ -120,7 +120,7 @@ public class BaseClub: SyncedModelObject, SyncedStorable { func creatorValue() -> CustomUser? { guard let creator = self.creator else { return nil } - return Store.main.findById(creator) + return self.store?.findById(creator) } public func copy(from other: any Storable) { @@ -143,8 +143,8 @@ public class BaseClub: SyncedModelObject, SyncedStorable { public static func relationships() -> [Relationship] { return [ - Relationship(type: CustomUser.self, keyPath: \BaseClub.creator), + Relationship(type: CustomUser.self, keyPath: \BaseClub.creator, mainStoreLookup: false), ] } -} \ No newline at end of file +} diff --git a/PadelClubData/Data/Gen/BaseCourt.swift b/PadelClubData/Data/Gen/BaseCourt.swift index e4ca668..71b7ea8 100644 --- a/PadelClubData/Data/Gen/BaseCourt.swift +++ b/PadelClubData/Data/Gen/BaseCourt.swift @@ -1,4 +1,4 @@ -// Generated by SwiftModelGenerator +// Generated by LeStorageGenerator // Do not modify this file manually import Foundation @@ -71,7 +71,7 @@ public class BaseCourt: SyncedModelObject, SyncedStorable { } func clubValue() -> Club? { - return Store.main.findById(club) + return self.store?.findById(club) } public func copy(from other: any Storable) { @@ -86,8 +86,8 @@ public class BaseCourt: SyncedModelObject, SyncedStorable { public static func relationships() -> [Relationship] { return [ - Relationship(type: Club.self, keyPath: \BaseCourt.club), + Relationship(type: Club.self, keyPath: \BaseCourt.club, mainStoreLookup: false), ] } -} \ No newline at end of file +} diff --git a/PadelClubData/Data/Gen/BaseCustomUser.swift b/PadelClubData/Data/Gen/BaseCustomUser.swift index 5abcf9e..d042c7f 100644 --- a/PadelClubData/Data/Gen/BaseCustomUser.swift +++ b/PadelClubData/Data/Gen/BaseCustomUser.swift @@ -1,4 +1,4 @@ -// Generated by SwiftModelGenerator +// Generated by LeStorageGenerator // Do not modify this file manually import Foundation diff --git a/PadelClubData/Data/Gen/BaseDateInterval.swift b/PadelClubData/Data/Gen/BaseDateInterval.swift index 79e788c..81ec179 100644 --- a/PadelClubData/Data/Gen/BaseDateInterval.swift +++ b/PadelClubData/Data/Gen/BaseDateInterval.swift @@ -1,4 +1,4 @@ -// Generated by SwiftModelGenerator +// Generated by LeStorageGenerator // Do not modify this file manually import Foundation diff --git a/PadelClubData/Data/Gen/BaseDrawLog.swift b/PadelClubData/Data/Gen/BaseDrawLog.swift index 12f0a12..e6d8b82 100644 --- a/PadelClubData/Data/Gen/BaseDrawLog.swift +++ b/PadelClubData/Data/Gen/BaseDrawLog.swift @@ -1,4 +1,4 @@ -// Generated by SwiftModelGenerator +// Generated by LeStorageGenerator // Do not modify this file manually import Foundation @@ -77,7 +77,7 @@ public class BaseDrawLog: SyncedModelObject, SyncedStorable { } func tournamentValue() -> Tournament? { - return Store.main.findById(tournament) + return self.store?.storeCenter.mainStore.findById(tournament) } public func copy(from other: any Storable) { @@ -93,7 +93,7 @@ public class BaseDrawLog: SyncedModelObject, SyncedStorable { public static func relationships() -> [Relationship] { return [ - Relationship(type: Tournament.self, keyPath: \BaseDrawLog.tournament), + Relationship(type: Tournament.self, keyPath: \BaseDrawLog.tournament, mainStoreLookup: true), ] } diff --git a/PadelClubData/Data/Gen/BaseEvent.swift b/PadelClubData/Data/Gen/BaseEvent.swift index 0d2c634..c51ca02 100644 --- a/PadelClubData/Data/Gen/BaseEvent.swift +++ b/PadelClubData/Data/Gen/BaseEvent.swift @@ -1,4 +1,4 @@ -// Generated by SwiftModelGenerator +// Generated by LeStorageGenerator // Do not modify this file manually import Foundation @@ -72,12 +72,12 @@ public class BaseEvent: SyncedModelObject, SyncedStorable { func creatorValue() -> CustomUser? { guard let creator = self.creator else { return nil } - return Store.main.findById(creator) + return self.store?.findById(creator) } func clubValue() -> Club? { guard let club = self.club else { return nil } - return Store.main.findById(club) + return self.store?.findById(club) } public func copy(from other: any Storable) { @@ -92,8 +92,8 @@ public class BaseEvent: SyncedModelObject, SyncedStorable { public static func relationships() -> [Relationship] { return [ - Relationship(type: CustomUser.self, keyPath: \BaseEvent.creator), - Relationship(type: Club.self, keyPath: \BaseEvent.club), + Relationship(type: CustomUser.self, keyPath: \BaseEvent.creator, mainStoreLookup: false), + Relationship(type: Club.self, keyPath: \BaseEvent.club, mainStoreLookup: false), ] } diff --git a/PadelClubData/Data/Gen/BaseGroupStage.swift b/PadelClubData/Data/Gen/BaseGroupStage.swift index 8f82856..d0c3804 100644 --- a/PadelClubData/Data/Gen/BaseGroupStage.swift +++ b/PadelClubData/Data/Gen/BaseGroupStage.swift @@ -1,4 +1,4 @@ -// Generated by SwiftModelGenerator +// Generated by LeStorageGenerator // Do not modify this file manually import Foundation @@ -95,7 +95,7 @@ public class BaseGroupStage: SyncedModelObject, SyncedStorable { } func tournamentValue() -> Tournament? { - return Store.main.findById(tournament) + return self.store?.storeCenter.mainStore.findById(tournament) } public func copy(from other: any Storable) { @@ -113,7 +113,7 @@ public class BaseGroupStage: SyncedModelObject, SyncedStorable { public static func relationships() -> [Relationship] { return [ - Relationship(type: Tournament.self, keyPath: \BaseGroupStage.tournament), + Relationship(type: Tournament.self, keyPath: \BaseGroupStage.tournament, mainStoreLookup: true), ] } diff --git a/PadelClubData/Data/Gen/BaseMatch.swift b/PadelClubData/Data/Gen/BaseMatch.swift index a558a8b..b1fef96 100644 --- a/PadelClubData/Data/Gen/BaseMatch.swift +++ b/PadelClubData/Data/Gen/BaseMatch.swift @@ -1,4 +1,4 @@ -// Generated by SwiftModelGenerator +// Generated by LeStorageGenerator // Do not modify this file manually import Foundation @@ -161,8 +161,8 @@ public class BaseMatch: SyncedModelObject, SyncedStorable { public static func relationships() -> [Relationship] { return [ - Relationship(type: Round.self, keyPath: \BaseMatch.round), - Relationship(type: GroupStage.self, keyPath: \BaseMatch.groupStage), + Relationship(type: Round.self, keyPath: \BaseMatch.round, mainStoreLookup: false), + Relationship(type: GroupStage.self, keyPath: \BaseMatch.groupStage, mainStoreLookup: false), ] } diff --git a/PadelClubData/Data/Gen/BaseMatchScheduler.swift b/PadelClubData/Data/Gen/BaseMatchScheduler.swift index 8e86c9d..d906d97 100644 --- a/PadelClubData/Data/Gen/BaseMatchScheduler.swift +++ b/PadelClubData/Data/Gen/BaseMatchScheduler.swift @@ -1,4 +1,4 @@ -// Generated by SwiftModelGenerator +// Generated by LeStorageGenerator // Do not modify this file manually import Foundation @@ -131,7 +131,7 @@ public class BaseMatchScheduler: BaseModelObject, Storable { } func tournamentValue() -> Tournament? { - return Store.main.findById(tournament) + return self.store?.storeCenter.mainStore.findById(tournament) } public func copy(from other: any Storable) { @@ -156,7 +156,7 @@ public class BaseMatchScheduler: BaseModelObject, Storable { public static func relationships() -> [Relationship] { return [ - Relationship(type: Tournament.self, keyPath: \BaseMatchScheduler.tournament), + Relationship(type: Tournament.self, keyPath: \BaseMatchScheduler.tournament, mainStoreLookup: true), ] } diff --git a/PadelClubData/Data/Gen/BaseMonthData.swift b/PadelClubData/Data/Gen/BaseMonthData.swift index 9ffe47e..08eac25 100644 --- a/PadelClubData/Data/Gen/BaseMonthData.swift +++ b/PadelClubData/Data/Gen/BaseMonthData.swift @@ -1,4 +1,4 @@ -// Generated by SwiftModelGenerator +// Generated by LeStorageGenerator // Do not modify this file manually import Foundation diff --git a/PadelClubData/Data/Gen/BasePlayerRegistration.swift b/PadelClubData/Data/Gen/BasePlayerRegistration.swift index 9cdfcef..ed622c6 100644 --- a/PadelClubData/Data/Gen/BasePlayerRegistration.swift +++ b/PadelClubData/Data/Gen/BasePlayerRegistration.swift @@ -1,4 +1,4 @@ -// Generated by SwiftModelGenerator +// Generated by LeStorageGenerator // Do not modify this file manually import Foundation @@ -186,7 +186,7 @@ public class BasePlayerRegistration: SyncedModelObject, SyncedStorable { func teamRegistrationValue() -> TeamRegistration? { guard let teamRegistration = self.teamRegistration else { return nil } - return Store.main.findById(teamRegistration) + return self.store?.findById(teamRegistration) } public func copy(from other: any Storable) { @@ -220,7 +220,7 @@ public class BasePlayerRegistration: SyncedModelObject, SyncedStorable { public static func relationships() -> [Relationship] { return [ - Relationship(type: TeamRegistration.self, keyPath: \BasePlayerRegistration.teamRegistration), + Relationship(type: TeamRegistration.self, keyPath: \BasePlayerRegistration.teamRegistration, mainStoreLookup: false), ] } diff --git a/PadelClubData/Data/Gen/BasePurchase.swift b/PadelClubData/Data/Gen/BasePurchase.swift index c1f3736..be39a8c 100644 --- a/PadelClubData/Data/Gen/BasePurchase.swift +++ b/PadelClubData/Data/Gen/BasePurchase.swift @@ -1,4 +1,4 @@ -// Generated by SwiftModelGenerator +// Generated by LeStorageGenerator // Do not modify this file manually import Foundation @@ -76,7 +76,7 @@ public class BasePurchase: SyncedModelObject, SyncedStorable { } func userValue() -> CustomUser? { - return Store.main.findById(user) + return self.store?.findById(user) } public func copy(from other: any Storable) { @@ -92,7 +92,7 @@ public class BasePurchase: SyncedModelObject, SyncedStorable { public static func relationships() -> [Relationship] { return [ - Relationship(type: CustomUser.self, keyPath: \BasePurchase.user), + Relationship(type: CustomUser.self, keyPath: \BasePurchase.user, mainStoreLookup: false), ] } diff --git a/PadelClubData/Data/Gen/BaseRound.swift b/PadelClubData/Data/Gen/BaseRound.swift index 398283e..d48a785 100644 --- a/PadelClubData/Data/Gen/BaseRound.swift +++ b/PadelClubData/Data/Gen/BaseRound.swift @@ -1,4 +1,4 @@ -// Generated by SwiftModelGenerator +// Generated by LeStorageGenerator // Do not modify this file manually import Foundation @@ -95,7 +95,7 @@ public class BaseRound: SyncedModelObject, SyncedStorable { } func tournamentValue() -> Tournament? { - return Store.main.findById(tournament) + return self.store?.storeCenter.mainStore.findById(tournament) } public func copy(from other: any Storable) { @@ -113,8 +113,8 @@ public class BaseRound: SyncedModelObject, SyncedStorable { public static func relationships() -> [Relationship] { return [ - Relationship(type: Tournament.self, keyPath: \BaseRound.tournament), + Relationship(type: Tournament.self, keyPath: \BaseRound.tournament, mainStoreLookup: true), ] } -} +} \ No newline at end of file diff --git a/PadelClubData/Data/Gen/BaseTeamRegistration.swift b/PadelClubData/Data/Gen/BaseTeamRegistration.swift index b98b267..6c7e68e 100644 --- a/PadelClubData/Data/Gen/BaseTeamRegistration.swift +++ b/PadelClubData/Data/Gen/BaseTeamRegistration.swift @@ -1,4 +1,4 @@ -// Generated by SwiftModelGenerator +// Generated by LeStorageGenerator // Do not modify this file manually import Foundation @@ -192,7 +192,7 @@ public class BaseTeamRegistration: SyncedModelObject, SyncedStorable { public static func relationships() -> [Relationship] { return [ - Relationship(type: GroupStage.self, keyPath: \BaseTeamRegistration.groupStage), + Relationship(type: GroupStage.self, keyPath: \BaseTeamRegistration.groupStage, mainStoreLookup: false), ] } diff --git a/PadelClubData/Data/Gen/BaseTeamScore.swift b/PadelClubData/Data/Gen/BaseTeamScore.swift index 025495c..e2ea941 100644 --- a/PadelClubData/Data/Gen/BaseTeamScore.swift +++ b/PadelClubData/Data/Gen/BaseTeamScore.swift @@ -1,4 +1,4 @@ -// Generated by SwiftModelGenerator +// Generated by LeStorageGenerator // Do not modify this file manually import Foundation @@ -91,8 +91,8 @@ public class BaseTeamScore: SyncedModelObject, SyncedStorable { public static func relationships() -> [Relationship] { return [ - Relationship(type: Match.self, keyPath: \BaseTeamScore.match), - Relationship(type: TeamRegistration.self, keyPath: \BaseTeamScore.teamRegistration), + Relationship(type: Match.self, keyPath: \BaseTeamScore.match, mainStoreLookup: false), + Relationship(type: TeamRegistration.self, keyPath: \BaseTeamScore.teamRegistration, mainStoreLookup: false), ] } diff --git a/PadelClubData/Data/Gen/BaseTournament.swift b/PadelClubData/Data/Gen/BaseTournament.swift index d837d1e..ecb76dc 100644 --- a/PadelClubData/Data/Gen/BaseTournament.swift +++ b/PadelClubData/Data/Gen/BaseTournament.swift @@ -1,4 +1,4 @@ -// Generated by SwiftModelGenerator +// Generated by LeStorageGenerator // Do not modify this file manually import Foundation @@ -519,7 +519,7 @@ public class BaseTournament: SyncedModelObject, SyncedStorable { func eventValue() -> Event? { guard let event = self.event else { return nil } - return Store.main.findById(event) + return self.store?.findById(event) } public func copy(from other: any Storable) { @@ -597,7 +597,7 @@ public class BaseTournament: SyncedModelObject, SyncedStorable { public static func relationships() -> [Relationship] { return [ - Relationship(type: Event.self, keyPath: \BaseTournament.event), + Relationship(type: Event.self, keyPath: \BaseTournament.event, mainStoreLookup: false), ] } diff --git a/PadelClubData/Data/Gen/Drawlog.json b/PadelClubData/Data/Gen/Drawlog.json index 8f872d9..adc374a 100644 --- a/PadelClubData/Data/Gen/Drawlog.json +++ b/PadelClubData/Data/Gen/Drawlog.json @@ -16,7 +16,7 @@ { "name": "tournament", "type": "String", - "foreignKey": "Tournament" + "foreignKey": "Tournament###" }, { "name": "drawDate", diff --git a/PadelClubData/Data/Gen/GroupStage.json b/PadelClubData/Data/Gen/GroupStage.json index b6f73b9..693fcfe 100644 --- a/PadelClubData/Data/Gen/GroupStage.json +++ b/PadelClubData/Data/Gen/GroupStage.json @@ -13,7 +13,7 @@ { "name": "tournament", "type": "String", - "foreignKey": "Tournament" + "foreignKey": "Tournament###" }, { "name": "index", diff --git a/PadelClubData/Data/Gen/Match.json b/PadelClubData/Data/Gen/Match.json index ff595c0..c106507 100644 --- a/PadelClubData/Data/Gen/Match.json +++ b/PadelClubData/Data/Gen/Match.json @@ -15,13 +15,13 @@ "name": "round", "type": "String", "optional": true, - "foreignKey": "Round*" + "foreignKey": "Round" }, { "name": "groupStage", "type": "String", "optional": true, - "foreignKey": "GroupStage*" + "foreignKey": "GroupStage" }, { "name": "startDate", diff --git a/PadelClubData/Data/Gen/MatchScheduler.json b/PadelClubData/Data/Gen/MatchScheduler.json index 132bb36..0ec9c77 100644 --- a/PadelClubData/Data/Gen/MatchScheduler.json +++ b/PadelClubData/Data/Gen/MatchScheduler.json @@ -15,7 +15,7 @@ { "name": "tournament", "type": "String", - "foreignKey": "Tournament" + "foreignKey": "Tournament###" }, { "name": "timeDifferenceLimit", diff --git a/PadelClubData/Data/Gen/Round.json b/PadelClubData/Data/Gen/Round.json index 12e734c..e26637f 100644 --- a/PadelClubData/Data/Gen/Round.json +++ b/PadelClubData/Data/Gen/Round.json @@ -15,7 +15,7 @@ { "name": "tournament", "type": "String", - "foreignKey": "Tournament" + "foreignKey": "Tournament###" }, { "name": "index", diff --git a/PadelClubData/Data/Gen/TeamRegistration.json b/PadelClubData/Data/Gen/TeamRegistration.json index 995d08e..572cf91 100644 --- a/PadelClubData/Data/Gen/TeamRegistration.json +++ b/PadelClubData/Data/Gen/TeamRegistration.json @@ -20,7 +20,7 @@ "name": "groupStage", "type": "String", "optional": true, - "foreignKey": "GroupStage*" + "foreignKey": "GroupStage" }, { "name": "registrationDate", diff --git a/PadelClubData/Data/Gen/TeamScore.json b/PadelClubData/Data/Gen/TeamScore.json index 9165c44..922d534 100644 --- a/PadelClubData/Data/Gen/TeamScore.json +++ b/PadelClubData/Data/Gen/TeamScore.json @@ -15,13 +15,13 @@ { "name": "match", "type": "String", - "foreignKey": "Match*" + "foreignKey": "Match" }, { "name": "teamRegistration", "type": "String", "optional": true, - "foreignKey": "TeamRegistration*" + "foreignKey": "TeamRegistration" }, { "name": "score", diff --git a/PadelClubData/Data/Gen/generator.py b/PadelClubData/Data/Gen/generator.py index 2335428..0b71cc2 100644 --- a/PadelClubData/Data/Gen/generator.py +++ b/PadelClubData/Data/Gen/generator.py @@ -9,7 +9,7 @@ import logging from datetime import datetime import inflect -class SwiftModelGenerator: +class LeStorageGenerator: def __init__(self, json_data: Dict[str, Any]): self.data = json_data self.pluralizer = inflect.engine() @@ -27,7 +27,7 @@ class SwiftModelGenerator: token_exempted = model_data.get("tokenExemptedMethods", []) copy_server_response = model_data.get("copy_server_response", "false") - lines = ["// Generated by SwiftModelGenerator", "// Do not modify this file manually", ""] + lines = ["// Generated by LeStorageGenerator", "// Do not modify this file manually", ""] # Import statement lines.append("import Foundation") @@ -102,6 +102,11 @@ class SwiftModelGenerator: lines.extend(self._generate_copy_method(model_name, properties)) lines.append("") + # Copy method +# if is_sync: +# lines.extend(self._generate_copy_for_update_method(model_name, properties)) +# lines.append("") + # Add relationships function lines.extend(self._generate_relationships(model_name, properties)) lines.append("") @@ -178,21 +183,21 @@ class SwiftModelGenerator: method_name = f"{prop_name}Value" is_optional = prop.get("optional", False) - lines.extend([f" func {method_name}() -> {foreign_key.rstrip('*')}? {{"]) + lines.extend([f" func {method_name}() -> {foreign_key.rstrip('###')}? {{"]) if is_optional: lines.extend([ f" guard let {prop_name} = self.{prop_name} else {{ return nil }}" ]) - if foreign_key.endswith("*"): + if foreign_key.endswith("###"): foreign_key = foreign_key[:-1] # Remove the asterisk lines.extend([ - f" return self.store?.findById({prop_name})" + f" return self.store?.storeCenter.mainStore.findById({prop_name})" ]) else: lines.extend([ - f" return Store.main.findById({prop_name})" + f" return self.store?.findById({prop_name})" ]) lines.extend([" }", ""]) # Close the method and add a blank line @@ -393,6 +398,31 @@ class SwiftModelGenerator: lines.append(" }") return lines + def _generate_copy_for_update_method(self, model_name: str, properties: List[Dict[str, Any]]) -> List[str]: + model_variable = model_name.lower() + lines = [f" public func copyForUpdate(from other: any Storable) {{"] + lines.append(f" guard let {model_variable} = other as? Base{model_name} else {{ return }}") + + # First handle foreign key properties with special deletion logic + for prop in properties: + if "foreignKey" in prop and prop["foreignKey"] != "CustomUser": + name = prop["name"] + foreign_key = prop["foreignKey"].rstrip('###') # Remove asterisk if present + foreign_variable = name + "Value" + + # Generate the foreign key check and deletion logic + lines.append(f" if {model_variable}.{name} != self.{name}, let {name}Object = self.{foreign_variable}(), {name}Object.shared == true, let store = {name}Object.store {{") + lines.append(f" store.deleteUnusedShared({name}Object)") + lines.append(f" }}") + + # Then copy all properties + for prop in properties: + name = prop['name'] + lines.append(f" self.{name} = {model_variable}.{name}") + + lines.append(" }") + return lines + def _generate_protocol_requirements(self, resource_name: str, token_exempted: List[str], copy_server_response: str) -> List[str]: """Generate the static functions required by SyncedStorable protocol.""" # Convert HTTP methods to proper format @@ -426,8 +456,9 @@ class SwiftModelGenerator: # 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}),") + located_on_main_store = "true" if prop["foreignKey"].endswith('###') else "false" + foreign_key = prop["foreignKey"].rstrip('###') # Remove asterisk if present + lines.append(f" Relationship(type: {foreign_key}.self, keyPath: \\Base{model_name}.{name}, mainStoreLookup: {located_on_main_store}),") # Close the array and function lines.extend([ @@ -488,7 +519,7 @@ def process_directory(input_dir: str, output_dir: str, logger: logging.Logger, d with open(json_file, 'r') as f: json_data = json.load(f) - generator = SwiftModelGenerator(json_data) + generator = LeStorageGenerator(json_data) # Generate each model in the JSON file for model in json_data["models"]: @@ -524,7 +555,7 @@ def process_directory(input_dir: str, output_dir: str, logger: logging.Logger, d def setup_logging(verbose: bool) -> logging.Logger: """Configure logging based on verbosity level.""" - logger = logging.getLogger('SwiftModelGenerator') + logger = logging.getLogger('LeStorageGenerator') handler = logging.StreamHandler() if verbose: diff --git a/PadelClubDataTests/SyncDataAccessTests.swift b/PadelClubDataTests/SyncDataAccessTests.swift index c605dbc..17eb576 100644 --- a/PadelClubDataTests/SyncDataAccessTests.swift +++ b/PadelClubDataTests/SyncDataAccessTests.swift @@ -275,9 +275,11 @@ struct SyncDataAccessTests { // Setup let eventColA: SyncedCollection = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection() let clubColA: SyncedCollection = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection() + let courtsColA: SyncedCollection = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection() let eventColB: SyncedCollection = await self.secondStoreCenter.mainStore.asyncLoadingSynchronizedCollection() let clubColB: SyncedCollection = await self.secondStoreCenter.mainStore.asyncLoadingSynchronizedCollection() - + let courtsColB: SyncedCollection = await self.secondStoreCenter.mainStore.asyncLoadingSynchronizedCollection() + if let dataAccessCollection = StoreCenter.main.dataAccessCollection { try await dataAccessCollection.deleteAsync(contentOfs: Array(dataAccessCollection)) } @@ -297,6 +299,10 @@ struct SyncDataAccessTests { let eventA = Event(creator: userId1, club: clubA.id, name: "event 1") try await eventColA.addOrUpdateAsync(instance: eventA) + let court1A = Court(index: 0, club: clubA.id) + let court2A = Court(index: 1, club: clubA.id) + try await courtsColA.addOrUpdateAsync(contentOfs: [court1A, court2A]) + // Share with user2 try await StoreCenter.main.setAuthorizedUsersAsync(for: eventA, users: [userId2]) let _ = try await self.secondStoreCenter.testSynchronizeOnceAsync() @@ -313,14 +319,11 @@ struct SyncDataAccessTests { eventB.club = club2B.id try await eventColB.addOrUpdateAsync(instance: eventB) - let dataA = try await StoreCenter.main.testSynchronizeOnceAsync() - let syncDataA = try SyncData(data: dataA, storeCenter: StoreCenter.main) - -// #expect(syncDataA.sharedRelationshipSets.count == 1) -// #expect(syncDataA.sharedRelationshipRemovals.count == 1) + let _ = try await StoreCenter.main.testSynchronizeOnceAsync() #expect(eventA.club == club2B.id) #expect(clubColB.count == 1) + #expect(courtsColB.count == 0) }