ExplorerError doc
This commit is contained in:
parent
f38e0bb51d
commit
7e5494b9e3
|
@ -5,6 +5,7 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
/// Errors that can be thrown when exploring data using a ``PathExplorer``
|
||||
public struct ExplorerError: LocalizedError, Equatable {
|
||||
public private(set) var path: Path
|
||||
let description: String
|
||||
|
@ -58,30 +59,37 @@ extension ExplorerError {
|
|||
|
||||
public extension ExplorerError {
|
||||
|
||||
/// The provided value is not convertible to an ``ExplorerValue``
|
||||
static func invalid(value: Any) -> Self {
|
||||
ExplorerError(description: "The value \(value) is not convertible to ExplorerValue")
|
||||
}
|
||||
|
||||
/// The key used to subscript is missing. A best match in the existing key is provided if one is found.
|
||||
static func missing(key: String, bestMatch: String?) -> Self {
|
||||
ExplorerError(description: "Missing key '\(key)' in dictionary. Best match found: '\(bestMatch ?? "none")'")
|
||||
}
|
||||
|
||||
/// Trying to subscript something with a key although it's not a dictionary
|
||||
static var subscriptKeyNoDict: Self {
|
||||
ExplorerError(description: "The value cannot be subscripted with a string as it is not a dictionary")
|
||||
}
|
||||
|
||||
/// The provided index is out of bounds to subscript the array
|
||||
static func wrong(index: Int, arrayCount: Int) -> Self {
|
||||
ExplorerError(description: "Index \(index) out of bounds to subscript the array with \(arrayCount) elements")
|
||||
}
|
||||
|
||||
/// Trying to subscript something with an index although it's not an array
|
||||
static var subscriptIndexNoArray: Self {
|
||||
ExplorerError(description: "The value cannot be subscripted with an index as it is not an array")
|
||||
}
|
||||
|
||||
/// The ``PathElement`` is misplaced or forbidden for the feature
|
||||
static func wrongUsage(of element: PathElement) -> Self {
|
||||
return ExplorerError(description: "The element \(element.kindDescription) \(element) cannot be used here. \(element.usage)")
|
||||
}
|
||||
|
||||
/// The bounds provided to the ``PathElement/slice(_:)`` element are not valid to slice the array.
|
||||
static func wrong(bounds: Bounds, arrayCount: Int) -> Self {
|
||||
let description =
|
||||
"""
|
||||
|
@ -93,14 +101,19 @@ public extension ExplorerError {
|
|||
return ExplorerError(description: description)
|
||||
}
|
||||
|
||||
/// The regular expression pattern is invalid
|
||||
static func wrong(regexPattern: String) -> Self {
|
||||
ExplorerError(description: "The string '\(regexPattern)' is not a valid regular expression pattern")
|
||||
}
|
||||
|
||||
/// The conversion from an ``ExplorerValue`` to the provided type has failed
|
||||
static func mismatchingType<T>(_ type: T.Type, value: ExplorerValue) -> Self {
|
||||
ExplorerError(description: "ExplorerValue '\(value)' cannot be represented as \(T.self)")
|
||||
}
|
||||
|
||||
/// The predicate in invalid.
|
||||
///
|
||||
/// For instance, a `String` value is evaluated against a predicate taking an `Int` as input
|
||||
static func predicateNotEvaluatable(_ predicate: String, description: String) -> Self {
|
||||
ExplorerError(description: #"Unable to evaluate the predicate "\#(predicate)". \#(description)"#)
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
Basically, a `Path` is a collection of ``PathElement``s in a specific order. The sequence of `PathElement`s lets the explorer know what value to target next. When navigating to a value is not possible, the explorer will throw an error.
|
||||
|
||||
The examples in this article will refer to this json file, stored in a ``PathExplorers/Json`` value referred to as `json`. To learn more about
|
||||
The examples in this article will refer to this json file, stored in a ``PathExplorers/Json`` value referred to as `json`.
|
||||
|
||||
> Note: The full "People" files are used to try Scout and can be found in the Playground folder.
|
||||
|
||||
|
@ -71,8 +71,8 @@ A `Path` can be instantiated from `PathElement`s in an array or as variadic para
|
|||
- Make a `Path` targeting Robert's second hobby
|
||||
```swift
|
||||
let path = Path(elements: "Robert", "hobbies", 1)
|
||||
let firstHobby = try json.get(path: path).string
|
||||
print(firstHobby)
|
||||
let secondHobby = try json.get(path: path).string
|
||||
print(secondHobby)
|
||||
// "party"
|
||||
```
|
||||
|
||||
|
@ -110,7 +110,7 @@ Scout offers to get a dictionary or array count with ``PathElement/count``. This
|
|||
For instance, to get Robert's hobbies count.
|
||||
```swift
|
||||
let path = Path(elements: "Robert", "hobbies", .count)
|
||||
let count = try json.get(path: path).count
|
||||
let count = try json.get(path: path).int
|
||||
print(count) // 3
|
||||
```
|
||||
|
||||
|
|
|
@ -59,8 +59,8 @@ let array: ExplorerValue = ["Riri", "Fifi", "Loulou"]
|
|||
```
|
||||
|
||||
### Codable
|
||||
``ExplorerValue`` conforms to `Codable`. The new SerializablePathExplorer (used for JSON, Plist and XML) uses this conformance to offer initialisation from Data. But this also means that any Coder can be used to read an ExplorerValue from Data. This was already possible to use a different serializer than the default one in the previous implementations. But customizing a Coder is much simpler and now more common in Swift. For instance, setting a custom `Date` decoding strategy is always offered in most coders.
|
||||
|
||||
``ExplorerValue`` conforms to `Codable`. The new SerializablePathExplorer (used for JSON, Plist and XML) uses this conformance to offer initialization from Data. But this also means that any Coder can be used to read an `ExplorerValue` from Data. This was already possible to use a different serializer than the default one in the previous implementations. But customizing a Coder is much simpler and now more common in Swift. For instance, setting a custom `Date` decoding strategy is always offered in most coders.
|
||||
be
|
||||
### Conversion with ExplorerValueRepresentable
|
||||
|
||||
Setting and adding a value to an explorer now works with ExplorerValue. For instance, to set Tom’s age to 60:
|
||||
|
|
Loading…
Reference in New Issue