Skip to content

Commit 247d5d1

Browse files
Callahan ChaseCallahan Chase
authored andcommitted
Adds splat/2 to HL7 API
Extends the HL7 API to allow for bulk updates of paths on a parsed_hl7 data structure. This is a convenience function that will allow consumers to either statically or dynamically bulk update HL7 messages.
1 parent e490b0f commit 247d5d1

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

lib/hl7.ex

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,19 @@ defmodule HL7 do
4545
@buffer_size 32768
4646
@type file_type_hl7 :: :mllp | :line | nil
4747

48-
@type hl7_map_data() :: %{optional(non_neg_integer) => hl7_map_data() | String.t()}
48+
@type hl7_map_data() :: %{optional(non_neg_integer) => hl7_value()}
49+
@type hl7_value() :: hl7_map_data() | String.t()
4950
@type hl7_list_data() :: String.t() | [hl7_list_data()]
5051

5152
@type segment() :: %{
5253
0 => String.t(),
53-
optional(pos_integer) => hl7_map_data() | String.t()
54+
optional(pos_integer) => hl7_value()
5455
}
5556

5657
@type t() :: %__MODULE__{tags: map(), segments: [segment()]}
5758

5859
@type parsed_hl7_segments :: t() | [segment()]
59-
@type parsed_hl7 :: t() | segment() | [segment()] | hl7_map_data() | String.t()
60+
@type parsed_hl7 :: t() | segment() | [segment()] | hl7_value()
6061

6162
alias HL7.Path
6263

@@ -303,6 +304,38 @@ defmodule HL7 do
303304
segment_data |> do_put(path, value)
304305
end
305306

307+
@doc """
308+
Will splat a map with keys `t:Path.t/0` and values of `t:String.t/0` or `t:hl7_map_data/0` onto a
309+
`t:parsed_hl7/0`. Meaning each `t:Path.t/0` specified in the map will be used to update that
310+
`t:Path.t/0` in the provided `t:parsed_hl7/0` with the provided value found at that key.
311+
312+
Useful when performing bulk updates either statically or dynamically based on other data attributes.
313+
314+
## Examples
315+
316+
iex> import HL7
317+
iex> hl7 = HL7.Examples.wikipedia_sample_hl7() |> HL7.new!()
318+
iex> mapping = %{
319+
...> ~p"PID-3" => "FOO",
320+
...> ~p"PID-50[1].3.1" => "BAR",
321+
...> ~p"OBX[*]-5" => "BUZ"
322+
...> }
323+
iex> updated_hl7 = splat(hl7, mapping)
324+
iex> get(updated_hl7, ~p"PID-3")
325+
"FOO"
326+
iex> get(updated_hl7, ~p"PID-50[*].3.1")
327+
["BAR"]
328+
iex> get(updated_hl7, ~p"OBX[*]-5")
329+
["BUZ", "BUZ"]
330+
"""
331+
332+
@spec splat(parsed_hl7(), %{Path.t() => String.t() | hl7_map_data()}) :: parsed_hl7()
333+
def splat(parsed_hl7, mapping) do
334+
Enum.reduce(mapping, parsed_hl7, fn {path, value}, parsed_hl7 ->
335+
put(parsed_hl7, path, value)
336+
end)
337+
end
338+
306339
@doc ~S"""
307340
Updates data within an `HL7` struct, parsed segments, or repetitions
308341
using an `HL7.Path` struct (see `sigil_p/2`).

0 commit comments

Comments
 (0)