Skip to content

Commit 3ba36da

Browse files
authored
dateDecodingStrategy and dateEncodingStrategy can be passed as a param for get/update/insert methods (#7)
1 parent c8e4b7f commit 3ba36da

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

Sources/CouchDBClient/CouchDBClient.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ public class CouchDBClient {
269269

270270
let data = Data(bytes)
271271
let decoder = JSONDecoder()
272-
decoder.dateDecodingStrategy = .secondsSince1970
273272

274273
do {
275274
let decodedResponse = try decoder.decode(UpdateDBResponse.self, from: data)
@@ -327,7 +326,6 @@ public class CouchDBClient {
327326

328327
let data = Data(bytes)
329328
let decoder = JSONDecoder()
330-
decoder.dateDecodingStrategy = .secondsSince1970
331329

332330
do {
333331
let decodedResponse = try decoder.decode(UpdateDBResponse.self, from: data)
@@ -453,7 +451,7 @@ public class CouchDBClient {
453451
/// - queryItems: Request query items.
454452
/// - eventLoopGroup: NIO's EventLoopGroup object. New will be created if nil value provided.
455453
/// - Returns: An object or a struct (of generic type) parsed from JSON.
456-
public func get <T: Codable & CouchDBRepresentable>(dbName: String, uri: String, queryItems: [URLQueryItem]? = nil, eventLoopGroup: EventLoopGroup? = nil) async throws -> T {
454+
public func get <T: Codable & CouchDBRepresentable>(dbName: String, uri: String, queryItems: [URLQueryItem]? = nil, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .secondsSince1970, eventLoopGroup: EventLoopGroup? = nil) async throws -> T {
457455
let response = try await get(dbName: dbName, uri: uri, queryItems: queryItems, eventLoopGroup: eventLoopGroup)
458456

459457
if response.status == .unauthorized {
@@ -466,7 +464,7 @@ public class CouchDBClient {
466464

467465
let data = Data(bytes)
468466
let decoder = JSONDecoder()
469-
decoder.dateDecodingStrategy = .secondsSince1970
467+
decoder.dateDecodingStrategy = dateDecodingStrategy
470468

471469
do {
472470
let doc = try decoder.decode(T.self, from: data)
@@ -557,7 +555,6 @@ public class CouchDBClient {
557555

558556
let data = Data(bytes)
559557
let decoder = JSONDecoder()
560-
decoder.dateDecodingStrategy = .secondsSince1970
561558

562559
do {
563560
let decodedResponse = try decoder.decode(CouchUpdateResponse.self, from: data)
@@ -605,12 +602,12 @@ public class CouchDBClient {
605602
/// - doc: Document object/struct. Should confirm to ``CouchDBRepresentable`` and Codable protocols.
606603
/// - eventLoopGroup: NIO's EventLoopGroup object. New will be created if nil value provided.
607604
/// - Returns: Update response.
608-
public func update <T: Codable & CouchDBRepresentable>(dbName: String, doc: inout T, eventLoopGroup: EventLoopGroup? = nil ) async throws {
605+
public func update <T: Codable & CouchDBRepresentable>(dbName: String, doc: inout T, dateEncodingStrategy: JSONEncoder.DateEncodingStrategy = .secondsSince1970, eventLoopGroup: EventLoopGroup? = nil ) async throws {
609606
guard let id = doc._id else { throw CouchDBClientError.idMissing }
610607
guard doc._rev?.isEmpty == false else { throw CouchDBClientError.revMissing }
611608

612609
let encoder = JSONEncoder()
613-
encoder.dateEncodingStrategy = .secondsSince1970
610+
encoder.dateEncodingStrategy = dateEncodingStrategy
614611
let encodedData = try JSONEncoder().encode(doc)
615612

616613
let updateResponse = try await update(
@@ -695,7 +692,6 @@ public class CouchDBClient {
695692

696693
let data = Data(bytes)
697694
let decoder = JSONDecoder()
698-
decoder.dateDecodingStrategy = .secondsSince1970
699695

700696
do {
701697
let decodedResponse = try decoder.decode(CouchUpdateResponse.self, from: data)
@@ -738,8 +734,11 @@ public class CouchDBClient {
738734
/// - dbName: DB name.
739735
/// - doc: Document object/struct. Should confirm to ``CouchDBRepresentable`` protocol.
740736
/// - eventLoopGroup: NIO's EventLoopGroup object. New will be created if nil value provided.
741-
public func insert <T: Codable & CouchDBRepresentable>(dbName: String, doc: inout T, eventLoopGroup: EventLoopGroup? = nil ) async throws {
742-
let insertEncodeData = try JSONEncoder().encode(doc)
737+
public func insert <T: Codable & CouchDBRepresentable>(dbName: String, doc: inout T, dateEncodingStrategy: JSONEncoder.DateEncodingStrategy = .secondsSince1970, eventLoopGroup: EventLoopGroup? = nil ) async throws {
738+
let encoder = JSONEncoder()
739+
encoder.dateEncodingStrategy = dateEncodingStrategy
740+
let insertEncodeData = try encoder.encode(doc)
741+
743742
let insertResponse = try await insert(
744743
dbName: dbName,
745744
body: .data(insertEncodeData),

Tests/CouchDBClientTests/CouchDBClientTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ final class CouchDBClientTests: XCTestCase {
242242
XCTAssertEqual(true, session?.ok)
243243
}
244244

245-
func test99_CreateDB() async throws {
245+
func test99_deleteDB() async throws {
246246
do {
247247
try await couchDBClient.deleteDB(testsDB)
248248
} catch {

0 commit comments

Comments
 (0)