<commit_before>//
//  MerkleNode.swift
//  SwiftIpfsApi
//
//  Created by <NAME> on 20/10/15.
//
//  Licensed under MIT See LICENCE file in the root of this project for details.

import Foundation
import SwiftMultihash

public class MerkleNode {
    public let hash: Multihash?
    public let name: String?
    public let size: Int?
    public let type: Int?
    public let links: [MerkleNode]?
    public let data: [UInt8]?
    
    public convenience init(hash: String) throws {
        try self.init(hash: hash, name: nil)
    }
    
    public convenience init(hash: String, name: String?) throws {
        try self.init(hash: hash, name: name, size: nil, type: nil, links: nil, data: nil)
    }
    
    public init(hash: String, name: String?, size: Int?, type: Int?, links: [MerkleNode]?, data: [UInt8]?) throws {
        self.name = name
        self.size = size
        self.type = type
        self.links = links
        self.data = data
        
        /// hash must be set before exiting with a throw.
        do {
            self.hash = try SwiftMultihash.fromB58String(hash)
        } catch {
            self.hash = nil
            throw error
        }
    }
}

public func merkleNodeFromJSON(rawJSON: AnyObject) throws -> MerkleNode {
    /// turn it into a dictionary
    let objs     = rawJSON as! [String : AnyObject]
    
    let hash     = objs["Hash"] == nil ? objs["Key"] as! String : objs["Hash"] as! String
    let name     = objs["Name"] as? String
    let size     = objs["Size"] as? Int
    let type     = objs["Type"] as? Int
    
    var links: [MerkleNode]?
    if let rawLinks = objs["Links"] as? [AnyObject] {
        links    = try rawLinks.map {
            (rawJSON) -> MerkleNode in
            return try merkleNodeFromJSON(rawJSON)
            }
    }
<commit_msg>Rename to CamelCase for consistency.<commit_after>//
//  MerkleNode.swift
//  SwiftIpfsApi
//
//  Created by <NAME> on 20/10/15.
//
//  Licensed under MIT See LICENCE file in the root of this project for details.

import Foundation
import SwiftMultihash

public class MerkleNode {
    public let hash: Multihash?
    public let name: String?
    public let size: Int?
    public let type: Int?
    public let links: [MerkleNode]?
    public let data: [UInt8]?
    
    public convenience init(hash: String) throws {
        try self.init(hash: hash, name: nil)
    }
    
    public convenience init(hash: String, name: String?) throws {
        try self.init(hash: hash, name: name, size: nil, type: nil, links: nil, data: nil)
    }
    
    public init(hash: String, name: String?, size: Int?, type: Int?, links: [MerkleNode]?, data: [UInt8]?) throws {
        self.name = name
        self.size = size
        self.type = type
        self.links = links
        self.data = data
        
        /// hash must be set before exiting with a throw.
        do {
            self.hash = try SwiftMultihash.fromB58String(hash)
        } catch {
            self.hash = nil
            throw error
        }
    }
}

public func merkleNodeFromJson(rawJson: AnyObject) throws -> MerkleNode {
    /// turn it into a dictionary
    let objs     = rawJson as! [String : AnyObject]
    
    let hash     = objs["Hash"] == nil ? objs["Key"] as! String : objs["Hash"] as! String
    let name     = objs["Name"] as? String
    let size     = objs["Size"] as? Int
    let type     = objs["Type"] as? Int
    
    var links: [MerkleNode]?
    if let rawLinks = objs["Links"] as? [AnyObject] {
        links    = try rawLinks.map {
            (rawJSON) -> MerkleNode in
            return try merkleNodeFromJson(rawJson)
            }
    }

{'fraction_non_alphanumeric': 0.10976616231086657, 'fraction_numerical': 0.005502063273727648, 'mean_word_length': 2.0918367346938775, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '3369993', 'n_tokens_mistral': 1144, 'n_tokens_neox': 1137, 'n_words': 374}