|
| 1 | +//===--- OrderedDictionary+Storage ------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the UDF open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2026 You are launched |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See https://opensource.org/licenses/Apache License-2.0 for license information |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +import Foundation |
| 13 | + |
| 14 | +public extension OrderedDictionary { |
| 15 | + /// Appends a value to an `OrderedSet` stored in the dictionary under the specified key. |
| 16 | + /// |
| 17 | + /// - Parameters: |
| 18 | + /// - value: The value to append. |
| 19 | + /// - key: The key under which the `OrderedSet` is stored. |
| 20 | + mutating func append<V>(_ value: V, by key: Key) where Value == OrderedSet<V> { |
| 21 | + var set = self[key] ?? [] |
| 22 | + set.append(value) |
| 23 | + self[key] = set |
| 24 | + } |
| 25 | + |
| 26 | + /// Appends an array of values to an `OrderedSet` stored in the dictionary under the specified key. |
| 27 | + /// |
| 28 | + /// - Parameters: |
| 29 | + /// - values: The array of values to append. |
| 30 | + /// - key: The key under which the `OrderedSet` is stored. |
| 31 | + mutating func append<V>(_ values: [V], by key: Key) where Value == OrderedSet<V> { |
| 32 | + var set = self[key] ?? [] |
| 33 | + set.append(contentsOf: values) |
| 34 | + self[key] = set |
| 35 | + } |
| 36 | + |
| 37 | + /// Appends an `Identifiable` value's `id` to an `OrderedSet` stored in the dictionary under the specified key. |
| 38 | + /// |
| 39 | + /// - Parameters: |
| 40 | + /// - value: The `Identifiable` value to append. |
| 41 | + /// - key: The key under which the `OrderedSet` of IDs is stored. |
| 42 | + mutating func append<V: Identifiable>(_ value: V, by key: Key) where Value == OrderedSet<V.ID> { |
| 43 | + append(value.id, by: key) |
| 44 | + } |
| 45 | + |
| 46 | + /// Appends an array of `Identifiable` values' `ids` to an `OrderedSet` stored in the dictionary under the specified key. |
| 47 | + /// |
| 48 | + /// - Parameters: |
| 49 | + /// - values: The array of `Identifiable` values to append. |
| 50 | + /// - key: The key under which the `OrderedSet` of IDs is stored. |
| 51 | + mutating func append<V: Identifiable>(_ values: [V], by key: Key) where Value == OrderedSet<V.ID> { |
| 52 | + append(values.map(\.id), by: key) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +public extension OrderedDictionary { |
| 57 | + /// Appends a dictionary to a nested dictionary stored in the current dictionary under the specified key. |
| 58 | + /// |
| 59 | + /// - Parameters: |
| 60 | + /// - value: The dictionary to append. |
| 61 | + /// - key: The key under which the nested dictionary is stored. |
| 62 | + mutating func append<VKey, VValue>(_ value: Value, by key: Key) where Value == [VKey: VValue] { |
| 63 | + var dict: Value = self[key] ?? [:] |
| 64 | + dict.merge(dict: value) |
| 65 | + self[key] = dict |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +public extension OrderedDictionary { |
| 70 | + /// Merges the given dictionary into the current dictionary, updating values for matching keys. |
| 71 | + /// |
| 72 | + /// - Parameter dict: The dictionary to merge. |
| 73 | + mutating func merge(dict: [Key: Value]) { |
| 74 | + for (k, v) in dict { |
| 75 | + updateValue(v, forKey: k) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments