Skip to content

Commit

Permalink
feat: Handle semver comparisons (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrykPiwowarczyk authored Oct 10, 2023
1 parent f45aa61 commit 0ffe8c4
Show file tree
Hide file tree
Showing 5 changed files with 482 additions and 25 deletions.
43 changes: 20 additions & 23 deletions Sources/FeaturevisorSDK/Conditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,26 @@ public func conditionIsMatched(condition: PlainCondition, context: Context) -> B
// string, string
case (.string(let valueInAttributes), .string(let valueInCondition)):
if String(op.rawValue).starts(with: "semver") {
// @TODO: handle semver comparisons here

// let semverInAttributes = Semver(valueInAttributes)
// let semverInCondition = Semver(valueInCondition)

// switch op {
// case .semverEquals:
// return semverInAttributes == semverInCondition
// case .semverNotEquals:
// return semverInAttributes != semverInCondition
// case .semverGreaterThan:
// return semverInAttributes > semverInCondition
// case .semverGreaterThanOrEquals:
// return semverInAttributes >= semverInCondition
// case .semverLessThan:
// return semverInAttributes < semverInCondition
// case .semverLessThanOrEquals:
// return semverInAttributes <= semverInCondition
// default:
// return false
// }

return false

let semverInAttributes = Semver(valueInAttributes)
let semverInCondition = Semver(valueInCondition)

switch op {
case .semverEquals:
return semverInAttributes == semverInCondition
case .semverNotEquals:
return semverInAttributes != semverInCondition
case .semverGreaterThan:
return semverInAttributes > semverInCondition
case .semverGreaterThanOrEquals:
return semverInAttributes >= semverInCondition
case .semverLessThan:
return semverInAttributes < semverInCondition
case .semverLessThanOrEquals:
return semverInAttributes <= semverInCondition
default:
return false
}
}

switch op {
Expand Down
47 changes: 47 additions & 0 deletions Sources/FeaturevisorSDK/Semver.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import FeaturevisorTypes
import Foundation

struct Semver: Equatable {
let version: String
let major: Int
let minor: Int
let patch: Int

init(_ version: String) {
self.version = version
let components = version.components(separatedBy: ".")
major = Int(components[0]) ?? 0
minor = components.count > 1 ? Int(components[1]) ?? 0 : 0
patch = components.count > 2 ? Int(components[2]) ?? 0 : 0
}

static func == (lhs: Semver, rhs: Semver) -> Bool {
return lhs.major == rhs.major && lhs.minor == rhs.minor && lhs.patch == rhs.patch
}

static func != (lhs: Semver, rhs: Semver) -> Bool {
return !(lhs == rhs)
}

static func > (lhs: Semver, rhs: Semver) -> Bool {
if lhs.major != rhs.major {
return lhs.major > rhs.major
}
if lhs.minor != rhs.minor {
return lhs.minor > rhs.minor
}
return lhs.patch > rhs.patch
}

static func >= (lhs: Semver, rhs: Semver) -> Bool {
return lhs == rhs || lhs > rhs
}

static func < (lhs: Semver, rhs: Semver) -> Bool {
return !(lhs >= rhs)
}

static func <= (lhs: Semver, rhs: Semver) -> Bool {
return !(lhs > rhs)
}
}
Loading

0 comments on commit 0ffe8c4

Please sign in to comment.