-
-
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.
feat: decode array safely extension for key decoding container
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
Sources/VRCKit/Extensions/KeyedDecodingContainer+decodeSafeNullableArray.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,27 @@ | ||
// | ||
// KeyedDecodingContainer+decodeSafeNullableArray.swift | ||
// VRCKit | ||
// | ||
// Created by makinosp on 2024/08/02. | ||
// | ||
|
||
import Foundation | ||
|
||
public extension KeyedDecodingContainer { | ||
/// Decodes an array for the specified key and returns it. If the decoding fails, it returns an empty array. | ||
/// | ||
/// This method attempts to decode a `SafeDecodingArray` from the container for the specified key. If the key is not | ||
/// present or if the decoding fails, it returns an empty `SafeDecodingArray`. | ||
/// | ||
/// - Parameters: | ||
/// - type: The type of elements in the array to decode. | ||
/// - key: The key that the decoded value is associated with. | ||
/// - Returns: A `SafeDecodingArray` of the specified type. | ||
/// - Throws: Rethrows any errors encountered during decoding. | ||
func decodeSafeNullableArray<T>( | ||
_ type: T.Type, | ||
forKey key: KeyedDecodingContainer<K>.Key | ||
) throws -> SafeDecodingArray<T> where T: Decodable { | ||
try decodeIfPresent(SafeDecodingArray<T>.self, forKey: key) ?? SafeDecodingArray() | ||
} | ||
} |