upgrade generator to match LeStorage changes

sync3
Laurent 6 months ago
parent 54800d3859
commit c30539390b
  1. 4
      PadelClubData/Data/Club.swift
  2. 5
      PadelClubData/Data/Event.swift
  3. 8
      PadelClubData/Data/Gen/BaseClub.swift
  4. 8
      PadelClubData/Data/Gen/BaseCourt.swift
  5. 2
      PadelClubData/Data/Gen/BaseCustomUser.swift
  6. 2
      PadelClubData/Data/Gen/BaseDateInterval.swift
  7. 6
      PadelClubData/Data/Gen/BaseDrawLog.swift
  8. 10
      PadelClubData/Data/Gen/BaseEvent.swift
  9. 6
      PadelClubData/Data/Gen/BaseGroupStage.swift
  10. 6
      PadelClubData/Data/Gen/BaseMatch.swift
  11. 6
      PadelClubData/Data/Gen/BaseMatchScheduler.swift
  12. 2
      PadelClubData/Data/Gen/BaseMonthData.swift
  13. 6
      PadelClubData/Data/Gen/BasePlayerRegistration.swift
  14. 6
      PadelClubData/Data/Gen/BasePurchase.swift
  15. 8
      PadelClubData/Data/Gen/BaseRound.swift
  16. 4
      PadelClubData/Data/Gen/BaseTeamRegistration.swift
  17. 6
      PadelClubData/Data/Gen/BaseTeamScore.swift
  18. 6
      PadelClubData/Data/Gen/BaseTournament.swift
  19. 2
      PadelClubData/Data/Gen/Drawlog.json
  20. 2
      PadelClubData/Data/Gen/GroupStage.json
  21. 4
      PadelClubData/Data/Gen/Match.json
  22. 2
      PadelClubData/Data/Gen/MatchScheduler.json
  23. 2
      PadelClubData/Data/Gen/Round.json
  24. 2
      PadelClubData/Data/Gen/TeamRegistration.json
  25. 4
      PadelClubData/Data/Gen/TeamScore.json
  26. 51
      PadelClubData/Data/Gen/generator.py
  27. 15
      PadelClubDataTests/SyncDataAccessTests.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 {

@ -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 }

@ -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),
]
}
}
}

@ -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),
]
}
}
}

@ -1,4 +1,4 @@
// Generated by SwiftModelGenerator
// Generated by LeStorageGenerator
// Do not modify this file manually
import Foundation

@ -1,4 +1,4 @@
// Generated by SwiftModelGenerator
// Generated by LeStorageGenerator
// Do not modify this file manually
import Foundation

@ -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),
]
}

@ -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),
]
}

@ -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),
]
}

@ -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),
]
}

@ -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),
]
}

@ -1,4 +1,4 @@
// Generated by SwiftModelGenerator
// Generated by LeStorageGenerator
// Do not modify this file manually
import Foundation

@ -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),
]
}

@ -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),
]
}

@ -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),
]
}
}
}

@ -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),
]
}

@ -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),
]
}

@ -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),
]
}

@ -16,7 +16,7 @@
{
"name": "tournament",
"type": "String",
"foreignKey": "Tournament"
"foreignKey": "Tournament###"
},
{
"name": "drawDate",

@ -13,7 +13,7 @@
{
"name": "tournament",
"type": "String",
"foreignKey": "Tournament"
"foreignKey": "Tournament###"
},
{
"name": "index",

@ -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",

@ -15,7 +15,7 @@
{
"name": "tournament",
"type": "String",
"foreignKey": "Tournament"
"foreignKey": "Tournament###"
},
{
"name": "timeDifferenceLimit",

@ -15,7 +15,7 @@
{
"name": "tournament",
"type": "String",
"foreignKey": "Tournament"
"foreignKey": "Tournament###"
},
{
"name": "index",

@ -20,7 +20,7 @@
"name": "groupStage",
"type": "String",
"optional": true,
"foreignKey": "GroupStage*"
"foreignKey": "GroupStage"
},
{
"name": "registrationDate",

@ -15,13 +15,13 @@
{
"name": "match",
"type": "String",
"foreignKey": "Match*"
"foreignKey": "Match"
},
{
"name": "teamRegistration",
"type": "String",
"optional": true,
"foreignKey": "TeamRegistration*"
"foreignKey": "TeamRegistration"
},
{
"name": "score",

@ -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:

@ -275,9 +275,11 @@ struct SyncDataAccessTests {
// Setup
let eventColA: SyncedCollection<Event> = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection()
let clubColA: SyncedCollection<Club> = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection()
let courtsColA: SyncedCollection<Court> = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection()
let eventColB: SyncedCollection<Event> = await self.secondStoreCenter.mainStore.asyncLoadingSynchronizedCollection()
let clubColB: SyncedCollection<Club> = await self.secondStoreCenter.mainStore.asyncLoadingSynchronizedCollection()
let courtsColB: SyncedCollection<Court> = 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)
}

Loading…
Cancel
Save