/
partmaster.go
60 lines (50 loc) · 1.46 KB
/
partmaster.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
package main
import (
"fmt"
"sort"
)
type partmasterLine struct {
IPN ipn `csv:"IPN"`
Description string `csv:"Description"`
Footprint string `csv:"Footprint"`
Value string `csv:"Value"`
Manufacturer string `csv:"Manufacturer"`
MPN string `csv:"MPN"`
Datasheet string `csv:"Datasheet"`
Priority int `csv:"Priority"`
Checked string `csv:"Checked"`
}
type partmaster []*partmasterLine
// findPart returns part with highest priority
func (p *partmaster) findPart(pn ipn) (*partmasterLine, error) {
found := []*partmasterLine{}
for _, l := range *p {
if l.IPN == pn {
found = append(found, l)
}
}
if len(found) <= 0 {
return nil, fmt.Errorf("Part not found")
}
sort.Sort(byPriority(found))
if len(found) > 1 {
// fill in blank fields with values from other items
for i := 1; i < len(found); i++ {
if found[0].Description == "" && found[i].Description != "" {
found[0].Description = found[i].Description
}
if found[0].Footprint == "" && found[i].Footprint != "" {
found[0].Footprint = found[i].Footprint
}
if found[0].Value == "" && found[i].Value != "" {
found[0].Value = found[i].Value
}
}
}
return found[0], nil
}
// type for sorting
type byPriority []*partmasterLine
func (p byPriority) Len() int { return len(p) }
func (p byPriority) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p byPriority) Less(i, j int) bool { return p[i].Priority < p[j].Priority }