-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added polygon data-type * added polygon data-type * added tests for polygon * fixed polygon typos
- Loading branch information
1 parent
4388d3c
commit 830abbc
Showing
4 changed files
with
120 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import Foundation | ||
|
||
/// A 2-dimensional list of (double[2]) points representing a polygon. | ||
public struct PostgreSQLPolygon: Codable, Equatable { | ||
/// The points that make up the polygon. | ||
public var points: [PostgreSQLPoint] | ||
|
||
/// Create a new `Polygon` | ||
public init(points: [PostgreSQLPoint]) { | ||
self.points = points | ||
} | ||
} | ||
|
||
extension PostgreSQLPolygon: CustomStringConvertible { | ||
/// See `CustomStringConvertible`. | ||
public var description: String { | ||
return "(\(self.points.map{ $0.description }.joined(separator: ",")))" | ||
} | ||
} | ||
|
||
extension PostgreSQLPolygon: PostgreSQLDataConvertible { | ||
/// See `PostgreSQLDataConvertible`. | ||
public static func convertFromPostgreSQLData(_ data: PostgreSQLData) throws -> PostgreSQLPolygon { | ||
guard case .polygon = data.type else { | ||
throw PostgreSQLError.decode(self, from: data) | ||
} | ||
switch data.storage { | ||
case .text(let string): | ||
var points = [PostgreSQLPoint]() | ||
var count = 0 | ||
|
||
let parts = string.split(separator: ",") | ||
while count < parts.count { | ||
var x = parts[count] | ||
var y = parts[count+1] | ||
|
||
// Check initial "(" | ||
if count == 0 { assert(x.popFirst() == "(") } | ||
|
||
count += 2 | ||
|
||
// Check end ")" | ||
if count == parts.count { assert(y.popLast() == ")") } | ||
|
||
// Check Normal "(" and ")" | ||
assert(x.popFirst() == "(") | ||
assert(y.popLast() == ")") | ||
|
||
// Create the point | ||
points.append(PostgreSQLPoint(x: Double(x)!, y: Double(y)!)) | ||
} | ||
return .init(points: points) | ||
case .binary(let value): | ||
let total = value[0..<4].as(UInt32.self, default: 0).bigEndian | ||
assert(total == (value.count-4)/16) | ||
|
||
var points = [PostgreSQLPoint]() | ||
var count = 4 | ||
while count < value.count { | ||
let x = Data(value[count..<count+8].reversed()) | ||
let y = Data(value[count+8..<count+16].reversed()) | ||
points.append(PostgreSQLPoint(x: x.as(Double.self, default: 0), y: y.as(Double.self, default: 0))) | ||
count += 16 | ||
} | ||
|
||
return .init(points: points) | ||
|
||
case .null: throw PostgreSQLError.decode(self, from: data) | ||
} | ||
} | ||
|
||
/// See `PostgreSQLDataConvertible`. | ||
public func convertToPostgreSQLData() throws -> PostgreSQLData { | ||
var data = Data.of(Int32(self.points.count).bigEndian) | ||
for point in self.points { | ||
data += Data.of(point.x).reversed() | ||
data += Data.of(point.y).reversed() | ||
} | ||
return PostgreSQLData(.polygon, binary: data) | ||
} | ||
} |
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