protobuf
This commit is contained in:
131
cart-grain.go
131
cart-grain.go
@@ -1,25 +1,102 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.tornberg.me/go-cart-actor/git.tornberg.me/go-cart-actor/messages"
|
||||||
"github.com/matst80/slask-finder/pkg/index"
|
"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 Message struct {
|
||||||
|
Type uint64
|
||||||
TimeStamp *int64
|
TimeStamp *int64
|
||||||
Type string
|
|
||||||
Content interface{}
|
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 {
|
type CartItem struct {
|
||||||
Sku string `json:"sku"`
|
Sku string `json:"sku"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Price int `json:"price"`
|
Price int64 `json:"price"`
|
||||||
Image string `json:"image"`
|
Image string `json:"image"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,14 +104,14 @@ type CartGrain struct {
|
|||||||
storageMessages []Message
|
storageMessages []Message
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
Items []CartItem `json:"items"`
|
Items []CartItem `json:"items"`
|
||||||
TotalPrice int `json:"totalPrice"`
|
TotalPrice int64 `json:"totalPrice"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Grain interface {
|
type Grain interface {
|
||||||
GetId() string
|
GetId() string
|
||||||
GetLastChange() int64
|
GetLastChange() int64
|
||||||
HandleMessage(message *Message, isReplay bool, reply *CartGrain) error
|
HandleMessage(message *Message, isReplay bool, reply *CartGrain) error
|
||||||
GetStorageMessage(since int64) []Message
|
GetStorageMessage(since int64) []StorableMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CartGrain) GetId() string {
|
func (c *CartGrain) GetId() string {
|
||||||
@@ -48,7 +125,7 @@ func (c *CartGrain) GetLastChange() int64 {
|
|||||||
return *c.storageMessages[len(c.storageMessages)-1].TimeStamp
|
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)
|
res, err := http.Get("https://slask-finder.tornberg.me/api/get/" + sku)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -75,11 +152,12 @@ func getItemData(sku string) (*CartItem, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &CartItem{
|
return &messages.AddItem{
|
||||||
Sku: item.Sku,
|
Quantity: 1,
|
||||||
Name: item.Title,
|
Price: int64(price),
|
||||||
Price: price,
|
Sku: sku,
|
||||||
Image: item.Img,
|
Name: item.Title,
|
||||||
|
Image: item.Img,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,16 +167,14 @@ func (c *CartGrain) AddItem(sku string, reply *CartGrain) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return c.HandleMessage(&Message{
|
return c.HandleMessage(&Message{
|
||||||
Type: "append",
|
Type: 2,
|
||||||
Content: *cartItem,
|
Content: cartItem,
|
||||||
}, false, reply)
|
}, false, reply)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CartGrain) GetStorageMessage(since int64) []Message {
|
func (c *CartGrain) GetStorageMessage(since int64) []StorableMessage {
|
||||||
if since == 0 {
|
|
||||||
return c.storageMessages
|
ret := make([]StorableMessage, 0)
|
||||||
}
|
|
||||||
ret := make([]Message, 0)
|
|
||||||
for _, message := range c.storageMessages {
|
for _, message := range c.storageMessages {
|
||||||
if *message.TimeStamp > since {
|
if *message.TimeStamp > since {
|
||||||
ret = append(ret, message)
|
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 {
|
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 {
|
if message.TimeStamp == nil {
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
message.TimeStamp = &now
|
message.TimeStamp = &now
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
switch message.Type {
|
switch message.Type {
|
||||||
case "add":
|
case 1:
|
||||||
sku, ok := message.Content.(string)
|
msg, ok := message.Content.(*messages.AddRequest)
|
||||||
if !ok {
|
if !ok {
|
||||||
err = fmt.Errorf("invalid content type")
|
err = fmt.Errorf("invalid content type")
|
||||||
} else {
|
} else {
|
||||||
return c.AddItem(sku, reply)
|
return c.AddItem(msg.Sku, reply)
|
||||||
}
|
}
|
||||||
case "append":
|
case 2:
|
||||||
item, ok := message.Content.(CartItem)
|
msg, ok := message.Content.(*messages.AddItem)
|
||||||
if !ok {
|
if !ok {
|
||||||
err = fmt.Errorf("invalid content type")
|
err = fmt.Errorf("invalid content type")
|
||||||
} else {
|
} else {
|
||||||
c.Items = append(c.Items, item)
|
c.Items = append(c.Items, CartItem{
|
||||||
c.TotalPrice += item.Price
|
Sku: msg.Sku,
|
||||||
|
Name: msg.Name,
|
||||||
|
Price: msg.Price,
|
||||||
|
Image: msg.Image,
|
||||||
|
})
|
||||||
|
c.TotalPrice += msg.Price
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf("unknown message type")
|
err = fmt.Errorf("unknown message type")
|
||||||
|
|||||||
BIN
data/1.prot
Normal file
BIN
data/1.prot
Normal file
Binary file not shown.
1
data/state.json
Normal file
1
data/state.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"1":1731051371}
|
||||||
1
data/state.json.bak
Normal file
1
data/state.json.bak
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"1":1731050604}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/gob"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -25,7 +24,7 @@ func NewDiskStorage(stateFile string) (*DiskStorage, error) {
|
|||||||
return ret, err
|
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)
|
log.Printf("%d messages to save for %s", len(messages), id)
|
||||||
if len(messages) == 0 {
|
if len(messages) == 0 {
|
||||||
return nil
|
return nil
|
||||||
@@ -38,17 +37,19 @@ func saveMessages(messages []Message, id string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
// z := gzip.NewWriter(file)
|
|
||||||
// defer z.Close()
|
|
||||||
enc := gob.NewEncoder(file)
|
|
||||||
for _, m := range messages {
|
for _, m := range messages {
|
||||||
err = enc.Encode(m)
|
b, err := m.GetBytes()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
file.Write(b)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCartPath(id string) string {
|
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 {
|
func loadMessages(grain Grain, id string) error {
|
||||||
@@ -62,21 +63,17 @@ func loadMessages(grain Grain, id string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
// z, err := gzip.NewReader(file)
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// defer z.Close()
|
|
||||||
|
|
||||||
var reply CartGrain
|
var reply CartGrain
|
||||||
decoder := gob.NewDecoder(file)
|
|
||||||
for err == nil {
|
for err == nil {
|
||||||
var message Message
|
msg := &Message{}
|
||||||
err = decoder.Decode(&message)
|
err = msg.FromReader(file, msg)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
grain.HandleMessage(&message, true, &reply)
|
grain.HandleMessage(msg, true, &reply)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err.Error() == "EOF" {
|
if err.Error() == "EOF" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
212
git.tornberg.me/go-cart-actor/add.pb.go
Normal file
212
git.tornberg.me/go-cart-actor/add.pb.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
212
git.tornberg.me/go-cart-actor/messages/add.pb.go
Normal file
212
git.tornberg.me/go-cart-actor/messages/add.pb.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
6
main.go
6
main.go
@@ -7,6 +7,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.tornberg.me/go-cart-actor/git.tornberg.me/go-cart-actor/messages"
|
||||||
)
|
)
|
||||||
|
|
||||||
func spawn(id string) Grain {
|
func spawn(id string) Grain {
|
||||||
@@ -56,8 +58,8 @@ func (a *App) HandleAddSku(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
message := &Message{
|
message := &Message{
|
||||||
Type: "add",
|
Type: 1,
|
||||||
Content: sku,
|
Content: &messages.AddRequest{Sku: sku},
|
||||||
}
|
}
|
||||||
var reply CartGrain
|
var reply CartGrain
|
||||||
err = grain.HandleMessage(message, false, &reply)
|
err = grain.HandleMessage(message, false, &reply)
|
||||||
|
|||||||
18
messages/add.proto
Normal file
18
messages/add.proto
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user