Skip to content

Commit

Permalink
♻️ Split up OrderComparator
Browse files Browse the repository at this point in the history
  • Loading branch information
oschrenk committed Jan 19, 2025
1 parent 66e3bcb commit 89f24ca
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 65 deletions.
65 changes: 0 additions & 65 deletions Sources/Model/Order.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,68 +63,3 @@ final class Order: ArgumentParser.ExpressibleByArgument, Hashable {
enum Direction: String, ExpressibleByArgument {
case asc, desc
}

class OrderComparator: SortComparator {
let field: String
var order: SortOrder

init(order: Order) {
field = order.field
if order.direction == Direction.asc {
self.order = SortOrder.forward
} else {
self.order = SortOrder.reverse
}
}

func hash(into hasher: inout Hasher) {
hasher.combine(field)
hasher.combine(order)
}

static func == (lhs: OrderComparator, rhs: OrderComparator) -> Bool {
return lhs.field == rhs.field
}

func compare(_ lhs: Event, _ rhs: Event) -> ComparisonResult {
let maybeLeftV = try? Object.valueForKeyPath(lhs, field)
let maybeRightV = try? Object.valueForKeyPath(rhs, field)

switch (maybeLeftV, maybeRightV) {
case let (.some(leftV), .some(rightV)):
if let leftValue = leftV as? Int, let rightValue = rightV as? Int {
if leftValue < rightValue {
return .orderedAscending
} else if leftValue > rightValue {
return .orderedDescending
} else {
return .orderedSame
}
} else if let leftValue = leftV as? String, let rightValue = rightV as? String {
if leftValue < rightValue {
return .orderedAscending
} else if leftValue > rightValue {
return .orderedDescending
} else {
return .orderedSame
}
} else if let leftValue = leftV as? Date, let rightValue = rightV as? Date {
if leftValue < rightValue {
return .orderedAscending
} else if leftValue > rightValue {
return .orderedDescending
} else {
return .orderedSame
}
} else {
// not future proof
// we might silently sort on new non-existing types
return .orderedSame
}
default:
// not future proof
// we might silently sort on new non-existing types
return .orderedSame
}
}
}
142 changes: 142 additions & 0 deletions Sources/Model/OrderComparator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import Foundation

class OrderComparator: SortComparator {
let field: String
var order: SortOrder

init(order: Order) {
field = order.field
if order.direction == Direction.asc {
self.order = SortOrder.forward
} else {
self.order = SortOrder.reverse
}
}

func hash(into hasher: inout Hasher) {
hasher.combine(field)
hasher.combine(order)
}

static func == (lhs: OrderComparator, rhs: OrderComparator) -> Bool {
return lhs.field == rhs.field
}

func compare(_ lhs: Event, _ rhs: Event) -> ComparisonResult {
let maybeLeftV = try? Object.valueForKeyPath(lhs, field)
let maybeRightV = try? Object.valueForKeyPath(rhs, field)

switch (maybeLeftV, maybeRightV) {
case let (.some(leftV), .some(rightV)):
if let leftValue = leftV as? Int, let rightValue = rightV as? Int {
return IntComparator(order: order).compare(leftValue, rightValue)
} else if let leftValue = leftV as? String, let rightValue = rightV as? String {
return StringComparator(order: order).compare(leftValue, rightValue)
} else if let leftValue = leftV as? Date, let rightValue = rightV as? Date {
return DateComparator(order: order).compare(leftValue, rightValue)
} else {
// not future proof
// we might silently sort on new non-existing types
return .orderedSame
}
default:
// not future proof
// we might silently sort on new non-existing types
return .orderedSame
}
}
}

class IntComparator: SortComparator, Hashable {
var order: SortOrder

init(order: SortOrder) {
self.order = order
}

func hash(into hasher: inout Hasher) {
hasher.combine(order)
}

static func == (lhs: IntComparator, rhs: IntComparator) -> Bool {
return lhs.order == rhs.order
}

func compare(_ lhs: Int, _ rhs: Int) -> ComparisonResult {
let result: ComparisonResult
if lhs < rhs {
result = .orderedAscending
} else if lhs > rhs {
result = .orderedDescending
} else {
result = .orderedSame
}
return order == .forward ? result : result.reversed
}
}

class StringComparator: SortComparator, Hashable {
var order: SortOrder

init(order: SortOrder) {
self.order = order
}

func hash(into hasher: inout Hasher) {
hasher.combine(order)
}

static func == (lhs: StringComparator, rhs: StringComparator) -> Bool {
return lhs.order == rhs.order
}

func compare(_ lhs: String, _ rhs: String) -> ComparisonResult {
let result: ComparisonResult
if lhs < rhs {
result = .orderedAscending
} else if lhs > rhs {
result = .orderedDescending
} else {
result = .orderedSame
}
return order == .forward ? result : result.reversed
}
}

class DateComparator: SortComparator, Hashable {
var order: SortOrder

init(order: SortOrder) {
self.order = order
}

func hash(into hasher: inout Hasher) {
hasher.combine(order)
}

static func == (lhs: DateComparator, rhs: DateComparator) -> Bool {
return lhs.order == rhs.order
}

func compare(_ lhs: Date, _ rhs: Date) -> ComparisonResult {
let result: ComparisonResult
if lhs < rhs {
result = .orderedAscending
} else if lhs > rhs {
result = .orderedDescending
} else {
result = .orderedSame
}
return order == .forward ? result : result.reversed
}
}

extension ComparisonResult {
var reversed: ComparisonResult {
switch self {
case .orderedAscending: return .orderedDescending
case .orderedSame: return .orderedSame
case .orderedDescending: return .orderedAscending
}
}
}

0 comments on commit 89f24ca

Please sign in to comment.