-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generic materialize function with throwable behaviour for throwable initializers #890
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ public enum SwiftyJSONError: Int, Swift.Error { | |
case wrongType = 901 | ||
case notExist = 500 | ||
case invalidJSON = 490 | ||
case someParsingError = 1 | ||
} | ||
|
||
extension SwiftyJSONError: CustomNSError { | ||
|
@@ -69,6 +70,8 @@ extension SwiftyJSONError: CustomNSError { | |
return [NSLocalizedDescriptionKey: "JSON is invalid."] | ||
case .elementTooDeep: | ||
return [NSLocalizedDescriptionKey: "Element too deep. Increase maxObjectDepth and make sure there is no reference loop."] | ||
case .someParsingError: | ||
return [NSLocalizedDescriptionKey: "Some parsing error"] | ||
} | ||
} | ||
} | ||
|
@@ -1451,3 +1454,93 @@ public enum writingOptionsKeys { | |
case maxObjextDepth | ||
case encoding | ||
} | ||
|
||
// MARK: - | ||
|
||
public protocol JSONMaterializableType { | ||
|
||
static var implementation: (JSON, () -> Error) throws -> Self { get } | ||
} | ||
|
||
extension Bool: JSONMaterializableType { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) |
||
public static var implementation: (JSON, () -> Error) throws -> Bool { | ||
return { (json: JSON, factory: () -> Error) throws -> Bool in | ||
if case .some(let bool) = json.bool { | ||
return bool | ||
} | ||
throw factory() | ||
} | ||
} | ||
} | ||
|
||
extension Double: JSONMaterializableType { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) |
||
public static var implementation: (JSON, () -> Error) throws -> Double { | ||
return { (json: JSON, factory: () -> Error) throws -> Double in | ||
if case .some(let double) = json.double { | ||
return double | ||
} | ||
throw factory() | ||
} | ||
} | ||
} | ||
|
||
extension Int: JSONMaterializableType { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) |
||
public static var implementation: (JSON, () -> Error) throws -> Int { | ||
return { (json: JSON, factory: () -> Error) throws -> Int in | ||
if case .some(let int) = json.int { | ||
return int | ||
} | ||
throw factory() | ||
} | ||
} | ||
} | ||
|
||
extension String: JSONMaterializableType { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) |
||
public static var implementation: (JSON, () -> Error) throws -> String { | ||
return { (json: JSON, factory: () -> Error) throws -> String in | ||
if case .some(let string) = json.string { | ||
return string | ||
} | ||
throw factory() | ||
} | ||
} | ||
} | ||
|
||
// might include file/line where the parsing error occured | ||
private func factoryError(file: String, line: Int) -> () -> Error { | ||
return { SwiftyJSONError.someParsingError } | ||
} | ||
|
||
public extension JSON { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) |
||
func materialize<T>(file: String = #file, line: Int = #line) -> T? where T: JSONMaterializableType { | ||
return try? T.implementation(self, factoryError(file: file, line: line)) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) |
||
func materialize<T>(file: String = #file, line: Int = #line) throws -> [T] where T: JSONMaterializableType { | ||
switch array { | ||
case .some(let array): | ||
return try array.map({ try T.implementation($0, factoryError(file: file, line: line)) }) | ||
default: | ||
throw factoryError(file: file, line: line)() | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) |
||
func materialize<T>(file: String = #file, line: Int = #line) throws -> T where T: JSONMaterializableType { | ||
return try T.implementation(self, factoryError(file: file, line: line)) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) |
||
func materialize<T>(file: String = #file, line: Int = #line) throws -> T where T: RawRepresentable, T.RawValue: JSONMaterializableType { | ||
let rawValue: T.RawValue = try materialize(file: file, line: line) | ||
switch T(rawValue: rawValue) { | ||
case .some(let value): | ||
return value | ||
default: | ||
throw factoryError(file: file, line: line)() | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. File Line Length Violation: File should contain 400 lines or less: currently contains 1546 (file_length) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)