<commit_before>/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

import Foundation

public protocol EventReporterSubject : CustomStringConvertible {
  var jsonDescription: JSON { get }
  var subSubjects: [EventReporterSubject] { get }
}

extension EventReporterSubject {
  public var subSubjects: [EventReporterSubject] { get {
    return [self]
  }}
}

extension EventReporterSubject  {
  public func append(_ other: EventReporterSubject) -> EventReporterSubject {
    let joined = self.subSubjects + other.subSubjects
    guard let firstElement = joined.first else {
      return CompositeSubject([])
    }
    if joined.count == 1 {
      return firstElement
    }
    return CompositeSubject(joined)
  }
}

struct SimpleSubject : EventReporterSubject {
  let eventName: EventName
  let eventType: EventType
  let subject: EventReporterSubject

  init(_ eventName: EventName, _ eventType: EventType, _ subject: EventReporterSubject) {
    self.eventName = eventName
    self.eventType = eventType
    self.subject = subject
  }

  var jsonDescription: JSON { get {
    return JSON.dictionary([
      JSONKeys.eventName.rawValue : JSON.string(self.eventName.rawValue),
      JSONKeys.eventType.rawValue : JSON.string(self.eventType.rawValue),
      JSONKeys.subject.rawValue : self.subject.jsonDescription,
      JSONKeys.timestamp.rawValue : JSON.number(NSNumber(value: round(Date().timeIntervalSince1970) as Double)),
    ])
  }}

  var shortDescription: String { get {
    switch self.eventType {
    case EventType.discrete:
      return self.subject.description
    default:
      return "\(self.eventName.rawValue) \(self.eventType.rawValue): \(self.subject.description)"
    }
  }}

  var description: String { get {
    return self.shortDescription
  }}
}

struct ControlCoreSubject : EventReporterSubject {
  let value: ControlCoreValue

  init(_ value: ControlCoreValue) {
    self.value = value
  }

  var jsonDescription: JSON { get {
    guard let json = try? JSON.encode(self.value.jsonSerializableRepresentation() as AnyObject) else {
      return JSON.null
    }
    return json
  }}

  var description: String { get {
    return self.value.description
  }}
}

struct iOSTargetSubject: EventReporterSubject {
  let target: FBiOSTarget
  let format: FBiOSTargetFormat

  var jsonDescription: JSON { get {
    let dictionary = self.format.extract(from: self.target)
    return try! JSON.encode(dictionary as AnyObject)
  }}

  var description: String { get {
    return self.format.format(self.target)
  }}
}

struct iOSTargetWithSubject : EventReporterSubject {
  let targetSubject: iOSTargetSubject
  let eventName: EventName
  let eventType: EventType
  let subject: EventReporterSubject
  let timestamp: Date

  init(targetSubject: iOSTargetSubject, eventName: EventName, eventType: EventType, subject: EventReporterSubject) {
    self.targetSubject = targetSubject
    self.eventName = eventName
    self.eventType = eventType
    self.subject = subject
    self.timestamp = Date()
  }

  var jsonDescription: JSON { get {
    return JSON.dictionary([
      JSONKeys.eventName.rawValue : JSON.string(self.eventName.rawValue),
      JSONKeys.eventType.rawValue : JSON.string(self.eventType.rawValue),
      JSONKeys.target.rawValue : self.targetSubject.jsonDescription,
      JSONKeys.subject.rawValue : self.subject.jsonDescription,
      JSONKeys.timestamp.rawValue : JSON.number(NSNumber(value: round(self.timestamp.timeIntervalSince1970) as Double)),
    ])
  }}

  var description: String { get {
    switch self.eventType {
    case EventType.discrete:
      return "\(self.targetSubject): \(self.eventName.rawValue): \(self.subject.description)"
    default:
      return ""
    }
  }}
}

struct LogSubject : EventReporterSubject {
  let logString: String
  let level: Int32

  var jsonDescription: JSON { get {
    return JSON.dictionary([
      JSONKeys.eventName.rawValue : JSON.string(EventName.log.rawValue),
      JSONKeys.eventType.rawValue : JSON.string(EventType.discrete.rawValue),
      JSONKeys.level.rawValue : JSON.string(self.levelString),
      JSONKeys.subject.rawValue : JSON.string(self.logString),
      JSONKeys.timestamp.rawValue : JSON.number(NSNumber(value: round(Date().timeIntervalSince1970) as Double)),
    ])
  }}

  var description: String { get {
    return self.logString
  }}

