aboutsummaryrefslogtreecommitdiffstats
path: root/formats.go
blob: 8401bc610bed90db307dd1db7b1a2af5ed598ca4 (plain)
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
package main

// Bom/BomMeta conversion/dump/load routines

import (
	"encoding/csv"
	"encoding/json"
	"encoding/xml"
	"fmt"
	"io"
	"log"
	"strconv"
	"strings"
	"text/tabwriter"
)

// This compound container struct is useful for serializing to XML and JSON
type BomContainer struct {
	BomMetadata *BomMeta `json:"metadata"`
	Bom         *Bom     `json:"bom"`
}

// --------------------- text (CLI only ) -----------------------

func DumpBomAsText(bm *BomMeta, b *Bom, out io.Writer) {
	fmt.Fprintln(out)
	fmt.Fprintf(out, "Name:\t\t%s\n", bm.Name)
	fmt.Fprintf(out, "Version:\t%s\n", b.Version)
	fmt.Fprintf(out, "Creator:\t%s\n", bm.Owner)
	fmt.Fprintf(out, "Timestamp:\t%s\n", b.Created)
	if bm.Homepage != "" {
		fmt.Fprintf(out, "Homepage:\t%s\n", bm.Homepage)
	}
	if b.Progeny != "" {
		fmt.Fprintf(out, "Source:\t\t%s\n", b.Progeny)
	}
	if bm.Description != "" {
		fmt.Fprintf(out, "Description:\t%s\n", bm.Description)
	}
	fmt.Println()
	tabWriter := tabwriter.NewWriter(out, 2, 4, 1, ' ', 0)
	// "by line item", not "by element"
	fmt.Fprintf(tabWriter, "qty\ttag\tmanufacturer\tmpn\t\tfunction\t\tcomment\n")
	for _, li := range b.LineItems {
		fmt.Fprintf(tabWriter, "%d\t%s\t%s\t%s\t\t%s\t\t%s\n",
			len(li.Elements),
			li.Tag,
			li.Manufacturer,
			li.Mpn,
			li.Function,
			li.Comment)
	}
	tabWriter.Flush()
}

// --------------------- csv -----------------------

func DumpBomAsCSV(b *Bom, out io.Writer) {
	dumper := csv.NewWriter(out)
	defer dumper.Flush()
	// "by line item"
	dumper.Write([]string{"qty",
		"elements",
		"manufacturer",
		"mpn",
		"function",
		"form_factor",
		"specs",
		"category",
		"tag",
		"comment"})
	for _, li := range b.LineItems {
		dumper.Write([]string{
			fmt.Sprint(len(li.Elements)),
			strings.Join(li.Elements, ","),
			li.Manufacturer,
			li.Mpn,
			li.Function,
			li.FormFactor,
			li.Specs,
			li.Category,
			li.Tag,
			li.Comment})
	}
}

func appendField(existing, next *string) {
	if *existing == "" {
		*existing += " " + strings.TrimSpace(*next)
	}
	*existing = strings.TrimSpace(*next)
}

func LoadBomFromCSV(input io.Reader) (*Bom, error) {
	b := Bom{LineItems: []LineItem{}}
	reader := csv.NewReader(input)
	reader.TrailingComma = true
	reader.TrimLeadingSpace = true

	header, err := reader.Read()
	if err != nil {
		log.Fatal(err)
	}
	var li *LineItem
	var el_count int
	var records []string
	var qty string
	for records, err = reader.Read(); err == nil; records, err = reader.Read() {
		qty = ""
		li = &LineItem{Elements: []string{}}
		for i, col := range header {
			switch strings.ToLower(col) {
			case "qty", "quantity":
				// if a quantity is specified, use it; else interpret it
				// from element id count
				appendField(&qty, &records[i])
			case "mpn", "manufacturer part number":
				appendField(&li.Mpn, &records[i])
			case "mfg", "manufacturer":
				appendField(&li.Manufacturer, &records[i])
			case "element", "id", "circuit element", "symbol_id", "symbol id":
				for _, symb := range strings.Split(records[i], ",") {
					symb = strings.TrimSpace(symb)
					if !isShortName(symb) {
						li.Elements = append(li.Elements, symb)
					} else if *verbose {
						log.Println("symbol not a ShortName, skipped: " + symb)
					}
				}
			case "function", "purpose", "role", "subsystem":
				appendField(&li.Function, &records[i])
			case "formfactor", "form_factor", "form factor", "case/package", "package", "symbol", "footprint":
				appendField(&li.FormFactor, &records[i])
			case "specs", "specifications", "properties", "attributes", "value", "type":
				appendField(&li.Specs, &records[i])
			case "comment", "comments", "note", "notes":
				appendField(&li.Comment, &records[i])
			case "category":
				appendField(&li.Category, &records[i])
			case "tag":
				appendField(&li.Tag, &records[i])
			default:
				// pass, no assignment
				// TODO: should warn on this first time around?
			}
		}
		if qty != "" {
			if n, err := strconv.Atoi(qty); err == nil && n >= 0 {
				el_count = len(li.Elements)
				// XXX: kludge
				if n > 99999 || el_count > 99999 {
					log.Fatal("too large a quantity of elements passed, crashing")
				} else if el_count > n {
					if *verbose {
						log.Println("more symbols than qty, taking all symbols")
					}
				} else if el_count < n {
					for j := 0; j < (n - el_count); j++ {
						li.Elements = append(li.Elements, "")
					}
				}
			}
		}
		if len(li.Elements) == 0 {
			li.Elements = []string{""}
		}
		b.LineItems = append(b.LineItems, *li)
	}
	if err.Error() != "EOF" {
		log.Fatal(err)
	}
	return &b, nil
}

// --------------------- JSON -----------------------

func DumpBomAsJSON(bm *BomMeta, b *Bom, out io.Writer) {

	container := &BomContainer{BomMetadata: bm, Bom: b}

	enc := json.NewEncoder(out)
	if err := enc.Encode(&container); err != nil {
		log.Fatal(err)
	}
}

func LoadBomFromJSON(input io.Reader) (*BomMeta, *Bom, error) {

	container := &BomContainer{}

	enc := json.NewDecoder(input)
	if err := enc.Decode(&container); err != nil {
		log.Fatal(err)
	}
	return container.BomMetadata, container.Bom, nil
}

// --------------------- XML -----------------------

func DumpBomAsXML(bm *BomMeta, b *Bom, out io.Writer) {

	container := &BomContainer{BomMetadata: bm, Bom: b}
	enc := xml.NewEncoder(out)

	// generic XML header
	io.WriteString(out, xml.Header)

	if err := enc.Encode(container); err != nil {
		log.Fatal(err)
	}
}

func LoadBomFromXML(input io.Reader) (*BomMeta, *Bom, error) {

	container := &BomContainer{}
	enc := xml.NewDecoder(input)
	if err := enc.Decode(&container); err != nil {
		log.Fatal(err)
	}
	return container.BomMetadata, container.Bom, nil
}