virtbuf/configfile/main.go

78 lines
1.6 KiB
Go
Raw Permalink Normal View History

2024-10-18 20:58:15 -05:00
package main
import "log"
import "bytes"
import "os"
import "bufio"
import "io/ioutil"
import "google.golang.org/protobuf/proto"
import pb "go.wit.com/lib/protobuf/virtbuf"
2024-10-18 20:58:15 -05:00
//
// saves entries in a config file
//
func main() {
TestWriteEvent()
in, err := ioutil.ReadFile("/tmp/testing4.protobuf")
if err != nil {
log.Fatalln("Error reading file:", err)
}
allEvents := &pb.Droplet{}
2024-10-18 20:58:15 -05:00
if err := proto.Unmarshal(in, allEvents); err != nil {
log.Fatalln("Failed to parse events:", err)
}
// listPeople(os.Stdout, allEvents)
// got := in.String()
log.Println(in)
}
func marshalWriteToFile(myWriter *bufio.Writer, e *pb.Droplet) {
2024-10-18 20:58:15 -05:00
buf, err := proto.Marshal(e)
if err != nil {
log.Fatal("marshaling error: ", err)
}
tmp, err := myWriter.Write(buf)
myWriter.Flush()
log.Println("bufio.Write() tmp, err = ", tmp, err)
buf, err = proto.Marshal(e)
tmp2, err := myWriter.Write(buf)
myWriter.Flush()
log.Println("bufio.Write() tmp2, err = ", tmp2, err)
}
func TestWriteEvent() {
buf := new(bytes.Buffer)
e := pb.CreateSampleDroplet()
2024-10-18 20:58:15 -05:00
got := buf.String()
log.Println(got)
newfile, _ := os.Create("/tmp/testing4.protobuf")
myWriter := bufio.NewWriter(newfile)
marshalWriteToFile(myWriter, e)
marshalUnmarshal()
}
func marshalUnmarshal() {
test := pb.CreateSampleDroplet()
2024-10-18 20:58:15 -05:00
data, err := proto.Marshal(test)
if err != nil {
log.Fatal("marshaling error: ", err)
}
newTest := &pb.Droplet{}
2024-10-18 20:58:15 -05:00
err = proto.Unmarshal(data, newTest)
if err != nil {
log.Fatal("unmarshaling error: ", err)
} else {
log.Println("proto.Marshal() and proto.Unmarshal() worked")
}
}