  var levelString: String { get {
    switch self.level {
    case Constants.asl_level_debug(): return "debug"
    case Constants.asl_level_err(): return "error"
    case Constants.asl_level_info(): return "info"
    default: return "unknown"
    }
  }}
}

struct CompositeSubject: EventReporterSubject {
  let array: [EventReporterSubject]

  init (_ array: [EventReporterSubject]) {
    self.array = array
  }

  var subSubjects: [EventReporterSubject] { get {
    return self.array
  }}

  var jsonDescription: JSON { get {
    return JSON.array(self.array.map { $0.jsonDescription } )
  }}

  var description: String { get {
    return "[\(self.array.map({ $0.description }).joined(separator: ", "))]"
  }}
}

struct StringsSubject: EventReporterSubject {
  let strings: [String]

  init (_ strings: [String]) {
    self.strings = strings
  }

  var jsonDescription: JSON { get {
    return JSON.array(self.strings.map { $0.jsonDescription } )
  }}

  var description: String { get {
    return "[\(self.strings.map({ $0.description }).joined(separator: ", "))]"
  }}
}

extension Record : EventReporterSubject {
  public var jsonDescription: JSON {
    var contents: [String : JSON] = [:]
    switch self {
    case .start(let maybePath):
      contents["start"] = JSON.bool(true)
      if let path = maybePath {
        contents["path"] = JSON.string(path)
      } else {
        contents["path"] = JSON.null
      }
    case .stop:
      contents["start"] = JSON.bool(false)
    }
    return JSON.dictionary(contents)
  }

  public var description: String { get {
    switch self {
    case .start(let maybePath):
      let destination = maybePath ?? "Default Destination"
      return "Start Recording \(destination)"
    case .stop:
      return "Stop Recording"
    }
  }}
}

extension String : EventReporterSubject {
  public var jsonDescription: JSON { get {
    return JSON.string(self)
  }}

  public var description: String { get {
    return self
  }}
}

extension Bool : EventReporterSubject {
  public var jsonDescription: JSON { get {
    return JSON.number(NSNumber(value: self as Bool))
  }}
}
<commit_msg>Bridge Objective-C FBEventReporterSubject to Swift EventReporterSubject<commit_after>/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

import Foundation

public protocol EventReporterSubject : CustomStringConvertible {
  var jsonDescription: JSON { get }
  var subSubjects: [EventReporterSubject] { get }
}

extension FBEventReporterSubject : EventReporterSubject {
  public var jsonDescription: JSON { get {
    return try! JSON.encode(self.jsonSerializableRepresentation() as AnyObject)
  }}
}

extension EventReporterSubject {
  public var subSubjects: [EventReporterSubject] { get {
    return [self]
  }}
}

extension EventReporterSubject  {
  public func append(_ other: EventReporterSubject) -> EventReporterSubject {
    let joined = self.subSubjects + other.subSubjects
    guard let firstElement = joined.first else {
      return CompositeSubject([])
    }
    if joined.count == 1 {
      return firstElement
    }
    return CompositeSubject(joined)
  }
}

struct SimpleSubject : EventReporterSubject {
  let eventName: EventName
  let eventType: EventType
  let subject: EventReporterSubject

  init(_ eventName: EventName, _ eventType: EventType, _ subject: EventReporterSubject) {
    self.eventName = eventName
    self.eventType = eventType
    self.subject = subject
  }

  var jsonDescription: JSON { get {
    return JSON.dictionary([
      JSONKeys.eventName.rawValue : JSON.string(self.eventName.rawValue),
      JSONKeys.eventType.rawValue : JSON.string(self.eventType.rawValue),
      JSONKeys.subject.rawValue : self.subject.jsonDescription,
      JSONKeys.timestamp.rawValue : JSON.number(NSNumber(value: round(Date().timeIntervalSince1970) as Double)),
    ])
  }}

  var shortDescription: String { get {
    switch self.eventType {
    case EventType.discrete:
      return self.subject.description
    default:
      return "\(self.eventName.rawValue) \(self.eventType.rawValue): \(self.subject.description)"
    }
  }}

  var description: String { get {
    return self.shortDescription
  }}
}

struct ControlCoreSubject : EventReporterSubject {
  let value: ControlCoreValue

  init(_ value: ControlCoreValue) {
    self.value = value
  }

  var jsonDescription: JSON { get {
    guard let json = try? JSON.encode(self.value.jsonSerializableRepresentation() as AnyObject) else {
      return JSON.null
    }
    return json
  }}

  var description: String { get {
    return self.value.description
  }}
}

struct iOSTargetSubject: EventReporterSubject {
  let target: FBiOSTarget
  let format: FBiOSTargetFormat

  var jsonDescription: JSON { get {
    let dictionary = self.format.extract(from: self.target)
    return try! JSON.encode(dictionary as AnyObject)
  }}

  var description: String { get {
    return self.format.format(self.target)
  }}
}

struct iOSTargetWithSubject : EventReporterSubject {
  let targetSubject: iOSTargetSubject
  let eventName: EventName
  let eventType: EventType
  let subject: EventReporterSubject
  let timestamp: Date

  init(targetSubject: iOSTargetSubject, eventName: EventName, eventType: EventType, subject: EventReporterSubject) {
    self.targetSubject = targetSubject
    self.eventName = eventName
    self.eventType = eventType
    self.subject = subject
    self.timestamp = Date()
  }

  var jsonDescription: JSON { get {
    return JSON.dictionary([
      JSONKeys.eventName.rawValue : JSON.string(self.eventName.rawValue),
      JSONKeys.eventType.rawValue : JSON.string(self.eventType.rawValue),
      JSONKeys.target.rawValue : self.targetSubject.jsonDescription,
      JSONKeys.subject.rawValue : self.subject.jsonDescription,
      JSONKeys.timestamp.rawValue : JSON.number(NSNumber(value: round(self.timestamp.timeIntervalSince1970) as Double)),
    ])
  }}

  var description: String { get {
    switch self.eventType {
    case EventType.discrete:
      return "\(self.targetSubject): \(self.eventName.rawValue): \(self.subject.description)"
    default:
      return ""
    }
  }}
}

struct LogSubject : EventReporterSubject {
  let logString: String
  let level: Int32

  var jsonDescription: JSON { get {
    return JSON.dictionary([
      JSONKeys.eventName.rawValue : JSON.string(EventName.log.rawValue),
      JSONKeys.eventType.rawValue : JSON.string(EventType.discrete.rawValue),
      JSONKeys.level.rawValue : JSON.string(self.levelString),
      JSONKeys.subject.rawValue : JSON.string(self.logString),
      JSONKeys.timestamp.rawValue : JSON.number(NSNumber(value: round(Date().timeIntervalSince1970) as Double)),
    ])
  }}

  var description: String { get {
    return self.logString
  }}

  var levelString: String { get {
    switch self.level {
    case Constants.asl_level_debug(): return "debug"
    case Constants.asl_level_err(): return "error"
    case Constants.asl_level_info(): return "info"
    default: return "unknown"
    }
  }}
}

struct CompositeSubject: EventReporterSubject {
  let array: [EventReporterSubject]

  init (_ array: [EventReporterSubject]) {
    self.array = array
  }

  var subSubjects: [EventReporterSubject] { get {
    return self.array
  }}

  var jsonDescription: JSON { get {
    return JSON.array(self.array.map { $0.jsonDescription } )
  }}

  var description: String { get {
    return "[\(self.array.map({ $0.description }).joined(separator: ", "))]"
  }}
}

struct StringsSubject: EventReporterSubject {
  let strings: [String]

  init (_ strings: [String]) {
    self.strings = strings
  }

  var jsonDescription: JSON { get {
    return JSON.array(self.strings.map { $0.jsonDescription } )
  }}

  var description: String { get {
    return "[\(self.strings.map({ $0.description }).joined(separator: ", "))]"
  }}
}

extension Record : EventReporterSubject {
  public var jsonDescription: JSON {
    var contents: [String : JSON] = [:]
    switch self {
    case .start(let maybePath):
      contents["start"] = JSON.bool(true)
      if let path = maybePath {
        contents["path"] = JSON.string(path)
      } else {
        contents["path"] = JSON.null
      }
    case .stop:
      contents["start"] = JSON.bool(false)
    }
    return JSON.dictionary(contents)
  }

  public var description: String { get {
    switch self {
    case .start(let maybePath):
      let destination = maybePath ?? "Default Destination"
      return "Start Recording \(destination)"
    case .stop:
      return "Stop Recording"
    }
  }}
}

extension String : EventReporterSubject {
  public var jsonDescription: JSON { get {
    return JSON.string(self)
  }}

  public var description: String { get {
    return self
  }}
}

extension Bool : EventReporterSubject {
  public var jsonDescription: JSON { get {
    return JSON.number(NSNumber(value: self as Bool))
  }}
}

{'fraction_non_alphanumeric': 0.10295728368017525, 'fraction_numerical': 0.003358890105878058, 'mean_word_length': 3.497865353037767, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5995862', 'n_tokens_mistral': 4069, 'n_tokens_neox': 3957, 'n_words': 998}