Skip to content

Commit

Permalink
Adds the Mapping Map for NSMutableDictionary, NSMutableArray and NSMu…
Browse files Browse the repository at this point in the history
…tableSet
  • Loading branch information
PopTo committed Aug 10, 2024
1 parent 6d10475 commit d77e196
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions Sources/Yams/Constructor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ extension Constructor {
.omap: [Any].construct_omap,
.pairs: [Any].construct_pairs
]

public static let dynamicMappingMap: MappingMap = [
.map: NSMutableDictionary.construct_mapping,
.set: NSMutableSet.construct_set
]

public static let dynamicSequenceMap: SequenceMap = [
.seq: NSMutableArray.construct_seq,
.omap: NSMutableArray.construct_omap,
.pairs: NSMutableArray.construct_pairs
]
}

// MARK: - ScalarConstructible
Expand Down Expand Up @@ -433,6 +444,31 @@ private extension Dictionary {
}
}

extension NSMutableDictionary {
/// Construct an `NSMutableDictionary`, if possible, from the specified mapping.
///
/// - parameter mapping: The `Node.Mapping` from which to extract an `NSMutableDictionary`, if possible.
///
/// - returns: An instance of `NSMutableDictionary`, if one was successfully extracted from the mapping.
public static func construct_mapping(from mapping: Node.Mapping) -> NSMutableDictionary? {
_construct_mapping(from: mapping)
}
}

private extension NSMutableDictionary {
static func _construct_mapping(from mapping: Node.Mapping) -> NSMutableDictionary {
let result = NSMutableDictionary()
let mapping = mapping.flatten()

mapping.forEach { key, value in
if let keyString = String.construct(from: key) {
result[keyString] = mapping.tag.constructor.any(from: value)
}
}
return result
}
}

extension Set {
/// Construct a `Set`, if possible, from the specified mapping.
///
Expand All @@ -447,6 +483,23 @@ extension Set {
}
}

extension NSMutableSet {
/// Construct an `NSMutableSet`, if possible, from the specified mapping.
///
/// - parameter mapping: The `Node.Mapping` from which to extract an `NSMutableSet`, if possible.
///
/// - returns: An instance of `NSMutableSet`, if one was successfully extracted from the mapping.
public static func construct_set(from mapping: Node.Mapping) -> NSMutableSet? {
let result = NSMutableSet()
mapping.forEach { key, _ in
if let keyString = String.construct(from: key) {
result.add(keyString as AnyHashable)
}
}
return result
}
}

// MARK: Sequence

extension Array {
Expand Down Expand Up @@ -488,6 +541,53 @@ extension Array {
}
}

extension NSMutableArray {
/// Construct an NSMutableArray of `Any` from the specified `sequence`.
///
/// - parameter sequence: Sequence to convert to `NSMutableArray`.
///
/// - returns: NSMutableArray of `Any`.
public static func construct_seq(from sequence: Node.Sequence) -> NSMutableArray {
let result = NSMutableArray()
sequence.forEach { subnode in
result.add(sequence.tag.constructor.any(from: subnode))
}
return result
}

/// Construct an "O-map" (NSMutableArray of `(Any, Any)` tuples) from the specified `sequence`.
///
/// - parameter sequence: Sequence to convert to `NSMutableArray`.
///
/// - returns: NSMutableArray of `(Any, Any)` tuples.
public static func construct_omap(from sequence: Node.Sequence) -> NSMutableArray {
let result = NSMutableArray()
sequence.forEach { subnode in
// TODO: Should raise error if subnode is not mapping or mapping.count != 1
if let (key, value) = subnode.mapping?.first {
result.add((sequence.tag.constructor.any(from: key), sequence.tag.constructor.any(from: value)))
}
}
return result
}

/// Construct an NSMutableArray of `(Any, Any)` tuples from the specified `sequence`.
///
/// - parameter sequence: Sequence to convert to `NSMutableArray`.
///
/// - returns: NSMutableArray of `(Any, Any)` tuples.
public static func construct_pairs(from sequence: Node.Sequence) -> NSMutableArray {
let result = NSMutableArray()
sequence.forEach { subnode in
if let (key, value) = subnode.mapping?.first {
result.add((sequence.tag.constructor.any(from: key), sequence.tag.constructor.any(from: value)))
}
}
return result
}
}


private extension String {
func substring(with range: NSRange) -> Substring? {
guard range.location != NSNotFound else { return nil }
Expand Down

0 comments on commit d77e196

Please sign in to comment.