forked from dshills/golevel7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
70 lines (65 loc) · 1.93 KB
/
build.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
package golevel7
import (
"fmt"
"time"
)
// MsgInfo describes the basic message fields
type MsgInfo struct {
SendingApp string `hl7:"MSH.3"`
SendingFacility string `hl7:"MSH.4"`
ReceivingApp string `hl7:"MSH.5"`
ReceivingFacility string `hl7:"MSH.6"`
MsgDate string `hl7:"MSH.7"` // if blank will generate
MessageType string `hl7:"MSH.9"` // Required example ORM^001
ControlID string `hl7:"MSH.10"` // if blank will generate
ProcessingID string `hl7:"MSH.11"` // default P
VersionID string `hl7:"MSH.12"` // default 2.4
}
// NewMsgInfo returns a MsgInfo with controlID, message date, Processing Id, and Version set
// Version = 2.4
// ProcessingID = P
func NewMsgInfo() *MsgInfo {
info := MsgInfo{}
now := time.Now()
t := now.Format("20060102150405")
info.MsgDate = t
info.ControlID = fmt.Sprintf("MSGID%s%d", t, now.Nanosecond())
info.ProcessingID = "P"
info.VersionID = "2.4"
return &info
}
// NewMsgInfoAck returns a MsgInfo ACK based on the MsgInfo passed in
func NewMsgInfoAck(mi *MsgInfo) *MsgInfo {
info := NewMsgInfo()
info.MessageType = "ACK"
info.ReceivingApp = mi.SendingApp
info.ReceivingFacility = mi.SendingFacility
info.SendingApp = mi.ReceivingApp
info.SendingFacility = mi.ReceivingFacility
info.ProcessingID = mi.ProcessingID
info.VersionID = mi.VersionID
return info
}
// StartMessage returns a Message with an MSH segment based on the MsgInfo struct
func StartMessage(info MsgInfo) (*Message, error) {
if info.MessageType == "" {
return nil, fmt.Errorf("Message Type is required")
}
now := time.Now()
t := now.Format("20060102150405")
if info.MsgDate == "" {
info.MsgDate = t
}
if info.ControlID == "" {
info.ControlID = fmt.Sprintf("MSGID%s%d", t, now.Nanosecond())
}
if info.ProcessingID == "" {
info.ProcessingID = "P"
}
if info.VersionID == "" {
info.VersionID = "2.4"
}
msg := NewMessage([]byte{})
Marshal(msg, &info)
return msg, nil
}