Dextrin.MapKey (Dextrin v0.3.2)

Copy Markdown View Source

Which values may be map keys (DXN.md §1.6): a string, keyword, integer, uuid, objectid, date, bytes, symbol or char. Anything else is refused as key/not-allowed -- in text, in binary and when encoding.

The allowed types are those whose identity is exact and obvious in every language: two keys are the same exactly when their canonical forms are. Floats, decimals and rationals are not (1 against 1.0, 0.0 against -0.0, 1.0M against 1.00M); neither are times, URIs or collections; nil and the booleans collide with other keys in some languages and are rare as keys anyway.

Judged by value, with one thing a value cannot say. NaN and the infinities are the atoms :nan, :positive_infinity and :negative_infinity here, which is also what the keywords of those names decode to. So an atom is allowed as a keyword, and the readers refuse a float key by what it was written as: the text decoder by its token, the binary decoder by its CBOR major type.

Summary

Functions

:ok when key may be a map key, or a key/not-allowed error naming it.

:ok when every key of pairs may be a map key, or the first refusal.

The error for a key that is not allowed, naming it as Dextrin.encode/2 writes it, or as Elixir does when DXN cannot write it.

Functions

check(key)

@spec check(term()) :: :ok | {:error, Dextrin.Error.t()}

:ok when key may be a map key, or a key/not-allowed error naming it.

Dextrin.MapKey.check("a")   #=> :ok
Dextrin.MapKey.check(1.5)   #=> {:error, %Dextrin.Error{category: "key/not-allowed", ...}}

check_pairs(pairs)

@spec check_pairs([{term(), term()}]) :: :ok | {:error, Dextrin.Error.t()}

:ok when every key of pairs may be a map key, or the first refusal.

refusal(key)

@spec refusal(term()) :: Dextrin.Error.t()

The error for a key that is not allowed, naming it as Dextrin.encode/2 writes it, or as Elixir does when DXN cannot write it.