-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improved query params config extraction
- Loading branch information
Showing
8 changed files
with
167 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// swift-tools-version:4.0 | ||
// swift-tools-version:4.1 | ||
|
||
import PackageDescription | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// FilterConfig.swift | ||
// FluentExt | ||
// | ||
// Created by Gustavo Perdomo on 08/14/18. | ||
// Copyright © 2018 Vapor Community. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct FilterConfig { | ||
var method: QueryFilterMethod | ||
var value: String | ||
} | ||
|
||
internal let FILTER_CONFIG_REGEX = "(\(QueryFilterMethod.allCases.map { $0.rawValue }.joined(separator: "|")))?:?(.*)" | ||
|
||
internal extension String { | ||
var toFilterConfig: FilterConfig { | ||
let result = self.capturedGroups(withRegex: FILTER_CONFIG_REGEX) | ||
|
||
var method = QueryFilterMethod.equal | ||
|
||
if let rawValue = result[0], let qfm = QueryFilterMethod.init(rawValue: rawValue) { | ||
method = qfm | ||
} | ||
|
||
return FilterConfig(method: method, value: result[1] ?? "") | ||
} | ||
|
||
func capturedGroups(withRegex pattern: String) -> [String?] { | ||
|
||
var results = [String?]() | ||
|
||
var regex: NSRegularExpression | ||
do { | ||
regex = try NSRegularExpression(pattern: pattern, options: []) | ||
} catch { | ||
return results | ||
} | ||
|
||
let matches = regex.matches(in: self, options: [], range: NSRange(location: 0, length: self.count)) | ||
|
||
guard let match = matches.first else { return results } | ||
|
||
let lastRangeIndex = match.numberOfRanges - 1 | ||
guard lastRangeIndex >= 1 else { return results } | ||
|
||
for i in 1...lastRangeIndex { | ||
let capturedGroupIndex = match.range(at: i) | ||
|
||
let matchedString: String? = capturedGroupIndex.location == NSNotFound ? nil : NSString(string: self).substring(with: capturedGroupIndex) | ||
|
||
results.append(matchedString) | ||
} | ||
|
||
return results | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters