Field
public struct Field<T>
Field
represents a field extractor for database entries returned by the
server.
For example:
let nameField = Field<String>("data", "name")
let user = try! client.query(
Get(
Ref("classes/user/42")
)
).await(timeout: .now() + 5)
let userName = user.get(field: nameField)
Every field has a path which is composed by a sequence of segments. A segment can be:
String
: when the desired field is contained in an object in which the segment will be used as the object key;Int
: when the desired field is contained in an array in which the segment will be used as the array index.
Rules for field extraction and data conversion:
- If the field is not present, it returns
nil
; - If the field can’t be converted to the expected type, it throws an
exception. E.g.:
Field<String>("data", "name")
expects thename
field to be aString
. If the end result is not aString
, it will fail; - If the path assumes a type that is not correct, it throws an exception.
E.g.:
Field<String>("data", "name")
expects the target value to contain a nested object at the keydata
in which there should be aString
field at the keyname
. Ifdata
field is not an object, it will fail.
-
Creates a new nested field extractor based on its parent’s path.
Declaration
Swift
public func at(_ segments: Segment...) -> Field
-
Creates a new nested field extractor based on its parent’s path.
Declaration
Swift
public func at(path segments: [Segment]) -> Field
-
Combine two field extractors to create a nested field.
Declaration
Swift
public func at<A>(field: Field<A>) -> Field<A>
-
Combine two field extractors to create a nested array field.
Declaration
Swift
public func get<A>(asArrayOf field: Field<A>) -> Field<[A]>
-
Combine two field extractors to create a nested object field.
Declaration
Swift
public func get<A>(asDictionaryOf field: Field<A>) -> Field<[String: A]>
-
Creates a new field by mapping the result of its parent’s extracted value.
Declaration
Swift
public func map<A>(_ transform: @escaping (T) throws -> A) -> Field<A>
-
Creates a new field by flat mapping the result of its parent’s extracted value.
Declaration
Swift
public func flatMap<A>(_ transform: @escaping (T) throws -> A?) -> Field<A>