Programmatic HL7v2 message construction.
Low-level message constructor for building HL7v2 messages from typed segment structs. The MSH segment is auto-populated with sensible defaults (field separator, encoding characters, processing ID, version, timestamp, control ID) and can be overridden via opts.
Note: This is an append-only builder — it does not enforce segment ordering
or required-segment presence. Use HL7v2.validate/1 after building to check
structural conformance.
Examples
msg =
HL7v2.Message.new("ADT", "A01",
sending_application: "PHAOS",
sending_facility: "HOSP"
)
|> HL7v2.Message.add_segment(%HL7v2.Segment.PID{
patient_identifier_list: [%HL7v2.Type.CX{id: "12345"}],
patient_name: [%HL7v2.Type.XPN{family_name: %HL7v2.Type.FN{surname: "Smith"}, given_name: "John"}]
})
wire = HL7v2.Message.encode(msg)
Summary
Functions
Adds a segment to the message.
Encodes the message to HL7v2 wire format binary.
Creates a new message with MSH auto-populated.
Returns the first segment of a given type, or nil.
Returns all segments of a given type.
Converts to a RawMessage for encoding.
Types
@type t() :: %HL7v2.Message{msh: HL7v2.Segment.MSH.t(), segments: [struct()]}
Functions
Adds a segment to the message.
Segments are appended in order. MSH is managed separately and cannot be added via this function.
Encodes the message to HL7v2 wire format binary.
Converts to a RawMessage and delegates to HL7v2.Encoder.
Creates a new message with MSH auto-populated.
Options
:sending_application-- string or%HD{}(defaultnil):sending_facility-- string or%HD{}(defaultnil):receiving_application-- string or%HD{}(defaultnil):receiving_facility-- string or%HD{}(defaultnil):date_time--%TS{}or%DTM{}(default: current UTC time):message_control_id-- string (default: auto-generated):processing_id-- string (default:"P"):version_id-- string (default:"2.5.1")
Returns the first segment of a given type, or nil.
Examples
HL7v2.Message.segment(msg, HL7v2.Segment.PID)
#=> %HL7v2.Segment.PID{...}
Returns all segments of a given type.
Examples
HL7v2.Message.segments(msg, HL7v2.Segment.OBX)
#=> [%HL7v2.Segment.OBX{...}, %HL7v2.Segment.OBX{...}]
@spec to_raw(t()) :: HL7v2.RawMessage.t()
Converts to a RawMessage for encoding.