-
Notifications
You must be signed in to change notification settings - Fork 46
/
location.go
61 lines (53 loc) · 1.19 KB
/
location.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
package golevel7
import (
"strconv"
"strings"
)
/**
Location syntax
// loc uses the format segment.field.component.subcomponent
// loc == "" returns the message
// loc == "MSH" returns the MSH segment
// loc == "MSH.2" returns the second field of the MSH segment
// etc
**/
// Location specifies a value or values in an Message
type Location struct {
Segment string
FieldSeq int
Comp int
SubComp int
}
// NewLocation creates a Location struct based on location string syntax
func NewLocation(l string) *Location {
la := strings.Split(l, ".")
loc := Location{FieldSeq: -1, Comp: -1, SubComp: -1}
lenLA := len(la)
if lenLA > 0 {
loc.Segment = la[0]
}
if lenLA > 1 {
if i, err := strconv.Atoi(la[1]); err == nil {
loc.FieldSeq = i
}
}
if lenLA > 2 {
if i, err := strconv.Atoi(la[2]); err == nil {
loc.Comp = i
}
}
if lenLA > 3 {
if i, err := strconv.Atoi(la[3]); err == nil {
loc.SubComp = i
}
}
return &loc
}
// mshOffset used just for building messages. Since the field seperator is used
// in the MSH seg 1 building messages gets confused about locations
func mshOffset(l *Location) *Location {
if l.Segment == "MSH" && l.FieldSeq > 2 {
l.FieldSeq--
}
return l
}