-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathEncodeOperators.swift
More file actions
56 lines (48 loc) · 1.26 KB
/
Copy pathEncodeOperators.swift
File metadata and controls
56 lines (48 loc) · 1.26 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
//
// EncodeOperators.swift
// Codable
//
// Created by Brandon Sneed on 11/10/15.
// Copyright © 2015 WalmartLabs. All rights reserved.
//
import Foundation
infix operator <== : Encode
precedencegroup Encode { associativity: right }
public func <== <T: Encodable>(lhs: String, rhs: T) throws -> (String, JSON) {
let value = try? rhs.encode()
if let value = value {
return (lhs, value)
} else {
throw EncodeError.unencodable
}
}
public func <== <T: Encodable>(lhs: String, rhs: [T]) throws -> (String, JSON) {
let value = try? rhs.encode()
if let value = value {
return (lhs, value)
} else {
throw EncodeError.unencodable
}
}
public func <== <T: Encodable>(lhs: String, rhs: T?) throws -> (String, JSON) {
if rhs == nil {
return (lhs, JSON())
}
let value = ((try? rhs?.encode()) as JSON??)
if let value = value {
return (lhs, value!)
} else {
return (lhs, JSON())
}
}
public func <== <T: Encodable>(lhs: String, rhs: [T]?) throws -> (String, JSON) {
if rhs == nil {
return (lhs, JSON())
}
let value = ((try? rhs?.encode()) as JSON??)
if let value = value {
return (lhs, value!)
} else {
return (lhs, JSON())
}
}