Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Sources/ACKLocalizationCore/Model/ValueRange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,32 @@ public struct ValueRange: Decodable {
public init(values: [[String]]) {
self.values = values
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let rawValues = try container.decode([[AnyCellValue]].self, forKey: .values)
self.values = rawValues.map { $0.map(\.stringValue) }
}

private enum CodingKeys: String, CodingKey {
case values
}
}

/// A helper type that decodes a single sheet cell value, supporting strings and numbers.
private struct AnyCellValue: Decodable {
let stringValue: String

init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let string = try? container.decode(String.self) {
stringValue = string
} else if let int = try? container.decode(Int.self) {
stringValue = String(int)
} else if let double = try? container.decode(Double.self) {
stringValue = String(double)
} else {
stringValue = ""
}
}
}
32 changes: 32 additions & 0 deletions Tests/ACKLocalizationCoreTests/ACKLocalizationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,36 @@ final class ACKLocalizationTests: XCTestCase {
let fileName = "Localizable.strings"
XCTAssertEqual("Localizable", fileName.removingSuffix(".strings"))
}

func test_valueRange_decodesIntegerCellValues() throws {
let json = #"{"values":[["key","en"],["some_key",42]]}"#
let valueRange = try JSONDecoder().decode(ValueRange.self, from: Data(json.utf8))
XCTAssertEqual(valueRange.values, [["key", "en"], ["some_key", "42"]])
}

func test_valueRange_decodesDoubleCellValues() throws {
let json = #"{"values":[["key","en"],["price_key",3.14]]}"#
let valueRange = try JSONDecoder().decode(ValueRange.self, from: Data(json.utf8))
XCTAssertEqual(valueRange.values, [["key", "en"], ["price_key", "3.14"]])
}

func test_valueRange_decodesMixedCellValues() throws {
let json = #"{"values":[["key","en"],["str_key","hello"],["int_key",7]]}"#
let valueRange = try JSONDecoder().decode(ValueRange.self, from: Data(json.utf8))
XCTAssertEqual(valueRange.values, [["key", "en"], ["str_key", "hello"], ["int_key", "7"]])
}

func test_transform_integerCellValue() throws {
let json = #"{"values":[["keys","en"],["count",42]]}"#
let valueRange = try JSONDecoder().decode(ValueRange.self, from: Data(json.utf8))
let mappedValues = try localization.transformValues(
valueRange,
with: ["en": "en"],
keyColumnName: "keys"
)
XCTAssertEqual(
mappedValues["en"],
[LocRow(key: "count", value: "42")]
)
}
}
Loading