<commit_before>//
//  ChangeReportJSONRenderer.swift
//  RendezVous
//
//  Created by <NAME> on 16/09/2015.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
class ChangeReportJSONRender : ChangeReportRenderer {
    
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    func render(changes: [String : [Change]], errors: [String : ErrorType]?, groupBy: ChangeReportGroupingStyle = .GroupByChangeType) {
        
        var renderFunction: (([String: [Change]], [String: ErrorType]?) -> Void)
        
        switch groupBy {
            case .NoGrouping:           renderFunction = renderNoGrouping
            case .GroupByChangeType:    renderFunction = renderGroupByChangeType
        }
        
        renderFunction(changes, errors)
    }
    
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    func renderNoGrouping(changes: [String: [Change]], errors: [String: ErrorType]?) {
        
    }
    
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    func renderGroupByChangeType(changes: [String: [Change]], errors: [String: ErrorType]?) {

        let fileList             = Array(changes.keys)
        var serializedChangeList = [[String: AnyObject]]()
        var serializedErrorList  = [[String: String]]()

        for (file, changeList) in changes {
            let changed = findChangesByType(.Changed, changes: changeList).map { $0.dictionaryValue }
            let created = findChangesByType(.Created, changes: changeList).map { $0.dictionaryValue }
            let deleted = findChangesByType(.Deleted, changes: changeList).map { $0.dictionaryValue }
            
            var dict = [String: AnyObject]()
            
            dict["file"] = file
            
            if changed.count > 0 { dict["changed"] = changed }
            if created.count > 0 { dict["created"] = created }
            if deleted.count > 0 { dict["deleted"] = deleted }
            
            serializedChangeList.append(dict)
        }
        
        if let errorList = errors {
            for (file, _) in errorList {
                serializedErrorList.append(["file": file, "error": ""])
            }
        }

        let report = [
            "files":    fileList,
            "changes":  serializedChangeList,
            "errors":   serializedErrorList
        ]
        
        do {
            let data   = try NSJSONSerialization.dataWithJSONObject(report, options: [.PrettyPrinted])
            let string = NSString(data: data, encoding: NSUTF8StringEncoding)
            
            print(string)
        }
        catch {}
    }<commit_msg>[FIX] wasn't handling the case where NSString(data:encoding:) fails<commit_after>//
//  ChangeReportJSONRenderer.swift
//  RendezVous
//
//  Created by <NAME> on 16/09/2015.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import Foundation

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
class ChangeReportJSONRender : ChangeReportRenderer {
    
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    func render(changes: [String : [Change]], errors: [String : ErrorType]?, groupBy: ChangeReportGroupingStyle = .GroupByChangeType) {
        
        var renderFunction: (([String: [Change]], [String: ErrorType]?) -> Void)
        
        switch groupBy {
            case .NoGrouping:           renderFunction = renderNoGrouping
            case .GroupByChangeType:    renderFunction = renderGroupByChangeType
        }
        
        renderFunction(changes, errors)
    }
    
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    func renderNoGrouping(changes: [String: [Change]], errors: [String: ErrorType]?) {
        
    }
    
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    func renderGroupByChangeType(changes: [String: [Change]], errors: [String: ErrorType]?) {

        let fileList             = Array(changes.keys)
        var serializedChangeList = [[String: AnyObject]]()
        var serializedErrorList  = [[String: String]]()

        for (file, changeList) in changes {
            let changed = findChangesByType(.Changed, changes: changeList).map { $0.dictionaryValue }
            let created = findChangesByType(.Created, changes: changeList).map { $0.dictionaryValue }
            let deleted = findChangesByType(.Deleted, changes: changeList).map { $0.dictionaryValue }
            
            var dict = [String: AnyObject]()
            
            dict["file"] = file
            
            if changed.count > 0 { dict["changed"] = changed }
            if created.count > 0 { dict["created"] = created }
            if deleted.count > 0 { dict["deleted"] = deleted }
            
            serializedChangeList.append(dict)
        }
        
        if let errorList = errors {
            for (file, _) in errorList {
                serializedErrorList.append(["file": file, "error": ""])
            }
        }

        let report = [
            "files":    fileList,
            "changes":  serializedChangeList,
            "errors":   serializedErrorList
        ]
        
        do {
            let data   = try NSJSONSerialization.dataWithJSONObject(report, options: [.PrettyPrinted])
            if let string = NSString(data: data, encoding: NSUTF8StringEncoding) {
                print(string)
            }
        }
        catch {}
    }
{'fraction_non_alphanumeric': 0.28886421319796957, 'fraction_numerical': 0.006027918781725888, 'mean_word_length': 1.0877483443708609, 'pattern_counts': {'":': 10, '<': 7, '<?xml version=': 0, '>': 15, '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': '919950', 'n_tokens_mistral': 1381, 'n_tokens_neox': 1336, 'n_words': 314}