-
Notifications
You must be signed in to change notification settings - Fork 46
/
component.go
87 lines (79 loc) · 1.97 KB
/
component.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
package golevel7
import (
"fmt"
"strings"
)
//Component is an HL7 component
type Component struct {
SubComponents []SubComponent
Value []rune
}
func (c *Component) String() string {
var str string
for _, s := range c.SubComponents {
str += "Component SubComponent: " + string(s.Value) + "\n"
}
return str
}
func (c *Component) parse(seps *Delimeters) error {
r := strings.NewReader(string(c.Value))
i := 0
ii := 0
for {
ch, _, _ := r.ReadRune()
ii++
switch {
case ch == eof || (ch == endMsg && seps.LFTermMsg):
if ii > i {
scmp := SubComponent{Value: c.Value[i : ii-1]}
c.SubComponents = append(c.SubComponents, scmp)
}
return nil
case ch == seps.SubComponent:
scmp := SubComponent{Value: c.Value[i : ii-1]}
c.SubComponents = append(c.SubComponents, scmp)
i = ii
case ch == seps.Escape:
ii++
r.ReadRune()
}
}
}
// SubComponent returns the subcomponent i
func (c *Component) SubComponent(i int) (*SubComponent, error) {
if i > len(c.SubComponents) || i < 1 {
return nil, fmt.Errorf("SubComponent out of range")
}
return &c.SubComponents[i-1], nil
}
func (c *Component) encode(seps *Delimeters) []rune {
buf := []string{}
for _, sc := range c.SubComponents {
buf = append(buf, string(sc.Value))
}
return []rune(string(strings.Join(buf, string(seps.SubComponent))))
}
// Get returns the value specified by the Location
func (c *Component) Get(l *Location) (string, error) {
if l.SubComp == -1 {
return string(c.Value), nil
}
sc, err := c.SubComponent(l.SubComp)
if err != nil {
return "", err
}
return string(sc.Value), nil
}
// Set will insert a value into a message at Location
func (c *Component) Set(l *Location, val string, seps *Delimeters) error {
subloc := l.SubComp
if subloc < 0 {
subloc = 0
}
if x := subloc - len(c.SubComponents) + 1; x > 0 {
c.SubComponents = append(c.SubComponents, make([]SubComponent, x)...)
}
c.SubComponents[subloc].Value = []rune(val)
c.Value = c.encode(seps)
return nil
}