<commit_before>//
//  CoreDataDecodable.swift
//  FFCoreData
//
//  Created by <NAME> on 20.10.17.
//  Copyright © 2017 <NAME>. All rights reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

import CoreData

public protocol CoreDataDecodable: Decodable {
    associatedtype DTO: Decodable

    @discardableResult
    static func findOrCreate(for dto: DTO, in context: NSManagedObjectContext) throws -> Self

    init(with dto: DTO, in context: NSManagedObjectContext) throws

    mutating func update(from dto: DTO) throws
}

extension CoreDataDecodable {
    public init(from decoder: Decoder) throws {
        try self.init(with: DTO(from: decoder), in: .decodingContext(at: decoder.codingPath))
    }
}

extension CoreDataDecodable where Self: FindOrCreatable {
    @discardableResult
    public static func findOrCreate(for dto: DTO, in context: NSManagedObjectContext) throws -> Self {
        var object = try findOrCreate(in: context)
        try object.update(from: dto)
        return object
    }
}

extension CoreDataDecodable where Self: NSManagedObject {
    public init(with dto: DTO, in context: NSManagedObjectContext) throws {
        self.init(context: context)
        try update(from: dto)
    }
}

/// Errors thrown during the process of decoding CoreData entities.
///
/// - missingContext: Thrown if a managed object context was missing during the decoding.
public enum CoreDataDecodingError: Error, CustomStringConvertible {
    case missingContext(codingPath: [CodingKey])

    public var description: String {
        switch self {
        case .missingContext(let codingPath): return "Missing context at \(codingPath)"
        }
    }<commit_msg>Use `asDecodingContext(do:)` in convenience implementation<commit_after>//
//  CoreDataDecodable.swift
//  FFCoreData
//
//  Created by <NAME> on 20.10.17.
//  Copyright © 2017 <NAME>. All rights reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

import CoreData

public protocol CoreDataDecodable: Decodable {
    associatedtype DTO: Decodable

    @discardableResult
    static func findOrCreate(for dto: DTO, in context: NSManagedObjectContext) throws -> Self

    init(with dto: DTO, in context: NSManagedObjectContext) throws

    mutating func update(from dto: DTO) throws
}

extension CoreDataDecodable {
    public init(from decoder: Decoder) throws {
        try self.init(with: DTO(from: decoder), in: .decodingContext(at: decoder.codingPath))
    }
}

extension CoreDataDecodable where Self: FindOrCreatable {
    @discardableResult
    public static func findOrCreate(for dto: DTO, in context: NSManagedObjectContext) throws -> Self {
        try context.asDecodingContext {
            var object = try findOrCreate(in: context)
            try object.update(from: dto)
            return object
        }
    }
}

extension CoreDataDecodable where Self: NSManagedObject {
    public init(with dto: DTO, in context: NSManagedObjectContext) throws {
        self.init(context: context)
        try update(from: dto)
    }
}

/// Errors thrown during the process of decoding CoreData entities.
///
/// - missingContext: Thrown if a managed object context was missing during the decoding.
public enum CoreDataDecodingError: Error, CustomStringConvertible {
    case missingContext(codingPath: [CodingKey])

    public var description: String {
        switch self {
        case .missingContext(let codingPath): return "Missing context at \(codingPath)"
        }
    }
{'fraction_non_alphanumeric': 0.0861647790361981, 'fraction_numerical': 0.00621807683766378, 'mean_word_length': 3.314176245210728, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6343226', 'n_tokens_mistral': 1283, 'n_tokens_neox': 1240, 'n_words': 462}