is this how to support multiple droplets?

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-22 02:51:45 -05:00
parent ec178de1d7
commit bcc97a550a
4 changed files with 29 additions and 11 deletions

View File

@ -43,6 +43,12 @@ droplet.pb.go: droplet.proto
--go_opt=Mdroplet.proto=go.wit.com/lib/protobuf/virtbuf \ --go_opt=Mdroplet.proto=go.wit.com/lib/protobuf/virtbuf \
droplet.proto 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 events.pb.go: events.proto
protoc --go_out=. events.proto protoc --go_out=. events.proto

9
cluster.proto Normal file
View File

@ -0,0 +1,9 @@
syntax = "proto3";
package virtbuf;
import "droplet.proto";
message Cluster {
int64 id = 1;
repeated Droplet droplets = 2;
}

View File

@ -21,15 +21,18 @@ func main() {
log.Fatalln("Error reading file:", err) log.Fatalln("Error reading file:", err)
} }
var aDroplet pb.Droplet var aCluster pb.Cluster
if err := proto.Unmarshal(in, &aDroplet); err != nil { if err := proto.Unmarshal(in, &aCluster); err != nil {
log.Fatalln("Failed to parse droplet:", err) 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) buf, err := proto.Marshal(e)
if err != nil { if err != nil {
log.Fatal("marshaling error: ", err) log.Fatal("marshaling error: ", err)
@ -47,7 +50,7 @@ func marshalWriteToFile(myWriter *bufio.Writer, e *pb.Droplet) {
func TestWriteDroplet() { func TestWriteDroplet() {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
e := pb.CreateSampleDroplet() e := pb.CreateSampleCluster(10)
got := buf.String() got := buf.String()
log.Println(got) log.Println(got)
@ -60,7 +63,7 @@ func TestWriteDroplet() {
} }
func marshalUnmarshal() { func marshalUnmarshal() {
test := pb.CreateSampleDroplet() test := pb.CreateSampleCluster(10)
data, err := proto.Marshal(test) data, err := proto.Marshal(test)
if err != nil { if err != nil {
log.Fatal("marshaling error: ", err) log.Fatal("marshaling error: ", err)

View File

@ -18,9 +18,9 @@ func CreateSampleDroplet() *Droplet {
return e return e
} }
func CreateSampleDroplets(total int) []Droplet { func CreateSampleCluster(total int) *Cluster {
var c *Cluster
var all []Droplet c = new(Cluster)
for i := 0; i < total; i++ { for i := 0; i < total; i++ {
d := CreateSampleDroplet() d := CreateSampleDroplet()
@ -28,8 +28,8 @@ func CreateSampleDroplets(total int) []Droplet {
// e.Id += 1000 + int32(i) // e.Id += 1000 + int32(i)
d.Comment = "Sample Droplet " + string(i) d.Comment = "Sample Droplet " + string(i)
all = append(all, *d) c.Droplets = append(c.Droplets, d)
} }
return all return c
} }