How can I map a Firestore Multi-Type Array to a structure to Swift to keep the main structure conforming to Codable?

Viewed 27

I am trying to map an array from Firestore that is kept as a multi-type array (customStructure, String), and trying to map it to a structure defined in swift

Below is how the structure is defined

struct multiType: Codable {
    var multiTypeArray: [multiTypeValue /* I want to add String here */ ]?
    static let empty = insights(affinityGroups: [multiTypeArray.empty])
    enum CodingKeys: String, CodingKey {
        case multiTypeArray
    }
}

struct multiTypeValue: Codable {
    var Key1: String?
    var Key2: String?
    static let empty = multiTypeValue()
    enum CodingKeys: String, CodingKey {
        case Key1
        case Key2
    }
}


Firestore stores my data as so:

{
    multiTypeArray: [
        0 : {"key1a": "value", "key2a": "value2"},
        1 : {"key1b": "value", "key2b": "value2"},
        2 : {"key1c": "value", "key2c": "value2"},
        3 : "String1",
        4 : "String2"
    ]
}

So far I have no clue how to do this, I can't seem to think of a protocol that String and my custom Structure can both conform to, or creating one.

0 Answers
Related