|
| 1 | +// |
| 2 | +// GlobalFieldModel.swift |
| 3 | +// ContentstackSwift |
| 4 | +// |
| 5 | +// Created by Reeshika Hosmani on 27/05/25. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +// Helper for decoding [String: Any] and other nested unknown types |
| 11 | +public struct AnyDecodable: Decodable { |
| 12 | + public let value: Any |
| 13 | + |
| 14 | + public init(from decoder: Decoder) throws { |
| 15 | + let container = try decoder.singleValueContainer() |
| 16 | + |
| 17 | + if let int = try? container.decode(Int.self) { |
| 18 | + value = int |
| 19 | + } else if let double = try? container.decode(Double.self) { |
| 20 | + value = double |
| 21 | + } else if let bool = try? container.decode(Bool.self) { |
| 22 | + value = bool |
| 23 | + } else if let string = try? container.decode(String.self) { |
| 24 | + value = string |
| 25 | + } else if let array = try? container.decode([AnyDecodable].self) { |
| 26 | + value = array.map { $0.value } |
| 27 | + } else if let dict = try? container.decode([String: AnyDecodable].self) { |
| 28 | + value = dict.mapValues { $0.value } |
| 29 | + } else { |
| 30 | + value = NSNull() |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +public protocol GlobalFieldDecodable: GlobalFields, FieldKeysQueryable, EndpointAccessible, Decodable {} |
| 36 | + |
| 37 | +public final class GlobalFieldModel : GlobalFieldDecodable { |
| 38 | + public var title: String |
| 39 | + |
| 40 | + public var uid: String |
| 41 | + |
| 42 | + public var createdAt: Date? |
| 43 | + |
| 44 | + public var updatedAt: Date? |
| 45 | + |
| 46 | + public var schema: [[String: Any]] = [] |
| 47 | + |
| 48 | + public var description: String? |
| 49 | + |
| 50 | + public var maintainRevisions: Bool? |
| 51 | + |
| 52 | + public var inbuiltClass: Bool? |
| 53 | + |
| 54 | + public var version: Int? |
| 55 | + |
| 56 | + public var branch: String? |
| 57 | + |
| 58 | + public var lastActivity: [String: Any] = [:] |
| 59 | + |
| 60 | + public enum FieldKeys: String, CodingKey { |
| 61 | + case title, uid, description |
| 62 | + case createdAt = "created_at" |
| 63 | + case updatedAt = "updated_at" |
| 64 | + case maintainRevisions = "maintain_revisions" |
| 65 | + case version = "_version" |
| 66 | + case branch = "_branch" |
| 67 | + case lastActivity = "last_activity" |
| 68 | + case inbuiltClass = "inbuilt_class" |
| 69 | + case schema |
| 70 | + } |
| 71 | + |
| 72 | + public enum QueryableCodingKey: String, CodingKey { |
| 73 | + case uid, title, description |
| 74 | + case createdAt = "created_at" |
| 75 | + case updatedAt = "updated_at" |
| 76 | + } |
| 77 | + |
| 78 | +// public required init(from decoder: Decoder) throws { |
| 79 | +// let container = try decoder.container(keyedBy: FieldKeys.self) |
| 80 | +// uid = try container.decode(String.self, forKey: .uid) |
| 81 | +// title = try container.decode(String.self, forKey: .title) |
| 82 | +// description = try? container.decodeIfPresent(String.self, forKey: .description) |
| 83 | +// createdAt = try? container.decode(Date.self, forKey: .createdAt) |
| 84 | +// updatedAt = try? container.decode(Date.self, forKey: .updatedAt) |
| 85 | +// maintainRevisions = try? container.decode(Bool.self, forKey: .maintainRevisions) |
| 86 | +// version = try? container.decode(Int.self, forKey: .version) |
| 87 | +// branch = try? container.decode(String.self, forKey: .branch) |
| 88 | +// inbuiltClass = try? container.decode(Bool.self, forKey: .inbuiltClass) |
| 89 | +// let containerFields = try? decoder.container(keyedBy: JSONCodingKeys.self) |
| 90 | +// let globalFieldSchema = try containerFields?.decode(Dictionary<String, Any>.self) |
| 91 | +// if let schema = globalFieldSchema?["schema"] as? [[String: Any]] { |
| 92 | +// self.schema = schema |
| 93 | +// } |
| 94 | +//// if let decodedSchema = try? container.decodeIfPresent([ [String: AnyDecodable] ].self, forKey: .schema) { |
| 95 | +//// self.schema = decodedSchema.map { $0.mapValues { $0.value } } |
| 96 | +//// } |
| 97 | +// if let decodedLastActivity = try? container.decodeIfPresent([String: AnyDecodable].self, forKey: .lastActivity) { |
| 98 | +// lastActivity = decodedLastActivity.mapValues { $0.value } |
| 99 | +// } |
| 100 | +// } |
| 101 | + |
| 102 | + |
| 103 | + public required init(from decoder: Decoder) throws { |
| 104 | + let container = try decoder.container(keyedBy: FieldKeys.self) |
| 105 | + |
| 106 | + uid = try container.decode(String.self, forKey: .uid) |
| 107 | + title = try container.decode(String.self, forKey: .title) |
| 108 | + description = try? container.decodeIfPresent(String.self, forKey: .description) |
| 109 | + createdAt = try? container.decode(Date.self, forKey: .createdAt) |
| 110 | + updatedAt = try? container.decode(Date.self, forKey: .updatedAt) |
| 111 | + maintainRevisions = try? container.decode(Bool.self, forKey: .maintainRevisions) |
| 112 | + version = try? container.decode(Int.self, forKey: .version) |
| 113 | + branch = try? container.decode(String.self, forKey: .branch) |
| 114 | + inbuiltClass = try? container.decode(Bool.self, forKey: .inbuiltClass) |
| 115 | + |
| 116 | + // ✅ Decode schema directly and safely |
| 117 | + if let decodedSchema = try? container.decodeIfPresent([[String: AnyDecodable]].self, forKey: .schema) { |
| 118 | + self.schema = decodedSchema.map { $0.mapValues { $0.value } } |
| 119 | + } |
| 120 | + |
| 121 | + if let decodedLastActivity = try? container.decodeIfPresent([String: AnyDecodable].self, forKey: .lastActivity) { |
| 122 | + lastActivity = decodedLastActivity.mapValues { $0.value } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +extension GlobalFieldModel : EndpointAccessible { |
| 128 | + public static var endpoint: Endpoint { |
| 129 | + return .globalfields |
| 130 | + } |
| 131 | +} |
0 commit comments