From 59354b5e854d8821cc8fd3df489f5aa08fdfcc91 Mon Sep 17 00:00:00 2001 From: hiddenviewer Date: Fri, 25 Oct 2024 18:42:53 +0900 Subject: [PATCH] Fix SwiftLint violations --- Sources/API/Converter.swift | 5 ++++- Sources/Document/CRDT/CRDTTree.swift | 6 ++++-- Sources/Util/Codable+Extension.swift | 2 +- Sources/Util/Dictionary+Extension.swift | 6 ++++-- Tests/Unit/Document/CRDT/PrimitiveTests.swift | 2 +- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Sources/API/Converter.swift b/Sources/API/Converter.swift index a067d366..5b3d72f2 100644 --- a/Sources/API/Converter.swift +++ b/Sources/API/Converter.swift @@ -34,7 +34,10 @@ enum Converter { let result = Double(bitPattern: UInt64(littleEndian: data.withUnsafeBytes { $0.load(as: UInt64.self) })) return .double(result) case .string: - return .string(String(decoding: data, as: UTF8.self)) + guard let stringValue = String(data: data, encoding: .utf8) else { + throw YorkieError.unimplemented(message: String(describing: valueType)) + } + return .string(stringValue) case .long: let result = Int64(littleEndian: data.withUnsafeBytes { $0.load(as: Int64.self) }) return .long(result) diff --git a/Sources/Document/CRDT/CRDTTree.swift b/Sources/Document/CRDT/CRDTTree.swift index 0408ce84..6df07239 100644 --- a/Sources/Document/CRDT/CRDTTree.swift +++ b/Sources/Document/CRDT/CRDTTree.swift @@ -487,8 +487,10 @@ final class CRDTTreeNode: IndexTreeNode { } var toJSONString: String { - if let data = try? JSONSerialization.data(withJSONObject: toDictionary, options: [.sortedKeys]) { - return String(decoding: data, as: UTF8.self) + if let data = try? JSONSerialization.data(withJSONObject: toDictionary, options: [.sortedKeys]), + let jsonString = String(data: data, encoding: .utf8) + { + return jsonString } return "{}" diff --git a/Sources/Util/Codable+Extension.swift b/Sources/Util/Codable+Extension.swift index efbe9566..285016d2 100644 --- a/Sources/Util/Codable+Extension.swift +++ b/Sources/Util/Codable+Extension.swift @@ -36,7 +36,7 @@ extension Encodable { return nil } - return String(decoding: data, as: UTF8.self) + return String(data: data, encoding: .utf8) } } diff --git a/Sources/Util/Dictionary+Extension.swift b/Sources/Util/Dictionary+Extension.swift index 07f9b2ee..4446faea 100644 --- a/Sources/Util/Dictionary+Extension.swift +++ b/Sources/Util/Dictionary+Extension.swift @@ -79,8 +79,10 @@ extension StringValueTypeDictionary { } return jsonObject.mapValues { - if let result = try? JSONSerialization.data(withJSONObject: $0, options: [.fragmentsAllowed, .withoutEscapingSlashes, .sortedKeys]) { - return String(decoding: result, as: UTF8.self) + if let result = try? JSONSerialization.data(withJSONObject: $0, options: [.fragmentsAllowed, .withoutEscapingSlashes, .sortedKeys]), + let stringValue = String(data: result, encoding: .utf8) + { + return stringValue } else { return "" } diff --git a/Tests/Unit/Document/CRDT/PrimitiveTests.swift b/Tests/Unit/Document/CRDT/PrimitiveTests.swift index 4df5bdb1..5df765b2 100644 --- a/Tests/Unit/Document/CRDT/PrimitiveTests.swift +++ b/Tests/Unit/Document/CRDT/PrimitiveTests.swift @@ -90,7 +90,7 @@ class PrimitiveTests: XCTestCase { let valueFromData = try Converter.valueFrom(.bytes, data: primitiveValue.toBytes()) switch valueFromData { case .bytes(let value): - XCTAssertEqual(String(decoding: value, as: UTF8.self), "abcdefg") + XCTAssertEqual(String(data: value, encoding: .utf8), "abcdefg") default: XCTFail("Type error.") }