forked from google/simhospital
/
parser.go
458 lines (396 loc) · 11 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package testhl7
import (
"fmt"
"strconv"
"testing"
"time"
"github.com/Arend-melissant/simhospital/pkg/hl7"
)
// Parse parses the given message.
func Parse(t *testing.T, message string) *hl7.Message {
t.Helper()
mo := hl7.NewParseMessageOptions()
mo.TimezoneLoc = time.UTC
m, err := hl7.ParseMessageWithOptions([]byte(message), mo)
if err != nil {
t.Fatalf("ParseMessageWithOptions(%s, %v) failed with %v", message, mo, err)
}
if m == nil {
t.Fatalf("ParseMessageWithOptions(%s, %v) got nil message, want non nil", message, mo)
}
return m
}
// Fields computes the given function that returns strings for each message.
func Fields(t *testing.T, messages []string, f func(*testing.T, string) string) []string {
t.Helper()
var result []string
for _, m := range messages {
result = append(result, f(t, m))
}
return result
}
// TimeFields computes the given function that returns times for each message.
func TimeFields(t *testing.T, messages []string, f func(*testing.T, string) *time.Time) []*time.Time {
t.Helper()
var result []*time.Time
for _, m := range messages {
result = append(result, f(t, m))
}
return result
}
// MSH returns the message's MSH segment.
func MSH(t *testing.T, message string) *hl7.MSH {
t.Helper()
m := Parse(t, message)
msh, err := m.MSH()
if err != nil {
t.Fatalf("MSH() failed with %v", err)
}
if msh == nil {
t.Fatal("MSH() got nil MSH segment, want non nil")
}
return msh
}
// OBX returns the message's OBX segment.
func OBX(t *testing.T, message string) *hl7.OBX {
t.Helper()
m := Parse(t, message)
obx, err := m.OBX()
if err != nil {
t.Fatalf("OBX() failed with %v", err)
}
if obx == nil {
t.Fatal("OBX() got nil OBX segment, want non nil")
}
return obx
}
// OBR returns the message's OBR segment.
func OBR(t *testing.T, message string) *hl7.OBR {
t.Helper()
m := Parse(t, message)
obx, err := m.OBR()
if err != nil {
t.Fatalf("OBR() failed with %v", err)
}
if obx == nil {
t.Fatal("OBR() got nil OBR segment, want non nil")
}
return obx
}
// MSA returns the message's MSA segment.
func MSA(t *testing.T, message string) *hl7.MSA {
t.Helper()
m := Parse(t, message)
msa, err := m.MSA()
if err != nil {
t.Fatalf("MSA() failed with %v", err)
}
if msa == nil {
t.Fatal("MSA() got nil MSA segment, want non nil")
}
return msa
}
// EVN returns the message's EVN segment.
func EVN(t *testing.T, message string) *hl7.EVN {
t.Helper()
m := Parse(t, message)
evn, err := m.EVN()
if err != nil {
t.Fatalf("EVN() failed with %v", err)
}
if evn == nil {
t.Fatal("EVN() got nil EVN segment, want non nil")
}
return evn
}
// PV1 returns the message's PV1 segment.
func PV1(t *testing.T, message string) *hl7.PV1 {
t.Helper()
m := Parse(t, message)
pv1, err := m.PV1()
if err != nil {
t.Fatalf("PV1() failed with %v", err)
}
if pv1 == nil {
t.Fatal("PV1() got nil PV1 segment, want non nil")
}
return pv1
}
// PID returns the message's PID segment.
func PID(t *testing.T, message string) *hl7.PID {
t.Helper()
m := Parse(t, message)
pid, err := m.PID()
if err != nil {
t.Fatalf("PID() failed with %v", err)
}
if pid == nil {
t.Fatal("PID() got nil PID segment, want non nil")
}
return pid
}
// PV2 returns the message's PV2 segment.
func PV2(t *testing.T, message string) *hl7.PV2 {
t.Helper()
m := Parse(t, message)
pv2, err := m.PV2()
if err != nil {
t.Fatalf("PV2() failed with %v", err)
}
if pv2 == nil {
t.Fatal("PV2() got nil PV2 segment, want non nil")
}
return pv2
}
// PV2OrNil returns the message's PV2 segment, or nil if there's no PV2 segment.
func PV2OrNil(t *testing.T, message string) *hl7.PV2 {
t.Helper()
m := Parse(t, message)
pv2, err := m.PV2()
if err != nil {
t.Fatalf("PV2() failed with %v", err)
}
return pv2
}
// TXA returns the message's TXA segment.
func TXA(t *testing.T, message string) *hl7.TXA {
t.Helper()
m := Parse(t, message)
txa, err := m.TXA()
if err != nil {
t.Fatalf("TXA() failed with %v", err)
}
return txa
}
// AllDG1 returns all DG1 segments.
func AllDG1(t *testing.T, message string) []*hl7.DG1 {
t.Helper()
m := Parse(t, message)
dg1, err := m.AllDG1()
if err != nil {
t.Fatalf("AllDG1() failed with %v", err)
}
return dg1
}
// AllPR1 returns all PR1 segments.
func AllPR1(t *testing.T, message string) []*hl7.PR1 {
t.Helper()
m := Parse(t, message)
pr1, err := m.AllPR1()
if err != nil {
t.Fatalf("AllPR1() failed with %v", err)
}
return pr1
}
// AllAL1 returns all AL1 segments.
func AllAL1(t *testing.T, message string) []*hl7.AL1 {
t.Helper()
m := Parse(t, message)
al1, err := m.AllAL1()
if err != nil {
t.Fatalf("AllAL1() failed with %v", err)
}
return al1
}
// AllOBX returns all OBX segments.
func AllOBX(t *testing.T, message string) []*hl7.OBX {
t.Helper()
m := Parse(t, message)
obx, err := m.AllOBX()
if err != nil {
t.Fatalf("AllOBX() failed with %v", err)
}
return obx
}
// ORC returns the message's ORC segment.
func ORC(t *testing.T, message string) *hl7.ORC {
t.Helper()
m := Parse(t, message)
orc, err := m.ORC()
if err != nil {
t.Fatalf("ORC() failed with %v", err)
}
if orc == nil {
t.Fatal("ORC() got nil ORC segment, want non nil")
}
return orc
}
// MRG returns the message's MRG segment.
func MRG(t *testing.T, message string) *hl7.MRG {
t.Helper()
m := Parse(t, message)
mrg, err := m.MRG()
if err != nil {
t.Fatalf("MRG() failed with %v", err)
}
if mrg == nil {
t.Fatal("MRG() got nil ORC segment, want non nil")
}
return mrg
}
// MessageDateTime returns the message's date time.
func MessageDateTime(t *testing.T, message string) time.Time {
t.Helper()
msh := MSH(t, message)
return msh.DateTimeOfMessage.Time.UTC()
}
// PlacerNumber returns the PlacerOrderNumber.
func PlacerNumber(t *testing.T, message string) string {
t.Helper()
orc := ORC(t, message)
return orc.PlacerOrderNumber.EntityIdentifier.String()
}
// DeathIndicator returns the PatientDeathIndicator.
func DeathIndicator(t *testing.T, message string) string {
t.Helper()
pid := PID(t, message)
return pid.PatientDeathIndicator.String()
}
// DeathTime returns the PatientDeathIndicator.
func DeathTime(t *testing.T, message string) *time.Time {
t.Helper()
pid := PID(t, message)
if pid.PatientDeathDateAndTime == nil {
return nil
}
return &pid.PatientDeathDateAndTime.Time
}
// OrderStatus returns OrderStatus from the ORC segment.
func OrderStatus(t *testing.T, message string) string {
t.Helper()
orc := ORC(t, message)
return orc.OrderStatus.String()
}
// OBXSetID returns the OBX's SetID.
func OBXSetID(t *testing.T, obx *hl7.OBX) string {
t.Helper()
return strconv.Itoa(int(obx.SetIDOBX.Value))
}
// ValueType returns the OBX's observation value type.
func ValueType(t *testing.T, obx *hl7.OBX) string {
t.Helper()
return obx.ValueType.String()
}
// OBXResultStatus returns the observation result status.
func OBXResultStatus(t *testing.T, obx *hl7.OBX) string {
t.Helper()
return obx.ObservationResultStatus.String()
}
// OBXFieldsFromOBXs applies the function f to all of the given OBX segments.
func OBXFieldsFromOBXs(t *testing.T, obxs []*hl7.OBX, f func(*testing.T, *hl7.OBX) string) []string {
var fromOBX []string
for _, o := range obxs {
fromOBX = append(fromOBX, f(t, o))
}
return fromOBX
}
// OBXFields applies the function f to all of the OBX segments in the messages.
func OBXFields(t *testing.T, messages []string, f func(*testing.T, *hl7.OBX) string) [][]string {
t.Helper()
var result [][]string
for _, m := range messages {
obxs := AllOBX(t, m)
fromOBX := OBXFieldsFromOBXs(t, obxs, f)
result = append(result, fromOBX)
}
return result
}
// MessageControlIDFromMSH returns the message control ID from the MSH segment.
func MessageControlIDFromMSH(t *testing.T, message string) string {
t.Helper()
msh := MSH(t, message)
return msh.MessageControlID.String()
}
// MessageControlIDFromMSA returns the message control ID from the MSA segment.
func MessageControlIDFromMSA(t *testing.T, message string) string {
t.Helper()
msa := MSA(t, message)
return msa.MessageControlID.String()
}
// PointOfCare returns the point of care from the PV1.AssignedPatientLocation.
func PointOfCare(t *testing.T, message string) string {
t.Helper()
pv1 := PV1(t, message)
return pv1.AssignedPatientLocation.PointOfCare.String()
}
// AccountStatus returns the PV1.AccountStatus.
func AccountStatus(t *testing.T, message string) string {
t.Helper()
pv1 := PV1(t, message)
return pv1.AccountStatus.String()
}
// PatientClass returns the PV1.PatientClass.
func PatientClass(t *testing.T, message string) string {
t.Helper()
pv1 := PV1(t, message)
return pv1.PatientClass.String()
}
// VisitNumber returns the VisitNumber ID.
func VisitNumber(t *testing.T, message string) string {
t.Helper()
pv1 := PV1(t, message)
return pv1.VisitNumber.IDNumber.String()
}
// EventDateTime returns the event's date time.
func EventDateTime(t *testing.T, message string) time.Time {
t.Helper()
evn := EVN(t, message)
return evn.RecordedDateTime.Time.UTC()
}
// FirstName returns the patient first name from the message.
func FirstName(t *testing.T, message string) string {
t.Helper()
pid := PID(t, message)
return pid.PatientName[0].GivenName.String()
}
// MRN returns the patient identifier.
func MRN(t *testing.T, message string) string {
t.Helper()
pid := PID(t, message)
return pid.PatientID.IDNumber.String()
}
// PriorPatientIdentifierList returns the PriorPatientIdentifierList from the MRG segment.
func PriorPatientIdentifierList(t *testing.T, message string) []string {
t.Helper()
mrg := MRG(t, message)
var ids []string
for _, id := range mrg.PriorPatientIdentifierList {
ids = append(ids, id.IDNumber.String())
}
return ids
}
// SendingApplication returns the message's sending application.
func SendingApplication(t *testing.T, message string) string {
t.Helper()
return MSH(t, message).SendingApplication.String()
}
// SendingFacility returns the message's sending facility.
func SendingFacility(t *testing.T, message string) string {
t.Helper()
return MSH(t, message).SendingFacility.String()
}
// MessageType returns this message's message type.
func MessageType(t *testing.T, message string) string {
t.Helper()
msh := MSH(t, message)
return fmt.Sprintf("%s^%s", msh.MessageType.MessageCode.String(), msh.MessageType.TriggerEvent.String())
}
// ObservationValue returns this message's OBX observation value.
func ObservationValue(t *testing.T, message string) string {
t.Helper()
obx := OBX(t, message)
return string(obx.ObservationValue[0])
}