forked from lenaten/hl7
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsegment.go
208 lines (195 loc) · 4.62 KB
/
segment.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
package hl7
import (
"errors"
"fmt"
"strings"
)
// Segment is an HL7 segment
type Segment struct {
Fields []Field
Value []rune
maxSeq int
}
func (s *Segment) String() string {
var str string
for _, f := range s.Fields {
str += fmt.Sprintf("Segment Field: Seq: %d Value: %s\n", f.SeqNum, string(f.Value))
str += f.String()
}
return str
}
func (s *Segment) isMSH() bool {
var toCheck []rune
if len(s.Value) >= 3 {
toCheck = s.Value[:3]
} else if len(s.Fields) != 0 {
f, err := s.Field(0)
if err != nil {
return false
}
toCheck = f.Value[:3]
} else {
return false
}
if string(toCheck) == "MSH" {
return true
} else {
return false
}
}
func (s *Segment) parse(seps *Delimeters) error {
if len(s.Value) < 3 {
return fmt.Errorf("Invalid segment. Length %v", len(s.Value))
}
isMSH := s.isMSH()
r := strings.NewReader(string(s.Value))
i := 0
ii := 0
seq := 0
for {
ch, _, _ := r.ReadRune()
ii++
switch {
case ch == eof || (ch == endMsg && seps.LFTermMsg):
if ii > i {
start := i
if ii > i+1 && s.Value[i] == seps.Component {
start++
}
fld := Field{Value: s.Value[i : ii-1], SeqNum: seq}
fld.parse(seps)
s.Fields = append(s.Fields, fld)
}
return nil
case isMSH && seq == 2 && ch == seps.Repetition:
// ignore repeat separator in separator definition
case isMSH && seq == 2 && ch == seps.Escape:
// ignore escape separator in separator definition
case ch == seps.Field:
if isMSH && seq == 2 {
// the separator list is a field in MSH seq 2
s.forceField(s.Value[i:ii-1], seq)
} else {
start := i
if s.Value[i] == seps.Component {
start++
}
fld := Field{Value: s.Value[start : ii-1], SeqNum: seq}
fld.parse(seps)
s.Fields = append(s.Fields, fld)
}
i = ii
seq++
if isMSH && seq == 1 {
// The field separator is itself a field for MSH seq 1
s.forceField([]rune(string(seps.Field)), seq)
seq++
}
case ch == seps.Repetition:
fld := Field{Value: s.Value[i : ii-1], SeqNum: seq}
fld.parse(seps)
s.Fields = append(s.Fields, fld)
i = ii
case ch == seps.Escape:
ii++
r.ReadRune()
}
}
}
// forceField will force the creation of a field / component / subcomponent
// This is used for separator defines in the MSH segemnt
// ...and the name forceField is cool ;)
func (s *Segment) forceField(val []rune, seq int) {
if seq > s.maxSeq {
s.maxSeq = seq
}
fld := Field{Value: val, SeqNum: seq}
cmp := Component{Value: val}
cmp.SubComponents = append(cmp.SubComponents, SubComponent{Value: val})
fld.Components = append(fld.Components, cmp)
s.Fields = append(s.Fields, fld)
}
func (s *Segment) encode(seps *Delimeters) []rune {
buf := []string{}
for _, f := range s.Fields {
buf = append(buf, string(f.Value))
}
if s.isMSH() {
firstFields := strings.Join(buf[0:3], "")
otherFields := strings.Join(buf[3:], string(seps.Field))
return []rune(strings.Join([]string{firstFields, otherFields}, string(seps.Field)))
} else {
return []rune(strings.Join(buf, string(seps.Field)))
}
}
// Field returns the field with sequence number i
func (s *Segment) Field(i int) (*Field, error) {
for idx, fld := range s.Fields {
if fld.SeqNum == i {
return &s.Fields[idx], nil
}
}
return nil, ErrFieldNotFound
}
// AllFields returns all fields with sequence number i
func (s *Segment) AllFields(i int) ([]*Field, error) {
flds := []*Field{}
for idx, fld := range s.Fields {
if fld.SeqNum == i {
flds = append(flds, &s.Fields[idx])
}
}
return flds, nil
}
// Get returns the first value specified by the Location
func (s *Segment) Get(l *Location) (string, error) {
if l.FieldSeq == -1 {
return string(s.Value), nil
}
fld, err := s.Field(l.FieldSeq)
if err != nil {
return "", err
}
return fld.Get(l)
}
// GetAll returns all values specified by the Location
func (s *Segment) GetAll(l *Location) ([]string, error) {
vals := []string{}
if l.FieldSeq == -1 {
vals = append(vals, string(s.Value))
return vals, nil
}
flds, err := s.AllFields(l.FieldSeq)
if err != nil {
return vals, err
}
for _, f := range flds {
v, err := f.Get(l)
if err != nil {
return vals, err
}
vals = append(vals, v)
}
return vals, nil
}
// Set will insert a value into a message at Location
func (s *Segment) Set(l *Location, val string, seps *Delimeters) error {
if l.FieldSeq == -1 {
return errors.New("Field is required")
}
if s.maxSeq < l.FieldSeq {
for i := s.maxSeq + 1; i <= l.FieldSeq; i++ {
s.forceField([]rune(""), i)
}
}
fld, err := s.Field(l.FieldSeq)
if err != nil {
return err
}
err = fld.Set(l, val, seps)
if err != nil {
return err
}
s.Value = s.encode(seps)
return nil
}