-
Notifications
You must be signed in to change notification settings - Fork 4
/
decode.go
651 lines (588 loc) · 14.2 KB
/
decode.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
package hl7
import (
"bytes"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"unicode"
)
type lineDecoder struct {
sep byte // usually a |
repeat byte // usually a ~
dividers [3]byte // usually |, ^, &
chars [4]byte // usually ^!\&
escape byte // usually a \
readSep bool
ignoreSep bool
unescaper *strings.Replacer
}
// Decode bytes into HL7 structures.
type Decoder struct {
registry Registry
opt DecodeOption
}
// Decode options for the HL7 decoder.
type DecodeOption struct {
ErrorZSegment bool // Error on an unknown Zxx segment when true.
HeaderOnly bool // Only decode first segment, usually the header.
IgnoreFieldSep bool // Ignore field separator values in text fields.
}
// Create a new Decoder. A registry must be provided. Option is optional.
func NewDecoder(registry Registry, opt *DecodeOption) *Decoder {
d := &Decoder{
registry: registry,
}
if opt != nil {
d.opt = *opt
}
return d
}
// Decode takes an hl7 message and returns a final trigger with all segments grouped.
func (d *Decoder) Decode(data []byte) (any, error) {
list, err := d.DecodeList(data)
if err != nil {
return nil, fmt.Errorf("segment list: %w", err)
}
g, err := d.DecodeGroup(list)
if err != nil {
return nil, fmt.Errorf("trigger group: %w", err)
}
return g, nil
}
// Group a list of elements into trigger groupings.
// A value and error may be present at the same time.
func (d *Decoder) DecodeGroup(list []any) (any, error) {
return group(list, d.registry)
}
// Varies should be implemented on a segment that knows how to
// decode a child VARIES data type.
type Varies interface {
ChildVaries(reg func(string) (any, bool)) (reflect.Value, error)
}
type variesFunc func() (reflect.Value, error)
var variesType = reflect.TypeFor[Varies]()
// SegmentError may be returned as part of the DecodeList result.
// This allows a single segment to be decoded poorly with error, while
// still decoding the rest of the message.
// This will not be returned as an error.
type SegmentError struct {
ErrorList []error
Segment any
}
func (e SegmentError) Error() string {
sb := &strings.Builder{}
fmt.Fprintf(sb, "errors in segment %[1]T=%[1]v:", e.Segment)
for _, err := range e.ErrorList {
sb.WriteRune('\n')
sb.WriteRune('\t')
sb.WriteString(err.Error())
}
return sb.String()
}
func (e SegmentError) Unwrap() []error {
return e.ErrorList
}
// Decode returns a list of segments without any grouping applied.
func (d *Decoder) DecodeList(data []byte) ([]any, error) {
// Explicitly accept both CR and LF as new lines. Some systems do use \n, despite the spec.
lines := bytes.FieldsFunc(data, func(r rune) bool {
switch r {
default:
return false
case '\r', '\n':
return true
}
})
type field struct {
name string
index int
tag tag
field reflect.Value
}
ret := []any{}
ld := &lineDecoder{
ignoreSep: d.opt.IgnoreFieldSep,
}
for index, line := range lines {
lineNumber := index + 1
if len(line) == 0 {
continue
}
segTypeName, n := ld.getID(line)
remain := line[n:]
if len(segTypeName) == 0 {
return nil, fmt.Errorf("line %d: missing segment type", lineNumber)
}
seg, ok := d.registry.Segment(segTypeName)
if !ok {
isZ := len(segTypeName) > 0 && segTypeName[0] == 'Z'
if isZ && !d.opt.ErrorZSegment {
continue
}
return nil, fmt.Errorf("line %d: unknown segment type %q", lineNumber, segTypeName)
}
rt := reflect.TypeOf(seg)
ct := rt.NumField()
fieldList := make([]field, 0, ct)
hasInit := false
rv := reflect.New(rt)
rvv := rv.Elem()
var SegmentName string
var SegmentSize int32
var maxOrd int32
for i := 0; i < ct; i++ {
ft := rt.Field(i)
tagText := ft.Tag.Get(tagName)
tag, err := parseTag(ft.Name, tagText)
if err != nil {
return nil, err
}
if !tag.Present {
continue
}
if tag.Meta {
SegmentName = tag.Name
SegmentSize = tag.Order
if ft.Type.Kind() == reflect.String {
rvv.Field(i).SetString(tag.Name)
}
continue
}
if tag.Order > maxOrd {
maxOrd = tag.Order
}
if tag.FieldSep || tag.FieldChars {
hasInit = true
}
f := field{
name: ft.Name,
index: i,
tag: tag,
}
f.field = rvv.Field(i)
if !f.field.IsValid() {
return nil, fmt.Errorf("%s.%s invalid reflect value", SegmentName, f.name)
}
fieldList = append(fieldList, f)
}
if SegmentSize == 0 {
SegmentSize = maxOrd
}
SegmentFieldLength := int(SegmentSize + 1)
offset := 0
if hasInit {
if len(remain) < 5 {
return nil, fmt.Errorf("missing format delims")
}
ld.sep = remain[0]
copy(ld.chars[:], remain[1:5])
ld.dividers = [3]byte{ld.sep, ld.chars[0], ld.chars[3]}
ld.repeat = ld.chars[1]
ld.escape = ld.chars[2]
ld.setupUnescaper()
ld.readSep = true
remain = remain[5:]
offset = 2
}
if ld.sep == 0 {
return nil, fmt.Errorf("missing sep prior to field")
}
parts := bytes.Split(remain, []byte{ld.sep})
ff := make([]field, SegmentFieldLength)
for _, f := range fieldList {
if f.tag.FieldSep {
f.field.SetString(string(ld.sep))
continue
}
if f.tag.FieldChars {
f.field.SetString(string(ld.chars[:]))
continue
}
index := int(f.tag.Order) - offset
if index < 0 || index >= SegmentFieldLength {
continue
}
ff[index] = f
}
var vfc variesFunc
if rvv.Type().Implements(variesType) {
vfc = func() (reflect.Value, error) {
return rvv.Interface().(Varies).ChildVaries(d.registry.DataType)
}
}
var segmentErrorList []error
for i, f := range ff {
if i >= len(parts) {
break
}
p := parts[i]
if !f.tag.Present {
continue
}
if f.tag.Omit {
continue
}
err := ld.decodeSegmentList(p, f.tag, f.field, vfc)
if err != nil {
if v, ok := err.(*DecodeSegmentError); ok && v.Line == 0 && len(v.SegmentName) == 0 && len(v.FieldName) == 0 {
v.Line = lineNumber
v.SegmentName = SegmentName
v.FieldName = f.name
} else {
err = &DecodeSegmentError{
Line: lineNumber,
SegmentName: SegmentName,
FieldName: f.name,
Inner: err,
}
}
segmentErrorList = append(segmentErrorList, err)
}
}
if segmentErrorList != nil {
ret = append(ret, SegmentError{
ErrorList: segmentErrorList,
Segment: rv.Interface(),
})
} else {
ret = append(ret, rv.Interface())
}
if d.opt.HeaderOnly {
return ret, nil
}
}
return ret, nil
}
func (d *lineDecoder) setupUnescaper() {
d.unescaper = strings.NewReplacer(
string([]byte{d.escape, 'F', d.escape}), string(d.sep),
string([]byte{d.escape, 'S', d.escape}), string(d.chars[0]),
string([]byte{d.escape, 'R', d.escape}), string(d.chars[1]),
string([]byte{d.escape, 'E', d.escape}), string(d.chars[2]),
string([]byte{d.escape, 'T', d.escape}), string(d.chars[3]),
)
}
var timeType reflect.Type = reflect.TypeOf(time.Time{})
func (d *lineDecoder) decodeSegmentList(data []byte, t tag, rv reflect.Value, vfc variesFunc) error {
if len(data) == 0 {
return nil
}
parts := bytes.Split(data, []byte{d.repeat})
for _, p := range parts {
if len(p) == 0 {
continue
}
err := d.decodeSegment(p, t, rv, 1, len(parts) > 1, vfc)
if err != nil {
return &DecodeSegmentError{
FieldType: rv.Type().String(),
Ordinal: t.Order,
Inner: err,
}
}
}
return nil
}
type DecodeSegmentError struct {
Line int
SegmentName string
FieldName string
FieldType string
Ordinal int32
Inner error
}
func (e *DecodeSegmentError) Error() string {
sb := &strings.Builder{}
if e.Line > 0 {
sb.WriteString("line ")
sb.WriteString(strconv.FormatInt(int64(e.Line), 10))
sb.WriteString(", ")
}
if len(e.SegmentName) > 0 {
sb.WriteString(e.SegmentName)
sb.WriteString(".")
}
if len(e.FieldName) > 0 {
sb.WriteString(e.FieldName)
}
if len(e.FieldType) > 0 {
sb.WriteRune('(')
sb.WriteString(e.FieldType)
sb.WriteRune(')')
}
if e.Ordinal > 0 {
sb.WriteRune('[')
sb.WriteString(strconv.FormatInt(int64(e.Ordinal), 10))
sb.WriteRune(']')
}
if e.Inner != nil {
sb.WriteString(": ")
sb.WriteString(e.Inner.Error())
}
return sb.String()
}
func (e *DecodeSegmentError) Unwrap() error {
return e.Inner
}
type DecodeDataError struct {
Tag string
Value string
}
func (e *DecodeDataError) Error() string {
return fmt.Sprintf("%s contains an escape character %s; data may be malformed, invalid type, or contain a bug", e.Tag, e.Value)
}
func (d *lineDecoder) decodeSegment(data []byte, t tag, rv reflect.Value, level int, mustBeSlice bool, vfc variesFunc) error {
type field struct {
tag tag
field reflect.Value
}
isSlice := rv.Kind() == reflect.Slice
if mustBeSlice && !isSlice {
return fmt.Errorf("data repeats but element %v does not", rv.Type())
}
switch rv.Kind() {
default:
return fmt.Errorf("unknown field kind %v value=%v(%v) tag=%v data=%q", rv.Kind(), rv, rv.Type(), t, data)
case reflect.Interface:
if vfc == nil {
return fmt.Errorf("unsupported interface field kind %#v data=%q", t, data)
}
nextRV, err := vfc()
if err != nil {
return err
}
err = d.decodeSegment(data, t, nextRV, level, mustBeSlice, vfc)
rv.Set(nextRV)
return err
case reflect.Pointer:
next := reflect.New(rv.Type().Elem())
rv.Set(next)
return d.decodeSegment(data, t, next.Elem(), level, false, vfc)
case reflect.Slice:
if len(data) == 0 {
return nil
}
itemType := rv.Type().Elem()
if itemType.Kind() == reflect.Uint8 {
rv.SetBytes(data)
return nil
}
itemValue := reflect.New(itemType)
ivv := itemValue.Elem()
err := d.decodeSegment(data, t, ivv, level, false, vfc)
if err != nil {
return fmt.Errorf("slice: %w", err)
}
rv.Set(reflect.Append(rv, ivv))
return nil
case reflect.Struct:
switch rv.Type() {
default:
sep := d.dividers[level]
rt := rv.Type()
ct := rv.NumField()
fieldList := []field{}
var SegmentName string
var SegmentSize int32
var maxOrd int32
for i := 0; i < ct; i++ {
ft := rt.Field(i)
fTag, err := parseTag(ft.Name, ft.Tag.Get(tagName))
if err != nil {
return err
}
if fTag.Meta {
SegmentName = fTag.Name
SegmentSize = fTag.Order
if ft.Type.Kind() == reflect.String {
rv.Field(i).SetString(SegmentName)
}
continue
}
if !fTag.Present {
continue
}
if fTag.Omit {
continue
}
if fTag.Order > maxOrd {
maxOrd = fTag.Order
}
fv := rv.Field(i)
f := field{
tag: fTag,
field: fv,
}
fieldList = append(fieldList, f)
}
if SegmentSize == 0 {
SegmentSize = maxOrd
}
ff := make([]field, int(SegmentSize))
for _, f := range fieldList {
index := int(f.tag.Order - 1)
if index < 0 || index >= len(ff) {
continue
}
ff[index] = f
}
// TODO: Make more robust. Watch for repeats, etc, other stuff.
parts := bytes.Split(data, []byte{sep})
for i, p := range parts {
if i >= len(ff) {
continue
}
f := ff[i]
err := d.decodeSegment(p, f.tag, f.field, level+1, false, vfc)
if err != nil {
return &DecodeSegmentError{
SegmentName: SegmentName,
FieldType: f.field.Type().String(),
FieldName: f.tag.Name,
Ordinal: f.tag.Order,
Inner: err,
}
}
}
return nil
case timeType:
v := d.decodeByte(data, t)
t, err := d.parseDateTime(v)
if err != nil {
return err
}
rv.Set(reflect.ValueOf(t))
return nil
}
case reflect.String:
c1, c2, c3 := d.dividers[0], d.dividers[1], d.dividers[2]
if !d.ignoreSep {
for _, b := range data {
switch b {
case c1, c2, c3:
return &DecodeDataError{
Tag: t.Name,
Value: string(data),
}
}
}
}
rv.SetString(d.decodeByte(data, t))
return nil
}
}
func (d *lineDecoder) decodeByte(v []byte, t tag) string {
if t.NoEscape {
return string(v)
}
return d.unescaper.Replace(string(v))
}
func (d *lineDecoder) getID(data []byte) (string, int) {
if d.readSep {
v, _, _ := bytes.Cut(data, []byte{d.sep})
return string(v), len(v)
}
for i, r := range data {
if unicode.IsLetter(rune(r)) || unicode.IsNumber(rune(r)) {
continue
}
return string(data[:i]), i
}
return string(data), len(data)
}
func dtClean(s string) string {
var i int
return strings.Map(func(r rune) rune {
i++
switch r {
default:
return r
case ' ', ':':
return -1
case '-':
if i <= 8 {
return -1
}
return r
}
}, s)
}
func (d *lineDecoder) parseDateTime(dt string) (time.Time, error) {
var zoneIndex int
dt = dtClean(dt)
dtLen := len(dt)
loop:
for i, r := range dt {
switch {
default:
return time.Time{}, fmt.Errorf("invalid characters in date: %q", dt)
case unicode.IsNumber(r):
case r == '.':
case r == '-':
zoneIndex = i
case r == '+':
zoneIndex = i
case r == '^':
dtLen = i
break loop
}
}
dt = dt[:dtLen]
// Format: YYYY[MM[DD[HH[MM[SS[.S[S[S[S]]]]]]]]][+/-ZZZZ]^<degree of precision>
// 20200522143859198-0700
// 20060102150405
in := dt
var t time.Time
var err error
if zoneIndex > 0 {
tp := dt[:zoneIndex]
zp := dt[zoneIndex:]
switch len(dt) {
default:
if len(tp) < 12 {
return t, fmt.Errorf("unknown date time string size %q", tp)
}
in = tp[:12] + zp
t, err = time.Parse("200601021504-0700", in)
case 4 + 5: // Year.
t, err = time.Parse("2006-0700", in)
case 6 + 5: // Month.
t, err = time.Parse("2006-0700", in)
case 8 + 5: // To the day.
t, err = time.Parse("20060102-0700", in)
case 12 + 5: // To the minute.
t, err = time.Parse("200601021504-0700", in)
case 14 + 5, 16 + 5: // To the second.
in = tp[:14] + zp
t, err = time.Parse("20060102150405-0700", in)
}
if err != nil {
err = fmt.Errorf("field %q: %w", dt, err)
}
return t, err
}
switch len(dt) {
default:
if len(dt) < 12 {
return t, fmt.Errorf("unknown date time string size %q", dt)
}
in = dt[:12]
t, err = time.Parse("200601021504", in)
case 0:
t, err = time.Time{}, nil // No date supplied, use zero value
case 4: // Year.
t, err = time.Parse("2006", in)
case 6: // Month
t, err = time.Parse("200601", in)
case 8: // To the day.
t, err = time.Parse("20060102", in)
case 12: // To the minute.
t, err = time.Parse("200601021504", in)
case 14, 16: // To the second.
in = dt[:14]
t, err = time.Parse("20060102150405", in)
}
return t, err
}