forked from dshills/golevel7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
57 lines (52 loc) · 1.26 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
package golevel7
import (
"errors"
"io"
"reflect"
)
// Encoder writes hl7 messages to a stream
type Encoder struct {
w io.Writer
}
// NewEncoder returns a new Encoder that writes to stream w
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{w: w}
}
// Encode writes the encoding of it to the stream
// It will panic if interface{} is not a pointer to a struct
func (e *Encoder) Encode(it interface{}) error {
msg := &Message{}
b, err := Marshal(msg, it)
if err != nil {
return err
}
i, err := e.w.Write(b)
if err != nil {
return err
}
if i < len(b) {
return errors.New("Failed to write all bytes")
}
return nil
}
// Marshal will insert values into a message
// It will panic if interface{} is not a pointer to a struct
func Marshal(m *Message, it interface{}) ([]byte, error) {
seg := Segment{Value: []rune("MSH" + string(m.Delimeters.Field) + m.Delimeters.DelimeterField)}
seg.parse(&m.Delimeters)
m.Segments = append(m.Segments, seg)
st := reflect.ValueOf(it).Elem()
stt := st.Type()
for i := 0; i < st.NumField(); i++ {
fld := stt.Field(i)
r := fld.Tag.Get("hl7")
if r != "" {
l := NewLocation(r)
val := st.Field(i).String()
if err := m.Set(l, val); err != nil {
return nil, err
}
}
}
return []byte(string(m.Value)), nil
}