From 9c6fdce0a149b7125d9fe9eca8b5bde73b5de5f4 Mon Sep 17 00:00:00 2001 From: Alexandr Grigoryev Date: Mon, 27 Feb 2023 16:00:23 +0530 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=A8=20added=20check=20for=20duplicate?= =?UTF-8?q?=20keys=20-#78843?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ACKLocalizationCore/ACKLocalization.swift | 18 ++++++++++++++++-- .../ACKLocalizationCoreTests/LocRowTests.swift | 10 ++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Sources/ACKLocalizationCore/ACKLocalization.swift b/Sources/ACKLocalizationCore/ACKLocalization.swift index 95d3048..b04e843 100644 --- a/Sources/ACKLocalizationCore/ACKLocalization.swift +++ b/Sources/ACKLocalizationCore/ACKLocalization.swift @@ -412,12 +412,26 @@ public final class ACKLocalization { /// Actually writes given `rows` to given `file` private func writeRows(_ rows: [LocRow], to file: String) throws { guard rows.count > 0 else { return } - + + try checkDuplicateKeys(form: rows) + try rows.map { $0.localizableRow } .joined(separator: "\n") .write(toFile: file, atomically: true, encoding: .utf8) } - + + /// Check if given `rows` have a duplicate keys + public func checkDuplicateKeys(form rows: [LocRow]) throws { + let keys = rows.map { $0.key } + let uniqueKeys = Set(rows.map { $0.key }) + + if keys.count != uniqueKeys.count { + let duplicates = Dictionary(grouping: rows, by: \.key) + .filter { $1.count > 1 }.keys + throw LocalizationError(message: "❌ Check your Google Spreadsheet, you have a duplicate keys: \(duplicates)") + } + } + /// Displays error to stdout private func displayError(_ localizationError: LocalizationError) { let message = "❌ " + localizationError.message diff --git a/Tests/ACKLocalizationCoreTests/LocRowTests.swift b/Tests/ACKLocalizationCoreTests/LocRowTests.swift index 49600d8..7aeeb2c 100644 --- a/Tests/ACKLocalizationCoreTests/LocRowTests.swift +++ b/Tests/ACKLocalizationCoreTests/LocRowTests.swift @@ -81,4 +81,14 @@ final class LocRowTests: XCTestCase { let locRow = LocRow(key: "pos_arg_key", value: "%1$@ people will arrive in %2$@ minutes") XCTAssertEqual(#""pos_arg_key" = "%1$@ people will arrive in %2$@ minutes";"#, locRow.localizableRow) } + + func testForDuplicateKeys() throws { + let localization = ACKLocalization() + let locRow = [ + LocRow(key: "key_1", value: "value1"), + LocRow(key: "key_1", value: "value2"), + LocRow(key: "key_2", value: "value3") + ] + XCTAssertThrowsError(try localization.checkDuplicateKeys(form: locRow)) + } } From 4345d2b83564d367583cc3e435a777625dff43b2 Mon Sep 17 00:00:00 2001 From: Alexandr Grigoryev Date: Mon, 27 Feb 2023 16:17:05 +0530 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9D=20Updated=20CHANGELOG=20and=20?= =?UTF-8?q?typo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + Sources/ACKLocalizationCore/ACKLocalization.swift | 8 ++++---- Tests/ACKLocalizationCoreTests/LocRowTests.swift | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed21872..49511bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ## master ### Added +- Add check for duplicated keys in Spreadsheet ([#38](https://github.com/AckeeCZ/ACKLocalization/pull/38), kudos to @AGr-AlexandrGrigoryev) - Replaces custom solution for getting auth credentials with [google auth library](https://github.com/googleapis/google-auth-library-swift) ([#36](https://github.com/AckeeCZ/ACKLocalization/pull/36), kudos to @babacros) ## 1.5.0 diff --git a/Sources/ACKLocalizationCore/ACKLocalization.swift b/Sources/ACKLocalizationCore/ACKLocalization.swift index b04e843..d94d6e7 100644 --- a/Sources/ACKLocalizationCore/ACKLocalization.swift +++ b/Sources/ACKLocalizationCore/ACKLocalization.swift @@ -413,22 +413,22 @@ public final class ACKLocalization { private func writeRows(_ rows: [LocRow], to file: String) throws { guard rows.count > 0 else { return } - try checkDuplicateKeys(form: rows) + try checkDuplicatedKeys(form: rows) try rows.map { $0.localizableRow } .joined(separator: "\n") .write(toFile: file, atomically: true, encoding: .utf8) } - /// Check if given `rows` have a duplicate keys - public func checkDuplicateKeys(form rows: [LocRow]) throws { + /// Check if given `rows` have a duplicated keys + public func checkDuplicatedKeys(form rows: [LocRow]) throws { let keys = rows.map { $0.key } let uniqueKeys = Set(rows.map { $0.key }) if keys.count != uniqueKeys.count { let duplicates = Dictionary(grouping: rows, by: \.key) .filter { $1.count > 1 }.keys - throw LocalizationError(message: "❌ Check your Google Spreadsheet, you have a duplicate keys: \(duplicates)") + throw LocalizationError(message: "❌ Check your Google Spreadsheet, you have a duplicated keys: \(duplicates)") } } diff --git a/Tests/ACKLocalizationCoreTests/LocRowTests.swift b/Tests/ACKLocalizationCoreTests/LocRowTests.swift index 7aeeb2c..4a514d1 100644 --- a/Tests/ACKLocalizationCoreTests/LocRowTests.swift +++ b/Tests/ACKLocalizationCoreTests/LocRowTests.swift @@ -82,13 +82,13 @@ final class LocRowTests: XCTestCase { XCTAssertEqual(#""pos_arg_key" = "%1$@ people will arrive in %2$@ minutes";"#, locRow.localizableRow) } - func testForDuplicateKeys() throws { + func testForDuplicatedKeys() throws { let localization = ACKLocalization() let locRow = [ LocRow(key: "key_1", value: "value1"), LocRow(key: "key_1", value: "value2"), LocRow(key: "key_2", value: "value3") ] - XCTAssertThrowsError(try localization.checkDuplicateKeys(form: locRow)) + XCTAssertThrowsError(try localization.checkDuplicatedKeys(form: locRow)) } } From 9ff329400d38f10e21dc824eca86ab911d0be60c Mon Sep 17 00:00:00 2001 From: Alexandr Grigoryev Date: Mon, 27 Feb 2023 18:01:16 +0530 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20=20Changes=20from=20co?= =?UTF-8?q?de=20review?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 +- .../ACKLocalizationCore/ACKLocalization.swift | 6 +++--- .../ACKLocalizationTests.swift | 21 ++++++++++++++++++- .../LocRowTests.swift | 10 --------- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49511bc..7051ec6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ ## master ### Added -- Add check for duplicated keys in Spreadsheet ([#38](https://github.com/AckeeCZ/ACKLocalization/pull/38), kudos to @AGr-AlexandrGrigoryev) +- Add check for duplicate keys in Spreadsheet ([#38](https://github.com/AckeeCZ/ACKLocalization/pull/38), kudos to @AGr-AlexandrGrigoryev) - Replaces custom solution for getting auth credentials with [google auth library](https://github.com/googleapis/google-auth-library-swift) ([#36](https://github.com/AckeeCZ/ACKLocalization/pull/36), kudos to @babacros) ## 1.5.0 diff --git a/Sources/ACKLocalizationCore/ACKLocalization.swift b/Sources/ACKLocalizationCore/ACKLocalization.swift index d94d6e7..45df297 100644 --- a/Sources/ACKLocalizationCore/ACKLocalization.swift +++ b/Sources/ACKLocalizationCore/ACKLocalization.swift @@ -413,7 +413,7 @@ public final class ACKLocalization { private func writeRows(_ rows: [LocRow], to file: String) throws { guard rows.count > 0 else { return } - try checkDuplicatedKeys(form: rows) + try checkDuplicateKeys(form: rows) try rows.map { $0.localizableRow } .joined(separator: "\n") @@ -421,9 +421,9 @@ public final class ACKLocalization { } /// Check if given `rows` have a duplicated keys - public func checkDuplicatedKeys(form rows: [LocRow]) throws { + public func checkDuplicateKeys(form rows: [LocRow]) throws { let keys = rows.map { $0.key } - let uniqueKeys = Set(rows.map { $0.key }) + let uniqueKeys = Set(keys) if keys.count != uniqueKeys.count { let duplicates = Dictionary(grouping: rows, by: \.key) diff --git a/Tests/ACKLocalizationCoreTests/ACKLocalizationTests.swift b/Tests/ACKLocalizationCoreTests/ACKLocalizationTests.swift index 3764742..33bbe31 100644 --- a/Tests/ACKLocalizationCoreTests/ACKLocalizationTests.swift +++ b/Tests/ACKLocalizationCoreTests/ACKLocalizationTests.swift @@ -2,8 +2,9 @@ import ACKLocalizationCore import XCTest final class ACKLocalizationTests: XCTestCase { + let localization = ACKLocalization() + func test_transform_emptyRow() throws { - let localization = ACKLocalization() let mappedValues = try localization.transformValues( .init( values: [ @@ -21,4 +22,22 @@ final class ACKLocalizationTests: XCTestCase { [LocRow(key: "key", value: "")] ) } + + func testForDuplicateKeys() throws { + let locRow = [ + LocRow(key: "key_1", value: "value1"), + LocRow(key: "key_1", value: "value2"), + LocRow(key: "key_2", value: "value3") + ] + XCTAssertThrowsError(try localization.checkDuplicateKeys(form: locRow)) + } + + func testForUniqueKeys() throws { + let locRow = [ + LocRow(key: "key_1", value: "value1"), + LocRow(key: "key_2", value: "value2"), + LocRow(key: "key_3", value: "value3") + ] + XCTAssertNoThrow(try localization.checkDuplicateKeys(form: locRow)) + } } diff --git a/Tests/ACKLocalizationCoreTests/LocRowTests.swift b/Tests/ACKLocalizationCoreTests/LocRowTests.swift index 4a514d1..49600d8 100644 --- a/Tests/ACKLocalizationCoreTests/LocRowTests.swift +++ b/Tests/ACKLocalizationCoreTests/LocRowTests.swift @@ -81,14 +81,4 @@ final class LocRowTests: XCTestCase { let locRow = LocRow(key: "pos_arg_key", value: "%1$@ people will arrive in %2$@ minutes") XCTAssertEqual(#""pos_arg_key" = "%1$@ people will arrive in %2$@ minutes";"#, locRow.localizableRow) } - - func testForDuplicatedKeys() throws { - let localization = ACKLocalization() - let locRow = [ - LocRow(key: "key_1", value: "value1"), - LocRow(key: "key_1", value: "value2"), - LocRow(key: "key_2", value: "value3") - ] - XCTAssertThrowsError(try localization.checkDuplicatedKeys(form: locRow)) - } }