Skip to content

Commit

Permalink
Fix query params (#14)
Browse files Browse the repository at this point in the history
* Improved query params config extraction
  • Loading branch information
gperdomor committed Aug 22, 2018
1 parent 0159cc8 commit 907209b
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 107 deletions.
70 changes: 55 additions & 15 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,39 +1,70 @@
version: 2

jobs:
macos:
macos-4.2:
macos:
xcode: "9.4.1"
xcode: "10.0.0"
steps:
- checkout
- run: brew install libressl
- run: swift package generate-xcodeproj
- run: xcodebuild -scheme VaporExt-Package -enableCodeCoverage YES test
- run: bash <(curl -s https://codecov.io/bash) || echo "Codecov did not collect coverage reports"

macos-release:
macos-release-4.2:
macos:
xcode: "10.0.0"
steps:
- checkout
- run: brew install libressl
- run: swift build -c release

macos-4.1:
macos:
xcode: "9.4.1"
steps:
- checkout
- run: brew install libressl
- run: swift test

macos-release-4.1:
macos:
xcode: "9.4.1"
steps:
- checkout
- run: brew install libressl
- run: swift build -c release

linux:
linux-4.1:
docker:
- image: swift:4.1
- image: swift:4.1.3
steps:
- checkout
- run: swift build
- run: swift test

linux-release:

linux-release-4.1:
docker:
- image: swift:4.1.3
steps:
- checkout
- run: swift build -c release

linux-4.2:
docker:
- image: norionomura/swift:swift-4.2-branch
steps:
- checkout
- run: swift build
- run: swift test

linux-release-4.2:
docker:
- image: swift:4.1
- image: norionomura/swift:swift-4.2-branch
steps:
- checkout
- run: swift build -c release

swiftlint:
docker:
- image: norionomura/swiftlint
Expand All @@ -46,10 +77,17 @@ workflows:
tests:
jobs:
- swiftlint
- linux
- linux-release
- macos
- macos-release
# Swift 4.1
- linux-4.1
- linux-release-4.1
- macos-4.1
- macos-release-4.1

# Swift 4.2
- linux-4.2
- linux-release-4.2
- macos-4.2
- macos-release-4.2

nightly:
triggers:
Expand All @@ -60,5 +98,7 @@ workflows:
only:
- master
jobs:
- linux
- macos
- linux-4.1
- linux-4.2
- macos-4.1
- macos-4.2
14 changes: 8 additions & 6 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ line_length: 120

identifier_name:
excluded:
- i
- id
- FILTER_CONFIG_REGEX

closure_end_indentation: error

file_header:
severity: error
required_pattern: |
\/\/
\/\/ .*?\.swift
\/\/ (Async|Fluent|Service|Vapor)Ext
\/\/
\/\/ Created by .*? on \d{2}\/\d{2}\/\d{2}\.
\/\/ Copyright © \d{4} Vapor Community\. All rights reserved\.
\/\/
\/\/ .*?\.swift
\/\/ (Async|Fluent|Service|Vapor)Ext
\/\/
\/\/ Created by .*? on \d{2}\/\d{2}\/\d{2}\.
\/\/ Copyright © \d{4} Vapor Community\. All rights reserved\.
reporter: "emoji"
2 changes: 1 addition & 1 deletion Package.swift
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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<img src="https://codecov.io/gh/vapor-community/vapor-ext/branch/master/graph/badge.svg" />
</a>
<a href="https://swift.org">
<img src="http://img.shields.io/badge/swift-4.1-brightgreen.svg" alt="Swift 4.1">
<img src="http://img.shields.io/badge/swift-4.2-brightgreen.svg" alt="Swift 4.1">
</a>
</p>

Expand Down
59 changes: 59 additions & 0 deletions Sources/FluentExt/FilterConfig.swift
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
}
}
51 changes: 9 additions & 42 deletions Sources/FluentExt/QueryBuilder+Filter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,10 @@ public extension QueryBuilder where Result: Model, Result.Database == Database {
return self
}

let splited = config.components(separatedBy: ":")
let filterConfig = config.toFilterConfig

let method: QueryFilterMethod
let value: String

if splited.count == 1 {
method = .equal
value = splited[0]
} else {
guard let mth = QueryFilterMethod(rawValue: splited[0]) else {
throw FluentError(identifier: "invalidFilterConfiguration", reason: "Invalid filter config for '\(config)'")
}
method = mth
value = splited[1]
}
let method = filterConfig.method
let value = filterConfig.value

