Path-based access to HL7v2 message fields.
Provides get/2, get/3, and fetch/2 for extracting values from typed
messages using path strings like "PID-5" or "MSH-9.1".
get/2 returns nil on any resolution failure (silent). fetch/2 returns
{:ok, value} or {:error, reason} for explicit error handling.
Path Syntax
"PID" — first PID segment struct
"PID-5" — PID field 5 (patient_name)
"PID-5.1" — first component of PID-5
"PID-3[2]" — second repetition of PID-3
"MSH-9.1" — message code from MSH-9
"OBX[*]-5" — field 5 from ALL OBX segments (returns list)
"OBX[2]-5" — field 5 from the 2nd OBX segment
"PID-3[*]" — ALL repetitions of PID-3 (returns list)Examples
{:ok, msg} = HL7v2.parse(text, mode: :typed)
patient_name = HL7v2.Access.get(msg, "PID-5")
mrn = HL7v2.Access.get(msg, "PID-3.1")
all_obx_values = HL7v2.Access.get(msg, "OBX[*]-5")
Summary
Functions
Fetches a value from a typed message, returning {:ok, value} or {:error, reason}.
Gets a value from a typed message using a path string or %Path{} struct.
Gets a value from a typed message with a default.
Types
@type path() :: %{ segment: binary(), segment_index: pos_integer() | :all | nil, field: pos_integer() | nil, component: pos_integer() | nil, repetition: pos_integer() | nil }
Functions
@spec fetch(HL7v2.TypedMessage.t(), binary() | HL7v2.Path.t()) :: {:ok, term()} | {:error, atom()}
Fetches a value from a typed message, returning {:ok, value} or {:error, reason}.
Unlike get/2, this function distinguishes between a nil field value and
a resolution failure (unknown segment, invalid path, unknown field).
Accepts both string paths and %HL7v2.Path{} structs.
Examples
{:ok, pid} = HL7v2.Access.fetch(msg, "PID")
{:error, :segment_not_found} = HL7v2.Access.fetch(msg, "ZZZ")
{:error, :invalid_path} = HL7v2.Access.fetch(msg, "not a path")
{:error, :field_not_found} = HL7v2.Access.fetch(msg, "PID-99")
@spec get(HL7v2.TypedMessage.t(), binary() | HL7v2.Path.t()) :: term()
Gets a value from a typed message using a path string or %Path{} struct.
Returns nil if the path doesn't resolve.
@spec get(HL7v2.TypedMessage.t(), binary() | HL7v2.Path.t(), term()) :: term()
Gets a value from a typed message with a default.