From 4cc41bcec604ba663168aea8b8009b5ee3ef8624 Mon Sep 17 00:00:00 2001 From: matst80 Date: Fri, 8 Nov 2024 08:37:12 +0100 Subject: [PATCH] protobuf --- cart-grain.go | 131 ++++++++--- data/1.prot | Bin 0 -> 383 bytes data/state.json | 1 + data/state.json.bak | 1 + disk-storage.go | 31 ++- git.tornberg.me/go-cart-actor/add.pb.go | 212 ++++++++++++++++++ .../go-cart-actor/messages/add.pb.go | 212 ++++++++++++++++++ main.go | 6 +- messages/add.proto | 18 ++ 9 files changed, 568 insertions(+), 44 deletions(-) create mode 100644 data/1.prot create mode 100644 data/state.json create mode 100644 data/state.json.bak create mode 100644 git.tornberg.me/go-cart-actor/add.pb.go create mode 100644 git.tornberg.me/go-cart-actor/messages/add.pb.go create mode 100644 messages/add.proto diff --git a/cart-grain.go b/cart-grain.go index 8dee653..0a218ae 100644 --- a/cart-grain.go +++ b/cart-grain.go @@ -1,25 +1,102 @@ package main import ( + "bufio" + "bytes" + "encoding/binary" "encoding/json" "fmt" + "io" "log" "net/http" "time" + "git.tornberg.me/go-cart-actor/git.tornberg.me/go-cart-actor/messages" "github.com/matst80/slask-finder/pkg/index" + "google.golang.org/protobuf/proto" ) +type StorableMessage interface { + GetBytes() ([]byte, error) + FromReader(io.Reader, *Message) error +} + type Message struct { + Type uint64 TimeStamp *int64 - Type string Content interface{} } +func (m Message) GetBytes() ([]byte, error) { + var b bytes.Buffer + var err error + w := bufio.NewWriter(&b) + bytes := make([]byte, 8) + binary.LittleEndian.PutUint64(bytes, m.Type) + w.Write(bytes) + binary.LittleEndian.PutUint64(bytes, uint64(*m.TimeStamp)) + w.Write(bytes) + var messageBytes []byte + if m.Type == 1 { + messageBytes, err = proto.Marshal(m.Content.(*messages.AddRequest)) + } else if m.Type == 2 { + messageBytes, err = proto.Marshal(m.Content.(*messages.AddItem)) + } else { + return nil, fmt.Errorf("unknown message type") + } + if err != nil { + return nil, err + } + binary.LittleEndian.PutUint64(bytes, uint64(len(messageBytes))) + w.Write(bytes) + w.Write(messageBytes) + w.Flush() + return b.Bytes(), nil +} + +func (i Message) FromReader(reader io.Reader, m *Message) error { + + bytes := make([]byte, 8) + if _, err := reader.Read(bytes); err != nil { + return err + } + m.Type = binary.LittleEndian.Uint64(bytes) + if _, err := reader.Read(bytes); err != nil { + return err + } + timestamp := int64(binary.LittleEndian.Uint64(bytes)) + m.TimeStamp = ×tamp + if _, err := reader.Read(bytes); err != nil { + return err + } + messageBytes := make([]byte, binary.LittleEndian.Uint64(bytes)) + if _, err := reader.Read(messageBytes); err != nil { + return err + } + var err error + + if m.Type == 1 { + msg := &messages.AddRequest{} + err = proto.Unmarshal(messageBytes, msg) + m.Content = msg + } else if m.Type == 2 { + msg := &messages.AddItem{} + err = proto.Unmarshal(messageBytes, msg) + m.Content = msg + } else { + return fmt.Errorf("unknown message type") + } + if err != nil { + return err + } + + return nil +} + type CartItem struct { Sku string `json:"sku"` Name string `json:"name"` - Price int `json:"price"` + Price int64 `json:"price"` Image string `json:"image"` } @@ -27,14 +104,14 @@ type CartGrain struct { storageMessages []Message Id string `json:"id"` Items []CartItem `json:"items"` - TotalPrice int `json:"totalPrice"` + TotalPrice int64 `json:"totalPrice"` } type Grain interface { GetId() string GetLastChange() int64 HandleMessage(message *Message, isReplay bool, reply *CartGrain) error - GetStorageMessage(since int64) []Message + GetStorageMessage(since int64) []StorableMessage } func (c *CartGrain) GetId() string { @@ -48,7 +125,7 @@ func (c *CartGrain) GetLastChange() int64 { return *c.storageMessages[len(c.storageMessages)-1].TimeStamp } -func getItemData(sku string) (*CartItem, error) { +func getItemData(sku string) (*messages.AddItem, error) { res, err := http.Get("https://slask-finder.tornberg.me/api/get/" + sku) if err != nil { return nil, err @@ -75,11 +152,12 @@ func getItemData(sku string) (*CartItem, error) { } } } - return &CartItem{ - Sku: item.Sku, - Name: item.Title, - Price: price, - Image: item.Img, + return &messages.AddItem{ + Quantity: 1, + Price: int64(price), + Sku: sku, + Name: item.Title, + Image: item.Img, }, nil } @@ -89,16 +167,14 @@ func (c *CartGrain) AddItem(sku string, reply *CartGrain) error { return err } return c.HandleMessage(&Message{ - Type: "append", - Content: *cartItem, + Type: 2, + Content: cartItem, }, false, reply) } -func (c *CartGrain) GetStorageMessage(since int64) []Message { - if since == 0 { - return c.storageMessages - } - ret := make([]Message, 0) +func (c *CartGrain) GetStorageMessage(since int64) []StorableMessage { + + ret := make([]StorableMessage, 0) for _, message := range c.storageMessages { if *message.TimeStamp > since { ret = append(ret, message) @@ -108,27 +184,32 @@ func (c *CartGrain) GetStorageMessage(since int64) []Message { } func (c *CartGrain) HandleMessage(message *Message, isReplay bool, reply *CartGrain) error { - log.Printf("Handling message %s", message.Type) + log.Printf("Handling message %d", message.Type) if message.TimeStamp == nil { now := time.Now().Unix() message.TimeStamp = &now } var err error switch message.Type { - case "add": - sku, ok := message.Content.(string) + case 1: + msg, ok := message.Content.(*messages.AddRequest) if !ok { err = fmt.Errorf("invalid content type") } else { - return c.AddItem(sku, reply) + return c.AddItem(msg.Sku, reply) } - case "append": - item, ok := message.Content.(CartItem) + case 2: + msg, ok := message.Content.(*messages.AddItem) if !ok { err = fmt.Errorf("invalid content type") } else { - c.Items = append(c.Items, item) - c.TotalPrice += item.Price + c.Items = append(c.Items, CartItem{ + Sku: msg.Sku, + Name: msg.Name, + Price: msg.Price, + Image: msg.Image, + }) + c.TotalPrice += msg.Price } default: err = fmt.Errorf("unknown message type") diff --git a/data/1.prot b/data/1.prot new file mode 100644 index 0000000000000000000000000000000000000000..07c9ed64ff07ade571759a5316a47fa6bc2d3c89 GIT binary patch literal 383 zcmZ{fyGq1B6hL)B!AeWZVatWZWGlfU zU*eybY!+7qZ*^~T&OL*zeoo#&cH7(fKG;5de%c@I24Or7_~>}*5Y-e-ftH-s_&_d< zCJw6`d{k&H5lnc*C6&a=&9zBM)+`N?Nm*16Bu`3BAtpsYLVIzS~0zV{>X#>6E=(?7J_3!yd-}16)dHviQ?)bvzk)_XPQN-nx!S>t^E~n`CmZDJwFQRicHlS46+4-`UZnDNvgDk7W?Pr EFFaRuY5)KL literal 0 HcmV?d00001 diff --git a/data/state.json b/data/state.json new file mode 100644 index 0000000..f086c5a --- /dev/null +++ b/data/state.json @@ -0,0 +1 @@ +{"1":1731051371} diff --git a/data/state.json.bak b/data/state.json.bak new file mode 100644 index 0000000..09eb1ad --- /dev/null +++ b/data/state.json.bak @@ -0,0 +1 @@ +{"1":1731050604} diff --git a/disk-storage.go b/disk-storage.go index 4b81044..4203ad2 100644 --- a/disk-storage.go +++ b/disk-storage.go @@ -1,7 +1,6 @@ package main import ( - "encoding/gob" "encoding/json" "errors" "fmt" @@ -25,7 +24,7 @@ func NewDiskStorage(stateFile string) (*DiskStorage, error) { return ret, err } -func saveMessages(messages []Message, id string) error { +func saveMessages(messages []StorableMessage, id string) error { log.Printf("%d messages to save for %s", len(messages), id) if len(messages) == 0 { return nil @@ -38,17 +37,19 @@ func saveMessages(messages []Message, id string) error { return err } defer file.Close() - // z := gzip.NewWriter(file) - // defer z.Close() - enc := gob.NewEncoder(file) + for _, m := range messages { - err = enc.Encode(m) + b, err := m.GetBytes() + if err != nil { + return err + } + file.Write(b) } return err } func getCartPath(id string) string { - return fmt.Sprintf("data/%s.gob", id) + return fmt.Sprintf("data/%s.prot", id) } func loadMessages(grain Grain, id string) error { @@ -62,21 +63,17 @@ func loadMessages(grain Grain, id string) error { return err } defer file.Close() - // z, err := gzip.NewReader(file) - // if err != nil { - // return err - // } - // defer z.Close() var reply CartGrain - decoder := gob.NewDecoder(file) + for err == nil { - var message Message - err = decoder.Decode(&message) - if err != nil { - grain.HandleMessage(&message, true, &reply) + msg := &Message{} + err = msg.FromReader(file, msg) + if err == nil { + grain.HandleMessage(msg, true, &reply) } } + if err.Error() == "EOF" { return nil } diff --git a/git.tornberg.me/go-cart-actor/add.pb.go b/git.tornberg.me/go-cart-actor/add.pb.go new file mode 100644 index 0000000..0211997 --- /dev/null +++ b/git.tornberg.me/go-cart-actor/add.pb.go @@ -0,0 +1,212 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.35.1 +// protoc v5.28.2 +// source: add.proto + +package go_cart_actor + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type AddRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Sku string `protobuf:"bytes,2,opt,name=Sku,proto3" json:"Sku,omitempty"` +} + +func (x *AddRequest) Reset() { + *x = AddRequest{} + mi := &file_add_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *AddRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AddRequest) ProtoMessage() {} + +func (x *AddRequest) ProtoReflect() protoreflect.Message { + mi := &file_add_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use AddRequest.ProtoReflect.Descriptor instead. +func (*AddRequest) Descriptor() ([]byte, []int) { + return file_add_proto_rawDescGZIP(), []int{0} +} + +func (x *AddRequest) GetSku() string { + if x != nil { + return x.Sku + } + return "" +} + +type AddItem struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Quantity int32 `protobuf:"varint,2,opt,name=Quantity,proto3" json:"Quantity,omitempty"` + Price int64 `protobuf:"varint,3,opt,name=Price,proto3" json:"Price,omitempty"` + Sku string `protobuf:"bytes,4,opt,name=Sku,proto3" json:"Sku,omitempty"` + Name string `protobuf:"bytes,5,opt,name=Name,proto3" json:"Name,omitempty"` + Image string `protobuf:"bytes,6,opt,name=Image,proto3" json:"Image,omitempty"` +} + +func (x *AddItem) Reset() { + *x = AddItem{} + mi := &file_add_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *AddItem) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AddItem) ProtoMessage() {} + +func (x *AddItem) ProtoReflect() protoreflect.Message { + mi := &file_add_proto_msgTypes[1] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use AddItem.ProtoReflect.Descriptor instead. +func (*AddItem) Descriptor() ([]byte, []int) { + return file_add_proto_rawDescGZIP(), []int{1} +} + +func (x *AddItem) GetQuantity() int32 { + if x != nil { + return x.Quantity + } + return 0 +} + +func (x *AddItem) GetPrice() int64 { + if x != nil { + return x.Price + } + return 0 +} + +func (x *AddItem) GetSku() string { + if x != nil { + return x.Sku + } + return "" +} + +func (x *AddItem) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *AddItem) GetImage() string { + if x != nil { + return x.Image + } + return "" +} + +var File_add_proto protoreflect.FileDescriptor + +var file_add_proto_rawDesc = []byte{ + 0x0a, 0x09, 0x61, 0x64, 0x64, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x08, 0x6d, 0x65, 0x73, + 0x73, 0x61, 0x67, 0x65, 0x73, 0x22, 0x1e, 0x0a, 0x0a, 0x41, 0x64, 0x64, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x53, 0x6b, 0x75, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x03, 0x53, 0x6b, 0x75, 0x22, 0x77, 0x0a, 0x07, 0x41, 0x64, 0x64, 0x49, 0x74, 0x65, 0x6d, + 0x12, 0x1a, 0x0a, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x18, 0x02, 0x20, 0x01, + 0x28, 0x05, 0x52, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x12, 0x14, 0x0a, 0x05, + 0x50, 0x72, 0x69, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x50, 0x72, 0x69, + 0x63, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x53, 0x6b, 0x75, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x03, 0x53, 0x6b, 0x75, 0x12, 0x12, 0x0a, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x49, 0x6d, 0x61, 0x67, + 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x49, 0x6d, 0x61, 0x67, 0x65, 0x42, 0x1f, + 0x5a, 0x1d, 0x67, 0x69, 0x74, 0x2e, 0x74, 0x6f, 0x72, 0x6e, 0x62, 0x65, 0x72, 0x67, 0x2e, 0x6d, + 0x65, 0x2f, 0x67, 0x6f, 0x2d, 0x63, 0x61, 0x72, 0x74, 0x2d, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x62, + 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_add_proto_rawDescOnce sync.Once + file_add_proto_rawDescData = file_add_proto_rawDesc +) + +func file_add_proto_rawDescGZIP() []byte { + file_add_proto_rawDescOnce.Do(func() { + file_add_proto_rawDescData = protoimpl.X.CompressGZIP(file_add_proto_rawDescData) + }) + return file_add_proto_rawDescData +} + +var file_add_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_add_proto_goTypes = []any{ + (*AddRequest)(nil), // 0: messages.AddRequest + (*AddItem)(nil), // 1: messages.AddItem +} +var file_add_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_add_proto_init() } +func file_add_proto_init() { + if File_add_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_add_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_add_proto_goTypes, + DependencyIndexes: file_add_proto_depIdxs, + MessageInfos: file_add_proto_msgTypes, + }.Build() + File_add_proto = out.File + file_add_proto_rawDesc = nil + file_add_proto_goTypes = nil + file_add_proto_depIdxs = nil +} diff --git a/git.tornberg.me/go-cart-actor/messages/add.pb.go b/git.tornberg.me/go-cart-actor/messages/add.pb.go new file mode 100644 index 0000000..be563af --- /dev/null +++ b/git.tornberg.me/go-cart-actor/messages/add.pb.go @@ -0,0 +1,212 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.35.1 +// protoc v5.28.2 +// source: add.proto + +package messages + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type AddRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Sku string `protobuf:"bytes,2,opt,name=Sku,proto3" json:"Sku,omitempty"` +} + +func (x *AddRequest) Reset() { + *x = AddRequest{} + mi := &file_add_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *AddRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AddRequest) ProtoMessage() {} + +func (x *AddRequest) ProtoReflect() protoreflect.Message { + mi := &file_add_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use AddRequest.ProtoReflect.Descriptor instead. +func (*AddRequest) Descriptor() ([]byte, []int) { + return file_add_proto_rawDescGZIP(), []int{0} +} + +func (x *AddRequest) GetSku() string { + if x != nil { + return x.Sku + } + return "" +} + +type AddItem struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Quantity int32 `protobuf:"varint,2,opt,name=Quantity,proto3" json:"Quantity,omitempty"` + Price int64 `protobuf:"varint,3,opt,name=Price,proto3" json:"Price,omitempty"` + Sku string `protobuf:"bytes,4,opt,name=Sku,proto3" json:"Sku,omitempty"` + Name string `protobuf:"bytes,5,opt,name=Name,proto3" json:"Name,omitempty"` + Image string `protobuf:"bytes,6,opt,name=Image,proto3" json:"Image,omitempty"` +} + +func (x *AddItem) Reset() { + *x = AddItem{} + mi := &file_add_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *AddItem) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*AddItem) ProtoMessage() {} + +func (x *AddItem) ProtoReflect() protoreflect.Message { + mi := &file_add_proto_msgTypes[1] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use AddItem.ProtoReflect.Descriptor instead. +func (*AddItem) Descriptor() ([]byte, []int) { + return file_add_proto_rawDescGZIP(), []int{1} +} + +func (x *AddItem) GetQuantity() int32 { + if x != nil { + return x.Quantity + } + return 0 +} + +func (x *AddItem) GetPrice() int64 { + if x != nil { + return x.Price + } + return 0 +} + +func (x *AddItem) GetSku() string { + if x != nil { + return x.Sku + } + return "" +} + +func (x *AddItem) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *AddItem) GetImage() string { + if x != nil { + return x.Image + } + return "" +} + +var File_add_proto protoreflect.FileDescriptor + +var file_add_proto_rawDesc = []byte{ + 0x0a, 0x09, 0x61, 0x64, 0x64, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x08, 0x6d, 0x65, 0x73, + 0x73, 0x61, 0x67, 0x65, 0x73, 0x22, 0x1e, 0x0a, 0x0a, 0x41, 0x64, 0x64, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x53, 0x6b, 0x75, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x03, 0x53, 0x6b, 0x75, 0x22, 0x77, 0x0a, 0x07, 0x41, 0x64, 0x64, 0x49, 0x74, 0x65, 0x6d, + 0x12, 0x1a, 0x0a, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x18, 0x02, 0x20, 0x01, + 0x28, 0x05, 0x52, 0x08, 0x51, 0x75, 0x61, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x12, 0x14, 0x0a, 0x05, + 0x50, 0x72, 0x69, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x50, 0x72, 0x69, + 0x63, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x53, 0x6b, 0x75, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x03, 0x53, 0x6b, 0x75, 0x12, 0x12, 0x0a, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x49, 0x6d, 0x61, 0x67, + 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x49, 0x6d, 0x61, 0x67, 0x65, 0x42, 0x28, + 0x5a, 0x26, 0x67, 0x69, 0x74, 0x2e, 0x74, 0x6f, 0x72, 0x6e, 0x62, 0x65, 0x72, 0x67, 0x2e, 0x6d, + 0x65, 0x2f, 0x67, 0x6f, 0x2d, 0x63, 0x61, 0x72, 0x74, 0x2d, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x2f, + 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_add_proto_rawDescOnce sync.Once + file_add_proto_rawDescData = file_add_proto_rawDesc +) + +func file_add_proto_rawDescGZIP() []byte { + file_add_proto_rawDescOnce.Do(func() { + file_add_proto_rawDescData = protoimpl.X.CompressGZIP(file_add_proto_rawDescData) + }) + return file_add_proto_rawDescData +} + +var file_add_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_add_proto_goTypes = []any{ + (*AddRequest)(nil), // 0: messages.AddRequest + (*AddItem)(nil), // 1: messages.AddItem +} +var file_add_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_add_proto_init() } +func file_add_proto_init() { + if File_add_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_add_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_add_proto_goTypes, + DependencyIndexes: file_add_proto_depIdxs, + MessageInfos: file_add_proto_msgTypes, + }.Build() + File_add_proto = out.File + file_add_proto_rawDesc = nil + file_add_proto_goTypes = nil + file_add_proto_depIdxs = nil +} diff --git a/main.go b/main.go index b9e35bd..2083dbf 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,8 @@ import ( "net/http" "os" "time" + + "git.tornberg.me/go-cart-actor/git.tornberg.me/go-cart-actor/messages" ) func spawn(id string) Grain { @@ -56,8 +58,8 @@ func (a *App) HandleAddSku(w http.ResponseWriter, r *http.Request) { return } message := &Message{ - Type: "add", - Content: sku, + Type: 1, + Content: &messages.AddRequest{Sku: sku}, } var reply CartGrain err = grain.HandleMessage(message, false, &reply) diff --git a/messages/add.proto b/messages/add.proto new file mode 100644 index 0000000..8a3f4a8 --- /dev/null +++ b/messages/add.proto @@ -0,0 +1,18 @@ +syntax = "proto3"; +package messages; +option go_package = "git.tornberg.me/go-cart-actor/messages"; + +message AddRequest { + string Sku = 2; +} + +message AddItem { + int32 Quantity = 2; + int64 Price = 3; + string Sku = 4; + string Name = 5; + string Image = 6; +} + + +