Skip to content

Commit

Permalink
Example proto for dynamic keys and inferred names
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsundar committed Dec 29, 2023
1 parent fbd55cd commit 8c2895a
Show file tree
Hide file tree
Showing 6 changed files with 599 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/protoc-gen-go-kvstore/internal/optparse/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import (
var keySuffixes = []string{
"key",
"Key",
"Request",
"Req",
}

var valueSuffixes = []string{
"val",
"Val",
"value",
"Value",
"Response",
"Resp",
}

func extractKeyName(name string) string {
Expand Down
7 changes: 7 additions & 0 deletions examples/views/app/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

func main() {
// see test file to usage examples
// you may try your custom usage of kvstore here
return
}
80 changes: 80 additions & 0 deletions examples/views/app/main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading

0 comments on commit 8c2895a

Please sign in to comment.