forked from azoner/gox12
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x12path.go
243 lines (227 loc) · 6.15 KB
/
x12path.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
Parses an x12 path
*/
package gox12
import (
"bytes"
"errors"
"fmt"
"path"
"regexp"
"strconv"
"strings"
)
var refdesRegexp = regexp.MustCompile("^(?P<seg_id>[A-Z][A-Z0-9]{1,2})?(\\[(?P<id_val>[A-Z0-9]+)\\])?(?P<ele_idx>[0-9]{2})?(-(?P<subele_idx>[0-9]+))?$")
// An X12 path is comprised of a path of loop identifiers, a segment
// identifier, and element position, and a composite position.
//
// The last loop id might be a segment id.
// /LOOP_1/LOOP_2
// /LOOP_1/LOOP_2/SEG
// /LOOP_1/LOOP_2/SEG02
// /LOOP_1/LOOP_2/SEG[424]02-1
// LOOP_2/SEG02
// SEG[434]02-1
// 02-1
// 02
type X12Path struct {
Path string // no leading slash indicates a relative path
SegmentId string
IdValue string
ElementIdx int
SubelementIdx int
}
// Maybe s is of the form t c u.
// If so, return t, c u (or t, u if cutc == true).
// If not, return s, "".
func split(s string, c string, cutc bool) (string, string) {
i := strings.Index(s, c)
if i < 0 {
return s, ""
}
if cutc {
return s[0:i], s[i+len(c):]
}
return s[0:i], s[i:]
}
// Parse parses an X12 Path string into component parts,
// The last part of the may may be a segment identifier
func ParseX12Path(rawpath string) (x12path *X12Path, err error) {
if rawpath == "" {
err = errors.New("empty x12path")
return nil, err
}
// get last part of path
// try to parse as a segment, rest is path
// if that fails, it is all the path with no segment
x12path = new(X12Path)
// set struct values...
basepath, refdes := path.Split(rawpath)
var found bool
var seg_id string
var id_val string
var ele_idx int
var subele_idx int
if found, seg_id, id_val, ele_idx, subele_idx, err = parseRefDes(refdes); err != nil {
// not a segment
fmt.Println(err.Error())
return nil, err
}
if !found {
x12path.Path = rawpath
return x12path, nil
}
if basepath != "" && basepath[len(basepath)-1] == '/' {
x12path.Path = basepath[:len(basepath)-1]
}
if seg_id == "" && id_val != "" {
err = fmt.Errorf("Path '%s' is invalid. Must specify a segment identifier with a qualifier", rawpath)
return nil, err
}
if seg_id == "" && (ele_idx != 0 || subele_idx != 0) && len(x12path.Path) > 0 {
err = fmt.Errorf("Path '%s' is invalid. Must specify a segment identifier", rawpath)
return nil, err
}
x12path.SegmentId = seg_id
x12path.IdValue = id_val
x12path.ElementIdx = ele_idx
x12path.SubelementIdx = subele_idx
return x12path, nil
}
func (x12path *X12Path) IsAbs() bool {
return path.IsAbs(x12path.Path)
}
func getSubeleIdx(refdes string) (rest string, idx int, err error) {
var part string
var num uint64
i := strings.LastIndex(refdes, "-")
if i != -1 {
rest, part = refdes[0:i], refdes[i+1:]
if num, err = strconv.ParseUint(part, 10, 8); err != nil {
return "", 0, errors.New("Subelement index must be in range [1,99]")
}
idx = int(num)
if 1 < idx || idx > 99 {
return "", 0, errors.New("Subelement index must be in range [1,99]")
}
return rest, idx, nil
}
return rest, 0, nil
}
//func getEleIdx(refdes string) (rest string, idx int, err error) {
// part string
// rest, part = refdes[0:i], refdes[i+1:]
// if idx, err = strconv.ParseUint(part, 10, 8); err != nil {
// return nil, nil, Error("Subelement index must be in range [1,99]")
// }
// x := int(idx)
// if 1 < x || x > 99 {
// return nil, nil, Error("Subelement index must be in range [1,99]")
// }
// return rest, x, nil
// return rest, nil, nil
//}
/*
func parseRefDes(refdes string) (seg_id, id_val string, ele_idx, subele_idx int, err error) {
// failure 1 - idx not int, depend not satisfied
// failure 2 - is not a refdes
var rest, myint string
if refdes == "" {
err = errors.New("empty refdes")
return
}
if rest, subele_idx, err = getSubeleIdx(refdes); err != nil {
goto Error2
}
*/
func parseRefDes(refdes string) (found bool, seg_id, id_val string, ele_idx, subele_idx int, err error) {
// failure 1 - idx not int, depend not satisfied
// failure 2 - is not a refdes
if refdes == "" {
found = false
//err = errors.New("empty refdes")
return
}
match := refdesRegexp.FindStringSubmatch(refdes)
if match == nil {
found = false
// no segment component
//err = errors.New("Not a refdes")
return
}
found = true
for i, name := range refdesRegexp.SubexpNames() {
// Ignore the whole regexp match and unnamed groups
if i == 0 || name == "" {
continue
}
switch name {
case "seg_id":
seg_id = match[i]
case "id_val":
id_val = match[i]
case "ele_idx":
v, _ := strconv.ParseInt(match[i], 10, 8)
ele_idx = int(v)
case "subele_idx":
v, _ := strconv.ParseInt(match[i], 10, 8)
subele_idx = int(v)
}
}
return found, seg_id, id_val, ele_idx, subele_idx, nil
}
// Is the path empty?
func (x12path *X12Path) Empty() bool {
return x12path.Path == "" && x12path.SegmentId == "" && x12path.ElementIdx == 0
}
/*
def _is_child_path(self, root_path, child_path):
"""
Is the child path really a child of the root path?
@type root_path: string
@type child_path: string
@return: True if a child
@rtype: boolean
"""
root = root_path.split('/')
child = child_path.split('/')
if len(root) >= len(child):
return False
for i in range(len(root)):
if root[i] != child[i]:
return False
return True
*/
// Assemble the segment parts of a X12Path into a string
func (p *X12Path) formatRefdes() string {
var parts []string
if p.SegmentId != "" {
parts = append(parts, p.SegmentId)
if p.IdValue != "" {
parts = append(parts, fmt.Sprintf("[%s]", p.IdValue))
}
}
if p.ElementIdx > 0 {
parts = append(parts, fmt.Sprintf("%02d", p.ElementIdx))
if p.SubelementIdx > 0 {
parts = append(parts, fmt.Sprintf("-%d", p.SubelementIdx))
}
}
return strings.Join(parts, "")
}
// String reassembles the X12Path into a valid X12Path string
// See pkg/net/url/url.go:String
func (p *X12Path) String() string {
var buf bytes.Buffer
if p.Path != "" {
buf.WriteString(p.Path)
}
rd := p.formatRefdes()
if p.Path != "" && rd != "" {
buf.WriteByte('/')
}
if rd != "" {
buf.WriteString(rd)
}
return buf.String()
}