-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencounter.go
289 lines (244 loc) · 8.54 KB
/
encounter.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
package main
import (
"bytes"
"github.com/google/uuid"
"log"
"regexp"
"strings"
"text/template"
"time"
)
type Hl7Encoding struct {
Field string `default:"|"`
Component string `default:"^"`
SubComponent string `default:"&"`
Repetition string `default:"~"`
Escape string `default:"\\"`
}
type Utility string
func (u Utility) Uuid() string {
return uuid.New().String()
}
type Encounter struct {
Output OutputTemplate
Patient Patient `json:"Patient"`
Providers []Provider `json:"Providers"`
Class CodedElement `json:"PatientClass"`
Event string `json:"Event"`
DateTime time.Time
Utility Utility
}
func NewEncounter(hl7template OutputTemplate) Encounter {
return Encounter{DateTime: time.Now(), Output: hl7template}
}
func (e Encounter) AsHl7() string {
hl7 := new(bytes.Buffer)
err := e.Output.Template.Execute(hl7, e)
if err != nil {
log.Fatal(err)
}
return hl7.String()
}
type Patient struct {
// PID
Ids []Identifier `json:"Ids"`
Name Name `json:"Name"`
MotherMaidenName string `json:"MotherMaidenName"`
DOB time.Time `json:"DOB"`
Gender string `json:"Gender"`
Race CodedElement `json:"Race"`
Addresses []ExtendedAddress `json:"Address"`
Phones []ExtendedTelecommunication `json:"Comm"`
Language CodedElement `json:"Language"`
MaritalStatus CodedElement `json:"MaritalStatus"`
Religion CodedElement `json:"Religion"`
SSN string `json:"SSN"`
DriversLicense DriversLicense `json:"DriversLicense"`
EthnicGroup CodedElement `json:"EthnicGroup"`
MultipleBirthIndicator string `json:"MultipleBirthIndicator"`
BirthOrder int `json:"BirthOrder"`
DeathIndicator string `json:"DeathIndicator"`
DeathDateTime time.Time `json:"DeathDateTime"`
}
func (p Patient) PhoneNumbersAsHl7(phones []ExtendedTelecommunication, encoding Hl7Encoding) string {
var phonesHl7 []string
for _, phone := range phones {
phonesHl7 = append(phonesHl7, phone.AsHl7(encoding))
}
return strings.Join(phonesHl7, encoding.Repetition)
}
func (p Patient) HomePhoneNumbersAsHl7(encoding Hl7Encoding) string {
phones := p.PhonesByUseCode("PRN")
phones = append(phones, p.PhonesByUseCode("ORN")...)
phones = append(phones, p.PhonesByUseCode("NET")...)
return p.PhoneNumbersAsHl7(phones, encoding)
}
func (p Patient) WorkPhoneNumbersAsHl7(encoding Hl7Encoding) string {
phones := p.PhonesByUseCode("WPN")
return p.PhoneNumbersAsHl7(phones, encoding)
}
func (p Patient) PhonesByUseCode(useCode string) []ExtendedTelecommunication {
var phones []ExtendedTelecommunication
for i := range p.Phones {
if p.Phones[i].UseCode == useCode {
phones = append(phones, p.Phones[i])
}
}
return phones
}
func (p Patient) IdentifierAsHl7(idTypeCode string, encoding Hl7Encoding) string {
var id Identifier
for i := range p.Ids {
if p.Ids[i].IdType == idTypeCode {
id = p.Ids[i]
break
}
}
return id.AsHl7(encoding)
}
func (p Patient) IdentifiersAsHl7(encoding Hl7Encoding) string {
var identifiers []string
for _, id := range p.Ids {
identifiers = append(identifiers, id.AsHl7(encoding))
}
return strings.Join(identifiers, encoding.Repetition)
}
func (p Patient) AddressesAsHl7(encoding Hl7Encoding) string {
var addresses []string
for _, address := range p.Addresses {
addresses = append(addresses, address.AsHl7(encoding))
}
return strings.Join(addresses, encoding.Repetition)
}
type Name struct {
Family string `json:"Family"`
Given string `json:"Given"`
Second string `json:"Second"`
Suffix string `json:"Suffix"`
Prefix string `json:"Prefix"`
}
func (n Name) AsHl7(encoding Hl7Encoding) string {
names := []string{n.Family, n.Given, n.Second, n.Suffix, n.Prefix}
return strings.Join(names, encoding.Component)
}
type Identifier struct {
IdNumber string `json:"IdNumber"`
CheckDigit string `json:"CheckDigit"`
CheckDigitScheme string `json:"CheckDigitScheme"`
IdType string `json:"IdType"`
}
func (id Identifier) AsHl7(encoding Hl7Encoding) string {
idSlice := []string{id.IdNumber, id.CheckDigit, id.CheckDigitScheme, "", id.IdType}
return strings.Join(idSlice, encoding.Component)
}
type CodedElement struct {
// CE HL7v2 Data Type
Identifier string `json:"Id"`
Text string `json:"Text"`
CodingSystem string `json:"CodingSystem"`
}
func (ce CodedElement) AsHl7(encoding Hl7Encoding) string {
ces := []string{ce.Identifier, ce.Text, ce.CodingSystem}
return strings.Join(ces, encoding.Component)
}
type ExtendedAddress struct {
// XAD HL7v2 Data Type - yes I know the address lines aren't named as in spec :)
// also haven't filled out for all possible fields for XAD because I haven't
// needed them yet.
Line1 string `json:"Line1"`
Line2 string `json:"Line2"`
City string `json:"City"`
State string `json:"State"`
Zip string `json:"Zip"`
Country string `json:"Country"`
Type string `json:"Type"`
}
func (ea ExtendedAddress) AsHl7(encoding Hl7Encoding) string {
eas := []string{ea.Line1, ea.Line2, ea.City, ea.State, ea.Zip, ea.Country, ea.Type}
return strings.Join(eas, encoding.Component)
}
type ExtendedTelecommunication struct {
// XTN HL7v2 Data Type
TelephoneNumber string `json:"PhoneNumber"`
UseCode string `json:"UseCode"`
EquipmentType string `json:"Type"`
EmailAddress string `json:"Email"`
CountryCode string `json:"CountryCode"`
AreaCode string `json:"AreaCode"`
LocalNumber string `json:"LocalNumber"`
Extension string `json:"Extension"`
AnyText string `json:"AnyText"`
ExtensionPrefix string `json:"ExtensionPrefix"`
SpeedDialCode string `json:"SpeedDialCode"`
UnformattedTelephoneNumber string `json:"UnformattedTelephoneNumber"`
}
func (et ExtendedTelecommunication) AsHl7(encoding Hl7Encoding) string {
ets := []string{
et.TelephoneNumber,
et.UseCode,
et.EquipmentType,
et.EmailAddress,
et.CountryCode,
et.AreaCode,
et.LocalNumber,
et.Extension,
et.AnyText,
et.ExtensionPrefix,
et.SpeedDialCode,
et.UnformattedTelephoneNumber,
}
return strings.Join(ets, encoding.Component)
}
type DriversLicense struct {
LicenseNumber string `json:"Number"`
State string `json:"State"`
ExpirationDate time.Time `json:"ExpirationDate"`
}
func (dl DriversLicense) AsHl7(encoding Hl7Encoding) string {
dls := []string{dl.LicenseNumber, dl.State, dl.ExpirationDate.Format("20060102")}
return strings.Join(dls, encoding.Component)
}
type Provider struct {
Identifier Identifier `json:"Id"`
Name Name `json:"Name"`
Degree string `json:"Degree"`
Role CodedElement `json:"Role"`
}
type OutputTemplate struct {
Encoding Hl7Encoding
Contents string
Template *template.Template
}
func NewOutputTemplate(templateContents string) OutputTemplate {
t := OutputTemplate{}
t.Encoding = GetHl7Encoding(templateContents)
t.Contents = templateContents
var err error
t.Template, err = template.New("").Parse(templateContents)
if err != nil {
log.Fatal(err)
}
return t
}
func GetHl7Encoding(templateContents string) Hl7Encoding {
var encoding = Hl7Encoding{}
rgx := regexp.MustCompile(`^(?P<segment>MSH)(?P<field>.)(?P<component>.)(?P<repetition>.)(?P<escape>.)(?P<subcomponent>.)(?P<field2>.)`)
matches := rgx.FindStringSubmatch(templateContents)
segment := rgx.SubexpIndex("segment")
field := rgx.SubexpIndex("field")
component := rgx.SubexpIndex("component")
repetition := rgx.SubexpIndex("repetition")
escape := rgx.SubexpIndex("escape")
subcomponent := rgx.SubexpIndex("subcomponent")
field2 := rgx.SubexpIndex("field2")
// We expect the segment to be MSH and the first match after the segment and the last matched character to be the same
// So given MSH|^~\&|... we expect 1st after the segment and last matched to be |
if matches != nil && matches[segment] == "MSH" && matches[field] == matches[field2] {
encoding.Component = matches[component]
encoding.SubComponent = matches[subcomponent]
encoding.Repetition = matches[repetition]
encoding.Escape = matches[escape]
encoding.Field = matches[field]
}
return encoding
}