All Downloads are FREE. Search and download functionalities are using the official Maven repository.

swift-combine.AnyDecodable.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
// AnyDecodable.swift
//
// Generated by openapi-generator
// https://openapi-generator.tech
//

import Foundation

extension KeyedDecodingContainer {
    func decode(_ type: Dictionary.Type, forKey key: K) throws -> Dictionary {
        let container = try self.nestedContainer(keyedBy: AnyCodingKeys.self, forKey: key)
        return try container.decode(type)
    }

    func decodeIfPresent(_ type: Dictionary.Type, forKey key: K) throws -> Dictionary? {
        if contains(key) {
            return try decode(type, forKey: key)
        }
        return nil
    }
    
    func decode(_ type: Array.Type, forKey key: K) throws -> Array {
        var container = try self.nestedUnkeyedContainer(forKey: key)
        return try container.decode(type)
    }

    func decodeIfPresent(_ type: Array.Type, forKey key: K) throws -> Array? {
        if contains(key) {
            return try decode(type, forKey: key)
        }
        return nil
    }

    func decode(_ type: Dictionary>.Type, forKey key: K) throws -> Dictionary> {
        let container = try self.nestedContainer(keyedBy: AnyCodingKeys.self, forKey: key)
        return try container.decode(type)
    }

    func decodeIfPresent(_ type: Dictionary>.Type, forKey key: K) throws -> Dictionary>? {
        if contains(key) {
            return try decode(type, forKey: key)
        }
        return nil
    }
    
    func decode(_ type: Dictionary.Type) throws -> Dictionary {
        var dictionary = Dictionary()
        
        for key in allKeys {
            if let boolValue = try? decode(Bool.self, forKey: key) {
                dictionary[key.stringValue] = boolValue
            } else if let stringValue = try? decode(String.self, forKey: key) {
                dictionary[key.stringValue] = stringValue
            } else if let intValue = try? decode(Int.self, forKey: key) {
                dictionary[key.stringValue] = intValue
            } else if let doubleValue = try? decode(Double.self, forKey: key) {
                dictionary[key.stringValue] = doubleValue
            } else if let nestedDictionary = try? decode(Dictionary.self, forKey: key) {
                dictionary[key.stringValue] = nestedDictionary
            } else if let nestedArray = try? decode(Array.self, forKey: key) {
                dictionary[key.stringValue] = nestedArray
            }
        }
        return dictionary
    }

    func decode(_ type: Dictionary>.Type) throws -> Dictionary> {
        var dictionary = Dictionary>()
        
        for key in allKeys {
            if let nestedDictionary = try? decode(Dictionary.self, forKey: key) {
                dictionary[key.stringValue] = nestedDictionary
            }
        }
        return dictionary
    }
}

extension UnkeyedDecodingContainer {
    mutating func decode(_ type: Array.Type) throws -> Array {
        var array: [Any] = []
        while isAtEnd == false {
            if let value = try? decode(Bool.self) {
                array.append(value)
            } else if let value = try? decode(Double.self) {
                array.append(value)
            } else if let value = try? decode(String.self) {
                array.append(value)
            } else if let nestedDictionary = try? decode(Dictionary.self) {
                array.append(nestedDictionary)
            } else if let nestedArray = try? decode(Array.self) {
                array.append(nestedArray)
            }
        }
        return array
    }

    mutating func decode(_ type: Dictionary.Type) throws -> Dictionary {
        let nestedContainer = try self.nestedContainer(keyedBy: AnyCodingKeys.self)
        return try nestedContainer.decode(type)
    }
}

private struct AnyCodingKeys: CodingKey {
    var stringValue: String
    
    init(stringValue: String) {
        self.stringValue = stringValue
    }
    
    var intValue: Int?
    
    init?(intValue: Int) {
        self.init(stringValue: "\(intValue)")
        self.intValue = intValue
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy