-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
bom.go
139 lines (120 loc) · 3.05 KB
/
bom.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
package main
import (
"fmt"
"log"
"strings"
)
type bomLine struct {
IPN ipn `csv:"IPN" yaml:"ipn"`
Qnty int `csv:"Qnty" yaml:"qnty"`
MPN string `csv:"MPN" yaml:"mpn"`
Manufacturer string `csv:"Manufacturer" yaml:"manufacturer"`
Ref string `csv:"Ref" yaml:"ref"`
Value string `csv:"Value" yaml:"value"`
CmpName string `csv:"Cmp name" yaml:"cmpName"`
Footprint string `csv:"Footprint" yaml:"footprint"`
Description string `csv:"Description" yaml:"description"`
Vendor string `csv:"Vendor" yaml:"vendor"`
Datasheet string `csv:"Datasheet" yaml:"datasheet"`
Checked string `csv:"Checked" yaml:"checked"`
}
func (bl *bomLine) String() string {
return fmt.Sprintf("%v;%v;%v;%v;%v;%v;%v;%v;%v;%v;%v;%v",
bl.Ref,
bl.Qnty,
bl.Value,
bl.CmpName,
bl.Footprint,
bl.Description,
bl.Vendor,
bl.IPN,
bl.Datasheet,
bl.Manufacturer,
bl.MPN,
bl.Checked)
}
func (bl *bomLine) removeRef(ref string) {
refs := strings.Split(bl.Ref, ",")
refsOut := []string{}
for _, r := range refs {
r = strings.Trim(r, " ")
if r != ref && r != "" {
refsOut = append(refsOut, r)
}
}
bl.Ref = strings.Join(refsOut, ", ")
bl.Qnty = len(refsOut)
}
type bom []*bomLine
func (b bom) String() string {
ret := "\n"
for _, l := range b {
ret += fmt.Sprintf("%v\n", l)
}
return ret
}
// merge can be used to merge partmaster attributes into a BOM
func (b *bom) mergePartmaster(p partmaster, logErr func(string)) {
// populate MPN info in our BOM
for i, l := range *b {
pmPart, err := p.findPart(l.IPN)
if err != nil {
logErr(fmt.Sprintf("Error finding part (%v:%v) on bom line #%v in pm: %v\n", l.CmpName, l.IPN, i+2, err))
continue
}
l.Manufacturer = pmPart.Manufacturer
l.MPN = pmPart.MPN
l.Datasheet = pmPart.Datasheet
l.Checked = pmPart.Checked
l.Description = pmPart.Description
}
}
func (b *bom) copy() bom {
ret := make([]*bomLine, len(*b))
for i, l := range *b {
ret[i] = &(*l)
}
return ret
}
func (b *bom) processOurIPN(pn ipn, qty int) error {
log.Println("processing our IPN: ", pn, qty)
// check if BOM exists
bomPath, err := findFile(pn.String() + ".csv")
if err != nil {
return fmt.Errorf("Error finding sub assy BOM: %v", err)
}
subBom := bom{}
err = loadCSV(bomPath, &subBom)
if err != nil {
return fmt.Errorf("Error parsing CSV for %v: %v", pn, err)
}
for _, l := range subBom {
isSub, _ := l.IPN.hasBOM()
if isSub {
err := b.processOurIPN(l.IPN, l.Qnty*qty)
if err != nil {
return fmt.Errorf("Error processing sub %v: %v", l.IPN, err)
}
}
n := *l
n.Qnty *= qty
b.addItem(&n)
}
return nil
}
func (b *bom) addItem(newItem *bomLine) {
for i, l := range *b {
if newItem.IPN == l.IPN {
(*b)[i].Qnty += newItem.Qnty
return
}
}
n := *newItem
// clear refs
n.Ref = ""
*b = append(*b, &n)
}
// sort methods
func (b bom) Len() int { return len(b) }
func (b bom) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b bom) Less(i, j int) bool { return strings.Compare(string(b[i].IPN), string(b[j].IPN)) < 0 }