TypeDB.ConceptRow (TypeDB v0.1.0)

Copy Markdown View Source

One row of a conceptRows answer: a map from variable name to concept.

Rows implement Access, so a variable can be read directly:

row["person"]
#=> %TypeDB.Concept.Entity{iid: "0x1e0...", type: %TypeDB.Concept.EntityType{label: "person"}}

get_in(row, ["name"])
#=> %TypeDB.Concept.Attribute{value: "Alice", value_type: "string"}

A variable bound to nothing yields nil, which is indistinguishable from an absent variable through Access. Use fetch/2 when the difference matters.

involved_blocks is populated only when the query ran with include_query_structure: true; it lists the ids of the query blocks that produced this row.

Summary

Functions

Fetches a variable, distinguishing "bound to nothing" from "not present".

Returns a variable's concept, or default when it is absent or unbound.

Returns the row as a plain map of variable name to wire value.

Returns the natively typed value of a variable. See TypeDB.Concept.typed_value/1.

Returns the wire value of a variable bound to an attribute or value.

Returns the variable names bound in this row.

Types

t()

@type t() :: %TypeDB.ConceptRow{
  data: %{optional(String.t()) => TypeDB.Concept.entry()},
  involved_blocks: [non_neg_integer()] | nil
}

Functions

fetch(concept_row, variable)

@spec fetch(t(), String.t()) :: {:ok, TypeDB.Concept.entry()} | :error

Fetches a variable, distinguishing "bound to nothing" from "not present".

{:ok, nil}   # the variable exists but matched nothing
:error       # the variable is not in this row

get(concept_row, variable, default \\ nil)

@spec get(t(), String.t(), term()) :: TypeDB.Concept.entry() | term()

Returns a variable's concept, or default when it is absent or unbound.

to_map(concept_row)

@spec to_map(t()) :: %{optional(String.t()) => term()}

Returns the row as a plain map of variable name to wire value.

The keys are strings — the query's own variable names, exactly as TypeDB sent them back.

That matters if you are reaching for Kernel.struct/2, which ignores keys that are not atoms naming a field: handed a string-keyed map it returns the struct's defaults and reports nothing, so the conversion silently produces a struct full of nils. Convert the keys first:

row
|> TypeDB.ConceptRow.to_map()
|> Map.new(fn {variable, value} -> {String.to_existing_atom(variable), value} end)
|> then(&struct(Person, &1))

String.to_existing_atom/1 rather than String.to_atom/1 so a query variable cannot grow the atom table; the struct's own fields already exist as atoms wherever Person is loaded. It raises ArgumentError for a variable that does not name one, which is the failure you want over a silent nil.

typed_value(row, variable)

@spec typed_value(t(), String.t()) :: term()

Returns the natively typed value of a variable. See TypeDB.Concept.typed_value/1.

value(row, variable)

@spec value(t(), String.t()) :: term()

Returns the wire value of a variable bound to an attribute or value.

variables(concept_row)

@spec variables(t()) :: [String.t()]

Returns the variable names bound in this row.