Skip to content
This repository has been archived by the owner on Oct 14, 2020. It is now read-only.

Commit

Permalink
Added defaulting to array accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
vdka committed Oct 27, 2016
1 parent 7cdc210 commit 5630599
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 20 deletions.
4 changes: 2 additions & 2 deletions JSON.xcodeproj/JSON_Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.16.0</string>
<string>0.16.1</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>0.16.0</string>
<string>0.16.1</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
Expand Down
66 changes: 48 additions & 18 deletions Sources/JSON/JSONAccessors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ extension JSON {
}
}

public func get<T: JSONInitializable>() throws -> [T] {
guard case .array(let array) = self else { throw JSON.Error.badValue(self) }
return try array.map(T.init(json:))
public func get<T: JSONInitializable>(`default`: [T]? = nil) throws -> [T] {
do {
guard case .array(let array) = self else { throw JSON.Error.badValue(self) }
return try array.map(T.init(json:))
} catch {
guard let `default` = `default` else { throw error }
return `default`
}
}

public func get<T: RawRepresentable>(`default`: T? = nil) throws -> T
Expand All @@ -51,11 +56,16 @@ extension JSON {
}
}

public func get<T: RawRepresentable>() throws -> [T]
public func get<T: RawRepresentable>(`default`: [T]? = nil) throws -> [T]
where T.RawValue: JSONInitializable
{
guard case .array(let array) = self else { throw JSON.Error.badValue(self) }
return try array.map(T.init(json:))
do {
guard case .array(let array) = self else { throw JSON.Error.badValue(self) }
return try array.map(T.init(json:))
} catch {
guard let `default` = `default` else { throw error }
return `default`
}
}

public func get<T: RawRepresentable & JSONInitializable>(`default`: T? = nil) throws -> T {
Expand All @@ -80,9 +90,14 @@ extension JSON {
}

/// Returns the content matching the type of its destination
public func get<T: RawRepresentable & JSONInitializable>() throws -> [T] {
guard case .array(let array) = self else { throw JSON.Error.badValue(self) }
return try array.map(T.init(json:))
public func get<T: RawRepresentable & JSONInitializable>(`default`: [T]? = nil) throws -> [T] {
do {
guard case .array(let array) = self else { throw JSON.Error.badValue(self) }
return try array.map(T.init(json:))
} catch {
guard let `default` = `default` else { throw error }
return `default`
}
}
}

Expand Down Expand Up @@ -120,9 +135,14 @@ extension JSON {
}
}

public func get<T: JSONInitializable>(_ field: String) throws -> [T] {
guard let array = self[field].array else { throw JSON.Error.badField(field) }
return try array.map(T.init(json:))
public func get<T: JSONInitializable>(_ field: String, `default`: [T]? = nil) throws -> [T] {
do {
guard let array = self[field].array else { throw JSON.Error.badField(field) }
return try array.map(T.init(json:))
} catch {
guard let `default` = `default` else { throw error }
return `default`
}
}

/// Returns the content matching the type of its destination
Expand All @@ -140,11 +160,16 @@ extension JSON {
}

/// Returns the content matching the type of its destination
public func get<T: RawRepresentable>(_ field: String) throws -> [T]
public func get<T: RawRepresentable>(_ field: String, `default`: [T]? = nil) throws -> [T]
where T.RawValue: JSONInitializable
{
guard let array = self[field].array else { throw JSON.Error.badField(field) }
return try array.map(T.init(json:))
do {
guard let array = self[field].array else { throw JSON.Error.badField(field) }
return try array.map(T.init(json:))
} catch {
guard let `default` = `default` else { throw error }
return `default`
}
}

public func get<T: RawRepresentable & JSONInitializable>(_ field: String, `default`: T? = nil) throws -> T? {
Expand All @@ -170,9 +195,14 @@ extension JSON {
}

/// Returns the content matching the type of its destination
public func get<T: RawRepresentable & JSONInitializable>(_ field: String) throws -> [T] {
guard let array = self[field].array else { throw JSON.Error.badField(field) }
return try array.map(T.init(json:))
public func get<T: RawRepresentable & JSONInitializable>(_ field: String, `default`: [T]? = nil) throws -> [T] {
do {
guard let array = self[field].array else { throw JSON.Error.badField(field) }
return try array.map(T.init(json:))
} catch {
guard let `default` = `default` else { throw error }
return `default`
}
}
}

Expand Down

0 comments on commit 5630599

Please sign in to comment.