-
Notifications
You must be signed in to change notification settings - Fork 4
/
encode.go
479 lines (444 loc) · 8.96 KB
/
encode.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
package hl7
import (
"bytes"
"fmt"
"reflect"
"strconv"
"time"
)
const nextLine = '\r'
const defaultSep = "|"
const defaultChars = `^~\&`
// Create a new Encoder. Options may be nil.
func NewEncoder(opt *EncodeOption) *Encoder {
e := &Encoder{}
if opt != nil {
e.opt = *opt
}
return e
}
// Encoding options.
type EncodeOption struct {
TrimTrailingSeparator bool
}
type Encoder struct {
// Set only from init.
initSep string
initChars string
sep byte // usually a |
repeat byte // usually a ~
dividers []byte
esc map[byte][]byte
deferred [3]*bytes.Buffer
buf *bytes.Buffer
opt EncodeOption
}
func (e *Encoder) Encode(message any) ([]byte, error) {
e.init("", "")
err := e.walk(1, reflect.ValueOf(message))
if err != nil {
return nil, err
}
return e.buf.Bytes(), nil
}
// Init separators and reset buffers.
// If sep or chars are empty, then the previous value or the default will be used.
func (e *Encoder) init(sep, chars string) {
if len(sep) > 0 {
sep = e.initSep
}
if len(sep) == 0 {
sep = defaultSep
}
if len(chars) == 0 {
chars = e.initSep
}
if len(chars) == 0 {
chars = defaultChars
}
e.sep = byte(sep[0])
e.repeat = chars[1]
e.dividers = []byte{sep[0], chars[0], chars[3]}
if e.deferred[0] == nil {
e.deferred[0] = &bytes.Buffer{}
}
if e.deferred[1] == nil {
e.deferred[1] = &bytes.Buffer{}
}
if e.deferred[2] == nil {
e.deferred[2] = &bytes.Buffer{}
}
if e.buf == nil {
e.buf = &bytes.Buffer{}
}
e.buf.Reset()
for _, d := range e.deferred {
d.Reset()
}
esc := chars[2]
e.esc = map[byte][]byte{
e.sep: {esc, 'F', esc},
chars[0]: {esc, 'S', esc},
chars[1]: {esc, 'R', esc},
chars[2]: {esc, 'E', esc},
chars[3]: {esc, 'T', esc},
}
}
func (e *Encoder) walk(seq int, wv reflect.Value) error {
if !wv.IsValid() {
return nil
}
metaTag, err := e.meta(wv.Type())
if err != nil {
return err
}
if !metaTag.Present {
return nil
}
switch metaTag.Type {
default:
return nil
case structTrigger, structTriggerGroup:
switch wv.Kind() {
default:
return nil
case reflect.Pointer:
return e.walk(seq, wv.Elem())
case reflect.Slice:
l := wv.Len()
for i := 0; i < l; i++ {
v := wv.Index(i)
err := e.walk(i+1, v)
if err != nil {
return err
}
}
return nil
case reflect.Struct:
ct := wv.NumField()
for i := 0; i < ct; i++ {
v := wv.Field(i)
err := e.walk(seq, v)
if err != nil {
return err
}
}
return nil
}
case structSegment:
switch wv.Kind() {
default:
return nil
case reflect.Pointer:
return e.walk(seq, wv.Elem())
case reflect.Slice:
l := wv.Len()
for i := 0; i < l; i++ {
v := wv.Index(i)
err := e.encodeSegment(i+1, v)
if err != nil {
return err
}
}
return nil
case reflect.Struct:
return e.encodeSegment(seq, wv)
}
}
}
func (e *Encoder) meta(wt reflect.Type) (tag, error) {
switch wt.Kind() {
default:
return tag{}, nil
case reflect.Pointer:
return e.meta(wt.Elem())
case reflect.Slice:
return e.meta(wt.Elem())
case reflect.Struct:
sf, ok := wt.FieldByName(hl7MetaName)
if !ok {
return tag{}, nil
}
return parseTag(sf.Name, sf.Tag.Get(tagName))
}
}
// encodeSegment the given message into buffer.
func (e *Encoder) encodeSegment(seq int, st reflect.Value) error {
stt := st.Type()
var SegmentName string
var SegmentSize int32
var maxOrd int32
type field struct {
name string
present bool
tag tag
value any
}
var fieldList []field
var msgSep string
for i := 0; i < st.NumField(); i++ {
fld := stt.Field(i)
f := st.Field(i)
tag, err := parseTag(fld.Name, fld.Tag.Get(tagName))
if err != nil {
return err
}
switch {
case tag.FieldSep:
msgSep = f.String()
case tag.FieldChars:
chars := f.String()
e.init(msgSep, chars)
}
if tag.Meta {
switch tag.Type {
case structTrigger, structTriggerGroup:
return fmt.Errorf("trigger and trigger group structures should not be passed in to encode, package error")
}
}
if !tag.Present {
continue
}
if tag.Meta {
SegmentName = tag.Name
SegmentSize = tag.Order
} else {
if tag.Order > maxOrd {
maxOrd = tag.Order
}
}
fieldList = append(fieldList, field{
name: fld.Name,
present: !f.IsZero(),
tag: tag,
value: f.Interface(),
})
}
if SegmentSize == 0 {
SegmentSize = maxOrd
}
ff := make([]field, SegmentSize)
for _, f := range fieldList {
index := f.tag.Order - 1
if index < 0 || index >= SegmentSize {
continue
}
ff[index] = f
}
e.write(SegmentName, 0, true)
for _, f := range ff {
if f.tag.Omit {
continue
}
v := f.value
switch {
// Auto populate SetID sequences.
case f.tag.Sequence:
if seq == 0 {
return fmt.Errorf("incorrect zero sequence number")
}
if s, ok := v.(string); ok && len(s) == 0 {
v = strconv.FormatInt(int64(seq), 10)
}
}
direct := !e.opt.TrimTrailingSeparator
e.writeSep(0, 0, direct)
err := e.encodeDataType(f.tag, v, 0)
if err != nil {
return err
}
}
e.resetAllDeferred()
e.writeSep(0, nextLine, false)
return nil
}
func (e *Encoder) flushDeferred(level int) {
// If level 0"|", then write level 0, remove 1, 2.
// If level 1"^", then write level 0 and 1, remove 2.
// If level 2"&", then write level 0, 1, and 2.
for index, d := range e.deferred {
if index <= level {
db := d.Bytes()
e.buf.Write(db)
}
d.Reset()
}
}
// write escapes individual values.
func (e *Encoder) write(val string, level int, noEscape bool) {
if len(val) > 0 {
e.flushDeferred(level)
}
buf := e.buf
if noEscape {
buf.WriteString(val)
return
}
for i := 0; i < len(val); i++ {
c := val[i]
if esc, is := e.esc[c]; is {
buf.Write(esc)
continue
}
buf.WriteByte(c)
}
}
func (e *Encoder) writeByte(val []byte, level int, noEscape bool) {
if len(val) > 0 {
e.flushDeferred(level)
}
buf := e.buf
if noEscape {
buf.Write(val)
return
}
for i := 0; i < len(val); i++ {
c := val[i]
if esc, is := e.esc[c]; is {
buf.Write(esc)
continue
}
buf.WriteByte(c)
}
}
func (e *Encoder) resetAllDeferred() {
for _, d := range e.deferred {
d.Reset()
}
}
func (e *Encoder) writeSep(level int, sep byte, direct bool) {
if sep == 0 {
sep = e.dividers[level]
}
// If level 0"|", then write it directly and reset the other layers.
// If level 1"&", then reset 2 and write to defered 1.
for index, d := range e.deferred {
if index > level {
d.Reset()
}
}
if direct {
e.deferred[level].WriteByte(sep)
e.flushDeferred(level)
return
}
e.deferred[level].WriteByte(sep)
}
func (e *Encoder) encodeDataType(t tag, o interface{}, level int) error {
if o == nil || !t.Present {
return nil
}
switch v := o.(type) {
default:
rv := reflect.ValueOf(o)
if rv.IsZero() {
return nil
}
switch rv.Kind() {
default:
return fmt.Errorf("unknown value kind: %v", rv.Kind())
case reflect.Pointer:
rv = rv.Elem()
fallthrough
case reflect.Struct:
var SegmentName string
var SegmentSize int32
var maxOrd int32
type field struct {
name string
present bool
tag tag
value any
}
var fieldList []field
rt := rv.Type()
ct := rt.NumField()
for i := 0; i < ct; i++ {
ft := rt.Field(i)
tag, err := parseTag(ft.Name, ft.Tag.Get(tagName))
if err != nil {
return err
}
if !tag.Present {
continue
}
if tag.Meta {
SegmentName = tag.Name
SegmentSize = tag.Order
} else {
if tag.Order > maxOrd {
maxOrd = tag.Order
}
}
f := rv.Field(i)
fieldList = append(fieldList, field{
name: ft.Name,
present: !f.IsZero(),
tag: tag,
value: f.Interface(),
})
}
if SegmentSize == 0 {
SegmentSize = maxOrd
}
ff := make([]field, SegmentSize)
for _, f := range fieldList {
index := f.tag.Order - 1
if index < 0 || index >= SegmentSize {
continue
}
ff[index] = f
}
for i, f := range ff {
if i != 0 {
e.writeSep(level+1, 0, false)
}
err := e.encodeDataType(f.tag, f.value, level+1)
if err != nil {
return fmt.Errorf("%s (%+v): %w", SegmentName, f.value, err)
}
}
case reflect.Slice:
if rv.Type().Elem().Kind() == reflect.Uint8 {
e.writeByte(rv.Bytes(), level, true)
return nil
}
ct := rv.Len()
for i := 0; i < ct; i++ {
if i != 0 {
e.writeSep(level, e.repeat, true)
}
x := rv.Index(i)
value := x.Interface()
err := e.encodeDataType(t, value, level)
if err != nil {
return fmt.Errorf("repeat (%+v): %w", value, err)
}
}
return nil
}
case []byte:
e.writeByte(v, level, true)
case string:
e.write(v, level, t.NoEscape)
case time.Time:
if v.IsZero() {
return nil
}
var sv string
switch t.Format {
default:
sv = v.Format("20060102150405")
case "YMDHMS":
sv = v.Format("20060102150405")
case "YMDHM":
sv = v.Format("200601021504")
case "YMD":
sv = v.Format("20060102")
case "HM":
sv = v.Format("1504")
}
e.write(sv, level, t.NoEscape)
}
return nil
}