-
Notifications
You must be signed in to change notification settings - Fork 928
/
generatemappings.go
77 lines (59 loc) · 1.2 KB
/
generatemappings.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
package main
import (
"encoding/csv"
"io"
"log"
"os"
)
const countrycsv = "generate/country.csv"
const zonecsv = "generate/zone.csv"
func main() {
fzone, err := os.Open(zonecsv)
if err != nil {
log.Fatal(err)
}
defer fzone.Close()
// fcountries, err := os.Open(countrycsv)
// if err != nil {
// log.Fatal(err)
// }
// defer fcountries.Close()
zones := ReadZones(fzone)
zoneW, err := os.Create("cc_to_zones.go")
if err != nil {
log.Fatal(err)
}
defer zoneW.Close()
zoneW.WriteString(`package timezonecompanion
var CCToZones = map[string][]string{
`)
for cc, z := range zones {
zoneW.WriteString(`"` + cc + `": []string{`)
for i, v := range z {
if i != 0 {
zoneW.WriteString(`, `)
}
zoneW.WriteString(`"` + v + `"`)
}
zoneW.WriteString(`},
`)
}
zoneW.WriteString("}")
}
func ReadZones(f *os.File) map[string][]string {
result := make(map[string][]string)
r := csv.NewReader(f)
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
cc := record[1]
zone := record[2]
result[cc] = append(result[cc], zone)
}
return result
}