Created
May 30, 2024 03:16
-
-
Save atulsmadhugiri/3f1ac163e0f53b46f496001d4cedda3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let NEW_LINE = "\n" | |
let SPACE = " " | |
let logPattern = #"LOG\s+⚠️\s+(.+?):\s+(\w+)\s+is\s+(enabled|disabled)\."# | |
let logPatternRegex = try! NSRegularExpression(pattern: logPattern) | |
func constructDictionary(lines: [String]) -> [String: [String: Bool]] { | |
let nestedDictionary = lines.compactMap { line -> (String, String, Bool)? in | |
guard line.contains("⚠️") else { return nil } | |
guard | |
let match = logPatternRegex.firstMatch(in: line, range: NSRange(line.startIndex..., in: line)) | |
else { return nil } | |
guard let threadRange = Range(match.range(at: 1), in: line), | |
let permissionRange = Range(match.range(at: 2), in: line), | |
let statusRange = Range(match.range(at: 3), in: line) | |
else { return nil } | |
let thread = String(line[threadRange]) | |
let permission = String(line[permissionRange]) | |
let status = String(line[statusRange]) | |
let isEnabled = (status == "enabled") | |
return (thread, permission, isEnabled) | |
} | |
.reduce(into: [String: [String: Bool]]()) { dict, data in | |
let (thread, permission, isEnabled) = data | |
dict[thread, default: [:]][permission] = isEnabled | |
} | |
return nestedDictionary | |
} | |
func compareDictionaries(before: [String: [String: Bool]], after: [String: [String: Bool]]) -> [( | |
thread: String, permission: String, before: Bool, after: Bool | |
)] { | |
var differences = [(thread: String, permission: String, before: Bool, after: Bool)]() | |
for (thread, permissions) in before { | |
for (permission, beforeValue) in permissions { | |
if let afterValue = after[thread]?[permission], beforeValue != afterValue { | |
differences.append((thread, permission, beforeValue, afterValue)) | |
} | |
} | |
} | |
for (thread, permissions) in after { | |
for (permission, afterValue) in permissions { | |
if let beforeValue = before[thread]?[permission], beforeValue != afterValue { | |
differences.append((thread, permission, beforeValue, afterValue)) | |
} | |
} | |
} | |
return differences | |
} | |
let beforePath = "./BEFORE.txt" | |
let beforeLines = try String(contentsOfFile: beforePath).split(separator: NEW_LINE).map(String.init) | |
var beforeDictionary = constructDictionary(lines: beforeLines) | |
let afterPath = "./AFTER.txt" | |
let afterLines = try String(contentsOfFile: afterPath).split(separator: NEW_LINE).map(String.init) | |
var afterDictionary = constructDictionary(lines: afterLines) | |
print(compareDictionaries(before: beforeDictionary, after: afterDictionary)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment