<commit_before>/*
 * Copyright 2016 Coodly LLC
 *
 * 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 Foundation

extension String {
    fileprivate static let recordName = "recordName"
    fileprivate static let recordChangeTag = "recordChangeTag"
    fileprivate static let created = "created"
    fileprivate static let modified = "modified"
    
    fileprivate static let system = [recordName, recordChangeTag, created, modified]
}

internal class RecordDecoder: Decoder {
    var codingPath: [CodingKey] = []
    
    var userInfo: [CodingUserInfoKey : Any] = [:]
    
    internal var record: Raw.Record!
    
    func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key : CodingKey {
        return KeyedDecodingContainer(KDC(record: record))
    }
    
    func unkeyedContainer() throws -> UnkeyedDecodingContainer {
        fatalError()
    }
    
    func singleValueContainer() throws -> SingleValueDecodingContainer {
        fatalError()
    }
    
    struct KDC<Key: CodingKey>: KeyedDecodingContainerProtocol {
        var codingPath: [CodingKey] = []
        
        var allKeys: [Key] = []
        
        private let record: Raw.Record
        init(record: Raw.Record) {
            self.record = record
        }
        
        func contains(_ key: Key) -> Bool {
            if record.fields.keys.contains(key.stringValue) {
                return true
            }
            
            return String.system.contains(key.stringValue)
        }
        
        func decodeNil(forKey key: Key) throws -> Bool {
            if String.system.contains(key.stringValue) {
                return false
            }
            
            fatalError(key.stringValue)
        }
        
        func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool {
            if key.stringValue == "deleted" {
                return record.deleted
            }
            
            fatalError()
        }
        
        func decode(_ type: String.Type, forKey key: Key) throws -> String {
            switch key.stringValue {
            case "recordName":
                return record.recordName
            case "recordChangeTag":
                return record.recordChangeTag
            default:
                break
            }
            
            return try field(for: key).string!
        }
        <commit_msg>Decode nil<commit_after>/*
 * Copyright 2016 Coodly LLC
 *
 * 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 Foundation

extension String {
    fileprivate static let recordName = "recordName"
    fileprivate static let recordChangeTag = "recordChangeTag"
    fileprivate static let created = "created"
    fileprivate static let modified = "modified"
    
    fileprivate static let system = [recordName, recordChangeTag, created, modified]
}

internal class RecordDecoder: Decoder {
    var codingPath: [CodingKey] = []
    
    var userInfo: [CodingUserInfoKey : Any] = [:]
    
    internal var record: Raw.Record!
    
    func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key : CodingKey {
        return KeyedDecodingContainer(KDC(record: record))
    }
    
    func unkeyedContainer() throws -> UnkeyedDecodingContainer {
        fatalError()
    }
    
    func singleValueContainer() throws -> SingleValueDecodingContainer {
        fatalError()
    }
    
    struct KDC<Key: CodingKey>: KeyedDecodingContainerProtocol {
        var codingPath: [CodingKey] = []
        
        var allKeys: [Key] = []
        
        private let record: Raw.Record
        init(record: Raw.Record) {
            self.record = record
        }
        
        func contains(_ key: Key) -> Bool {
            if record.fields.keys.contains(key.stringValue) {
                return true
            }
            
            return String.system.contains(key.stringValue)
        }
        
        func decodeNil(forKey key: Key) throws -> Bool {
            if String.system.contains(key.stringValue) {
                return false
            }
            
            return true
        }
        
        func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool {
            if key.stringValue == "deleted" {
                return record.deleted
            }
            
            fatalError()
        }
        
        func decode(_ type: String.Type, forKey key: Key) throws -> String {
            switch key.stringValue {
            case "recordName":
                return record.recordName
            case "recordChangeTag":
                return record.recordChangeTag
            default:
                break
            }
            
            return try field(for: key).string!
        }
        
{'fraction_non_alphanumeric': 0.0782789009849663, 'fraction_numerical': 0.002764817694833247, 'mean_word_length': 1.9350912778904665, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 23, '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': '6631895', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1455, 'n_words': 466}