From b7f0990269802ae684392e185ff17be0a32baa5d Mon Sep 17 00:00:00 2001 From: matst80 Date: Sat, 18 Oct 2025 15:21:11 +0200 Subject: [PATCH] simpler rules --- pkg/cart/cart-grain.go | 12 +-- pkg/messages/messages.pb.go | 139 ++++++++----------------------- pkg/voucher/service.go | 13 +-- proto/messages.proto | 159 ++++++++++++++++-------------------- 4 files changed, 114 insertions(+), 209 deletions(-) diff --git a/pkg/cart/cart-grain.go b/pkg/cart/cart-grain.go index 712f20e..8682d4d 100644 --- a/pkg/cart/cart-grain.go +++ b/pkg/cart/cart-grain.go @@ -92,10 +92,10 @@ type CartGrain struct { } type Voucher struct { - Code string `json:"code"` - Rules []*messages.VoucherRule `json:"rules"` - Id uint32 `json:"id"` - Value int64 `json:"value"` + Code string `json:"code"` + Rules []string `json:"rules"` + Id uint32 `json:"id"` + Value int64 `json:"value"` } func (v *Voucher) AppliesTo(cart *CartGrain) ([]*CartItem, bool) { @@ -127,8 +127,8 @@ func (v *Voucher) AppliesTo(cart *CartGrain) ([]*CartItem, bool) { } // All voucher rules must pass (logical AND) - for _, rule := range v.Rules { - expr := rule.GetCondition() + for _, expr := range v.Rules { + if expr == "" { // Empty condition treated as pass (acts like a comment / placeholder) continue diff --git a/pkg/messages/messages.pb.go b/pkg/messages/messages.pb.go index 7303467..9f4f8a5 100644 --- a/pkg/messages/messages.pb.go +++ b/pkg/messages/messages.pb.go @@ -926,86 +926,18 @@ func (x *InitializeCheckout) GetPaymentInProgress() bool { return false } -type VoucherRule struct { - state protoimpl.MessageState `protogen:"open.v1"` - Type string `protobuf:"bytes,2,opt,name=type,proto3" json:"type,omitempty"` - Description string `protobuf:"bytes,3,opt,name=description,proto3" json:"description,omitempty"` - Condition string `protobuf:"bytes,4,opt,name=condition,proto3" json:"condition,omitempty"` - Action string `protobuf:"bytes,5,opt,name=action,proto3" json:"action,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *VoucherRule) Reset() { - *x = VoucherRule{} - mi := &file_messages_proto_msgTypes[12] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *VoucherRule) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*VoucherRule) ProtoMessage() {} - -func (x *VoucherRule) ProtoReflect() protoreflect.Message { - mi := &file_messages_proto_msgTypes[12] - 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 VoucherRule.ProtoReflect.Descriptor instead. -func (*VoucherRule) Descriptor() ([]byte, []int) { - return file_messages_proto_rawDescGZIP(), []int{12} -} - -func (x *VoucherRule) GetType() string { - if x != nil { - return x.Type - } - return "" -} - -func (x *VoucherRule) GetDescription() string { - if x != nil { - return x.Description - } - return "" -} - -func (x *VoucherRule) GetCondition() string { - if x != nil { - return x.Condition - } - return "" -} - -func (x *VoucherRule) GetAction() string { - if x != nil { - return x.Action - } - return "" -} - type AddVoucher struct { state protoimpl.MessageState `protogen:"open.v1"` Code string `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"` Value int64 `protobuf:"varint,2,opt,name=value,proto3" json:"value,omitempty"` - VoucherRules []*VoucherRule `protobuf:"bytes,3,rep,name=voucherRules,proto3" json:"voucherRules,omitempty"` + VoucherRules []string `protobuf:"bytes,3,rep,name=voucherRules,proto3" json:"voucherRules,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } func (x *AddVoucher) Reset() { *x = AddVoucher{} - mi := &file_messages_proto_msgTypes[13] + mi := &file_messages_proto_msgTypes[12] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1017,7 +949,7 @@ func (x *AddVoucher) String() string { func (*AddVoucher) ProtoMessage() {} func (x *AddVoucher) ProtoReflect() protoreflect.Message { - mi := &file_messages_proto_msgTypes[13] + mi := &file_messages_proto_msgTypes[12] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1030,7 +962,7 @@ func (x *AddVoucher) ProtoReflect() protoreflect.Message { // Deprecated: Use AddVoucher.ProtoReflect.Descriptor instead. func (*AddVoucher) Descriptor() ([]byte, []int) { - return file_messages_proto_rawDescGZIP(), []int{13} + return file_messages_proto_rawDescGZIP(), []int{12} } func (x *AddVoucher) GetCode() string { @@ -1047,7 +979,7 @@ func (x *AddVoucher) GetValue() int64 { return 0 } -func (x *AddVoucher) GetVoucherRules() []*VoucherRule { +func (x *AddVoucher) GetVoucherRules() []string { if x != nil { return x.VoucherRules } @@ -1063,7 +995,7 @@ type RemoveVoucher struct { func (x *RemoveVoucher) Reset() { *x = RemoveVoucher{} - mi := &file_messages_proto_msgTypes[14] + mi := &file_messages_proto_msgTypes[13] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1075,7 +1007,7 @@ func (x *RemoveVoucher) String() string { func (*RemoveVoucher) ProtoMessage() {} func (x *RemoveVoucher) ProtoReflect() protoreflect.Message { - mi := &file_messages_proto_msgTypes[14] + mi := &file_messages_proto_msgTypes[13] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1088,7 +1020,7 @@ func (x *RemoveVoucher) ProtoReflect() protoreflect.Message { // Deprecated: Use RemoveVoucher.ProtoReflect.Descriptor instead. func (*RemoveVoucher) Descriptor() ([]byte, []int) { - return file_messages_proto_rawDescGZIP(), []int{14} + return file_messages_proto_rawDescGZIP(), []int{13} } func (x *RemoveVoucher) GetId() uint32 { @@ -1110,7 +1042,7 @@ type UpsertSubscriptionDetails struct { func (x *UpsertSubscriptionDetails) Reset() { *x = UpsertSubscriptionDetails{} - mi := &file_messages_proto_msgTypes[15] + mi := &file_messages_proto_msgTypes[14] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1122,7 +1054,7 @@ func (x *UpsertSubscriptionDetails) String() string { func (*UpsertSubscriptionDetails) ProtoMessage() {} func (x *UpsertSubscriptionDetails) ProtoReflect() protoreflect.Message { - mi := &file_messages_proto_msgTypes[15] + mi := &file_messages_proto_msgTypes[14] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1135,7 +1067,7 @@ func (x *UpsertSubscriptionDetails) ProtoReflect() protoreflect.Message { // Deprecated: Use UpsertSubscriptionDetails.ProtoReflect.Descriptor instead. func (*UpsertSubscriptionDetails) Descriptor() ([]byte, []int) { - return file_messages_proto_rawDescGZIP(), []int{15} + return file_messages_proto_rawDescGZIP(), []int{14} } func (x *UpsertSubscriptionDetails) GetId() string { @@ -1177,7 +1109,7 @@ type PreConditionFailed struct { func (x *PreConditionFailed) Reset() { *x = PreConditionFailed{} - mi := &file_messages_proto_msgTypes[16] + mi := &file_messages_proto_msgTypes[15] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -1189,7 +1121,7 @@ func (x *PreConditionFailed) String() string { func (*PreConditionFailed) ProtoMessage() {} func (x *PreConditionFailed) ProtoReflect() protoreflect.Message { - mi := &file_messages_proto_msgTypes[16] + mi := &file_messages_proto_msgTypes[15] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1202,7 +1134,7 @@ func (x *PreConditionFailed) ProtoReflect() protoreflect.Message { // Deprecated: Use PreConditionFailed.ProtoReflect.Descriptor instead. func (*PreConditionFailed) Descriptor() ([]byte, []int) { - return file_messages_proto_rawDescGZIP(), []int{16} + return file_messages_proto_rawDescGZIP(), []int{15} } func (x *PreConditionFailed) GetOperation() string { @@ -1335,17 +1267,12 @@ const file_messages_proto_rawDesc = "" + "\x12InitializeCheckout\x12\x18\n" + "\aorderId\x18\x01 \x01(\tR\aorderId\x12\x16\n" + "\x06status\x18\x02 \x01(\tR\x06status\x12,\n" + - "\x11paymentInProgress\x18\x03 \x01(\bR\x11paymentInProgress\"y\n" + - "\vVoucherRule\x12\x12\n" + - "\x04type\x18\x02 \x01(\tR\x04type\x12 \n" + - "\vdescription\x18\x03 \x01(\tR\vdescription\x12\x1c\n" + - "\tcondition\x18\x04 \x01(\tR\tcondition\x12\x16\n" + - "\x06action\x18\x05 \x01(\tR\x06action\"q\n" + + "\x11paymentInProgress\x18\x03 \x01(\bR\x11paymentInProgress\"Z\n" + "\n" + "AddVoucher\x12\x12\n" + "\x04code\x18\x01 \x01(\tR\x04code\x12\x14\n" + - "\x05value\x18\x02 \x01(\x03R\x05value\x129\n" + - "\fvoucherRules\x18\x03 \x03(\v2\x15.messages.VoucherRuleR\fvoucherRules\"\x1f\n" + + "\x05value\x18\x02 \x01(\x03R\x05value\x12\"\n" + + "\fvoucherRules\x18\x03 \x03(\tR\fvoucherRules\"\x1f\n" + "\rRemoveVoucher\x12\x0e\n" + "\x02id\x18\x01 \x01(\rR\x02id\"\xa7\x01\n" + "\x19UpsertSubscriptionDetails\x12\x13\n" + @@ -1371,7 +1298,7 @@ func file_messages_proto_rawDescGZIP() []byte { return file_messages_proto_rawDescData } -var file_messages_proto_msgTypes = make([]protoimpl.MessageInfo, 17) +var file_messages_proto_msgTypes = make([]protoimpl.MessageInfo, 16) var file_messages_proto_goTypes = []any{ (*ClearCartRequest)(nil), // 0: messages.ClearCartRequest (*AddItem)(nil), // 1: messages.AddItem @@ -1385,23 +1312,21 @@ var file_messages_proto_goTypes = []any{ (*OrderCreated)(nil), // 9: messages.OrderCreated (*Noop)(nil), // 10: messages.Noop (*InitializeCheckout)(nil), // 11: messages.InitializeCheckout - (*VoucherRule)(nil), // 12: messages.VoucherRule - (*AddVoucher)(nil), // 13: messages.AddVoucher - (*RemoveVoucher)(nil), // 14: messages.RemoveVoucher - (*UpsertSubscriptionDetails)(nil), // 15: messages.UpsertSubscriptionDetails - (*PreConditionFailed)(nil), // 16: messages.PreConditionFailed - (*anypb.Any)(nil), // 17: google.protobuf.Any + (*AddVoucher)(nil), // 12: messages.AddVoucher + (*RemoveVoucher)(nil), // 13: messages.RemoveVoucher + (*UpsertSubscriptionDetails)(nil), // 14: messages.UpsertSubscriptionDetails + (*PreConditionFailed)(nil), // 15: messages.PreConditionFailed + (*anypb.Any)(nil), // 16: google.protobuf.Any } var file_messages_proto_depIdxs = []int32{ 6, // 0: messages.SetDelivery.pickupPoint:type_name -> messages.PickupPoint - 12, // 1: messages.AddVoucher.voucherRules:type_name -> messages.VoucherRule - 17, // 2: messages.UpsertSubscriptionDetails.data:type_name -> google.protobuf.Any - 17, // 3: messages.PreConditionFailed.input:type_name -> google.protobuf.Any - 4, // [4:4] is the sub-list for method output_type - 4, // [4:4] is the sub-list for method input_type - 4, // [4:4] is the sub-list for extension type_name - 4, // [4:4] is the sub-list for extension extendee - 0, // [0:4] is the sub-list for field type_name + 16, // 1: messages.UpsertSubscriptionDetails.data:type_name -> google.protobuf.Any + 16, // 2: messages.PreConditionFailed.input:type_name -> google.protobuf.Any + 3, // [3:3] is the sub-list for method output_type + 3, // [3:3] is the sub-list for method input_type + 3, // [3:3] is the sub-list for extension type_name + 3, // [3:3] is the sub-list for extension extendee + 0, // [0:3] is the sub-list for field type_name } func init() { file_messages_proto_init() } @@ -1413,14 +1338,14 @@ func file_messages_proto_init() { file_messages_proto_msgTypes[4].OneofWrappers = []any{} file_messages_proto_msgTypes[5].OneofWrappers = []any{} file_messages_proto_msgTypes[6].OneofWrappers = []any{} - file_messages_proto_msgTypes[15].OneofWrappers = []any{} + file_messages_proto_msgTypes[14].OneofWrappers = []any{} type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: unsafe.Slice(unsafe.StringData(file_messages_proto_rawDesc), len(file_messages_proto_rawDesc)), NumEnums: 0, - NumMessages: 17, + NumMessages: 16, NumExtensions: 0, NumServices: 0, }, diff --git a/pkg/voucher/service.go b/pkg/voucher/service.go index e5913c3..01a9677 100644 --- a/pkg/voucher/service.go +++ b/pkg/voucher/service.go @@ -17,7 +17,7 @@ type Rule struct { type Voucher struct { Code string `json:"code"` Value int64 `json:"value"` - rules string `json:"rules"` + Rules string `json:"rules"` } type Service struct { @@ -42,13 +42,8 @@ func (s *Service) GetVoucher(code string) (*messages.AddVoucher, error) { return &messages.AddVoucher{ Code: code, Value: v.Value, - VoucherRules: []*messages.VoucherRule{ - { - Condition: v.rules, - Type: "", - Description: "", - Action: "", - }, + VoucherRules: []string{ + v.Rules, }, }, nil } @@ -59,7 +54,7 @@ type State struct { type StateFile struct { State State `json:"state"` - Vercion int `json:"version"` + Version int `json:"version"` } func (sf *StateFile) GetVoucher(code string) (*Voucher, bool) { diff --git a/proto/messages.proto b/proto/messages.proto index 0b9f827..4ec43f5 100644 --- a/proto/messages.proto +++ b/proto/messages.proto @@ -4,129 +4,114 @@ option go_package = "git.tornberg.me/go-cart-actor/proto;messages"; import "google/protobuf/any.proto"; -message ClearCartRequest { - -} +message ClearCartRequest {} message AddItem { - uint32 item_id = 1; - int32 quantity = 2; - int64 price = 3; - int64 orgPrice = 9; - string sku = 4; - string name = 5; - string image = 6; - int32 stock = 7; - int32 tax = 8; - string brand = 13; - string category = 14; - string category2 = 15; - string category3 = 16; - string category4 = 17; - string category5 = 18; - string disclaimer = 10; - string articleType = 11; - string sellerId = 19; - string sellerName = 20; - string country = 21; - string saleStatus = 24; - optional string outlet = 12; - optional string storeId = 22; - optional uint32 parentId = 23; + uint32 item_id = 1; + int32 quantity = 2; + int64 price = 3; + int64 orgPrice = 9; + string sku = 4; + string name = 5; + string image = 6; + int32 stock = 7; + int32 tax = 8; + string brand = 13; + string category = 14; + string category2 = 15; + string category3 = 16; + string category4 = 17; + string category5 = 18; + string disclaimer = 10; + string articleType = 11; + string sellerId = 19; + string sellerName = 20; + string country = 21; + string saleStatus = 24; + optional string outlet = 12; + optional string storeId = 22; + optional uint32 parentId = 23; } -message RemoveItem { - uint32 Id = 1; -} +message RemoveItem { uint32 Id = 1; } message ChangeQuantity { - uint32 Id = 1; - int32 quantity = 2; + uint32 Id = 1; + int32 quantity = 2; } message SetDelivery { - string provider = 1; - repeated uint32 items = 2; - optional PickupPoint pickupPoint = 3; - string country = 4; - string zip = 5; - optional string address = 6; - optional string city = 7; + string provider = 1; + repeated uint32 items = 2; + optional PickupPoint pickupPoint = 3; + string country = 4; + string zip = 5; + optional string address = 6; + optional string city = 7; } message SetPickupPoint { - uint32 deliveryId = 1; - string id = 2; - optional string name = 3; - optional string address = 4; - optional string city = 5; - optional string zip = 6; - optional string country = 7; + uint32 deliveryId = 1; + string id = 2; + optional string name = 3; + optional string address = 4; + optional string city = 5; + optional string zip = 6; + optional string country = 7; } message PickupPoint { - string id = 1; - optional string name = 2; - optional string address = 3; - optional string city = 4; - optional string zip = 5; - optional string country = 6; + string id = 1; + optional string name = 2; + optional string address = 3; + optional string city = 4; + optional string zip = 5; + optional string country = 6; } -message RemoveDelivery { - uint32 id = 1; -} +message RemoveDelivery { uint32 id = 1; } message CreateCheckoutOrder { - string terms = 1; - string checkout = 2; - string confirmation = 3; - string push = 4; - string validation = 5; - string country = 6; + string terms = 1; + string checkout = 2; + string confirmation = 3; + string push = 4; + string validation = 5; + string country = 6; } message OrderCreated { - string orderId = 1; - string status = 2; + string orderId = 1; + string status = 2; } message Noop { - // Intentionally empty - used for ownership acquisition or health pings + // Intentionally empty - used for ownership acquisition or health pings } message InitializeCheckout { - string orderId = 1; - string status = 2; - bool paymentInProgress = 3; -} - -message VoucherRule { - string type = 2; - string description = 3; - string condition = 4; - string action = 5; + string orderId = 1; + string status = 2; + bool paymentInProgress = 3; } message AddVoucher { - string code = 1; - int64 value = 2; - repeated VoucherRule voucherRules = 3; + string code = 1; + int64 value = 2; + repeated string voucherRules = 3; } -message RemoveVoucher { - uint32 id = 1; -} +message RemoveVoucher { uint32 id = 1; } message UpsertSubscriptionDetails { - optional string id = 1; - string offeringCode = 2; - string signingType = 3; - google.protobuf.Any data = 4; + optional string id = 1; + string offeringCode = 2; + string signingType = 3; + google.protobuf.Any data = 4; } message PreConditionFailed { - string operation = 1; - string error = 2; - google.protobuf.Any input = 3; + string operation = 1; + string error = 2; + google.protobuf.Any input = 3; }