Fix tournament decoding

sync_v2
Laurent 7 months ago
parent 56308b0df1
commit 433dcb18ed
  1. 14
      PadelClub/Data/Gen/BaseTournament.swift
  2. 23
      PadelClub/Data/Gen/generator.py

@ -195,6 +195,8 @@ class BaseTournament: SyncedModelObject, SyncedStorable {
}
enum CodingKeys: String, CodingKey {
case isCanceled = "isCanceled"
case payment = "payment"
case _id = "id"
case _event = "event"
case _name = "name"
@ -257,7 +259,10 @@ class BaseTournament: SyncedModelObject, SyncedStorable {
}
private static func _decodePayment(container: KeyedDecodingContainer<CodingKeys>) throws -> TournamentPayment? {
let data = try container.decodeIfPresent(Data.self, forKey: ._payment)
var data = try container.decodeIfPresent(Data.self, forKey: ._payment)
if data == nil {
data = try container.decodeIfPresent(Data.self, forKey: .payment)
}
if let data {
do {
@ -290,7 +295,10 @@ class BaseTournament: SyncedModelObject, SyncedStorable {
}
}
private static func _decodeIscanceled(container: KeyedDecodingContainer<CodingKeys>) throws -> Bool {
let data = try container.decodeIfPresent(Data.self, forKey: ._isCanceled)
var data = try container.decodeIfPresent(Data.self, forKey: ._isCanceled)
if data == nil {
data = try container.decodeIfPresent(Data.self, forKey: .isCanceled)
}
if let data {
do {
let decoded: String = try data.decryptData(pass: CryptoKey.pass.rawValue)
@ -519,4 +527,4 @@ class BaseTournament: SyncedModelObject, SyncedStorable {
]
}
}
}

@ -61,7 +61,7 @@ class SwiftModelGenerator:
lines.append("")
# CodingKeys
lines.extend(self._generate_coding_keys(properties, is_observable, is_sync))
lines.extend(self._generate_coding_keys(model_name, properties, is_observable))
lines.append("")
# Encryption methods
@ -166,16 +166,21 @@ class SwiftModelGenerator:
lines.extend([" }", ""]) # Close the method and add a blank line
return lines
def _generate_coding_keys(self, properties: List[Dict[str, Any]], is_observable: bool, is_sync: bool = False) -> List[str]:
def _generate_coding_keys(self, model_name: str, properties: List[Dict[str, Any]], is_observable: bool) -> List[str]:
lines = [" enum CodingKeys: String, CodingKey {"]
if model_name == 'Tournament':
lines.append(" case isCanceled = \"isCanceled\"")
lines.append(" case payment = \"payment\"")
for prop in properties:
name = prop['name']
# Add underscore prefix to case name if observable
case_name = f"_{name}" if is_observable else name
# Use custom codingKey if provided, otherwise use the property name
coding_key_value = prop.get("codingKey", name)
lines.append(f" case {case_name} = \"{coding_key_value}\"")
lines.append(" }")
@ -190,7 +195,10 @@ class SwiftModelGenerator:
if enc_type == "tournament_payment":
lines.extend([
f" private static func _decode{name.capitalize()}(container: KeyedDecodingContainer<CodingKeys>) throws -> TournamentPayment? {{",
f" let data = try container.decodeIfPresent(Data.self, forKey: ._{name})",
f" var data = try container.decodeIfPresent(Data.self, forKey: ._{name})",
" if data == nil {",
" data = try container.decodeIfPresent(Data.self, forKey: .payment)",
" }",
" ",
" if let data {",
" do {",
@ -226,7 +234,10 @@ class SwiftModelGenerator:
elif enc_type == "tournament_iscanceled":
lines.extend([
f" private static func _decode{name.capitalize()}(container: KeyedDecodingContainer<CodingKeys>) throws -> Bool {{",
f" let data = try container.decodeIfPresent(Data.self, forKey: ._{name})",
f" var data = try container.decodeIfPresent(Data.self, forKey: ._{name})",
" if data == nil {",
" data = try container.decodeIfPresent(Data.self, forKey: .isCanceled)",
" }",
" if let data {",
" do {",
" let decoded: String = try data.decryptData(pass: CryptoKey.pass.rawValue)",

Loading…
Cancel
Save