forked from azoner/gox12
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_finder.go
113 lines (96 loc) · 2.9 KB
/
path_finder.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
package gox12
import ()
// Given the current location, find the path of the new segment
type X12PathFinder interface {
FindNext(x12Path string, segment Segment) (foundPath string, found bool, err error)
}
// Hardcoded lookups for standard X12 structure wrappers
type HeaderPathFinder struct {
hardMap map[string]string
}
func NewHeaderMapFinder() *HeaderPathFinder {
f := new(HeaderPathFinder)
f.hardMap = map[string]string{
"ISA": "/ISA_LOOP/ISA",
"IEA": "/ISA_LOOP/IEA",
"GS": "/ISA_LOOP/GS_LOOP/GS",
"GE": "/ISA_LOOP/GS_LOOP/GE",
"ST": "/ISA_LOOP/GS_LOOP/ST_LOOP/ST",
"SE": "/ISA_LOOP/GS_LOOP/ST_LOOP/SE",
}
return f
}
func (finder *HeaderPathFinder) FindNext(x12Path string, segment Segment) (foundPath string, found bool, err error) {
segId := segment.SegmentId
p, ok := finder.hardMap[segId]
if ok {
return p, ok, nil
}
return "", false, nil
}
type FirstMatchPathFinder struct {
Finders []X12PathFinder
}
func NewFirstMatchPathFinder(finder ...X12PathFinder) *FirstMatchPathFinder {
f := new(FirstMatchPathFinder)
f.Finders = make([]X12PathFinder, 0)
for _, fn := range finder {
f.Finders = append(f.Finders, fn)
}
return f
}
func (finder *FirstMatchPathFinder) FindNext(x12Path string, segment Segment) (foundPath string, found bool, err error) {
for _, f2 := range finder.Finders {
res, ok, err := f2.FindNext(x12Path, segment)
if ok {
return res, ok, err
}
}
return "", false, nil
}
// type PathFinder func(string, Segment) (string, bool, error)
type EmptyPath struct {
Path string
}
//func (e *EmptyPath) Run2 PathFinder {
// return "", true, nil
//}
// this is the method signature
// need to close lookup maps
func findPath(rawpath string, seg Segment) (foundPath string, ok bool, err error) {
return "", true, nil
}
// segMatcher is the function signature for segment matcher
// is the segment "matched"
type segMatcher func(seg Segment) bool
// segmentMatchBySegmentId matches a segment only by the segment ID
func segmentMatchBySegmentId(segmentId string) segMatcher {
return func(seg Segment) bool {
return seg.SegmentId == segmentId
}
}
// segmentMatchIdByPath matches a segment by the segment ID and the ID value of the
// element at the x12path
func segmentMatchIdByPath(segmentId string, x12path string, id_value string) segMatcher {
return func(seg Segment) bool {
v, found, _ := seg.GetValue(x12path)
return seg.SegmentId == segmentId && found && v == id_value
}
}
// segmentMatchIdByPath matches a segment by the segment ID and one of the ID value of the
// element at the x12path
func segmentMatchIdListByPath(segmentId string, x12path string, id_list []string) segMatcher {
return func(seg Segment) bool {
v, found, _ := seg.GetValue(x12path)
x := stringInSlice(v, id_list)
return seg.SegmentId == segmentId && found && x
}
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}