let multiple = [.in, .notIn].contains(method)
var parsed: FilterValue<T, [T]>
Expand Down Expand Up @@ -97,21 +86,10 @@ public extension QueryBuilder where Result: Model, Result.Database == Database,
return self
}

let splited = config.components(separatedBy: ":")
let filterConfig = config.toFilterConfig

let method: QueryFilterMethod
let value: String

if splited.count == 1 {
method = .equal
value = splited[0]
} else {
guard let mth = QueryFilterMethod(rawValue: splited[0]) else {
throw FluentError(identifier: "invalidFilterConfiguration", reason: "Invalid filter config for '\(config)'")
}
method = mth
value = splited[1]
}
let method = filterConfig.method
let value = filterConfig.value

let multiple = [.in, .notIn].contains(method)
var parsed: FilterValue<String, [String]>
Expand Down Expand Up @@ -174,21 +152,10 @@ public extension QueryBuilder where Result: Model, Result.Database == Database,
return self
}

let splited = config.components(separatedBy: ":")

let method: QueryFilterMethod
let value: String
let filterConfig = config.toFilterConfig

if splited.count == 1 {
method = .equal
value = splited[0]
} else {
guard let mth = QueryFilterMethod(rawValue: splited[0]) else {
throw FluentError(identifier: "invalidFilterConfiguration", reason: "Invalid filter config for '\(config)'")
}
method = mth
value = splited[1]
}
let method = filterConfig.method
let value = filterConfig.value

let multiple = [.in, .notIn].contains(method)
var parsed: FilterValue<String, [String]>
Expand Down
25 changes: 25 additions & 0 deletions Sources/FluentExt/QueryFilterMethod.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,28 @@ internal enum QueryFilterMethod: String {
case contains = "ct"
case notContains = "nct"
}

#if swift(>=4.2)
extension QueryFilterMethod: CaseIterable { }
#else
extension QueryFilterMethod {
static var allCases: [QueryFilterMethod] {
return [
.equal,
.notEqual,
.in,
.notIn,
.greaterThan,
.greaterThanOrEqual,
.lessThan,
.lessThanOrEqual,
.startsWith,
.notStartsWith,
.endsWith,
.notEndsWith,
.contains,
.notContains
]
}
}
#endif
51 changes: 9 additions & 42 deletions Sources/FluentExt/Request+Filter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,10 @@ public extension Request {
return nil
}

let splited = config.components(separatedBy: ":")
let filterConfig = config.toFilterConfig

let method: QueryFilterMethod
let value: String

if splited.count == 1 {
method = .equal
value = splited[0]
} else {
guard let mth = QueryFilterMethod(rawValue: splited[0]) else {
throw FluentError(identifier: "invalidFilterConfiguration", reason: "Invalid filter config for '\(config)'")
}
method = mth
value = splited[1]
}
let method = filterConfig.method
let value = filterConfig.value

let multiple = [.in, .notIn].contains(method)
var parsed: FilterValue<T, [T]>
Expand Down Expand Up @@ -86,21 +75,10 @@ public extension Request {
return nil
}

let splited = config.components(separatedBy: ":")
let filterConfig = config.toFilterConfig

let method: QueryFilterMethod
let value: String

if splited.count == 1 {
method = .equal
value = splited[0]
} else {
guard let mth = QueryFilterMethod(rawValue: splited[0]) else {
throw FluentError(identifier: "invalidFilterConfiguration", reason: "Invalid filter config for '\(config)'")
}
method = mth
value = splited[1]
}
let method = filterConfig.method
let value = filterConfig.value

let multiple = [.in, .notIn].contains(method)
var parsed: FilterValue<String, [String]>
Expand Down Expand Up @@ -161,21 +139,10 @@ public extension Request {
return nil
}

let splited = config.components(separatedBy: ":")

let method: QueryFilterMethod
let value: String
let filterConfig = config.toFilterConfig

if splited.count == 1 {
method = .equal
value = splited[0]
} else {
guard let mth = QueryFilterMethod(rawValue: splited[0]) else {
throw FluentError(identifier: "invalidFilterConfiguration", reason: "Invalid filter config for '\(config)'")
}
method = mth
value = splited[1]
}
let method = filterConfig.method
let value = filterConfig.value

let multiple = [.in, .notIn].contains(method)
var parsed: FilterValue<String, [String]>
Expand Down

0 comments on commit 907209b

Please sign in to comment.