-
Notifications
You must be signed in to change notification settings - Fork 14
/
BoolCoding.swift
76 lines (63 loc) · 1.93 KB
/
BoolCoding.swift
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
73
74
75
76
//
// BoolCoding.swift
// CodableExample
//
// Created by Condy on 2024/6/18.
//
import Foundation
public typealias BoolFalse = BooleanValue<False>
public typealias BoolTrue = BooleanValue<True>
public protocol BooleanTogglable {
static var value: Bool { get }
}
public enum False: BooleanTogglable {
public static let value: Bool = false
}
public enum True: BooleanTogglable {
public static let value: Bool = true
}
/// String or Int -> Bool converter.
/// Uses <= 0 as false, and > 0 as true.
/// Uses lowercase "true"/"yes"/"y"/"t"/"1"/">0" and "false"/"no"/"f"/"n"/"0".
/// `@BoolCoding` decodes int/string/bool value json into `Bool`.
public struct BooleanValue<HasDefault: BooleanTogglable>: Transformer {
let boolean: Bool
public typealias DecodeType = Bool
public typealias EncodeType = Bool
public init?(value: Any) {
if let val = value as? Bool {
self.boolean = val
return
} else if let val = value as? Int {
self.boolean = val > 0 ? true : false
return
} else if let val = value as? Float {
self.boolean = val > 0 ? true : false
return
}
guard let string = Hollow.transfer2String(with: value), !string.hc.isEmpty2 else {
return nil
}
let value = string.lowercased()
switch value {
case "1", "y", "t", "yes", "true":
self.boolean = true
case "0", "n", "f", "no", "false":
self.boolean = false
default:
guard let val = Double(value) else {
return nil
}
self.boolean = val > 0 ? true : false
}
}
public func transform() throws -> Bool? {
boolean
}
}
extension BooleanValue: DefaultValueProvider {
public typealias DefaultType = Bool
public static var hasDefaultValue: DefaultType {
HasDefault.value
}
}