-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv.go
152 lines (131 loc) · 3.33 KB
/
csv.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
// package fcc
// Copyright (C) 2019 Lars Lehtonen KJ6CBE
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package fcc
import (
"strconv"
"strings"
"time"
)
const timeFormat = "01/02/2006 15:04:05"
const sep = "\u001e"
// ReadRecord reads a database record from boltdb and returns
// a MinimalLicense.
func ReadRecord(record []byte) MinimalLicense {
var ml MinimalLicense
split := strings.Split(string(record), sep)
ml.Name = split[0]
ml.Address = split[1]
ml.City = split[2]
ml.State = split[3]
ml.ZIP = split[4]
return ml
}
// Minimal returns a MinimalLicense.
func (l *License) Minimal() MinimalLicense {
return MinimalLicense{
Name: l.LicName,
Address: l.LicAddress,
City: l.LicCity,
State: l.LicState,
ZIP: l.LicZipCode,
}
}
// DiskFormat converts a MinimalLicense to its on-disk format.
func (m *MinimalLicense) DiskFormat() []byte {
join := strings.Join([]string{
m.Name,
m.Address,
m.City,
m.State,
m.ZIP}, sep)
return []byte(join)
}
// ParseLicense takes a license CSV parsed into a string and generates
// an *fcc.License{}.
func ParseLicense(line []string) (*License, error) {
var err error
var gd, ed, cd, lad time.Time
lic := License{}
lid, err := strconv.ParseUint(line[0], 10, 32)
if err != nil {
return &lic, err
}
lic.LicenseID = uint32(lid)
lic.SourceSystem = line[1]
lic.Callsign = line[2]
if line[3] != "" {
fid, err := strconv.ParseUint(line[3], 10, 32)
if err != nil {
return &lic, err
}
fid32 := uint32(fid)
lic.FacilityID = &fid32
}
if line[4] != "" {
frn, err := strconv.ParseUint(line[4], 10, 64)
if err != nil {
return &lic, err
}
lic.FRN = &frn
}
lic.LicName = line[5]
lic.CommonName = line[6]
lic.RadioServiceCode = line[7]
lic.RadioServiceDesc = line[8]
lic.RollupCategoryCode = line[9]
lic.RollupCategoryDesc = line[10]
if line[11] != "" {
gd, err = parseTime(line[11])
if err != nil {
return &lic, err
}
lic.GrantDate = &gd
}
if line[12] != "" {
ed, err = parseTime(line[12])
if err != nil {
return &lic, err
}
lic.ExpiredDate = &ed
}
if line[13] != "" {
cd, err = parseTime(line[13])
if err != nil {
return &lic, err
}
lic.CancellationDate = &cd
}
if line[14] != "" {
lad, err = parseTime(line[14])
if err != nil {
return &lic, err
}
lic.LastActionDate = &lad
}
lic.LicStatusCode = line[15]
lic.LicStatusDesc = line[16]
lic.RollupStatusCode = line[17]
lic.RollupStatusDesc = line[18]
lic.EntityTypeCode = line[19]
lic.EntityTypeDesc = line[20]
lic.RollupEntityCode = line[21]
lic.RollupEntityDesc = line[22]
lic.LicAddress = line[23]
lic.LicCity = line[24]
lic.LicState = line[25]
lic.LicZipCode = line[26]
return &lic, nil
}
func parseTime(ts string) (time.Time, error) {
return time.Parse(timeFormat, ts)
}