-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
134 lines (115 loc) · 2.55 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
package main
import (
"fmt"
"io/ioutil"
"log"
"github.com/Prithvipal/proto-buffer/src/complex/complexpb"
"github.com/Prithvipal/proto-buffer/src/enumexample/enumexamplepb"
"github.com/Prithvipal/proto-buffer/src/simple/simplepb"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
func main() {
// sm := dosimlpe()
// readAndWriteDemo(sm)
// jsonDemo(sm)
// doenum()
doComplex()
}
func doComplex() {
cm := complexpb.ComplexMessage{
OneDummy: &complexpb.DummyMessage{
Id: 1,
Name: "First Dummy",
},
MultipleDummy: []*complexpb.DummyMessage{
{
Id: 2,
Name: "Second Dummy",
},
{
Id: 3,
Name: "Third Dummy",
},
},
}
fmt.Println(cm)
}
func doenum() {
em := enumexamplepb.EnumMessage{
Id: 42,
DayOfTheWeek: enumexamplepb.DayOfTheWeek_THURSDAY,
}
em.DayOfTheWeek = enumexamplepb.DayOfTheWeek_MONDAY
fmt.Println(em)
}
func jsonDemo(pb proto.Message) {
res := toJSON(pb)
fmt.Println("========")
fmt.Println(res)
var sm simplepb.SimpleMessage
fromJSON(res, &sm)
fmt.Println("*********")
fmt.Printf("%+v", sm)
}
func toJSON(pb proto.Message) string {
jStr, err := protojson.Marshal(pb)
if err != nil {
log.Fatalln("can't marshall to json", err)
return ""
}
return string(jStr)
}
func fromJSON(in string, pb proto.Message) proto.Message {
err := protojson.Unmarshal([]byte(in), pb)
if err != nil {
log.Fatal("can't unmarshall from json", err)
return nil
}
return pb
}
func readAndWriteDemo(sm proto.Message) {
writeToFile("simple.bin", sm)
sm2 := simplepb.SimpleMessage{}
readFromFile("simple.bin", &sm2)
fmt.Println("READ OPEARATION SUCCESSFUL")
fmt.Printf("%+v", sm2)
}
func writeToFile(fname string, pb proto.Message) error {
out, err := proto.Marshal(pb)
if err != nil {
log.Fatal("Can't serialize")
return err
}
if err := ioutil.WriteFile(fname, out, 0644); err != nil {
log.Fatal("Can't write to file")
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.Fatal("Someting went wrong while reading file")
return err
}
err = proto.Unmarshal(in, pb)
if err != nil {
log.Fatal("Can't unmarshal data")
return err
}
return nil
}
func dosimlpe() *simplepb.SimpleMessage {
sm := simplepb.SimpleMessage{
Id: 12345,
IsSimple: true,
Name: "My Simple Message",
SimpleList: []int32{1, 4, 7, 9},
}
fmt.Printf("%+v\n", sm)
sm.Name = "I renamed you"
fmt.Printf("%+v\n", sm)
return &sm
}