From e33b2b3533dc57cc87be326cebfdf510e15c9fae Mon Sep 17 00:00:00 2001 From: makinosp Date: Fri, 2 Aug 2024 22:46:26 +0900 Subject: [PATCH] feat: modify nullable array to safe decoding array --- Sources/VRCKit/Utils/NullableArray.swift | 33 -------------------- Sources/VRCKit/Utils/SafeDecodingArray.swift | 25 +++++++++++++++ 2 files changed, 25 insertions(+), 33 deletions(-) delete mode 100644 Sources/VRCKit/Utils/NullableArray.swift create mode 100644 Sources/VRCKit/Utils/SafeDecodingArray.swift diff --git a/Sources/VRCKit/Utils/NullableArray.swift b/Sources/VRCKit/Utils/NullableArray.swift deleted file mode 100644 index 37e2b5c..0000000 --- a/Sources/VRCKit/Utils/NullableArray.swift +++ /dev/null @@ -1,33 +0,0 @@ -// -// NullableArray.swift -// -// -// Created by makinosp on 2024/07/28. -// - -import Foundation - -@propertyWrapper -public struct NullableArray { - public let wrappedValue: [T] - public init(wrappedValue: [T]) { - self.wrappedValue = wrappedValue - } -} - -extension NullableArray: Codable { - public init(from decoder: Decoder) throws { - let container = try decoder.singleValueContainer() - if container.decodeNil() { - wrappedValue = [] - } else { - wrappedValue = try container.decode([T].self) - } - } -} - -extension NullableArray: Hashable { - public static func == (lhs: NullableArray, rhs: NullableArray) -> Bool { - lhs == rhs - } -} diff --git a/Sources/VRCKit/Utils/SafeDecodingArray.swift b/Sources/VRCKit/Utils/SafeDecodingArray.swift new file mode 100644 index 0000000..21e378e --- /dev/null +++ b/Sources/VRCKit/Utils/SafeDecodingArray.swift @@ -0,0 +1,25 @@ +// +// SafeDecodingArray.swift +// +// +// Created by makinosp on 2024/07/28. +// + +import Foundation + +public struct SafeDecodingArray { + public let elements: [Element] +} + +extension SafeDecodingArray: Codable { + public init(from decoder: Decoder) throws { + let container = try decoder.singleValueContainer() + elements = (try? container.decode([Element].self)) ?? [] + } +} + +extension SafeDecodingArray: Hashable { + public static func == (lhs: SafeDecodingArray, rhs: SafeDecodingArray) -> Bool { + lhs == rhs + } +}