forked from pvieito/PythonCodable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonDecoder.swift
More file actions
182 lines (154 loc) · 5.1 KB
/
Copy pathPythonDecoder.swift
File metadata and controls
182 lines (154 loc) · 5.1 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// PythonDecoder.swift
// PythonCodable
//
// Created by Pedro José Pereira Vieito on 11/12/2019.
// Copyright © 2019 Pedro José Pereira Vieito. All rights reserved.
//
import Foundation
import PythonKit
import MoreCodable
public struct PythonDecoder {
/// Tries to decodes the input `pythonObject` into the Swift `type` type.
/// - Parameters:
/// - type: Decodable Swift type.
/// - pythonObject: Input Python object.
public static func decode<T: Decodable>(_ type: T.Type, from pythonObject: PythonObject) throws -> T {
let decodableDictionary = try pythonObject.bridgeToDecodableDictionary()
return try DictionaryDecoder().decode(type, from: decodableDictionary)
}
}
extension PythonObject {
/// Tries to bridge the `PythonObject` into an equivalent Swift type.
public func bridgeToSwift() throws -> Any? {
return try self.bridgeToDecodableValue()
}
}
extension PythonDecoder {
enum Error: Swift.Error {
case unsupportedType
case unsupportedListType
case unsupportedDictionaryType
}
}
extension PythonObject {
func bridgeToDecodableDictionaryKey() throws -> String {
guard self.isPythonString else {
throw PythonError.invalidCall(self)
}
return String(self)!
}
func bridgeToDecodableValue() throws -> Any? {
if self.isPythonString {
return String(self)!
}
else if self.isPythonBool {
return Bool(self)!
}
else if self.isPythonInteger {
return Int(self)!
}
else if self.isPythonFloat {
return Double(self)!
}
else if self.isPythonNone {
return nil
}
else if self.isPythonListOrListConvertible {
return try self.bridgeToDecodableArray()
}
else if self.isPythonDictionaryOrDictionaryConvertible {
return try self.bridgeToDecodableDictionary()
}
else {
throw PythonDecoder.Error.unsupportedType
}
}
func bridgeToDecodableArray() throws -> Array<Any?> {
let pythonList = try self.convertToPythonList()
var bridgedArray: [Any?] = []
for item in pythonList {
let bridgedValue = try item.bridgeToDecodableValue()
bridgedArray.append(bridgedValue)
}
return bridgedArray
}
func bridgeToDecodableDictionary() throws -> Dictionary<String, Any> {
let pythonDictionary = try self.convertToPythonDictionary()
var bridgedDictionary: [String : Any] = [:]
for item in pythonDictionary.items() {
let (key, value) = item.tuple2
if key.isPythonString, let bridgedValue = try value.bridgeToDecodableValue() {
bridgedDictionary[String(key)!] = bridgedValue
}
}
return bridgedDictionary
}
}
extension PythonObject {
var isPythonListOrListConvertible: Bool {
return self.isPythonList || self.isPythonListConvertible
}
var isPythonListConvertible: Bool {
return Bool(Python.hasattr(self, "__iter__"))!
}
func convertToPythonList() throws -> PythonObject {
if self.isPythonList {
return self
}
else if self.isPythonListConvertible {
return Python.list(pythonObject)
}
else {
throw PythonDecoder.Error.unsupportedListType
}
}
}
extension PythonObject {
var isPythonDictionaryOrDictionaryConvertible: Bool {
return self.isPythonDictionary || self.isPythonDictionaryConvertible
}
var isPythonNamedTupleConvertible: Bool {
return Bool(Python.hasattr(self, "_asdict"))!
}
var isPythonDictionaryConvertible: Bool {
return Bool(Python.hasattr(self, "__dict__"))!
}
func convertToPythonDictionary() throws -> PythonObject {
if self.isPythonDictionary {
return self
}
else if self.isPythonDictionaryConvertible {
return Python.vars(pythonObject)
}
else if self.isPythonNamedTupleConvertible {
return Python.dict(pythonObject._asdict())
}
else {
throw PythonDecoder.Error.unsupportedDictionaryType
}
}
}
extension PythonObject {
var isPythonString: Bool {
return Bool(Python.isinstance(pythonObject, Python.str))!
}
var isPythonInteger: Bool {
return Bool(Python.isinstance(pythonObject, Python.int))!
}
var isPythonFloat: Bool {
return Bool(Python.isinstance(pythonObject, Python.float))!
}
var isPythonBool: Bool {
return Bool(Python.isinstance(pythonObject, Python.bool))!
}
var isPythonList: Bool {
return Bool(Python.isinstance(pythonObject, Python.list))!
}
var isPythonDictionary: Bool {
return Bool(Python.isinstance(pythonObject, Python.dict))!
}
var isPythonNone: Bool {
return Bool(Python.isinstance(pythonObject, Python.type(Python.None)))!
}
}