Skip to content

Commit

Permalink
RUMM-1870 Improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
maxep committed Apr 4, 2022
1 parent bb6b228 commit 7f2da6e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
8 changes: 4 additions & 4 deletions Sources/Datadog/Core/Persistence/DataEncryption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public protocol DataEncryption {

/// Decrypts given `Data` with user-chosen encryption.
///
/// Beware that data to decrypt could be encrypted in a previous
/// app launch, so implementation should be aware of the case when decryption could
/// fail (for example, key used for encryption is different from key used for decryption, if
/// they are unique for every app launch).
/// Beware that data to decrypt could be encrypted in a previous app launch, so
/// implementation should be aware of the case when decryption could fail (for example,
/// key used for encryption is different from key used for decryption, if they are unique
/// for every app launch).
///
/// - Parameter data: Data to decrypt.
/// - Returns: The decrypted data.
Expand Down
13 changes: 11 additions & 2 deletions Sources/Datadog/Core/Persistence/Reading/FileReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal final class FileReader: Reader {
}

do {
let fileData = try decode(data: file.read())
let fileData = try decrypt(data: file.read())
let batchData = dataFormat.prefixData + fileData + dataFormat.suffixData
return Batch(data: batchData, file: file)
} catch {
Expand All @@ -47,7 +47,16 @@ internal final class FileReader: Reader {
}
}

func decode(data: Data) -> Data {
/// Decrypts data if encryption is available.
///
/// When encryption is provided, the data is splitted using data-format separator, each slices
/// is then decoded from base64 and decrypted. Data is finally re-joined with data-format separator.
///
/// If no encryption, the data is returned.
///
/// - Parameter data: The data to decrypt.
/// - Returns: Decrypted data.
private func decrypt(data: Data) -> Data {
guard let encryption = encryption else {
return data
}
Expand Down
8 changes: 7 additions & 1 deletion Sources/Datadog/Core/Persistence/Writing/FileWriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ internal final class FileWriter: Writer {
}
}

func encode<T: Encodable>(value: T) throws -> Data {
/// Encodes the given encodable valud and encrypt it if encryption is available.
///
/// If encryption is available, encryption result is base64 encoded,
///
/// - Parameter value: The value to encode.
/// - Returns: Data representation of the value.
private func encode<T: Encodable>(value: T) throws -> Data {
let data = try jsonEncoder.encode(value)

guard let encryption = encryption else {
Expand Down

0 comments on commit 7f2da6e

Please sign in to comment.