<commit_before>//
//  NativeTypesDecoder.swift
//  SwiftPlusPlus
//
//  Created by <NAME> on 2/27/16.
//  Copyright © 2016 Drewag LLC. All rights reserved.
//

import Foundation

struct DecodingError: Error {
    let description: String
}

public final class NativeTypesDecoder: DecoderType {
    let raw: Any
    public let mode: DecodingMode

    public class func decodableTypeFromObject<E: DecodableType>(_ raw: Any, mode: DecodingMode) throws -> E {
        guard !(raw is NSNull) else {
            throw DecodingError(description: "Was null")
        }

        let decoder = NativeTypesDecoder(raw: raw, mode: mode)
        return try E(decoder: decoder)
    }

    fileprivate init(raw: Any, mode: DecodingMode) {
        self.raw = raw
        self.mode = mode
    }

    public func decode<Value: DecodableType>(_ key: CoderKey<Value>.Type) throws -> Value {
        guard let object = self.object(forKeyPath: key.path) else {
            throw DecodingError(description: "Required value not found for key \(key.path)")
        }
        if Value.self is RawEncodableType.Type {
            guard let value = object as? Value else {
                throw DecodingError(description: "Value of wrong type found for key \(key.path)")
            }
            return value
        }
        return try NativeTypesDecoder.decodableTypeFromObject(object, mode: self.mode)
    }

    public func decode<Value: DecodableType>(_ key: OptionalCoderKey<Value>.Type) throws -> Value? {
        guard let object = self.object(forKeyPath: key.path) else {
            return nil
        }
        if Value.self is RawEncodableType.Type {
            return object as? Value
        }
        return try? NativeTypesDecoder.decodableTypeFromObject(object, mode: self.mode)
    }

    public func decodeArray<Value: DecodableType>(_ key: CoderKey<Value>.Type) throws -> [Value] {
        let object = self.object(forKeyPath: key.path)
        guard let array = object as? [Any] else {
            if object is NSNull || object == nil {
                return []
            }
            else {
                fatalError("Unexpected type")
            }
        }
        var output: [Value] = []
        for raw in array {
            if raw is RawEncodableType {
                guard let value = raw as? Value else {
                    throw DecodingError(description: "Value of wrong type found in array for key \(key.path)")
                }
                output.append(raw as! Value)
            }
            else {
                let object: Value = try NativeTypesDecoder.decodableTypeFromObject(raw, mode: self.mode)
                output.append(object)
            }
        }
        return output
    }

    public func decodeAsEntireValue<Value: DecodableType>() throws -> Value {
        if Value.self is RawEncodableType.Type {
            guard let value = self.raw as? Value else {
                throw DecodingError(description: "Value of wrong type found as entire value")
            }
            return value
        }
        else {<commit_msg>[NF] Made decoding errors UserReportable<commit_after>//
//  NativeTypesDecoder.swift
//  SwiftPlusPlus
//
//  Created by <NAME> on 2/27/16.
//  Copyright © 2016 Drewag LLC. All rights reserved.
//

import Foundation

struct DecodingError: UserReportableError {
    let alertTitle: String = "Error Decoding"
    let alertMessage: String
    let otherInfo: [String : String]? = nil

    init(description: String) {
        self.alertMessage = description
    }
}

public final class NativeTypesDecoder: DecoderType {
    let raw: Any
    public let mode: DecodingMode

    public class func decodableTypeFromObject<E: DecodableType>(_ raw: Any, mode: DecodingMode) throws -> E {
        guard !(raw is NSNull) else {
            throw DecodingError(description: "Was null")
        }

        let decoder = NativeTypesDecoder(raw: raw, mode: mode)
        return try E(decoder: decoder)
    }

    fileprivate init(raw: Any, mode: DecodingMode) {
        self.raw = raw
        self.mode = mode
    }

    public func decode<Value: DecodableType>(_ key: CoderKey<Value>.Type) throws -> Value {
        guard let object = self.object(forKeyPath: key.path) else {
            throw DecodingError(description: "Required value not found for key \(key.path)")
        }
        if Value.self is RawEncodableType.Type {
            guard let value = object as? Value else {
                throw DecodingError(description: "Value of wrong type found for key \(key.path)")
            }
            return value
        }
        return try NativeTypesDecoder.decodableTypeFromObject(object, mode: self.mode)
    }

    public func decode<Value: DecodableType>(_ key: OptionalCoderKey<Value>.Type) throws -> Value? {
        guard let object = self.object(forKeyPath: key.path) else {
            return nil
        }
        if Value.self is RawEncodableType.Type {
            return object as? Value
        }
        return try? NativeTypesDecoder.decodableTypeFromObject(object, mode: self.mode)
    }

    public func decodeArray<Value: DecodableType>(_ key: CoderKey<Value>.Type) throws -> [Value] {
        let object = self.object(forKeyPath: key.path)
        guard let array = object as? [Any] else {
            if object is NSNull || object == nil {
                return []
            }
            else {
                fatalError("Unexpected type")
            }
        }
        var output: [Value] = []
        for raw in array {
            if raw is RawEncodableType {
                guard let _ = raw as? Value else {
                    throw DecodingError(description: "Value of wrong type found in array for key \(key.path)")
                }
                output.append(raw as! Value)
            }
            else {
                let object: Value = try NativeTypesDecoder.decodableTypeFromObject(raw, mode: self.mode)
                output.append(object)
            }
        }
        return output
    }

    public func decodeAsEntireValue<Value: DecodableType>() throws -> Value {
        if Value.self is RawEncodableType.Type {
            guard let value = self.raw as? Value else {
                throw DecodingError(description: "Value of wrong type found as entire value")
            }
            return value
        }
        else {
{'fraction_non_alphanumeric': 0.08707290190521177, 'fraction_numerical': 0.002834199338686821, 'mean_word_length': 1.9696119682094437, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7297657', 'n_tokens_mistral': 1750, 'n_tokens_neox': 1688, 'n_words': 533}