Skip to content

Commit

Permalink
Merge pull request #254 from Zewo/0.16.1
Browse files Browse the repository at this point in the history
0.16.1
  • Loading branch information
paulofaria committed Apr 4, 2018
2 parents b0342cc + b1978ab commit 9fd03a5
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0
4.0.2
8 changes: 4 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ let package = Package(
.library(name: "Zewo", targets: ["CYAJL", "CHTTPParser", "Core", "IO", "Media", "HTTP", "Zewo"])
],
dependencies: [
.package(url: "https://github.com/Zewo/CLibdill.git", .branch("swift-4")),
.package(url: "https://github.com/Zewo/Venice.git", .branch("swift-4")),
.package(url: "https://github.com/Zewo/CBtls.git", .branch("swift-4")),
.package(url: "https://github.com/Zewo/CLibreSSL.git", .branch("swift-4")),
.package(url: "https://github.com/Zewo/CLibdill.git", from: "2.0.0"),
.package(url: "https://github.com/Zewo/Venice.git", from: "0.20.0"),
.package(url: "https://github.com/Zewo/CBtls.git", from: "1.1.0"),
.package(url: "https://github.com/Zewo/CLibreSSL.git", from: "3.1.0"),
],
targets: [
.target(name: "CYAJL"),
Expand Down
10 changes: 5 additions & 5 deletions Sources/Core/Environment/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,17 @@ public struct Environment {
value = value.trimmingCharacters(in: .whitespaces)

if
value.characters.count > 1,
value.characters.first == "\"",
value.characters.last == "\""
value.count > 1,
value.first == "\"",
value.last == "\""
{
value = value.replacingOccurrences(
of: "\\n",
with: "\n"
)

value.characters.removeFirst()
value.characters.removeLast()
value.removeFirst()
value.removeLast()
}

variables[key] = value
Expand Down
9 changes: 6 additions & 3 deletions Sources/Core/Logger/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public struct Logger {
public struct Event {
public let locationInfo: LocationInfo
public let timestamp: Int
public let usec: Int
public let level: Logger.Level
public var message: Any?
public var error: Error?
Expand Down Expand Up @@ -56,9 +57,11 @@ public struct Logger {
public static var appenders: [LogAppender] = [StandardOutputAppender()]

private static func log(level: Level, item: Any?, error: Error? = nil, locationInfo: LocationInfo) {
let (ts, usec) = getTimestamp()
let event = Event(
locationInfo: locationInfo,
timestamp: getTimestamp(),
timestamp: ts,
usec: usec,
level: level,
message: item,
error: error
Expand Down Expand Up @@ -283,10 +286,10 @@ public struct Logger {
)
}

private static func getTimestamp() -> Int {
private static func getTimestamp() -> (Int,Int) {
var time: timeval = timeval(tv_sec: 0, tv_usec: 0)
gettimeofday(&time, nil)
return time.tv_sec
return (time.tv_sec, Int(time.tv_usec))
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Core/String/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ extension Character {

extension String {
public func uppercasedFirstCharacter() -> String {
let first = String(characters.prefix(1)).capitalized
let other = String(characters.dropFirst())
let first = prefix(1).capitalized
let other = dropFirst()
return first + other
}
}
Expand Down
7 changes: 4 additions & 3 deletions Sources/HTTP/Message/Body.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ extension Body {

fileprivate final class ReadableBytes : Readable {
var buffer: ArraySlice<UInt8>
var index: Int = 0

fileprivate init(_ buffer: [UInt8]) {
self.buffer = ArraySlice<UInt8>(buffer)
Expand All @@ -82,14 +83,14 @@ fileprivate final class ReadableBytes : Readable {
return UnsafeRawBufferPointer(start: nil, count: 0)
}

let readCount = min(buffer.count, self.buffer.count)
let readCount = min(buffer.count, self.buffer.count - self.index)

guard let destination = buffer.baseAddress else {
return UnsafeRawBufferPointer(start: nil, count: 0)
}

self.buffer.withUnsafeBytes {
memcpy(destination, $0.baseAddress!, readCount)
memcpy(destination, $0.baseAddress! + self.index, readCount)
return
}

Expand All @@ -99,7 +100,7 @@ fileprivate final class ReadableBytes : Readable {
let read = UnsafeRawBufferPointer(buffer.prefix(readCount))
#endif

self.buffer = self.buffer.suffix(from: readCount)
self.index += readCount
return read
}
}
2 changes: 1 addition & 1 deletion Sources/HTTP/Message/Headers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public struct Headers {
public let original: String

public init(_ original: String) {
self.original = original
self.original = original.lowercased()
}
}

Expand Down
24 changes: 24 additions & 0 deletions Sources/Media/JSON/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@ extension JSON {
public static var mediaType: MediaType = .json
}

extension JSON {
public subscript(str: String) -> JSON? {
get {
switch self {
case .object(let dictionary):
return dictionary[str]
default:
return nil
}
}
}

public subscript(_ int: Int) -> JSON? {
get {
switch self {
case .array(let elements):
return elements[int]
default:
return nil
}
}
}
}

extension JSON {
public var isNull: Bool {
if case .null = self {
Expand Down
76 changes: 76 additions & 0 deletions Sources/Media/JSON/JSONHelpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import Foundation
extension String {
public init?(json: JSON?) {
guard let json = json else { return nil}
switch json {
case .string(let v):
self = v
default:
return nil
}
}
}

extension Double {
public init?(json: JSON?) {
guard let json = json else { return nil}
switch json {
case .double(let v):
self = v
default:
return nil
}
}
}

extension Int {
public init?(json: JSON?) {
guard let json = json else { return nil}
switch json {
case .int(let v):
self = v
default:
return nil
}
}
}


extension Bool {
public init?(json: JSON?) {
guard let json = json else { return nil }
switch json {
case .bool(let v):
self = v
default:
return nil
}
}
}


extension Array where Array.Element == JSON {
public init?(json: JSON?) {
guard let json = json else { return nil }
switch json {
case .array(let v):
self = v
default:
return nil
}
}
}


extension Dictionary where Dictionary.Key == String, Dictionary.Value == JSON {
public init?(json: JSON?) {
guard let json = json else { return nil }
switch json {
case .object(let v):
self = v
default:
return nil
}
}
}

10 changes: 5 additions & 5 deletions Sources/Media/JSON/JSONSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ func validateLength(
) -> (_ value: JSON) -> ValidationResult {
return { value in
if let value = try? value.decode(String.self) {
if !comparator(value.characters.count, length) {
if !comparator(value.count, length) {
return .invalid([error])
}
}
Expand All @@ -599,7 +599,7 @@ func validatePattern(_ pattern: String) -> (_ value: JSON) -> ValidationResult {
)

if let expression = expression {
let range = NSMakeRange(0, value.characters.count)
let range = NSMakeRange(0, value.count)

if expression.matches(in: value, options: [], range: range).count == 0 {
return .invalid(["'\(value)' does not match pattern: '\(pattern)'"])
Expand Down Expand Up @@ -833,7 +833,7 @@ func validateIPv4(_ value:Any) -> ValidationResult {
pattern: "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$",
options: []
) {
if expression.matches(in: ipv4, options: [], range: NSMakeRange(0, ipv4.characters.count)).count == 1 {
if expression.matches(in: ipv4, options: [], range: NSMakeRange(0, ipv4.count)).count == 1 {
return .valid
}
}
Expand Down Expand Up @@ -861,8 +861,8 @@ func validateIPv6(_ value:Any) -> ValidationResult {
extension String {
func stringByRemovingPrefix(_ prefix: String) -> String? {
if hasPrefix(prefix) {
let index = characters.index(startIndex, offsetBy: prefix.characters.count)
return String(self[index...])
let idx = index(startIndex, offsetBy: prefix.count)
return String(self[idx...])
}

return nil
Expand Down
14 changes: 13 additions & 1 deletion Tests/MediaTests/JSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,18 @@ public class JSONTests : XCTestCase {
let json2: JSON = ["string": "string", "int": 1, "bool": true, "nil": nil, "array":["a": 1, "b": 2], "object": ["c": "d", "e": "f"], "intarray": [1, 2, 3, 5]]

XCTAssertEqual(json, json2)

}

func testSubscript() throws {
let str = "{\"nil\":null,\"intarray\":[1,2,3,5],\"object\":{\"e\":\"f\",\"c\":\"d\"},\"array\":{\"b\":2,\"a\":1},\"int\":1,\"bool\":true,\"string\":\"string\"}"

let json = try str.withBuffer { (b) -> JSON in
return try JSON(from: b, deadline: .never)
}

XCTAssertEqual(json["not"], nil)
XCTAssertEqual(json["intarray"]![1], 2)
XCTAssertEqual(json["object"]!["e"], "f")
}
}

Expand All @@ -63,6 +74,7 @@ extension JSONTests {
("testJSONSchema", testJSONSchema),
("testSerialize", testSerialize),
("testParseFromString", testParseFromString),
("testSubscript", testSubscript),
]
}
}

0 comments on commit 9fd03a5

Please sign in to comment.