<commit_before>//
//  Serializable.swift
//  Mapping
//
//  Created by ET|冰琳 on 16/10/25.
//  Copyright © 2016年 <NAME>. All rights reserved.
//

import Foundation

public protocol Serializable{
    func toDictionary() -> [String : Any]
    func toNullableValueDictionary() -> [String : Any?]
}

public extension Serializable{
    
    func toDictionary() -> [String : Any]{
       
        let structInfo = MetadataInfoFor(type: Self.self)
        
        var rawPointer: UnsafeRawPointer
        
        if structInfo.kind == .struct{
            var toSerialize = self
            let selfPointer = withUnsafePointer(to: &toSerialize, {$0})
            rawPointer  = UnsafeRawPointer(selfPointer)
        }else{
            let opaquePointer = Unmanaged.passUnretained(self as AnyObject).toOpaque()
            rawPointer = unsafeBitCast(opaquePointer, to: UnsafeRawPointer.self)
        }
        
        
        var result = [String : Any]()
        
        for i in 0..<structInfo.propertyNames.count{
            
            let pName = structInfo.propertyNames[i]
            let poffs = structInfo.propertyOffsets[i]
            let pType = structInfo.propertyTypes[i]
            
            if let type = pType as? ValueMapping.Type{
                let currentPointer = rawPointer.advanced(by: poffs)
                if let value = type.fetchValue(fromPointer: currentPointer){
                    result.updateValue(value, forKey: pName)
                }
            }else{
                fatalError("not implement for serialize for type: \(pType)")
            }
            
        }
        return result
    }
    
    func toNullableValueDictionary() -> [String : Any?]{
        
        let structInfo = MetadataInfoFor(type: Self.self)
        
        var rawPointer: UnsafeRawPointer
        
        if structInfo.kind == .struct{
            var toSerialize = self
            let selfPointer = withUnsafePointer(to: &toSerialize, {$0})
            rawPointer  = UnsafeRawPointer(selfPointer)
        }else{
            let opaquePointer = Unmanaged.passUnretained(self as AnyObject).toOpaque()
            rawPointer = unsafeBitCast(opaquePointer, to: UnsafeRawPointer.self)
        }
        
        var result = [String : Any]()
        
        for i in 0..<structInfo.propertyNames.count{
            
            let pName = structInfo.propertyNames[i]
            let poffs = structInfo.propertyOffsets[i]
            let pType = structInfo.propertyTypes[i]
            
            if let type = pType as? ValueMapping.Type{
                let currentPointer = rawPointer.advanced(by: poffs)
                let value = type.fetchValue(fromPointer: currentPointer)
                result.updateValue(value, forKey: pName)
            }else{
                fatalError("not implement for serialize for type: \(pType)")
            }
            
        }
        return result
    }
}
<commit_msg>[Fix warning] implicit convert Any? to Any<commit_after>//
//  Serializable.swift
//  Mapping
//
//  Created by ET|冰琳 on 16/10/25.
//  Copyright © 2016年 <NAME>. All rights reserved.
//

import Foundation

public protocol Serializable{
    func toDictionary() -> [String : Any]
    func toNullableValueDictionary() -> [String : Any?]
}

public extension Serializable{
    
    func toDictionary() -> [String : Any]{
       
        let structInfo = MetadataInfoFor(type: Self.self)
        
        var rawPointer: UnsafeRawPointer
        
        if structInfo.kind == .struct{
            var toSerialize = self
            let selfPointer = withUnsafePointer(to: &toSerialize, {$0})
            rawPointer  = UnsafeRawPointer(selfPointer)
        }else{
            let opaquePointer = Unmanaged.passUnretained(self as AnyObject).toOpaque()
            rawPointer = unsafeBitCast(opaquePointer, to: UnsafeRawPointer.self)
        }
        
        
        var result = [String : Any]()
        
        for i in 0..<structInfo.propertyNames.count{
            
            let pName = structInfo.propertyNames[i]
            let poffs = structInfo.propertyOffsets[i]
            let pType = structInfo.propertyTypes[i]
            
            if let type = pType as? ValueMapping.Type{
                let currentPointer = rawPointer.advanced(by: poffs)
                if let value = type.fetchValue(fromPointer: currentPointer){
                    result.updateValue(value, forKey: pName)
                }
            }else{
                fatalError("not implement for serialize for type: \(pType)")
            }
            
        }
        return result
    }
    
    func toNullableValueDictionary() -> [String : Any?]{
        
        let structInfo = MetadataInfoFor(type: Self.self)
        
        var rawPointer: UnsafeRawPointer
        
        if structInfo.kind == .struct{
            var toSerialize = self
            let selfPointer = withUnsafePointer(to: &toSerialize, {$0})
            rawPointer  = UnsafeRawPointer(selfPointer)
        }else{
            let opaquePointer = Unmanaged.passUnretained(self as AnyObject).toOpaque()
            rawPointer = unsafeBitCast(opaquePointer, to: UnsafeRawPointer.self)
        }
        
        var result = [String : Any?]()
        
        for i in 0..<structInfo.propertyNames.count{
            
            let pName = structInfo.propertyNames[i]
            let poffs = structInfo.propertyOffsets[i]
            let pType = structInfo.propertyTypes[i]
            
            if let type = pType as? ValueMapping.Type{
                let currentPointer = rawPointer.advanced(by: poffs)
                let value = type.fetchValue(fromPointer: currentPointer)
                result.updateValue(value, forKey: pName)
            }else{
                fatalError("not implement for serialize for type: \(pType)")
            }
            
        }
        return result
    }
}

{'fraction_non_alphanumeric': 0.08764805414551607, 'fraction_numerical': 0.0047377326565143825, 'mean_word_length': 1.7327785483125289, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '4124782', 'n_tokens_mistral': 1623, 'n_tokens_neox': 1571, 'n_words': 382}