Advertisement
Larme

Untitled

Feb 19th, 2023
960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 7.66 KB | None | 0 0
  1. func mergingModels()  {
  2.     struct FirstModel: Codable, CustomStringConvertible, Equatable {
  3.         var id: Int?
  4.         var date: String?
  5.         var name: String?
  6.  
  7.         var description: String {
  8.             return "FirstModel(id: \(id!), date: \(date!), name: \(name!)]"
  9.         }
  10.         static func ==(lhs: FirstModel, rhs: FirstModel) -> Bool {
  11.             return lhs.id == rhs.id && lhs.date == rhs.date && lhs.name == rhs.name
  12.         }
  13.     }
  14.  
  15.     struct SecondModel: Codable, CustomStringConvertible, Equatable {
  16.         var id: Int?
  17.         var date: String?
  18.         var age: Int?
  19.         var position: String?
  20.  
  21.         var description: String {
  22.             return "SecondModel(id: \(id!), date: \(date!), age: \(age!), position: \(position!)]"
  23.         }
  24.  
  25.         static func ==(lhs: SecondModel, rhs: SecondModel) -> Bool {
  26.             return lhs.id == rhs.id && lhs.date == rhs.date && lhs.age == rhs.age && lhs.position == rhs.position
  27.         }
  28.     }
  29.  
  30.     struct FinalModel: Codable, CustomStringConvertible, Equatable {
  31.         var first:[FirstModel]?
  32.         var second: SecondModel?
  33.         var description: String {
  34.             return "FinalModel(first: \(first)), second: \(second))]"
  35.         }
  36.  
  37.         static func ==(lhs: FinalModel, rhs: FinalModel) -> Bool {
  38.             return lhs.first == rhs.first && lhs.second == rhs.second
  39.         }
  40.     }
  41.  
  42.     // dict1: [date: [FirstModel]]
  43.     var dict1 = [
  44.         "2023-02-19": [FirstModel(id: 10, date: "2023-02-19", name: "Harry"),
  45.                        FirstModel(id: 11, date: "2023-02-19", name: "John")],
  46.         "2023-02-13": [FirstModel(id: 12, date: "2023-02-13", name: "Harry"),
  47.                        FirstModel(id: 11, date: "2023-02-13", name: "John")],
  48.         "2023-02-12": [FirstModel(id: 13, date: "2023-02-12", name: "Harry"),
  49.                        FirstModel(id: 11, date: "2023-02-12", name: "John")],
  50.         "2023-02-10": [FirstModel(id: 14, date: "2023-02-10", name: "Harry"),
  51.                        FirstModel(id: 11, date: "2023-02-10", name: "John")],
  52.     ]
  53.  
  54.     // dict1: [date: [SecondModel]]
  55.     var dict2 = [
  56.         "2023-02-19": [SecondModel(id: 10, date: "2023-02-19", age: 12, position: "A"),
  57.                        SecondModel(id: 15, date: "2023-02-19", age: 12, position: "A")],
  58.         "2023-02-09": [SecondModel(id: 20, date: "2023-02-09", age: 12, position: "A"),
  59.                        SecondModel(id: 17, date: "2023-02-09", age: 12, position: "A")],
  60.         "2023-02-10": [SecondModel(id: 14, date: "2023-02-10", age: 12, position: "A"),
  61.                        SecondModel(id: 16, date: "2023-02-10", age: 12, position: "A")],
  62.         "2023-02-12": [SecondModel(id: 27, date: "2023-02-12", age: 12, position: "A"),
  63.                        SecondModel(id: 11, date: "2023-02-12", age: 12, position: "A")],
  64.         "2023-02-08": [SecondModel(id: 22, date: "2023-02-08", age: 12, position: "A"),
  65.                        SecondModel(id: 11, date: "2023-02-08", age: 12, position: "A")]
  66.     ]
  67.  
  68.     // newDict: [date: [id: [FinalModel]]]
  69.     var exptectation = [
  70.         "2023-02-08": [
  71.             11: [FinalModel(first: nil,
  72.                             second: SecondModel(id: 11, date: "2023-02-08", age: 12, position: "A"))],
  73.             22: [FinalModel(first: nil,
  74.                             second: SecondModel(id: 22, date: "2023-02-08", age: 12, position: "A"))]
  75.         ],
  76.         "2023-02-09": [
  77.             17: [FinalModel(first: nil,
  78.                             second: SecondModel(id: 17, date: "2023-02-09", age: 12, position: "A"))],
  79.             20: [FinalModel(first: nil,
  80.                             second: SecondModel(id: 20, date: "2023-02-09", age: 12, position: "A"))]
  81.         ],
  82.         "2023-02-10": [
  83.             11: [FinalModel(first: [FirstModel(id: 11, date: "2023-02-10", name: "John")],
  84.                             second: nil)],
  85.             14: [FinalModel(first: [FirstModel(id: 14, date: "2023-02-10", name: "Harry")],
  86.                             second: SecondModel(id: 14, date: "2023-02-10", age: 12, position: "A"))],
  87.             16: [FinalModel(first: nil,
  88.                             second: SecondModel(id: 16, date: "2023-02-10", age: 12, position: "A"))]
  89.         ],
  90.         "2023-02-12": [
  91.             11: [FinalModel(first: [FirstModel(id: 11, date: "2023-02-12", name: "John")],
  92.                             second: SecondModel(id: 11, date: "2023-02-12", age: 12, position: "A"))],
  93.             13: [FinalModel(first: [FirstModel(id: 13, date: "2023-02-12", name: "Harry")],
  94.                             second: nil)],
  95.             27: [FinalModel(first: nil,
  96.                             second: SecondModel(id: 27, date: "2023-02-12", age: 12, position: "A"))]
  97.         ],
  98.         "2023-02-13": [
  99.             11: [FinalModel(first: [FirstModel(id: 11, date: "2023-02-13", name: "John")],
  100.                             second: nil)],
  101.             12: [FinalModel(first: [FirstModel(id: 12, date: "2023-02-13", name: "Harry")],
  102.                             second: nil)]
  103.         ],
  104.         "2023-02-19": [
  105.             10: [FinalModel(first: [FirstModel(id: 10, date: "2023-02-19", name: "Harry")],
  106.                             second: SecondModel(id: 10, date: "2023-02-19", age: 12, position: "A"))],
  107.             11: [FinalModel(first: [FirstModel(id: 11, date: "2023-02-19", name: "John")],
  108.                             second: nil)],
  109.             15: [FinalModel(first: nil,
  110.                             second: SecondModel(id: 15, date: "2023-02-19", age: 12, position: "A"))]
  111.         ]
  112.     ]
  113.  
  114.     // Retrieve all dates
  115.     let allKeysDate = Set(dict1.keys).union(dict2.keys)
  116.  
  117.     let output = allKeysDate.reduce(into: [String: [Int: [FinalModel]]]()) { partialOutput, currentDateKey in
  118.  
  119.         let firstModels: [FirstModel]? = dict1[currentDateKey]      //Get the firstModel for that date
  120.         let secondModels: [SecondModel]? = dict2[currentDateKey]    //Get the SecondModel for that date
  121.         //Get all ids for that date, depending on the ids of firstModels & secondModels
  122.         let allIds: Set<Int> = Set(firstModels?.compactMap { $0.id } ?? []).union( secondModels?.compactMap { $0.id } ?? [])
  123.  
  124.         //Create models
  125.         let models: [Int: [FinalModel]] = allIds.reduce(into: [Int: [FinalModel]]()) { partialModelResult, currentIdKey  in
  126.             let first: [FirstModel] = firstModels?.filter { $0.id == currentIdKey } ?? [] //Get all FirstModel for that id
  127.             let second: [SecondModel]? = secondModels?.filter { $0.id == currentIdKey } //Get all SecondtModel for that id
  128.  
  129.             //I supposed there can only be one SecondModel for each id (see `second?.first`), if not, you need to decide on the behavior
  130.             let finalModel = FinalModel(first: first.isEmpty ? nil : first, second: second?.first)
  131.             var currentlySaved = partialModelResult[currentIdKey, default: []]
  132.             currentlySaved.append(finalModel)
  133.             partialModelResult[currentIdKey] = currentlySaved
  134.         }
  135.  
  136.         //Set the value for the currentDate
  137.         partialOutput[currentDateKey] = models
  138.     }
  139.  
  140.     print(output) //or print(output as NSDictionary), as sometimes I find it more readable, maybe Objective-C old habits
  141.  
  142.     print("output == exptectation \(output == exptectation)")
  143.  
  144.     //In case it's not equal, let's analyze a little more to see which one failed
  145.     let keys = Set(output.keys).union(exptectation.keys)
  146.     keys.forEach {
  147.         let subEqual = output[$0] == exptectation[$0]
  148.         if !subEqual {
  149.             print("Diff between:")
  150.             print(output[$0])
  151.             print(exptectation[$0])
  152.         } else {
  153.             print("EQUALS for: \($0)")
  154.         }
  155.     }
  156. }
  157.  
  158. mergingModels()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement