Signal-iOS/SignalServiceKit/src/Storage/Database/SDSDeserialization.swift
Evan Hahn 370ff654e7
Change license to AGPL
Change license to AGPL

This commit:

- Updates the `LICENSE` file

- Start every file with something like:

      // Copyright YEAR_FIRST_PUBLISHED Signal Messenger, LLC
      // SPDX-License-Identifier: AGPL-3.0-only

---

First, I removed existing license headers with this Ruby 3.1.2 script:

    require 'set'

    EXTENSIONS_TO_CHECK = Set['.h', '.hpp', '.cpp', '.m', '.mm', '.pch', '.swift']

    same = 0
    different = 0

    all_files = `git ls-files`.lines.map { |line| line.strip }
    all_files.each do |relative_path|
      if relative_path == 'Pods'
        next
      end

      unless EXTENSIONS_TO_CHECK.include? File.extname(relative_path)
        next
      end

      path = File.expand_path(relative_path)

      contents = File.read(path)
      new_contents = contents.sub(/\/\/\n\/\/  Copyright .*\n\/\/\n\n/, '')

      if contents == new_contents
        same += 1
      else
        different += 1
      end

      File.write(path, new_contents)
    end

    puts "updated #{different} file(s), left #{same} untouched"

I'm sure this script could be improved, but it worked well enough.

Then, I created `Scripts/lint/lint-license-headers` and ran it to auto-
fix a lot of files. This changed the mode of some files, but I think
that's actually desirable. For example,
`SignalServiceKit/src/Util/AppContext.m` previously had a mode of
`0755/-rwxr-xr-x`, and it's now `0644/-rw-r--r--`.

Then I fixed some stragglers and updated the precommit script.

See [a similar change in the Desktop app][0].

[0]: 8bfaf598af
2022-10-13 08:25:37 -05:00

118 lines
4.0 KiB
Swift

//
// Copyright 2019 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import Foundation
import GRDB
import SignalCoreKit
// Sentinel protocol used to convey that a Codable type should be encoded/decoded to database storage
// using Swift.Codable instead of NS(Secure)Coding.
public protocol SDSSwiftSerializable: Codable {}
extension Array: SDSSwiftSerializable where Element: SDSSwiftSerializable {}
extension Dictionary: SDSSwiftSerializable where Key: SDSSwiftSerializable, Value: SDSSwiftSerializable {}
extension Set: SDSSwiftSerializable where Element: SDSSwiftSerializable {}
extension Optional: SDSSwiftSerializable where Wrapped: SDSSwiftSerializable {}
// This class can be used to convert database values to Swift values.
//
// TODO: Maybe we should rename this to a SDSSerializer protocol and
// move these methods to an extension?
public class SDSDeserialization {
private init() {}
// MARK: - Data
public class func required<T>(_ value: T?, name: String) throws -> T {
guard let value = value else {
owsFailDebug("Missing required field: \(name).")
throw SDSError.missingRequiredField
}
return value
}
// MARK: - Data
public class func optionalData(_ value: Data?, name: String) -> Data? {
return value
}
// MARK: - Date
public class func requiredDoubleAsDate(_ value: Double, name: String) -> Date {
return Date(timeIntervalSince1970: value)
}
public class func optionalDoubleAsDate(_ value: Double?, name: String) -> Date? {
guard let value = value else {
return nil
}
return requiredDoubleAsDate(value, name: name)
}
// MARK: - Numeric Primitive
public class func optionalNumericAsNSNumber<T>(_ value: T?, name: String, conversion: (T) -> NSNumber) -> NSNumber? {
guard let value = value else {
return nil
}
return conversion(value)
}
// MARK: - Blob
public class func optionalUnarchive<T: SDSSwiftSerializable>(_ encoded: Data?, name: String) throws -> T? {
guard let encoded = encoded else {
return nil
}
// The only time we ever return nil is if we fail the above guard condition.
// Explicitly declaring our expected return type helps the type checker pick the correct specialization
// since otherwise it will infer the result of `unarchive(_:name:)` to be Optional<T> and not T.
// (This isn't so important since if T conforms to SDSSwiftSerializable then Optional<T> does too, but
// it doesn't hurt to be explicit).
let result: T = try unarchive(encoded, name: name)
return result
}
public class func unarchive<T: SDSSwiftSerializable>(_ encoded: Data?, name: String) throws -> T {
guard let encoded = encoded else {
owsFailDebug("Missing required field: \(name).")
throw SDSError.missingRequiredField
}
do {
return try JSONDecoder().decode(T.self, from: encoded)
} catch {
owsFailDebug("Read failed[\(name)]: \(error).")
throw SDSError.invalidValue
}
}
public class func optionalUnarchive<T: Any>(_ encoded: Data?, name: String) throws -> T? {
guard let encoded = encoded else {
return nil
}
return try unarchive(encoded, name: name)
}
public class func unarchive<T: Any>(_ encoded: Data?, name: String) throws -> T {
guard let encoded = encoded else {
owsFailDebug("Missing required field: \(name).")
throw SDSError.missingRequiredField
}
do {
guard let decoded = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(encoded) as? T else {
owsFailDebug("Invalid value: \(name).")
throw SDSError.invalidValue
}
return decoded
} catch {
owsFailDebug("Read failed[\(name)]: \(error).")
throw SDSError.invalidValue
}
}
}