Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#154] Paths value filter #156

Merged
merged 3 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@
"repositoryURL": "https://github.com/tadija/AEXML.git",
"state": {
"branch": null,
"revision": "8623e73b193386909566a9ca20203e33a09af142",
"version": "4.5.0"
"revision": "502c4d43a6cc9c395d19111e09dc62ad834977b5",
"version": "4.6.0"
}
},
{
"package": "BooleanExpressionEvaluation",
"repositoryURL": "https://github.com/ABridoux/BooleanExpressionEvaluation",
"state": {
"branch": "develop",
"revision": "0cc620e646f7622dd8c7b578dc7ada85f45ff495",
"version": null
}
},
{
Expand All @@ -24,26 +33,26 @@
"repositoryURL": "https://github.com/JohnSundell/Splash",
"state": {
"branch": null,
"revision": "f25dd8c9f16be1f81a152f6861d7216c3c9302da",
"version": "0.14.0"
"revision": "81de0389558ad40579027735841593b21a511fa8",
"version": "0.15.0"
}
},
{
"package": "swift-argument-parser",
"repositoryURL": "https://github.com/apple/swift-argument-parser",
"state": {
"branch": null,
"revision": "92646c0cdbaca076c8d3d0207891785b3379cbff",
"version": "0.3.1"
"revision": "9564d61b08a5335ae0a36f789a7d71493eacadfc",
"version": "0.3.2"
}
},
{
"package": "Yams",
"repositoryURL": "https://github.com/jpsim/Yams.git",
"state": {
"branch": null,
"revision": "88caa2e6fffdbef2e91c2022d038576062042907",
"version": "4.0.0"
"revision": "9003d51672e516cc59297b7e96bff1dfdedcb4ea",
"version": "4.0.4"
}
}
]
Expand Down
7 changes: 5 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ let package = Package(
from: "0.1.0"),
.package(
url: "https://github.com/jpsim/Yams.git",
from: "4.0.0")
from: "4.0.0"),
.package(
url: "https://github.com/ABridoux/BooleanExpressionEvaluation",
.branch("develop"))
],
targets: [
.target(
name: "Scout",
dependencies: ["AEXML", "Yams"]),
dependencies: ["AEXML", "Yams", "BooleanExpressionEvaluation"]),
.target(
name: "ScoutCLTCore",
dependencies: ["Scout"]),
Expand Down
31 changes: 15 additions & 16 deletions Sources/Scout/Definitions/KeyAllowedType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
import Foundation

/// A value can take a type conforming to this protocol
public protocol KeyAllowedType: LosslessStringConvertible, Equatable {
public protocol KeyAllowedType: LosslessStringConvertible, Hashable {

static var typeDescription: String { get }

init(value: Any) throws
}

public extension KeyAllowedType {
extension KeyAllowedType {

/// Try to instantiate a type with the given value
init(value: Any) throws {
Expand All @@ -22,21 +20,22 @@ public extension KeyAllowedType {
return
}

if let stringValue = (value as? CustomStringConvertible)?.description {
guard let stringValue = (value as? CustomStringConvertible)?.description else {
throw PathExplorerError.valueConversionError(value: String(describing: value), type: String(describing: Self.typeDescription))
}

if Self.self == Bool.self {
// specific case for Bool values as we allow other string than "true" or "false"
if Bool.trueSet.contains(stringValue) {
self = try Self(value: true)
return
} else if Bool.falseSet.contains(stringValue) {
self = try Self(value: false)
return
}
} else if let convertedValue = Self(stringValue) {
self = convertedValue
if Self.self == Bool.self {
// specific case for Bool values as we allow other string than "true" or "false"
if Bool.trueSet.contains(stringValue) {
self = try Self(value: true)
return
} else if Bool.falseSet.contains(stringValue) {
self = try Self(value: false)
return
}
} else if let convertedValue = Self(stringValue) {
self = convertedValue
return
}

throw PathExplorerError.valueConversionError(value: String(describing: value), type: String(describing: Self.typeDescription))
Expand Down
9 changes: 9 additions & 0 deletions Sources/Scout/Definitions/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,15 @@ extension Path: ExpressibleByArrayLiteral {

extension Path {

public var lastKeyElementName: String? {
let lastKey = elements.last { (element) -> Bool in
if case .key = element { return true }
return false
}
guard case let .key(name) = lastKey else { return nil }
return name
}

/// Last key component matching the regular expression
public func lastKeyComponent(matches regularExpression: NSRegularExpression) -> Bool {
let lastKey = elements.last { (element) -> Bool in
Expand Down
29 changes: 0 additions & 29 deletions Sources/Scout/Definitions/PathElementFilter.swift

This file was deleted.

4 changes: 2 additions & 2 deletions Sources/Scout/Definitions/PathExplorer+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ extension PathExplorer {

extension PathExplorer {

public func getPaths(startingAt initialPath: Path?, for filter: PathElementFilter?) throws -> [Path] {
try listPaths(startingAt: initialPath, for: filter, valueType: .singleAndGroup)
public func getPaths(startingAt initialPath: Path) throws -> [Path] {
try listPaths(startingAt: initialPath, filter: .noFilter)
}
}
5 changes: 2 additions & 3 deletions Sources/Scout/Definitions/PathExplorer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,8 @@ where
/// Returns all the paths leading to single or group values
/// - Parameters:
/// - initialPath: Scope the return paths with this path as a starting point
/// - filter: Optionnally provide a filter on the key
/// - valueType: Allow group, single values or both. Default to both.
func listPaths(startingAt initialPath: Path?, for filter: PathElementFilter?, valueType: PathElementFilter.ValueType) throws -> [Path]
/// - filter: Optionnally provide a filter on the key and/or value. Default is `noFilter`
func listPaths(startingAt initialPath: Path?, filter: PathsFilter) throws -> [Path]

// MARK: Conversion

Expand Down
4 changes: 4 additions & 0 deletions Sources/Scout/Definitions/PathExplorerError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public enum PathExplorerError: LocalizedError, Equatable {
case csvExportWrongGroupValue
case csvExportAmbiguous(expected: String, path: Path)

case predicateError(predicate: String, description: String)

public var errorDescription: String? {
switch self {
case .invalidData(let type): return "Cannot intialize a \(String(describing: type)) object with the given data"
Expand Down Expand Up @@ -77,6 +79,8 @@ public enum PathExplorerError: LocalizedError, Equatable {
case .groupSampleConversionError(let path): return "Internal error. Group sample conversion error in '\(path.description)'"
case .csvExportWrongGroupValue: return "CSV export requires either first object to be an array or a dictionary of arrays"
case .csvExportAmbiguous(let expectedType, let path): return "Ambiguous type for value at '\(path.description). Expected \(expectedType) as the first value is of type \(expectedType)"

case .predicateError(let predicate, let description): return #"Unable to evaluate the predicate "\#(predicate)". \#(description)"#
}
}
}
Loading