Value
public protocol Value: Expr, CustomStringConvertible
Value
is the top level of the value tree hierarchy. It represents a valid
FaunaDB entry returned by the server.
Traversal API
You can use the traversal API to traverse and convert a database entry to an native type.
The traversal API uses type inference to convert the returned value to the desired type.
The traversal API methods are shortcuts for field extractors. See
FaunaDB.Field
for more information.
Examples of field extractions and type conversions:
// Attempts to convert the root value to a String
let name: String? = try! value.get()
// Transverses to the path "array" -> 1 -> "value" and
// attempts to convert its result to an Int
let count: Int? = try! value.get("array", 1, "count")
// Using a predefined field
let nameField = Field<String>("data", "name")
let name = try! value.get(field: nameField)
// Extracting an array
let tags1 = try! value
.at("data")
.get(asArrayOf: Field<String>())
let tags2: [String] = try! value.get("data")
// Extracting a dictionary
let nickNamesByName1 = try! value
.at("data")
.get(asDictionaryOf: Field<String>())
let nickNamesByName2: [String: String] =
try! value.get("data")
// Extracting a struct that implements
// FaunaDB.Decodable
let blogPost: Post = try! value.get("data")!
-
get(_:)
Extension methodConverts the value at the provided path to the desired type
T
.Declaration
Swift
public func get<T>(_ path: Segment...) throws -> T?
-
get(path:)
Extension methodConverts the value at the provided path to the desired type
T
.Declaration
Swift
public func get<T>(path: [Segment]) throws -> T?
-
get(field:)
Extension methodConverts the value at the provided field to the desired type
T
.Declaration
Swift
public func get<T>(field: Field<T>) throws -> T?
-
at(_:)
Extension methodReturns the value at the provided path.
Declaration
Swift
public func at(_ path: Segment...) throws -> Value
-
at(path:)
Extension methodReturns the value at the provided path.
Declaration
Swift
public func at(path: [Segment]) throws -> Value
-
at(field:)
Extension methodExtracts the provided field from this value.
Declaration
Swift
public func at(field: Field<Value>) throws -> Value
-
get(_:)
Extension methodConverts the value at the provided path to an array of the desired type
T
.Declaration
Swift
public func get<T>(_ path: Segment...) throws -> [T]
-
get(path:)
Extension methodConverts the value at the provided path to an array of the desired type
T
.Declaration
Swift
public func get<T>(path: [Segment]) throws -> [T]
-
get(field:)
Extension methodConverts the value at the provided field to an array of the desired type
T
.Declaration
Swift
public func get<T>(field: Field<[T]>) throws -> [T]
-
get(asArrayOf:)
Extension methodConverts each nested array element in this value using the provided field. Assumes this value is an array.
Declaration
Swift
public func get<T>(asArrayOf field: Field<T>) throws -> [T]
-
get(_:)
Extension methodConverts the value at the provided path to an object of
String
to the desired typeT
.Declaration
Swift
public func get<T>(_ path: Segment...) throws -> [String: T]
-
get(path:)
Extension methodConverts the value at the provided path to an object of
String
to the desired typeT
.Declaration
Swift
public func get<T>(path: [Segment]) throws -> [String: T]
-
get(field:)
Extension methodConverts the value at the provided field to an object of
String
to the desired typeT
.Declaration
Swift
public func get<T>(field: Field<[String: T]>) throws -> [String: T]
-
get(asDictionaryOf:)
Extension methodConverts each nested object element in this value using the provided field. Assumes this value is an object.
Declaration
Swift
public func get<T>(asDictionaryOf field: Field<T>) throws -> [String: T]
-
map(_:)
Extension methodMaps this value using the provided function.
Declaration
Swift
public func map<T>(_ transform: @escaping (Value) throws -> T) throws -> T?
-
flatMap(_:)
Extension methodFlat maps this value using the provided function.
Declaration
Swift
public func flatMap<T>(_ transform: @escaping (Value) throws -> T?) throws -> T?