-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
584a84b
commit 35ed5c4
Showing
8 changed files
with
252 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
Sources/OpenAPIKit/AnyCodable/DataEncodingStrategies.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Foundation | ||
|
||
public extension ValueEncodingStrategy { | ||
|
||
/// Data encoding strategy to use when encoding `AnyCodable` values. | ||
enum Data { | ||
} | ||
} | ||
|
||
public extension ValueEncodingStrategy.Data { | ||
static var `default`: ValueEncodingStrategy = .Data.base64 | ||
|
||
/// Base64 string, schema: .string(format: .byte) | ||
static var base64: ValueEncodingStrategy { | ||
.Data.base64(options: []) | ||
} | ||
|
||
/// Base64 string, schema: .string(format: .byte) | ||
static func base64(options: Data.Base64EncodingOptions) -> ValueEncodingStrategy { | ||
ValueEncodingStrategy(Data.self) { data, encoder in | ||
var container = encoder.singleValueContainer() | ||
try container.encode(data.base64EncodedString(options: options)) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
Sources/OpenAPIKit/AnyCodable/DateEncodingStrategies.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import Foundation | ||
|
||
public extension ValueEncodingStrategy { | ||
|
||
/// Date encoding strategy to use when encoding `AnyCodable` values. | ||
enum Date { | ||
} | ||
} | ||
|
||
public extension ValueEncodingStrategy.Date { | ||
static var `default`: ValueEncodingStrategy = .Date.dateTime | ||
|
||
/// full-date notation as defined by RFC 3339, section 5.6, for example, 2017-07-21, schema: .string(format: .date) | ||
static var date: ValueEncodingStrategy { | ||
.Date.custom { date, encoder in | ||
try encoder.encode(ValueEncodingStrategy.Date.date(date)) | ||
} | ||
} | ||
|
||
/// the date-time notation as defined by RFC 3339, section 5.6, for example, 2017-07-21T17:32:28Z, schema: .string(format: .dateTime) | ||
static var dateTime: ValueEncodingStrategy { | ||
.Date.custom { date, encoder in | ||
try encoder.encode(ValueEncodingStrategy.Date.dateTime(date)) | ||
} | ||
} | ||
|
||
/// the interval between the date value and 00:00:00 UTC on 1 January 1970, schema: .number(format: .other("timestamp")) | ||
static var timestamp: ValueEncodingStrategy { | ||
.Date.custom { date, encoder in | ||
try encoder.encode(date.timeIntervalSince1970) | ||
} | ||
} | ||
|
||
/// Custom date encoding strategy | ||
static func custom( | ||
encode: @escaping (Date, inout SingleValueEncodingContainer) throws -> Void | ||
) -> ValueEncodingStrategy { | ||
ValueEncodingStrategy(Date.self) { | ||
var container = $1.singleValueContainer() | ||
try encode($0, &container) | ||
} | ||
} | ||
|
||
/// Custom date encoding strategy | ||
static func custom( | ||
_ dataFormat: JSONTypeFormat.StringFormat, | ||
formatter: DateFormatter | ||
) -> ValueEncodingStrategy { | ||
.Date.custom { date, encoder in | ||
try encoder.encode(formatter.string(from: date)) | ||
} | ||
} | ||
} | ||
|
||
extension ValueEncodingStrategy.Date { | ||
static func dateTime(_ date: Date) -> String { | ||
isoFormatter.string(from: date) | ||
} | ||
|
||
static func date(_ date: Date) -> String { | ||
dateFormatter.dateFormat = "yyyy-MM-dd" | ||
return dateFormatter.string(from: date) | ||
} | ||
} | ||
|
||
private let isoFormatter: DateFormatter = { | ||
let formatter = DateFormatter() | ||
formatter.locale = Locale(identifier: "en_US_POSIX") | ||
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" | ||
formatter.timeZone = TimeZone(secondsFromGMT: 0) | ||
return formatter | ||
}() | ||
private let dateFormatter = DateFormatter() |
28 changes: 28 additions & 0 deletions
28
Sources/OpenAPIKit/AnyCodable/DecimalEncodingStrategies.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Foundation | ||
|
||
public extension ValueEncodingStrategy { | ||
|
||
/// Decimal encoding strategy to use when encoding `AnyCodable` values. | ||
enum Decimal { | ||
} | ||
} | ||
|
||
public extension ValueEncodingStrategy.Decimal { | ||
static var `default`: ValueEncodingStrategy = .Decimal.number | ||
|
||
/// Quoted string | ||
static var string: ValueEncodingStrategy { | ||
ValueEncodingStrategy(Decimal.self) { decimal, encoder in | ||
var container = encoder.singleValueContainer() | ||
try container.encode(decimal.description) | ||
} | ||
} | ||
|
||
/// Number | ||
static var number: ValueEncodingStrategy { | ||
ValueEncodingStrategy(Decimal.self) { decimal, encoder in | ||
var container = encoder.singleValueContainer() | ||
try container.encode((decimal as NSDecimalNumber).doubleValue) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Foundation | ||
|
||
public extension ValueEncodingStrategy { | ||
|
||
/// URL encoding strategy to use when encoding `AnyCodable` values. | ||
enum URL { | ||
} | ||
} | ||
|
||
public extension ValueEncodingStrategy.URL { | ||
static var `default`: ValueEncodingStrategy = .URL.uri | ||
|
||
/// URI string, schema: .string(format: .other("uri")) | ||
static var uri: ValueEncodingStrategy { | ||
ValueEncodingStrategy(URL.self) { url, encoder in | ||
var container = encoder.singleValueContainer() | ||
try container.encode(url.absoluteString) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import Foundation | ||
|
||
public struct ValueEncodingStrategy { | ||
|
||
public let encode: (Encodable, Encoder) throws -> Bool | ||
|
||
public init<T: Encodable>( | ||
_ type: T.Type, | ||
encode: @escaping (T, Encoder) throws -> Void | ||
) { | ||
self.encode = { | ||
guard let value = $0 as? T else { | ||
return false | ||
} | ||
try encode(value, $1) | ||
return true | ||
} | ||
} | ||
} |
Oops, something went wrong.