forked from Couchbase-Ecosystem/cbl-js-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryHelper.swift
More file actions
72 lines (66 loc) · 2.58 KB
/
Copy pathQueryHelper.swift
File metadata and controls
72 lines (66 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// QueryHelper.swift
// CbliteSwiftJsLib
//
import Foundation
import CouchbaseLiteSwift
enum QueryError: Error {
case invalidParameter(message: String)
case unknownError(message: String)
}
public struct QueryHelper {
public static func getParamatersFromJson(_ data: [String: Any])
throws -> Parameters? {
let parameters = Parameters()
for (key, value) in data {
// Ensure the value is a dictionary
guard let innerDictionary = value as? [String: Any] else {
continue
}
// Extract type and value from the inner dictionary
if let type = innerDictionary["type"] as? String,
let value = innerDictionary["value"] {
// switch through types adding the parameters
switch type {
case "string":
if let stringValue = value as? String {
parameters.setString(stringValue, forName: key)
}
case "float":
if let floatValue = value as? Float {
parameters.setFloat(floatValue, forName: key)
}
case "boolean":
if let booleanValue = value as? Bool {
parameters.setBoolean(booleanValue, forName: key)
}
case "double":
if let doubleValue = value as? Double {
parameters.setDouble(doubleValue, forName: key)
}
case "date":
if let dateValue = value as? String {
//convert the date
let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
let date = dateFormatter.date(from: dateValue)
parameters.setDate(date, forName: key)
}
case "int":
if let intValue = value as? Int {
parameters.setInt(intValue, forName: key)
}
case "long":
if let int64Value = value as? Int64 {
parameters.setInt64(int64Value, forName: key)
}
case "value":
parameters.setValue(value, forName: key)
default:
throw QueryError.invalidParameter(message: type)
}
}
}
return parameters
}
}