-
Notifications
You must be signed in to change notification settings - Fork 39
/
encode_pb.go
79 lines (71 loc) · 1.51 KB
/
encode_pb.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
package main
import (
"encoding/csv"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strconv"
"github.com/golang/protobuf/proto"
"github.com/vladimirvivien/go-grpc/proto-encoding/curproto"
)
const fileName = "data.pb"
func main() {
currencyItems, err := createPbFromCsv("../curdata.csv")
if err != nil {
log.Fatalf("failed to load csv: %v\n", err)
}
// print on screen
for i, item := range currencyItems.Items {
fmt.Printf("%-25s%-20s\n", item.Name, item.Code)
if i > 10 {
fmt.Println("...")
break
}
}
// encode as protobuf data
data, err := proto.Marshal(currencyItems)
if err != nil {
log.Fatal(err)
}
// save to file
if err := ioutil.WriteFile(fileName, data, 0644); err != nil {
log.Fatal(err)
}
log.Println("data file saved as", fileName)
}
// createPbFromCsv loads the currency data from csv into protobuf values
func createPbFromCsv(path string) (*curproto.CurrencyList, error) {
items := make([]*curproto.Currency, 0)
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
// create CSV reader from file
reader := csv.NewReader(file)
for {
row, err := reader.Read()
if err != nil {
if err == io.EOF {
break
} else {
return nil, err
}
}
var num int32
if i, err := strconv.Atoi(row[3]); err == nil {
num = int32(i)
}
// create data row with protobuf
c := &curproto.Currency{
Country: row[0],
Name: row[1],
Code: row[2],
Number: num,
}
items = append(items, c)
}
return &curproto.CurrencyList{Items: items}, err
}