diff --git a/cmd/protoc-gen-go-kvstore/internal/optparse/names.go b/cmd/protoc-gen-go-kvstore/internal/optparse/names.go index 88d8050..361f8f5 100644 --- a/cmd/protoc-gen-go-kvstore/internal/optparse/names.go +++ b/cmd/protoc-gen-go-kvstore/internal/optparse/names.go @@ -9,6 +9,8 @@ import ( var keySuffixes = []string{ "key", "Key", + "Request", + "Req", } var valueSuffixes = []string{ @@ -16,6 +18,8 @@ var valueSuffixes = []string{ "Val", "value", "Value", + "Response", + "Resp", } func extractKeyName(name string) string { diff --git a/examples/views/app/main.go b/examples/views/app/main.go new file mode 100644 index 0000000..34b7e2e --- /dev/null +++ b/examples/views/app/main.go @@ -0,0 +1,7 @@ +package main + +func main() { + // see test file to usage examples + // you may try your custom usage of kvstore here + return +} diff --git a/examples/views/app/main_test.go b/examples/views/app/main_test.go new file mode 100644 index 0000000..dd270f9 --- /dev/null +++ b/examples/views/app/main_test.go @@ -0,0 +1,80 @@ +package main + +import ( + "context" + "testing" + "time" + + "github.com/redis/go-redis/v9" + "github.com/stretchr/testify/suite" + + "github.com/ehsundar/kvstore" + "github.com/ehsundar/kvstore/examples/views" +) + +type ViewsTestSuite struct { + suite.Suite + + r *redis.Client +} + +func TestViewsTestSuite(t *testing.T) { + suite.Run(t, new(ViewsTestSuite)) +} + +func (s *ViewsTestSuite) SetupSuite() { + r := redis.NewClient(&redis.Options{ + Network: "tcp", + Addr: "localhost:6379", + ClientName: "tests", + }) + + _, err := r.FlushAll(context.Background()).Result() + s.Nil(err) + + s.r = r +} + +func (s *ViewsTestSuite) TearDownTest() { + _, err := s.r.FlushAll(context.Background()).Result() + s.Nil(err) +} + +func (s *ViewsTestSuite) TestShouldReadWriteToStorageRespectingTTL() { + ctx := context.Background() + storage := views.NewGetItemsStore(s.r) + + key := &views.GetItemsRequest{ + ViewId: 12, + Filters: []string{}, + } + + _, err := storage.Set(ctx, key, &views.GetItemsResponse{ + Title: "test_title", + Items: []*views.Item{ + { + Id: 1, + Visible: true, + Display: "display 1", + }, + { + Id: 2, + Visible: false, + Display: "display 2", + }, + }, + }, kvstore.WithSetTTL(100*time.Millisecond)) + s.Nil(err) + + value, err := storage.Get(ctx, key, kvstore.WithGetTTL(time.Millisecond)) + s.Nil(err) + + s.Equal("test_title", value.Title) + s.Equal(2, len(value.Items)) + s.Equal("display 1", value.Items[0].Display) + + time.Sleep(time.Millisecond) + + _, err = storage.Get(ctx, key) + s.ErrorIs(err, redis.Nil) +} diff --git a/examples/views/views.pb.go b/examples/views/views.pb.go new file mode 100644 index 0000000..67e355a --- /dev/null +++ b/examples/views/views.pb.go @@ -0,0 +1,312 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.1 +// protoc v4.25.1 +// source: examples/views/views.proto + +package views + +import ( + _ "github.com/ehsundar/kvstore/protobuf/kvstore" + 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 GetItemsRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + ViewId int64 `protobuf:"varint,1,opt,name=view_id,json=viewId,proto3" json:"view_id,omitempty"` + Filters []string `protobuf:"bytes,2,rep,name=filters,proto3" json:"filters,omitempty"` +} + +func (x *GetItemsRequest) Reset() { + *x = GetItemsRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_examples_views_views_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetItemsRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetItemsRequest) ProtoMessage() {} + +func (x *GetItemsRequest) ProtoReflect() protoreflect.Message { + mi := &file_examples_views_views_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use GetItemsRequest.ProtoReflect.Descriptor instead. +func (*GetItemsRequest) Descriptor() ([]byte, []int) { + return file_examples_views_views_proto_rawDescGZIP(), []int{0} +} + +func (x *GetItemsRequest) GetViewId() int64 { + if x != nil { + return x.ViewId + } + return 0 +} + +func (x *GetItemsRequest) GetFilters() []string { + if x != nil { + return x.Filters + } + return nil +} + +type GetItemsResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Title string `protobuf:"bytes,1,opt,name=title,proto3" json:"title,omitempty"` + Items []*Item `protobuf:"bytes,2,rep,name=items,proto3" json:"items,omitempty"` +} + +func (x *GetItemsResponse) Reset() { + *x = GetItemsResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_examples_views_views_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetItemsResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetItemsResponse) ProtoMessage() {} + +func (x *GetItemsResponse) ProtoReflect() protoreflect.Message { + mi := &file_examples_views_views_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use GetItemsResponse.ProtoReflect.Descriptor instead. +func (*GetItemsResponse) Descriptor() ([]byte, []int) { + return file_examples_views_views_proto_rawDescGZIP(), []int{1} +} + +func (x *GetItemsResponse) GetTitle() string { + if x != nil { + return x.Title + } + return "" +} + +func (x *GetItemsResponse) GetItems() []*Item { + if x != nil { + return x.Items + } + return nil +} + +type Item struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` + Visible bool `protobuf:"varint,2,opt,name=visible,proto3" json:"visible,omitempty"` + Display string `protobuf:"bytes,3,opt,name=display,proto3" json:"display,omitempty"` +} + +func (x *Item) Reset() { + *x = Item{} + if protoimpl.UnsafeEnabled { + mi := &file_examples_views_views_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Item) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Item) ProtoMessage() {} + +func (x *Item) ProtoReflect() protoreflect.Message { + mi := &file_examples_views_views_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Item.ProtoReflect.Descriptor instead. +func (*Item) Descriptor() ([]byte, []int) { + return file_examples_views_views_proto_rawDescGZIP(), []int{2} +} + +func (x *Item) GetId() int64 { + if x != nil { + return x.Id + } + return 0 +} + +func (x *Item) GetVisible() bool { + if x != nil { + return x.Visible + } + return false +} + +func (x *Item) GetDisplay() string { + if x != nil { + return x.Display + } + return "" +} + +var File_examples_views_views_proto protoreflect.FileDescriptor + +var file_examples_views_views_proto_rawDesc = []byte{ + 0x0a, 0x1a, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x73, 0x2f, 0x76, 0x69, 0x65, 0x77, 0x73, + 0x2f, 0x76, 0x69, 0x65, 0x77, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x76, 0x69, + 0x65, 0x77, 0x73, 0x1a, 0x15, 0x6b, 0x76, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x2f, 0x6f, 0x70, 0x74, + 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x4b, 0x0a, 0x0f, 0x47, 0x65, + 0x74, 0x49, 0x74, 0x65, 0x6d, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x17, 0x0a, + 0x07, 0x76, 0x69, 0x65, 0x77, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, + 0x76, 0x69, 0x65, 0x77, 0x49, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x66, 0x69, 0x6c, 0x74, 0x65, 0x72, + 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x09, 0x52, 0x07, 0x66, 0x69, 0x6c, 0x74, 0x65, 0x72, 0x73, + 0x3a, 0x05, 0xc2, 0x3e, 0x02, 0x1a, 0x00, 0x22, 0x52, 0x0a, 0x10, 0x47, 0x65, 0x74, 0x49, 0x74, + 0x65, 0x6d, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x74, + 0x69, 0x74, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x69, 0x74, 0x6c, + 0x65, 0x12, 0x21, 0x0a, 0x05, 0x69, 0x74, 0x65, 0x6d, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, + 0x32, 0x0b, 0x2e, 0x76, 0x69, 0x65, 0x77, 0x73, 0x2e, 0x49, 0x74, 0x65, 0x6d, 0x52, 0x05, 0x69, + 0x74, 0x65, 0x6d, 0x73, 0x3a, 0x05, 0xca, 0x3e, 0x02, 0x12, 0x00, 0x22, 0x4a, 0x0a, 0x04, 0x49, + 0x74, 0x65, 0x6d, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, + 0x02, 0x69, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x76, 0x69, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x76, 0x69, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x12, 0x18, 0x0a, + 0x07, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, + 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x42, 0x2c, 0x5a, 0x2a, 0x67, 0x69, 0x74, 0x68, 0x75, + 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x65, 0x68, 0x73, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x2f, 0x6b, + 0x76, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x73, 0x2f, + 0x76, 0x69, 0x65, 0x77, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_examples_views_views_proto_rawDescOnce sync.Once + file_examples_views_views_proto_rawDescData = file_examples_views_views_proto_rawDesc +) + +func file_examples_views_views_proto_rawDescGZIP() []byte { + file_examples_views_views_proto_rawDescOnce.Do(func() { + file_examples_views_views_proto_rawDescData = protoimpl.X.CompressGZIP(file_examples_views_views_proto_rawDescData) + }) + return file_examples_views_views_proto_rawDescData +} + +var file_examples_views_views_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_examples_views_views_proto_goTypes = []interface{}{ + (*GetItemsRequest)(nil), // 0: views.GetItemsRequest + (*GetItemsResponse)(nil), // 1: views.GetItemsResponse + (*Item)(nil), // 2: views.Item +} +var file_examples_views_views_proto_depIdxs = []int32{ + 2, // 0: views.GetItemsResponse.items:type_name -> views.Item + 1, // [1:1] is the sub-list for method output_type + 1, // [1:1] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_examples_views_views_proto_init() } +func file_examples_views_views_proto_init() { + if File_examples_views_views_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_examples_views_views_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetItemsRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_examples_views_views_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetItemsResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_examples_views_views_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Item); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_examples_views_views_proto_rawDesc, + NumEnums: 0, + NumMessages: 3, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_examples_views_views_proto_goTypes, + DependencyIndexes: file_examples_views_views_proto_depIdxs, + MessageInfos: file_examples_views_views_proto_msgTypes, + }.Build() + File_examples_views_views_proto = out.File + file_examples_views_views_proto_rawDesc = nil + file_examples_views_views_proto_goTypes = nil + file_examples_views_views_proto_depIdxs = nil +} diff --git a/examples/views/views.proto b/examples/views/views.proto new file mode 100644 index 0000000..ca62019 --- /dev/null +++ b/examples/views/views.proto @@ -0,0 +1,33 @@ +syntax = "proto3"; + +package views; + +option go_package = "github.com/ehsundar/kvstore/examples/views"; + +import "kvstore/options.proto"; + +message GetItemsRequest { + option (kvstore.key_options) = { + // can be omitted, could be inferred + dynamic_key: {}, + }; + + int64 view_id = 1; + repeated string filters = 2; +} + +message GetItemsResponse { + option (kvstore.value_options) = { + // can be omitted, would be inferred + general: {}, + }; + + string title = 1; + repeated Item items = 2; +} + +message Item { + int64 id = 1; + bool visible = 2; + string display = 3; +} diff --git a/examples/views/views_kvstore.pb.go b/examples/views/views_kvstore.pb.go new file mode 100644 index 0000000..073bff8 --- /dev/null +++ b/examples/views/views_kvstore.pb.go @@ -0,0 +1,163 @@ +// Code generated by protoc-gen-go-kvstore . DO NOT EDIT. + +package views + +import ( + "context" + "errors" + "fmt" + + "github.com/ehsundar/kvstore" + "github.com/redis/go-redis/v9" + "google.golang.org/protobuf/encoding/protojson" +) + +// generated code for GetItems +// storage interface + +type GetItemsKVStore interface { + Get(context.Context, *GetItemsRequest, ...kvstore.GetOption) (*GetItemsResponse, error) + Set(context.Context, *GetItemsRequest, + *GetItemsResponse, ...kvstore.SetOption) (*GetItemsResponse, error) + Del(context.Context, *GetItemsRequest) error +} + +// storage construction + +func NewGetItemsStore(r redis.Cmdable, opts ...kvstore.InitOption) GetItemsKVStore { + + oc := kvstore.InitOptionContext{} + for _, opt := range opts { + opt(&oc) + } + + return &getItemsStorage{ + r: r, + opts: oc, + } +} + +// storage implementation + +type getItemsStorage struct { + r redis.Cmdable + opts kvstore.InitOptionContext +} + +func (s *getItemsStorage) Get( + ctx context.Context, key *GetItemsRequest, opts ...kvstore.GetOption) (*GetItemsResponse, error) { + + var err error + + o := kvstore.GetOptionContext{} + for _, opt := range opts { + opt(&o) + } + + k, err := key.marshal() + if err != nil { + return nil, err + } + + var v string + switch { + case o.Del: + v, err = s.r.GetDel(ctx, k).Result() + case o.TTL != 0: + v, err = s.r.GetEx(ctx, k, o.TTL).Result() + case !o.ExAt.IsZero(): + // TODO: PR to go-redis for exAt + err = errors.New("exat is not supported by go-redis") + default: + v, err = s.r.Get(ctx, k).Result() + } + + if err != nil { + return nil, err + } + + msg := &GetItemsResponse{} + err = msg.unmarshal(v) + if err != nil { + return nil, err + } + + return msg, nil +} + +func (s *getItemsStorage) Set(ctx context.Context, key *GetItemsRequest, + value *GetItemsResponse, opts ...kvstore.SetOption) (*GetItemsResponse, error) { + + o := kvstore.SetOptionContext{ + Get: true, + KeepTTL: true, + } + for _, opt := range opts { + opt(&o) + } + + k, err := key.marshal() + if err != nil { + return nil, err + } + + mv, err := value.marshal() + if err != nil { + return nil, err + } + + v, err := s.r.SetArgs(ctx, k, mv, redis.SetArgs{ + Mode: o.Mode, + TTL: o.TTL, + ExpireAt: o.ExAt, + Get: o.Get, + KeepTTL: o.KeepTTL, + }).Result() + if err != nil && !errors.Is(err, redis.Nil) { + return nil, err + } + + if v != "" { + msg := &GetItemsResponse{} + err = msg.unmarshal(v) + if err != nil { + return nil, err + } + return msg, nil + } + + return nil, nil +} + +func (s *getItemsStorage) Del(ctx context.Context, key *GetItemsRequest) error { + + k, err := key.marshal() + if err != nil { + return err + } + + _, err = s.r.Del(ctx, k).Result() + return err +} + +// message marshallers + +func (msg *GetItemsRequest) marshal() (string, error) { + + v := fmt.Sprintf("get-items:%v:%v", msg.ViewId, msg.Filters) + + return v, nil +} + +func (msg *GetItemsResponse) marshal() (string, error) { + v, err := protojson.MarshalOptions{}.Marshal(msg) + if err != nil { + return "", err + } + + return string(v), nil +} + +func (msg *GetItemsResponse) unmarshal(value string) error { + return protojson.UnmarshalOptions{}.Unmarshal([]byte(value), msg) +}