-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmain.go
139 lines (112 loc) · 2.64 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"fmt"
"io/ioutil"
"log"
"github.com/simplesteph/protobuf-example-go/src/complex"
"github.com/simplesteph/protobuf-example-go/src/enum_example"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/simplesteph/protobuf-example-go/src/simple"
)
func main() {
sm := doSimple()
readAndWriteDemo(sm)
jsonDemo(sm)
doEnum()
doComplex()
}
func doComplex() {
cm := complexpb.ComplexMessage{
OneDummy: &complexpb.DummyMessage{
Id: 1,
Name: "First message",
},
MultipleDummy: []*complexpb.DummyMessage{
&complexpb.DummyMessage{
Id: 2,
Name: "Second message",
},
&complexpb.DummyMessage{
Id: 3,
Name: "Third message",
},
},
}
fmt.Println(cm)
}
func doEnum() {
em := enumpb.EnumMessage{
Id: 42,
DayOfTheWeek: enumpb.DayOfTheWeek_THURSDAY,
}
em.DayOfTheWeek = enumpb.DayOfTheWeek_MONDAY
fmt.Println(em)
}
func jsonDemo(sm proto.Message) {
smAsString := toJSON(sm)
fmt.Println(smAsString)
sm2 := &simplepb.SimpleMessage{}
fromJSON(smAsString, sm2)
fmt.Println("Successfully created proto struct:", sm2)
}
func toJSON(pb proto.Message) string {
marshaler := jsonpb.Marshaler{}
out, err := marshaler.MarshalToString(pb)
if err != nil {
log.Fatalln("Can't convert to JSON", err)
return ""
}
return out
}
func fromJSON(in string, pb proto.Message) {
err := jsonpb.UnmarshalString(in, pb)
if err != nil {
log.Fatalln("Couldn't unmarshal the JSON into the pb struct", err)
}
}
func readAndWriteDemo(sm proto.Message) {
writeToFile("simple.bin", sm)
sm2 := &simplepb.SimpleMessage{}
readFromFile("simple.bin", sm2)
fmt.Println("Read the content:", sm2)
}
func writeToFile(fname string, pb proto.Message) error {
out, err := proto.Marshal(pb)
if err != nil {
log.Fatalln("Can't serialise to bytes", err)
return err
}
if err := ioutil.WriteFile(fname, out, 0644); err != nil {
log.Fatalln("Can't write to file", err)
return err
}
fmt.Println("Data has been written!")
return nil
}
func readFromFile(fname string, pb proto.Message) error {
in, err := ioutil.ReadFile(fname)
if err != nil {
log.Fatalln("Something went wrong when reading the file", err)
return err
}
err2 := proto.Unmarshal(in, pb)
if err2 != nil {
log.Fatalln("Couldn't put the bytes into the protocol buffers struct", err2)
return err2
}
return nil
}
func doSimple() *simplepb.SimpleMessage {
sm := simplepb.SimpleMessage{
Id: 12345,
IsSimple: true,
Name: "My Simple Message",
SampleList: []int32{1, 4, 7, 8},
}
fmt.Println(sm)
sm.Name = "I renamed you"
fmt.Println(sm)
fmt.Println("The ID is:", sm.GetId())
return &sm
}