Skip to content

Commit

Permalink
Merge pull request #11 from iWECon/dev
Browse files Browse the repository at this point in the history
add Expressionable initialize
  • Loading branch information
iWECon authored Jun 30, 2022
2 parents 5cb9bf5 + edf9cbb commit 502e9a9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 21 deletions.
59 changes: 39 additions & 20 deletions Sources/Lookup/Lookup.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
import Foundation

// MARK: - extension Array Helper merging multi lookup into one lookup
public extension Array where Element == Lookup {

/// Merging multi `rawDict` into one
/// ⚠️ Only support `Dictionary`
///
/// - Parameter uniquingKeysWith: uniquing keys with conflict
/// - Returns: Merged `Lookup`
func merging(uniquingKeysWith: (Any, Any) -> Any) -> Lookup {
let dictLookups = self.compactMap({ $0.dict })
var temp: [String: Any] = [:]
for value in dictLookups {
temp.merge(value, uniquingKeysWith: uniquingKeysWith)
}
return Lookup(temp)
}
}

extension Array {
var countIndex: Int {
count - 1
Expand All @@ -34,7 +16,7 @@ fileprivate extension String {
}

// MARK: Unwrap
fileprivate func unwrap(_ object: Any) -> Any {
fileprivate func unwrap(_ object: Any?) -> Any {
switch object {
case let lookup as Lookup:
return unwrap(lookup.object)
Expand All @@ -58,7 +40,7 @@ fileprivate func unwrap(_ object: Any) -> Any {
return array.map(unwrap)

default:
return object
return object ?? NSNull()
}
}

Expand Down Expand Up @@ -286,6 +268,17 @@ public struct Lookup: Swift.CustomStringConvertible, Swift.CustomDebugStringConv
public var debugDescription: String { description }

fileprivate static var null: Lookup { Lookup(NSNull()) }

fileprivate mutating func merge(other: Lookup) {
switch (self.rawType, other.rawType) {
case (.dict, .dict):
self.rawDict.merge(other.rawDict, uniquingKeysWith: { $1 })
case (.array, .array):
self.rawArray += other.rawArray
default:
break
}
}
}

extension Lookup: ExpressibleByArrayLiteral {
Expand All @@ -296,6 +289,14 @@ extension Lookup: ExpressibleByArrayLiteral {
}
}

extension Lookup: ExpressibleByDictionaryLiteral {
public typealias Key = String
public typealias Value = Any
public init(dictionaryLiteral elements: (Key, Value)...) {
self.init(jsonObject: Dictionary(uniqueKeysWithValues: elements))
}
}

extension Lookup: ExpressibleByStringLiteral {
public init(stringLiteral value: StringLiteralType) {
self.init(jsonObject: value)
Expand Down Expand Up @@ -529,6 +530,24 @@ public extension Lookup {
}
}


// MARK: - Operator
public func + (lhs: Lookup, rhs: Lookup) -> Lookup {
switch (lhs.rawType, rhs.rawType) {
case (.dict, .dict):
let lhsRawDict = lhs.rawDict
return Lookup(lhsRawDict.merging(rhs.rawDict, uniquingKeysWith: { $1 }))
case (.array, .array):
return Lookup(lhs.rawArray + rhs.rawArray)
default:
return .null
}
}

public func += (lhs: inout Lookup, rhs: Lookup) {
lhs.merge(other: rhs)
}

// MARK: - Codable
extension Lookup: Codable {

Expand Down
16 changes: 15 additions & 1 deletion Tests/LookupTests/LookupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,23 @@ final class LookupTests: QuickSpec {
it("merge dictionary lookups") {
let lookup1 = Lookup(["name": "Lookup", "age": 3])
let lookup2 = Lookup(["age": 1])
let merged = [lookup1, lookup2].merging(uniquingKeysWith: { $1 })
let merged = lookup1 + lookup2
expect(merged.name.string) == "Lookup"
expect(merged.age.int) == 1

var lookup3 = Lookup(["name": "Lookup", "brief": "A data handle tools."])
let lookup4 = Lookup(["age": 1])
lookup3 += lookup4
expect(lookup3.age.int) == 1
expect(lookup3.name.string) == "Lookup"
expect(lookup3.brief.string) == "A data handle tools."

let lookup5 = lookup3 + ["brief": "json handle tools"]
expect(lookup5.brief.string) == "json handle tools"

let lookup6 = Lookup([1, 2, 3, 4, 5])
let lookup7 = lookup6 + [4, 5, 6, 7, 8]
expect(lookup7.arrayValue.count) == 10
}

// MARK: NOT SUPOORT NOW
Expand Down

0 comments on commit 502e9a9

Please sign in to comment.