Skip to content

Commit

Permalink
feat: decode array safely extension for key decoding container
Browse files Browse the repository at this point in the history
  • Loading branch information
makinosp committed Aug 2, 2024
1 parent 334e1b7 commit 0a81bc8
Showing 1 changed file with 27 additions and 0 deletions.
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()
}
}

0 comments on commit 0a81bc8

Please sign in to comment.