Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : Groupe Ka — app iOS native7// File : Shared/Souple.swift8// Module : Utilities9// Repository : https://git.spboucher.ai/groupe-ka-ios10// Project Website : Not specified11// Created : 2026-12-0312// Last Modified : 2026-12-0313//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation4243// Décodage tolérant : l'API dit que toute section est facultative et qu'un site ne publie que ce qu'il mesure.44// Un champ absent, nul ou d'un type inattendu donne `nil` (ou une liste vide) ; il ne fait jamais échouer la réponse.45// Rien n'est inventé à la place : une valeur manquante reste absente jusqu'à l'écran.4647/// Champ facultatif : absent, `null` ou mal typé → `nil`.48@propertyWrapper49struct Souple<T: Decodable & Sendable>: Decodable, Sendable {50 var wrappedValue: T?51 init(wrappedValue: T?) { self.wrappedValue = wrappedValue }52 init(from decoder: Decoder) throws { wrappedValue = try? T(from: decoder) }53}5455/// Liste facultative : absente → vide ; un élément illisible est sauté, les autres restent.56@propertyWrapper57struct ListeSouple<T: Decodable & Sendable>: Decodable, Sendable {58 var wrappedValue: [T]59 init(wrappedValue: [T]) { self.wrappedValue = wrappedValue }60 init(from decoder: Decoder) throws {61 guard var c = try? decoder.unkeyedContainer() else { wrappedValue = []; return }62 var elements: [T] = []63 while !c.isAtEnd {64 if let e = try? c.decode(T.self) { elements.append(e) } else { _ = try? c.decode(Rien.self) }65 }66 wrappedValue = elements67 }6869 private struct Rien: Decodable { init(from decoder: Decoder) throws {} }70}7172extension KeyedDecodingContainer {73 func decode<T>(_ type: Souple<T>.Type, forKey key: Key) throws -> Souple<T> {74 (try? decodeIfPresent(type, forKey: key)) ?? Souple(wrappedValue: nil)75 }7677 func decode<T>(_ type: ListeSouple<T>.Type, forKey key: Key) throws -> ListeSouple<T> {78 (try? decodeIfPresent(type, forKey: key)) ?? ListeSouple(wrappedValue: [])79 }80}8182/// Valeur JSON quelconque : cellules de tableau, valeur d'un indicateur (nombre ou texte), attributs d'une annonce.83enum JSONValeur: Decodable, Sendable, Hashable {84 case nul85 case bool(Bool)86 case nombre(Double)87 case texte(String)88 case liste([JSONValeur])89 case objet([String: JSONValeur])9091 init(from decoder: Decoder) throws {92 let c = try decoder.singleValueContainer()93 if c.decodeNil() { self = .nul }94 else if let b = try? c.decode(Bool.self) { self = .bool(b) }95 else if let n = try? c.decode(Double.self) { self = .nombre(n) }96 else if let s = try? c.decode(String.self) { self = .texte(s) }97 else if let l = try? c.decode([JSONValeur].self) { self = .liste(l) }98 else if let o = try? c.decode([String: JSONValeur].self) { self = .objet(o) }99 else { self = .nul }100 }101102 var nombre: Double? { if case .nombre(let n) = self { n } else { nil } }103 var chaine: String? { if case .texte(let s) = self { s } else { nil } }104 var estNul: Bool { if case .nul = self { true } else { false } }105 subscript(cle: String) -> JSONValeur? { if case .objet(let o) = self { o[cle] } else { nil } }106107 /// lecture d'un JSON quelconque (console, réponses brutes)108 static func lire(_ donnees: Data) -> JSONValeur? { try? JSONDecoder().decode(JSONValeur.self, from: donnees) }109}110