forked from lsarodnik/golevel7
-
Notifications
You must be signed in to change notification settings - Fork 2
/
subcomponent.go
48 lines (42 loc) · 900 Bytes
/
subcomponent.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
package golevel7
import (
"strconv"
"strings"
)
// SubComponent is an HL7 subcomponent
type SubComponent struct {
Value []rune
}
func (s SubComponent) GetAll(loc *Location) ([]string, error) {
return []string{string(s.Value)}, nil
}
func (s SubComponent) Get(loc *Location) (string, error) {
return string(s.Value), nil
}
func (s SubComponent) RelativeLocation(tag string) *Location {
location := &Location{}
parts := strings.Split(tag, ".")
parts = reverse(parts[1:])
for i, part := range parts {
number, err := strconv.Atoi(part)
if err != nil {
continue
}
switch i {
case 0:
location.SubComp = number
case 1:
location.Comp = number
case 2:
location.FieldSeq = number
}
}
return location
}
func reverse(parts []string) []string {
for i := 0; i < len(parts)/2; i++ {
j := len(parts) - i - 1
parts[i], parts[j] = parts[j], parts[i]
}
return parts
}