1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import Foundation
extension KeyedEncodingContainer {
/// Encodes the given variant collection.
mutating func encodeVariantCollection<Value>(
_ variantCollection: VariantCollection<Value>,
forKey key: Key,
encoder: Encoder
) throws {
try encode(variantCollection, forKey: key)
}
/// Encodes the given boolean variant collection if its value is true.
mutating func encodeVariantCollectionIfTrue(
_ variantCollection: VariantCollection<Bool>,
forKey key: Key,
encoder: Encoder
) throws {
if variantCollection.defaultValue {
try encode(variantCollection.defaultValue, forKey: key)
}
variantCollection.addVariantsToEncoder(
encoder,
// Add the key to the encoder's coding path, since the coding path refers to the value's parent.
pointer: JSONPointer(from: encoder.codingPath + [key]),
isDefaultValueEncoded: variantCollection.defaultValue
)
}
/// Encodes the given variant collection for its non-empty values.
mutating func encodeVariantCollectionIfNotEmpty(
_ variantCollection: VariantCollection<some Collection>,
forKey key: Key,
encoder: Encoder
) throws {
try encodeIfNotEmpty(variantCollection.defaultValue, forKey: key)
variantCollection.mapValues { value in
// Encode `nil` if the value is empty, so that when the patch is applied, it effectively
// removes the default value.
value.isEmpty ? nil : value
}.addVariantsToEncoder(
encoder,
// Add the key to the encoder's coding path, since the coding path refers to the value's parent.
pointer: JSONPointer(from: encoder.codingPath + [key]),
isDefaultValueEncoded: !variantCollection.defaultValue.isEmpty
)
}
/// Encodes the given variant collection.
mutating func encodeVariantCollectionIfNotEmpty(
_ variantCollection: VariantCollection<(some Collection)?>,
forKey key: Key,
encoder: Encoder
) throws {
if let defaultValue = variantCollection.defaultValue {
try encodeIfNotEmpty(defaultValue, forKey: key)
}
variantCollection.addVariantsToEncoder(
encoder,
// Add the key to the encoder's coding path, since the coding path refers to the value's parent.
pointer: JSONPointer(from: encoder.codingPath + [key]),
isDefaultValueEncoded: variantCollection.defaultValue.map { !$0.isEmpty } ?? false
)
}
/// Encodes the given variant collection, writing the default value if it's non-nil.
///
/// Use this API to encode a variant collection and accumulate variants into the given encoder.
///
/// > Note: The default value is encoded only if it's non-nil.
mutating func encodeVariantCollection<Value>(
_ variantCollection: VariantCollection<Value?>,
forKey key: Key,
encoder: Encoder
) throws {
try encodeIfPresent(variantCollection.defaultValue, forKey: key)
variantCollection.addVariantsToEncoder(
encoder,
// Add the key to the encoder's coding path, since the coding path refers to the value's parent.
pointer: JSONPointer(from: encoder.codingPath + [key]),
isDefaultValueEncoded: variantCollection.defaultValue != nil
)
}
/// Encodes the given variant collection array if it's non-empty.
mutating func encodeVariantCollectionArrayIfNotEmpty<Value>(
_ variantCollectionValues: [VariantCollection<Value?>],
forKey key: Key,
encoder: Encoder
) throws {
try encodeIfNotEmpty(variantCollectionValues.compactMap(\.defaultValue), forKey: key)
for (index, variantCollection) in variantCollectionValues.enumerated() {
variantCollection.addVariantsToEncoder(
encoder,
// Add the index to the encoder's coding path, since the coding path refers to the array.
pointer: JSONPointer(from: encoder.codingPath + [key, JSON.IntegerKey(index)]),
// Since `nil` default values are filtered out above, we need to know when to `add` instead of `replace` an item.
isDefaultValueEncoded: variantCollection.defaultValue != nil
)
}
}
}
extension KeyedDecodingContainer {
/// Decodes the given variant collection.
func decodeVariantCollection<Value>(
ofValueType: Value.Type,
forKey key: Key
) throws -> VariantCollection<Value> {
try decode(VariantCollection<Value>.self, forKey: key)
}
/// Decodes the given variant collection and returns nil if it's not present.
func decodeVariantCollectionIfPresent<Value>(
ofValueType: Value.Type,
forKey key: Key
) throws -> VariantCollection<Value>? {
try decodeIfPresent(VariantCollection<Value>.self, forKey: key)
}
/// Decodes the given variant collection of optional value and empty variant collection if it's no value is present.
func decodeVariantCollectionIfPresent<Value>(
ofValueType: Value?.Type,
forKey key: Key
) throws -> VariantCollection<Value?> {
try decodeIfPresent(VariantCollection<Value?>.self, forKey: key) ?? .init(defaultValue: nil)
}
/// Decodes the given array of variant collections.
func decodeVariantCollectionArrayIfPresent<Value>(
ofValueType: Value?.Type,
forKey key: Key
) throws -> [VariantCollection<Value?>] {
try decodeIfPresent([VariantCollection<Value?>].self, forKey: key) ?? []
}
}
|