From bcc97a550a70305f9182b452ef650789c2fa2b45 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Tue, 22 Oct 2024 02:51:45 -0500 Subject: [PATCH] is this how to support multiple droplets? Signed-off-by: Jeff Carr --- Makefile | 6 ++++++ cluster.proto | 9 +++++++++ configfile/main.go | 15 +++++++++------ sampleData.go | 10 +++++----- 4 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 cluster.proto diff --git a/Makefile b/Makefile index 029c8d8..350fd4b 100644 --- a/Makefile +++ b/Makefile @@ -43,6 +43,12 @@ droplet.pb.go: droplet.proto --go_opt=Mdroplet.proto=go.wit.com/lib/protobuf/virtbuf \ droplet.proto +cluster.pb.go: cluster.proto + cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/lib/protobuf/virtbuf \ + --go_opt=Mdroplet.proto=go.wit.com/lib/protobuf/virtbuf \ + --go_opt=Mcluster.proto=go.wit.com/lib/protobuf/virtbuf \ + cluster.proto + events.pb.go: events.proto protoc --go_out=. events.proto diff --git a/cluster.proto b/cluster.proto new file mode 100644 index 0000000..d50af52 --- /dev/null +++ b/cluster.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; +package virtbuf; + +import "droplet.proto"; + +message Cluster { + int64 id = 1; + repeated Droplet droplets = 2; +} diff --git a/configfile/main.go b/configfile/main.go index cf4e545..727559d 100644 --- a/configfile/main.go +++ b/configfile/main.go @@ -21,15 +21,18 @@ func main() { log.Fatalln("Error reading file:", err) } - var aDroplet pb.Droplet - if err := proto.Unmarshal(in, &aDroplet); err != nil { + var aCluster pb.Cluster + if err := proto.Unmarshal(in, &aCluster); err != nil { log.Fatalln("Failed to parse droplet:", err) } - log.Println(aDroplet) + log.Println(aCluster) + for i, d := range aCluster.Droplets { + log.Println("\tdrop =", i, d) + } } -func marshalWriteToFile(myWriter *bufio.Writer, e *pb.Droplet) { +func marshalWriteToFile(myWriter *bufio.Writer, e *pb.Cluster) { buf, err := proto.Marshal(e) if err != nil { log.Fatal("marshaling error: ", err) @@ -47,7 +50,7 @@ func marshalWriteToFile(myWriter *bufio.Writer, e *pb.Droplet) { func TestWriteDroplet() { buf := new(bytes.Buffer) - e := pb.CreateSampleDroplet() + e := pb.CreateSampleCluster(10) got := buf.String() log.Println(got) @@ -60,7 +63,7 @@ func TestWriteDroplet() { } func marshalUnmarshal() { - test := pb.CreateSampleDroplet() + test := pb.CreateSampleCluster(10) data, err := proto.Marshal(test) if err != nil { log.Fatal("marshaling error: ", err) diff --git a/sampleData.go b/sampleData.go index b75970b..c910200 100644 --- a/sampleData.go +++ b/sampleData.go @@ -18,9 +18,9 @@ func CreateSampleDroplet() *Droplet { return e } -func CreateSampleDroplets(total int) []Droplet { - - var all []Droplet +func CreateSampleCluster(total int) *Cluster { + var c *Cluster + c = new(Cluster) for i := 0; i < total; i++ { d := CreateSampleDroplet() @@ -28,8 +28,8 @@ func CreateSampleDroplets(total int) []Droplet { // e.Id += 1000 + int32(i) d.Comment = "Sample Droplet " + string(i) - all = append(all, *d) + c.Droplets = append(c.Droplets, d) } - return all + return c }