Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[] Add config handler to Codable parser. #16

Merged
merged 2 commits into from
May 13, 2024
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
12 changes: 11 additions & 1 deletion Sources/Dyson/Map/CodableMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import Foundation

public struct CodableMap<Value: Decodable>: Map {
// MARK: - Property
private let config: (JSONDecoder) -> Void

// MARK: - Initializer
public init() { }
public init(config: @escaping (JSONDecoder) -> Void = { _ in }) {
self.config = config
}

// MARK: - Public
public func map(_ data: Data) throws -> Value {
let decoder = JSONDecoder()
config(decoder)

do {
return try decoder.decode(Value.self, from: data)
Expand All @@ -26,3 +30,9 @@ public struct CodableMap<Value: Decodable>: Map {

// MARK: - Private
}

public extension Mapper where Value: Decodable {
static func codable(config: @escaping (JSONDecoder) -> Void = { _ in }) -> Self {
Mapper(CodableMap<Value>(config: config))
}
}
8 changes: 0 additions & 8 deletions Sources/Dyson/Map/Map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,3 @@ public struct Mapper<Value>: Map {

// MARK: - Private
}

public extension Mapper {
static var none: Self { Mapper(NoneMap()) }
}

public extension Mapper where Value: Decodable {
static var codable: Self { Mapper(CodableMap<Value>()) }
}
4 changes: 4 additions & 0 deletions Sources/Dyson/Map/NoneMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ public struct NoneMap<Value>: Map {

// MARK: - Private
}

public extension Mapper {
static var none: Self { Mapper(NoneMap()) }
}
15 changes: 15 additions & 0 deletions Sources/Dyson/Request/BodyRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,18 @@ public struct BodyRequest<Parameter>: Request {

// MARK: - Private
}

public extension Request {
static func body<Parameter>(
_ parameter: Parameter,
encoder: Encoder<Parameter>
) -> Self where Self == BodyRequest<Parameter> {
BodyRequest(parameter, encoder: encoder)
}

static func body(
_ parameter: Data
) -> Self where Self == BodyRequest<Data> {
BodyRequest(parameter, encoder: .none)
}
}
12 changes: 12 additions & 0 deletions Sources/Dyson/Request/Encode/CodableEncode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,26 @@ import Foundation

public struct CodableEncode<Value: Encodable>: Encode {
// MARK: - Property
private let config: (JSONEncoder) -> Void

// MARK: - Initalizer
public init(config: @escaping (JSONEncoder) -> Void = { _ in }) {
self.config = config
}

// MARK: - Public
public func encode(_ value: Value) throws -> Data {
let encoder = JSONEncoder()
config(encoder)

return try encoder.encode(value)
}

// MARK: - Private
}

public extension Encoder where Value: Encodable {
static func codable(config: @escaping (JSONEncoder) -> Void = { _ in }) -> Self {
Encoder(CodableEncode(config: config))
}
}
8 changes: 0 additions & 8 deletions Sources/Dyson/Request/Encode/Encode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,3 @@ public struct Encoder<Value>: Encode {

// MARK: - Private
}

public extension Encoder where Value == Data {
static var none: Self { Encoder(NoneEncode()) }
}

public extension Encoder where Value: Encodable {
static var codable: Self { Encoder(CodableEncode()) }
}
4 changes: 4 additions & 0 deletions Sources/Dyson/Request/Encode/NoneEncode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ public struct NoneEncode: Encode {

// MARK: - Private
}

public extension Encoder where Value == Data {
static var none: Self { Encoder(NoneEncode()) }
}
4 changes: 4 additions & 0 deletions Sources/Dyson/Request/NoneRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ public struct NoneRequest: Request {

// MARK: - Private
}

public extension Request where Self == NoneRequest {
static var none: Self { NoneRequest() }
}
8 changes: 8 additions & 0 deletions Sources/Dyson/Request/QueryRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ public struct QueryRequest: Request {

// MARK: - Private
}

public extension Request {
static func query(
_ parameter: [String: String]
) -> Self where Self == QueryRequest {
QueryRequest(parameter)
}
}
27 changes: 0 additions & 27 deletions Sources/Dyson/Request/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,3 @@ public extension Request {
try make(url: url)
}
}

public extension Request where Self == NoneRequest {
static var none: Self { NoneRequest() }
}

public extension Request {
static func query(
_ parameter: [String: String]
) -> Self where Self == QueryRequest {
QueryRequest(parameter)
}
}

public extension Request {
static func body<Parameter>(
_ parameter: Parameter,
encoder: Encoder<Parameter>
) -> Self where Self == BodyRequest<Parameter> {
BodyRequest(parameter, encoder: encoder)
}

static func body(
_ parameter: Data
) -> Self where Self == BodyRequest<Data> {
BodyRequest(parameter, encoder: .none)
}
}