generated from Apodini/Template-Repository
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
10 changed files
with
218 additions
and
25 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
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
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
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
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,43 @@ | ||
// | ||
// This source file is part of the Apodini open source project | ||
// | ||
// SPDX-FileCopyrightText: 2019-2022 Paul Schmiedmayer and the Apodini project authors (see CONTRIBUTORS.md) <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import Foundation | ||
|
||
/// Type erased ``CodableContextKey``. | ||
public protocol AnyCodableContextKey: AnyContextKey { | ||
static var identifier: String { get } | ||
|
||
static func anyEncode(value: Any) throws -> Data | ||
} | ||
|
||
/// An ``OptionalContextKey`` which value is able to be encoded and decoded. | ||
public protocol CodableContextKey: AnyCodableContextKey, OptionalContextKey where Value: Codable { | ||
static func decode(from data: Data) throws -> Value | ||
} | ||
|
||
// Data, by default, is encoded as base64 | ||
private let encoder = JSONEncoder() | ||
private let decoder = JSONDecoder() | ||
|
||
extension CodableContextKey { | ||
public static var identifier: String { | ||
"\(Self.self)" | ||
} | ||
|
||
public static func anyEncode(value: Any) throws -> Data { | ||
guard let value = value as? Self.Value else { | ||
fatalError("CodableContextKey.anyEncode(value:) received illegal value type \(type(of: value)) instead of \(Value.self)") | ||
} | ||
|
||
return try encoder.encode(value) | ||
} | ||
|
||
public static func decode(from data: Data) throws -> Value { | ||
try decoder.decode(Value.self, from: data) | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -1,17 +1,20 @@ | ||
// | ||
// | ||
// This source file is part of the Apodini open source project | ||
// | ||
// SPDX-FileCopyrightText: 2019-2021 Paul Schmiedmayer and the Apodini project authors (see CONTRIBUTORS.md) <[email protected]> | ||
// SPDX-FileCopyrightText: 2019-2022 Paul Schmiedmayer and the Apodini project authors (see CONTRIBUTORS.md) <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
// | ||
|
||
/// Type erased ContextKey. | ||
public protocol AnyContextKey {} | ||
|
||
/// A `OptionalContextKey` serves as a key definition for a `ContextNode`. | ||
/// Optionally it can serve a reduction logic when inserting a new value into the `ContextNode`, | ||
/// see `OptionalContextKey.reduce(...)`. | ||
/// The `OptionalContextKey` is optional in the sense that it doesn't provide a default value, meaning | ||
/// it may not exist on the `Context` for a given `Handler`. | ||
public protocol OptionalContextKey { | ||
public protocol OptionalContextKey: AnyContextKey { | ||
/// The type of the value the `OptionalContextKey` identifies. The value MUST NOT be of type `Optional`. | ||
associatedtype Value | ||
|
||
|
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
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