<commit_before>//
//  JSONMapping+Reactive.swift
//  Example
//
//  Created by <NAME> on 03/04/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation
import Argo
import ReactiveSwift
import Result

public let ArgonautErrorDomain = "com.aschuch.Argonaut.ErrorDomain"

public enum ArgonautError: Error {
    case decoding(type: String, reason: String)
    case underlying(Error)
    
    public var nsError: NSError {
        switch self {
        case let .decoding(type, reason):
            let info = [NSLocalizedFailureReasonErrorKey: "\(type): \(reason)"]
            return NSError(domain: ArgonautErrorDomain, code: -1, userInfo: info)
        case let .underlying(error):
            return error as NSError
        }
    }
}

/// Transforms a decoded object to a result or an Argonaut error
private func result<T>(_ decoded: Decoded<T>) -> Result<T, ArgonautError> {
    switch decoded {
    case .success(let value):
        return .success(value)
    case .failure(let error):
        return .failure(ArgonautError.decoding(type: String(describing: T.self), reason: error.description))
    }
}


extension Signal where Value == Any {

    /// Maps the given JSON object within the stream to an object of given classType
    ///
    /// - parameter classType: The type of the object that should be returned
    /// - returns: A new Signal emitting the decoded object
    public func mapToType<X: Argo.Decodable>(_ classType: X.Type, rootKey: String? = nil) -> Signal<X, ArgonautError> where X == X.DecodedType {
        return mapError { ArgonautError.underlying($0) }
            .attemptMap { object in
                let decoded: Decoded<X>
                if let rootKey = rootKey, let object = object as? [String: Any] {
                    decoded = decode(object, rootKey: rootKey)
                } else {
                    decoded = decode(object)
                }

                return result(decoded)
            }
    }

    /// Maps the given JSON object array within the stream to an array of objects of the given classType
    ///
    /// - parameter classType: The type of the array that should be returned
    /// - returns: A new Signal emitting an array of decoded objects
    public func mapToTypeArray<X: Argo.Decodable>(_ classType: X.Type, rootKey: String? = nil) -> Signal<[X], ArgonautError> where X == X.DecodedType {
        return mapError { ArgonautError.underlying($0) }
            .attemptMap { object in
                let decoded: Decoded<[X]>
                if let rootKey = rootKey, let object = object as? [String: Any] {
                    decoded = decode(object, rootKey: rootKey)
                } else {
                    decoded = decode(object)
                }
                return result(decoded)
            }
    }

}

extension SignalProducer where Value == Any {

    /// Maps the given JSON object within the stream to an object of given classType
    ///
    /// - parameter classType: The type of the object that should be returned
    /// - returns: A new Signal emitting the decoded object
    public func mapToType<X: Argo.Decodable>(_ classType: X.Type, rootKey: String? = nil) -> SignalProducer<X, ArgonautError> where X == X.DecodedType {
        return lift { $0.mapToType(classType, rootKey: rootKey) }
    }

    /// Maps the given JSON object array within the stream to an array of objects of the given classType
    ///
    /// - parameter classType: The type of the array that should be returned
    /// - returns: A new Signal emitting an array of decoded objects
    public func mapToTypeArray<X: Argo.Decodable>(_ classType: X.Type, rootKey: String? = nil) -> SignalProducer<[X], ArgonautError> where X == X.DecodedType {
        return lift { $0.mapToTypeArray(classType, rootKey: rootKey) }
    }
<commit_msg>fixes Signal and SignalProducer Extensions<commit_after>//
//  JSONMapping+Reactive.swift
//  Example
//
//  Created by <NAME> on 03/04/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation
import Argo
import ReactiveSwift
import Result

public let ArgonautErrorDomain = "com.aschuch.Argonaut.ErrorDomain"

public enum ArgonautError: Error {
    case decoding(type: String, reason: String)
    case underlying(Error)
    
    public var nsError: NSError {
        switch self {
        case let .decoding(type, reason):
            let info = [NSLocalizedFailureReasonErrorKey: "\(type): \(reason)"]
            return NSError(domain: ArgonautErrorDomain, code: -1, userInfo: info)
        case let .underlying(error):
            return error as NSError
        }
    }
}

/// Transforms a decoded object to a result or an Argonaut error
private func result<T>(_ decoded: Decoded<T>) -> Result<T, ArgonautError> {
    switch decoded {
    case .success(let value):
        return .success(value)
    case .failure(let error):
        return .failure(ArgonautError.decoding(type: String(describing: T.self), reason: error.description))
    }
}


extension SignalProtocol where Value == Any {

    /// Maps the given JSON object within the stream to an object of given classType
    ///
    /// - parameter classType: The type of the object that should be returned
    /// - returns: A new Signal emitting the decoded object
    public func mapToType<X: Argo.Decodable>(_ classType: X.Type, rootKey: String? = nil) -> Signal<X, ArgonautError> where X == X.DecodedType {
        return signal
            .mapError { ArgonautError.underlying($0) }
            .attemptMap { object in
                let decoded: Decoded<X>
                if let rootKey = rootKey, let object = object as? [String: Any] {
                    decoded = decode(object, rootKey: rootKey)
                } else {
                    decoded = decode(object)
                }

                return result(decoded)
            }
    }

    /// Maps the given JSON object array within the stream to an array of objects of the given classType
    ///
    /// - parameter classType: The type of the array that should be returned
    /// - returns: A new Signal emitting an array of decoded objects
    public func mapToTypeArray<X: Argo.Decodable>(_ classType: X.Type, rootKey: String? = nil) -> Signal<[X], ArgonautError> where X == X.DecodedType {
        return signal
            .mapError { ArgonautError.underlying($0) }
            .attemptMap { object in
                let decoded: Decoded<[X]>
                if let rootKey = rootKey, let object = object as? [String: Any] {
                    decoded = decode(object, rootKey: rootKey)
                } else {
                    decoded = decode(object)
                }
                return result(decoded)
            }
    }

}

extension SignalProducerProtocol where Value == Any {

    /// Maps the given JSON object within the stream to an object of given classType
    ///
    /// - parameter classType: The type of the object that should be returned
    /// - returns: A new Signal emitting the decoded object
    public func mapToType<X: Argo.Decodable>(_ classType: X.Type, rootKey: String? = nil) -> SignalProducer<X, ArgonautError> where X == X.DecodedType {
        return producer.lift { $0.mapToType(classType, rootKey: rootKey) }
    }

    /// Maps the given JSON object array within the stream to an array of objects of the given classType
    ///
    /// - parameter classType: The type of the array that should be returned
    /// - returns: A new Signal emitting an array of decoded objects
    public func mapToTypeArray<X: Argo.Decodable>(_ classType: X.Type, rootKey: String? = nil) -> SignalProducer<[X], ArgonautError> where X == X.DecodedType {
        return producer.lift { $0.mapToTypeArray(classType, rootKey: rootKey) }
    }

{'fraction_non_alphanumeric': 0.09647878240681027, 'fraction_numerical': 0.0038694698826260802, 'mean_word_length': 2.33505376344086, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 43, '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': '7538808', 'n_tokens_mistral': 2267, 'n_tokens_neox': 2106, 'n_words': 760}