-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
123 lines (111 loc) · 2.51 KB
/
main.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
package main
import (
"encoding/csv"
"fmt"
"github.com/erik-olsson-op/go-protobuf/model"
"google.golang.org/protobuf/proto"
"math/rand"
"os"
"strconv"
"time"
)
func main() {
fmt.Println("get authors...")
authors := getAuthors()
fmt.Println("get books...")
books := getBooks(authors)
fmt.Println("writes proto messages to disk...")
err := writeProtoToDisk(books)
if err != nil {
panic(err)
}
fmt.Println("read proto messages from disk...")
for i := 1; i <= 10; i++ {
var data, _ = os.ReadFile(fmt.Sprintf("data/book-%d.binpb", i))
b := model.Book{}
err := proto.Unmarshal(data, &b)
if err != nil {
panic(err)
}
fmt.Printf("Id: %d - Title: %v - Author: %v - Category: %v\n", b.Id, b.Title, b.Author, b.Category)
time.Sleep(time.Second)
}
}
func readCsvFile(csvFilePath string) [][]string {
fd, err := os.Open(csvFilePath)
if err != nil {
panic(err)
}
defer fd.Close()
fileReader := csv.NewReader(fd)
records, err := fileReader.ReadAll()
if err != nil {
panic(err)
}
return records
}
// getAuthors - read csv file and creates author protobuf message
func getAuthors() []model.Author {
authorCsvRows := readCsvFile("data/author.csv")
var authors = make([]model.Author, 0)
for _, rows := range authorCsvRows {
id, _ := strconv.ParseInt(rows[0], 10, 64)
name := rows[1]
var author = model.Author{
Id: id,
Name: name,
}
authors = append(authors, author)
}
return authors
}
// getBooks - read csv file and creates book protobuf message
func getBooks(authors []model.Author) []model.Book {
bookCsvRows := readCsvFile("data/book.csv")
var books = make([]model.Book, 0)
for _, rows := range bookCsvRows {
id, _ := strconv.ParseInt(rows[0], 10, 64)
title := rows[1]
a := authors[rand.Intn(5)]
c := getRandomCategory(rand.Intn(3))
var book = model.Book{
Id: id,
Title: title,
Author: []*model.Author{
{
Id: a.GetId(),
Name: a.GetName(),
},
},
Category: c,
}
books = append(books, book)
}
return books
}
func getRandomCategory(rn int) model.Category {
switch rn {
case 1:
return model.Category_Novel
case 2:
return model.Category_SciFi
case 3:
return model.Category_Fantasy
default:
return model.Category_Novel
}
}
func writeProtoToDisk(books []model.Book) error {
for _, book := range books {
data, err := proto.Marshal(&book)
if err != nil {
return err
}
err = os.WriteFile(fmt.Sprintf("data/book-%d.binpb", book.GetId()), data, 0777)
if err != nil {
return err
}
}
fmt.Println("done!")
return nil
}